コード例 #1
0
        private void AddMaskingFunction(AlterColumnOperation operation, MigrationCommandListBuilder builder)
        {
            var sqlHelper      = Dependencies.SqlGenerationHelper;
            var addDynamicMask = operation.FindAnnotation(AnnotationConstants.DynamicDataMasking);

            builder.Append("ALTER TABLE ")
            .Append(sqlHelper.DelimitIdentifier(operation.Table, operation.Schema))
            .Append($" ALTER COLUMN {operation.Name}")
            .Append($" ADD MASKED WITH (FUNCTION='{addDynamicMask.Value}')")
            .Append(sqlHelper.StatementTerminator)
            .EndCommand();
        }
        protected override void Generate(AlterColumnOperation operation, IModel model, MigrationCommandListBuilder builder)
        {
            Check.NotNull(operation, nameof(operation));
            Check.NotNull(builder, nameof(builder));

            var type = operation.ColumnType;

            if (operation.ColumnType == null)
            {
                var property = FindProperty(model, operation.Schema, operation.Table, operation.Name);
                type = property != null
                    ? TypeMapper.GetMapping(property).StoreType
                    : TypeMapper.GetMapping(operation.ClrType).StoreType;
            }

            var serial   = operation.FindAnnotation(MySqlAnnotationNames.Prefix + MySqlAnnotationNames.Serial);
            var isSerial = serial != null && (bool)serial.Value;

            var identifier = SqlGenerationHelper.DelimitIdentifier(operation.Table, operation.Schema);
            var alterBase  = $"ALTER TABLE {identifier} MODIFY COLUMN {SqlGenerationHelper.DelimitIdentifier(operation.Name)}";

            // TYPE
            builder.Append(alterBase)
            .Append(" ")
            .Append(type)
            .Append(operation.IsNullable ? " NULL" : " NOT NULL")
            .AppendLine(SqlGenerationHelper.StatementTerminator);

            switch (type)
            {
            case "tinyblob":
            case "blob":
            case "mediumblob":
            case "longblob":

            case "tinytext":
            case "text":
            case "mediumtext":
            case "longtext":

            case "geometry":
            case "point":
            case "linestring":
            case "polygon":
            case "multipoint":
            case "multilinestring":
            case "multipolygon":
            case "geometrycollection":

            case "json":
                if (operation.DefaultValue != null || !string.IsNullOrWhiteSpace(operation.DefaultValueSql) || isSerial)
                {
                    throw new NotSupportedException($"{type} column can't have a default value");
                }
                break;

            default:
                alterBase = $"ALTER TABLE {identifier} ALTER COLUMN {SqlGenerationHelper.DelimitIdentifier(operation.Name)}";

                builder.Append(alterBase);

                if (operation.DefaultValue != null)
                {
                    builder.Append(" SET DEFAULT ")
                    .Append(SqlGenerationHelper.GenerateLiteral((dynamic)operation.DefaultValue))
                    .AppendLine(SqlGenerationHelper.BatchTerminator);
                }
                else if (!string.IsNullOrWhiteSpace(operation.DefaultValueSql))
                {
                    builder.Append(" SET DEFAULT ")
                    .Append(operation.DefaultValueSql)
                    .AppendLine(SqlGenerationHelper.BatchTerminator);
                }
                else if (isSerial)
                {
                    builder.Append(" SET DEFAULT ");

                    switch (type)
                    {
                    case "smallint":
                    case "int":
                    case "bigint":
                    case "real":
                    case "double precision":
                    case "numeric":
                        //TODO: need function CREATE SEQUENCE IF NOT EXISTS and set to it...
                        //Until this is resolved changing IsIdentity from false to true
                        //on types int2, int4 and int8 won't switch to type serial2, serial4 and serial8
                        throw new NotImplementedException("Not supporting creating sequence for integer types");

                    case "char(38)":
                    case "uuid":
                    case "uniqueidentifier":
                        break;

                    default:
                        throw new NotImplementedException($"Not supporting creating IsIdentity for {type}");
                    }
                }
                else
                {
                    builder.Append(" DROP DEFAULT;");
                }
                break;
            }

            EndStatement(builder);
        }
        protected override void Generate(AlterColumnOperation operation, IModel model, MigrationCommandListBuilder builder)
        {
            Check.NotNull(operation, nameof(operation));
            Check.NotNull(builder, nameof(builder));

            // TODO: There is probably duplication here with other methods. See ColumnDefinition.

            //TODO: this should provide feature parity with the EF6 provider, check if there's anything missing for EF7

            var type = operation.ColumnType;

            if (operation.ColumnType == null)
            {
                var property = FindProperty(model, operation.Schema, operation.Table, operation.Name);
                type = property != null
                    ? TypeMapper.GetMapping(property).StoreType
                    : TypeMapper.GetMapping(operation.ClrType).StoreType;
            }

            var serial   = operation.FindAnnotation(MySqlAnnotationNames.Prefix + MySqlAnnotationNames.Serial);
            var isSerial = serial != null && (bool)serial.Value;

            var identifier = SqlGenerationHelper.DelimitIdentifier(operation.Table, operation.Schema);
            var alterBase  = $"ALTER TABLE {identifier} MODIFY COLUMN {SqlGenerationHelper.DelimitIdentifier(operation.Name)}";

            // TYPE
            builder.Append(alterBase)
            .Append(" ")
            .Append(type)
            .Append(operation.IsNullable ? " NULL" : " NOT NULL")
            .AppendLine(SqlGenerationHelper.StatementTerminator);

            alterBase = $"ALTER TABLE {identifier} ALTER COLUMN {SqlGenerationHelper.DelimitIdentifier(operation.Name)}";

            builder.Append(alterBase);

            if (operation.DefaultValue != null)
            {
                builder.Append(" SET DEFAULT ")
                .Append(SqlGenerationHelper.GenerateLiteral((dynamic)operation.DefaultValue))
                .AppendLine(SqlGenerationHelper.BatchTerminator);
            }
            else if (!string.IsNullOrWhiteSpace(operation.DefaultValueSql))
            {
                builder.Append(" SET DEFAULT ")
                .Append(operation.DefaultValueSql)
                .AppendLine(SqlGenerationHelper.BatchTerminator);
            }
            else if (isSerial)
            {
                builder.Append(" SET DEFAULT ");

                switch (type)
                {
                case "smallint":
                case "int":
                case "bigint":
                case "real":
                case "double precision":
                case "numeric":
                    //TODO: need function CREATE SEQUENCE IF NOT EXISTS and set to it...
                    //Until this is resolved changing IsIdentity from false to true
                    //on types int2, int4 and int8 won't switch to type serial2, serial4 and serial8
                    throw new NotImplementedException("Not supporting creating sequence for integer types");

                case "char(38)":
                case "uuid":
                case "uniqueidentifier":
                    builder.Append("UUID()");
                    break;

                default:
                    throw new NotImplementedException($"Not supporting creating IsIdentity for {type}");
                }
            }
            else
            {
                builder.Append(" DROP DEFAULT ");
            }
        }