コード例 #1
0
        public void Can_get_and_set_table_and_column_info()
        {
            var column = new ColumnModel(PrimitiveTypeKind.Boolean);
            var alterColumnOperation = new AlterColumnOperation("T", column, isDestructiveChange: false);

            Assert.Equal("T", alterColumnOperation.Table);
            Assert.Same(column, alterColumnOperation.Column);
        }
コード例 #2
0
        public void Inverse_should_produce_change_column_operation()
        {
            var column = new ColumnModel(PrimitiveTypeKind.Boolean);
            var inverse = new AlterColumnOperation("T", column, isDestructiveChange: false);
            var alterColumnOperation = new AlterColumnOperation("T", column, isDestructiveChange: false, inverse: inverse);

            Assert.Same(inverse, alterColumnOperation.Inverse);
        }
コード例 #3
0
        public void Inverse_should_produce_change_column_operation()
        {
            var column  = new ColumnModel(PrimitiveTypeKind.Boolean);
            var inverse = new AlterColumnOperation("T", column, isDestructiveChange: false);
            var alterColumnOperation = new AlterColumnOperation("T", column, isDestructiveChange: false, inverse: inverse);

            Assert.Same(inverse, alterColumnOperation.Inverse);
        }
コード例 #4
0
        public void Can_get_and_set_table_and_column_info()
        {
            var column = new ColumnModel(PrimitiveTypeKind.Boolean);
            var alterColumnOperation = new AlterColumnOperation("T", column, isDestructiveChange: false);

            Assert.Equal("T", alterColumnOperation.Table);
            Assert.Same(column, alterColumnOperation.Column);
        }
コード例 #5
0
        public AlterColumnOperation(
            string table, ColumnModel column, bool isDestructiveChange, AlterColumnOperation inverse,
            object anonymousArguments = null)
            : this(table, column, isDestructiveChange, anonymousArguments)
        {
            Check.NotNull(inverse, "inverse");

            _inverse = inverse;
        }
コード例 #6
0
        public AlterColumnOperation(
            string table, ColumnModel column, bool isDestructiveChange, AlterColumnOperation inverse,
            object anonymousArguments = null)
            : this(table, column, isDestructiveChange, anonymousArguments)
        {
            Check.NotNull(inverse, "inverse");

            _inverse = inverse;
        }
コード例 #7
0
        public AlterColumnOperation(
            string table, ColumnModel column, bool isDestructiveChange, AlterColumnOperation inverse,
            object anonymousArguments = null)
            : this(table, column, isDestructiveChange, anonymousArguments)
        {
            //Contract.Requires(inverse != null);

            _inverse = inverse;
        }
        protected override void Generate(AlterColumnOperation alterColumnOperation)
        {
            var column = alterColumnOperation.Column;

            if ((column.DefaultValue != null)
                || !string.IsNullOrWhiteSpace(column.DefaultValueSql))
            {
                using (var writer = Writer())
                {
                    DropDefaultConstraint(alterColumnOperation.Table, column.Name, writer);

                    writer.Write("ALTER TABLE ");
                    writer.Write(Name(alterColumnOperation.Table));
                    writer.Write(" ADD CONSTRAINT ");
                    writer.Write(Quote("DF_" + alterColumnOperation.Table + "_" + column.Name));
                    writer.Write(" DEFAULT ");
                    writer.Write(
                        (column.DefaultValue != null)
                            ? Generate((dynamic)column.DefaultValue)
                            : column.DefaultValueSql
                        );
                    writer.Write(" FOR ");
                    writer.Write(Quote(column.Name));

                    Statement(writer);
                }
            }

            using (var writer = Writer())
            {
                writer.Write("ALTER TABLE ");
                writer.Write(Name(alterColumnOperation.Table));
                writer.Write(" ALTER COLUMN ");
                writer.Write(Quote(column.Name));
                writer.Write(" ");
                writer.Write(BuildColumnType(column));

                var collation = TryGetCollation(alterColumnOperation.Column.Annotations);
                if (collation != null && column.ClrType == typeof(string))
                {
                    writer.Write(" COLLATE " + collation.CollationName + " ");
                }

                if ((column.IsNullable != null)
                    && !column.IsNullable.Value)
                {
                    writer.Write(" NOT");
                }

                writer.Write(" NULL");

                Statement(writer);
            }
        }
コード例 #9
0
        protected override void Generate(AlterColumnOperation alterColumnOperation, IndentedTextWriter writer)
        {
            AlterColumnOperation alter = null;
              if (alterColumnOperation.Inverse != null)
            alter = new AlterColumnOperation(TrimSchemaPrefix(alterColumnOperation.Table), alterColumnOperation.Column, alterColumnOperation.IsDestructiveChange, (AlterColumnOperation)alterColumnOperation.Inverse);
              else
            alter = new AlterColumnOperation(TrimSchemaPrefix(alterColumnOperation.Table), alterColumnOperation.Column, alterColumnOperation.IsDestructiveChange);

              if (alter != null)
            base.Generate(alter, writer);
              else
            base.Generate(alterColumnOperation);
        }
コード例 #10
0
        /// <inheritdoc />
        protected override void Generate(AlterColumnOperation alterColumnOperation)
        {
            var column = alterColumnOperation.Column;

            using (var writer = Writer())
            {
                writer.Write("ALTER TABLE ");
                writer.Write(Name(alterColumnOperation.Table));
                writer.Write(" ALTER COLUMN ");
                writer.Write(Quote(column.Name));
                writer.Write(" ");
                writer.Write(BuildColumnType(column));

                if ((column.IsNullable != null)
                    && !column.IsNullable.Value)
                {
                    writer.Write(" NOT NULL");
                }

                Statement(writer);
            }

            if ((column.DefaultValue != null)
                || !string.IsNullOrWhiteSpace(column.DefaultValueSql))
            {
                using (var writer = Writer())
                {
                    writer.Write("ALTER TABLE ");
                    writer.Write(Name(alterColumnOperation.Table));
                    writer.Write(" ALTER COLUMN ");
                    writer.Write(Quote(column.Name));
                    writer.Write(" SET DEFAULT ");
                    writer.Write(
                        (column.DefaultValue != null)
                            ? Generate((dynamic)column.DefaultValue)
                            : column.DefaultValueSql
                        );

                    Statement(writer);
                }
            }
        }
        public void GenerateAlterDecimalColumn(bool? nullable, string contain, string notContain)
        {
            var migrationProvider = new PostgreSqlMigrationSqlGenerator();

            var column = new ColumnModel(PrimitiveTypeKind.Decimal)
            {
                Name = "C",
                IsNullable = nullable
            };

            var alterColumnOperation = new AlterColumnOperation("dbo.T", column, false);

            var sql = migrationProvider.Generate(new[] { alterColumnOperation }, "9.2").Join(s => s.Sql, Environment.NewLine);

            const string returnSqlBase = "ALTER TABLE \"dbo\".\"T\" ALTER COLUMN \"C\" ";

            Assert.IsNotNull(sql);
            Assert.True(sql.Contains(returnSqlBase));
            Assert.True(sql.Contains(returnSqlBase + "TYPE decimal"));
            Assert.True(sql.Contains(returnSqlBase + contain));
            Assert.False(sql.Contains(returnSqlBase + notContain));
        }
コード例 #12
0
        private void Convert(AlterColumnOperation alterColumnOperation)
        {
            StringBuilder sql = new StringBuilder();

            //TYPE
            AppendAlterColumn(alterColumnOperation, sql);
            sql.Append(" TYPE ");
            AppendColumnType(alterColumnOperation.Column, sql, false);
            AddStatment(sql);
            sql.Clear();

            //NOT NULL
            AppendAlterColumn(alterColumnOperation, sql);
            if (alterColumnOperation.Column.IsNullable != null && !alterColumnOperation.Column.IsNullable.Value)
                sql.Append(" SET NOT NULL");
            else
                sql.Append(" DROP NOT NULL");
            AddStatment(sql);
            sql.Clear();

            //DEFAULT
            AppendAlterColumn(alterColumnOperation, sql);
            if (alterColumnOperation.Column.DefaultValue != null)
            {
                sql.Append(" SET DEFAULT ");
                AppendValue(alterColumnOperation.Column.DefaultValue, sql);
            }
            else if (!string.IsNullOrWhiteSpace(alterColumnOperation.Column.DefaultValueSql))
            {
                sql.Append(" SET DEFAULT ");
                sql.Append(alterColumnOperation.Column.DefaultValueSql);
            }
            else if (alterColumnOperation.Column.IsIdentity)
            {
                sql.Append(" SET DEFAULT ");
                switch (alterColumnOperation.Column.Type)
                {
                    case PrimitiveTypeKind.Byte:
                    case PrimitiveTypeKind.SByte:
                    case PrimitiveTypeKind.Int16:
                    case PrimitiveTypeKind.Int32:
                    case PrimitiveTypeKind.Int64:
                        //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 PrimitiveTypeKind.Guid:
                        //CreateExtension("uuid-ossp");
                        //If uuid-ossp is not enabled migrations throw exception
                        AddStatment("select * from uuid_generate_v4()");
                        sql.Append("uuid_generate_v4()");
                        break;
                    default:
                        throw new NotImplementedException("Not supporting creating IsIdentity for " + alterColumnOperation.Column.Type);
                }
            }
            else
            {
                sql.Append(" DROP DEFAULT");
            }
            AddStatment(sql);
        }
        protected override void Generate(AlterColumnOperation alterColumnOperation)
        {
            using (var writer = Writer())
            {

                // create initial SQL
                var sql = string.Format("ALTER TABLE {0} ALTER COLUMN ",
                                        Name(alterColumnOperation.Table));

                writer.Write(sql);

                // generate the column name and type part
                var column = alterColumnOperation.Column;

                Generate(column, writer, true);

                // end the column type
                writer.Write(";");


                if (column.IsNullable != null)
                {

                    // create a new row to set nullable
                    writer.Write(sql);

                    writer.Write(Quote(column.Name));

                    writer.Write(" {0} NOT NULL;", column.IsNullable == true ? "DROP" : "SET" );

                }

                Statement(writer);
            }
        }
コード例 #14
0
        /// <summary>
        /// Gera SQL para uma operação <see cref="AlterColumnOperation" />.
        /// </summary>
        /// <param name="opeAlteraColuna"> The operation to produce SQL for. </param>
        protected virtual void Generate(AlterColumnOperation opeAlteraColuna)
        {
            var lcmColuna = opeAlteraColuna.Column;

            using (var ltextWriter = TextWriter())
            {
                ltextWriter.Write("ALTER TABLE ");
                ltextWriter.Write(RemoveDBO(opeAlteraColuna.Table));
                ltextWriter.Write(" ALTER COLUMN ");
                ltextWriter.Write(lcmColuna.Name);
                ltextWriter.Write(" ");
                ltextWriter.Write(ConstruirTipoColuna(lcmColuna));

                if ((lcmColuna.IsNullable != null)
                    && !lcmColuna.IsNullable.Value)
                {
                    ltextWriter.Write(" NOT NULL");
                }

                ComandoSQL(ltextWriter);
            }

            if ((lcmColuna.DefaultValue == null) && string.IsNullOrWhiteSpace(lcmColuna.DefaultValueSql))
                return;

            using (var ltextWriter = TextWriter())
            {
                ltextWriter.Write("ALTER TABLE ");
                ltextWriter.Write(RemoveDBO(opeAlteraColuna.Table));
                ltextWriter.Write(" ALTER COLUMN ");
                ltextWriter.Write(lcmColuna.Name);
                ltextWriter.Write(" DROP DEFAULT");

                ComandoSQL(ltextWriter);
            }

            using (var ltextWriter = TextWriter())
            {
                ltextWriter.Write("ALTER TABLE ");
                ltextWriter.Write(RemoveDBO(opeAlteraColuna.Table));
                ltextWriter.Write(" ALTER COLUMN ");
                ltextWriter.Write(lcmColuna.Name);
                ltextWriter.Write(" SET DEFAULT ");
                ltextWriter.Write(
                    (lcmColuna.DefaultValue != null)
                        ? Generate((dynamic)lcmColuna.DefaultValue)
                        : lcmColuna.DefaultValueSql
                    );

                ComandoSQL(ltextWriter);
            }
        }
コード例 #15
0
        private static MigrationOperation[] Create_operations_to_upgrade_primary_key_of_history_table()
        {
            const string tableName = "dbo." + HistoryContext.DefaultTableName;

            CreateTableOperation createTableOperation;
            using (var context = new HistoryContext())
            {
                var emptyModel = new DbModelBuilder()
                    .Build(context.Database.Connection).GetModel();
                createTableOperation = (CreateTableOperation)
                                       new EdmModelDiffer().Diff(emptyModel, context.GetModel()).Single();
            }

            var addColumnOperation =
                new AddColumnOperation(
                    tableName,
                    new ColumnModel(PrimitiveTypeKind.String)
                        {
                            MaxLength = 512,
                            Name = "ContextKey",
                            IsNullable = false,
                            DefaultValue = "DefaultContextKey"
                        });

            var dropPrimaryKeyOperation
                = new DropPrimaryKeyOperation
                      {
                          Table = tableName,
                          CreateTableOperation = createTableOperation
                      };

            dropPrimaryKeyOperation.Columns.Add("MigrationId");

            var alterColumnOperation
                = new AlterColumnOperation(
                    tableName,
                    new ColumnModel(PrimitiveTypeKind.String)
                        {
                            MaxLength = 150,
                            Name = "MigrationId",
                            IsNullable = false
                        },
                    isDestructiveChange: false);

            var addPrimaryKeyOperation
                = new AddPrimaryKeyOperation
                      {
                          Table = tableName
                      };

            addPrimaryKeyOperation.Columns.Add("MigrationId");
            addPrimaryKeyOperation.Columns.Add("ContextKey");

            return
                new MigrationOperation[]
                    {
                        addColumnOperation,
                        dropPrimaryKeyOperation,
                        alterColumnOperation,
                        addPrimaryKeyOperation
                    };
        }
コード例 #16
0
        public void Generate_should_output_column_nullability_for_altered_non_nullable_columns()
        {
            var migrationSqlGenerator = new SqlServerMigrationSqlGenerator();

            var alterColumnOperation =
                new AlterColumnOperation("Customers", new ColumnModel(PrimitiveTypeKind.Int32) { Name = "Baz", IsNullable = false }, false);

            var sql = migrationSqlGenerator.Generate(new[] { alterColumnOperation }, "2012").Join(s => s.Sql, Environment.NewLine);

            Assert.Contains("ALTER TABLE [Customers] ALTER COLUMN [Baz] [int] NOT NULL", sql);
        }
コード例 #17
0
        public void Generate_can_output_alter_geometry_column_operation_with_SQL_default_value()
        {
            var operation
                = new AlterColumnOperation(
                    "T",
                    new ColumnModel(PrimitiveTypeKind.Geometry)
                        {
                            IsNullable = false,
                            Name = "C",
                            DefaultValueSql = "'POINT (8 9)'"
                        },
                    isDestructiveChange: false);

            var sql = new SqlServerMigrationSqlGenerator().Generate(new[] { operation }, "2008").Join(s => s.Sql, Environment.NewLine);

            Assert.Equal(
                @"DECLARE @var0 nvarchar(128)
SELECT @var0 = name
FROM sys.default_constraints
WHERE parent_object_id = object_id(N'T')
AND col_name(parent_object_id, parent_column_id) = 'C';
IF @var0 IS NOT NULL
    EXECUTE('ALTER TABLE [T] DROP CONSTRAINT [' + @var0 + ']')
ALTER TABLE [T] ADD CONSTRAINT [DF_T_C] DEFAULT 'POINT (8 9)' FOR [C]
ALTER TABLE [T] ALTER COLUMN [C] [geometry] NOT NULL", sql);
        }
コード例 #18
0
        public void Generate_can_output_alter_geometry_column_operation_with_no_default_value()
        {
            var operation
                = new AlterColumnOperation(
                    "T",
                    new ColumnModel(PrimitiveTypeKind.Geometry)
                        {
                            IsNullable = false,
                            Name = "C",
                        },
                    isDestructiveChange: false);

            var sql = new SqlServerMigrationSqlGenerator().Generate(new[] { operation }, "2008").Join(s => s.Sql, Environment.NewLine);

            Assert.Equal(
                @"ALTER TABLE [T] ALTER COLUMN [C] [geometry] NOT NULL", sql);
        }
		protected virtual IEnumerable<MigrationStatement> Generate(AlterColumnOperation operation)
		{
			var column = operation.Column;
			var tableName = ExtractName(operation.Table);
			// drop NOT NULL first, either it will be recreated or it was to drop
			using (var writer = SqlWriter())
			{
				writer.WriteLine("EXECUTE BLOCK");
				writer.WriteLine("AS");
				writer.WriteLine("declare constraint_name type of column rdb$relation_constraints.rdb$constraint_name;");
				writer.WriteLine("BEGIN");
				writer.Indent++;
				writer.WriteLine("select rc.rdb$constraint_name");
				writer.WriteLine("from rdb$relation_constraints rc");
				writer.WriteLine("join rdb$check_constraints cc on rc.rdb$constraint_name = cc.rdb$constraint_name");
				writer.Write("where rc.rdb$constraint_type = 'NOT NULL' and rc.rdb$relation_name = '");
				writer.Write(tableName);
				writer.Write("' and cc.rdb$trigger_name  = '");
				writer.Write(column.Name);
				writer.Write("'");
				writer.WriteLine();
				writer.WriteLine("into :constraint_name;");
				writer.WriteLine("if (constraint_name is not null) then");
				writer.WriteLine("begin");
				writer.Indent++;
				writer.Write("execute statement 'alter table ");
				writer.Write(Quote(tableName));
				writer.Write(" drop constraint ' || :constraint_name;");
				writer.WriteLine();
				writer.Indent--;
				writer.WriteLine("end");
				writer.Indent--;
				writer.Write("END");
				yield return Statement(writer);
			}
			// drop identity trigger first, either it will be recreated or it was to drop
			foreach (var item in _behavior.DropIdentityForColumn(column.Name, tableName))
				yield return Statement(item);

			using (var writer = SqlWriter())
			{
				writer.Write("ALTER TABLE ");
				writer.Write(Quote(tableName));
				writer.Write(" ALTER COLUMN ");
				writer.Write(Quote(column.Name));
				writer.Write(" TYPE ");
				writer.Write(BuildPropertyType(column));
				// possible NOT NULL drop was dropped with statement above
				if (column.IsNullable != null && !column.IsNullable.Value)
				{
					writer.Write(" NOT NULL");
				}
				yield return Statement(writer);
			}

			if (column.DefaultValue != null || !string.IsNullOrWhiteSpace(column.DefaultValueSql))
			{
				using (var writer = SqlWriter())
				{
					writer.Write("ALTER TABLE ");
					writer.Write(Quote(tableName));
					writer.Write(" ALTER COLUMN ");
					writer.Write(Quote(column.Name));
					writer.Write(" DROP DEFAULT");
					yield return Statement(writer);
				}

				using (var writer = SqlWriter())
				{
					writer.Write("ALTER TABLE ");
					writer.Write(Quote(tableName));
					writer.Write(" ALTER COLUMN ");
					writer.Write(Quote(column.Name));
					writer.Write(" SET DEFAULT ");
					writer.Write(column.DefaultValue != null ? WriteValue((dynamic)column.DefaultValue) : column.DefaultValueSql);
					yield return Statement(writer);
				}
			}

			if (column.IsIdentity)
			{
				// possible identity drop was dropped with statement above
				foreach (var item in _behavior.CreateIdentityForColumn(column.Name, tableName))
					yield return Statement(item);
			}
		}
コード例 #20
0
        public void Generate_can_output_alter_column_with_default_constraint()
        {
            var migrationSqlGenerator = new SqlCeMigrationSqlGenerator();

            var column = new ColumnModel(PrimitiveTypeKind.Int32)
                             {
                                 Name = "Bar",
                                 DefaultValue = 42
                             };

            var alterColumnOperation = new AlterColumnOperation("Foo", column, false);

            var sql = migrationSqlGenerator
                .Generate(new[] { alterColumnOperation }, "4.0").Join(s => s.Sql, Environment.NewLine);

            Assert.Equal(@"ALTER TABLE [Foo] ALTER COLUMN [Bar] [int]
ALTER TABLE [Foo] ALTER COLUMN [Bar] DROP DEFAULT
ALTER TABLE [Foo] ALTER COLUMN [Bar] SET DEFAULT 42", sql);
        }
コード例 #21
0
        protected virtual MigrationStatement Generate(DropPrimaryKeyOperation op)
        {
            object obj2;
              bool deleteAutoIncrement = false;
              StringBuilder sb = new StringBuilder();

              op.AnonymousArguments.TryGetValue("DeleteAutoIncrement", out obj2);
              if (obj2 != null)
            bool.TryParse(obj2.ToString(), out deleteAutoIncrement);

              if (deleteAutoIncrement && op.Columns.Count == 1)
              {
            var newColumn = new ColumnModel(PrimitiveTypeKind.Int32, null);
            newColumn.Name = op.Columns[0];
            var alterColumn = new AlterColumnOperation(op.Table, newColumn, false);
            var ms = Generate(alterColumn);
            sb.Append(ms.Sql + "; ");
              }

              return new MigrationStatement { Sql = sb.ToString() + " alter table `" + op.Table + "` drop primary key " };
        }
コード例 #22
0
        /// <summary>
        /// Generates code to perform an <see cref="AlterColumnOperation" />.
        /// </summary>
        /// <param name="alterColumnOperation"> The operation to generate code for. </param>
        /// <param name="writer"> Text writer to add the generated code to. </param>
        protected virtual void Generate(AlterColumnOperation alterColumnOperation, IndentedTextWriter writer)
        {
            Check.NotNull(alterColumnOperation, "alterColumnOperation");
            Check.NotNull(writer, "writer");

            writer.Write("AlterColumn(");
            writer.Write(Quote(alterColumnOperation.Table));
            writer.Write(", ");
            writer.Write(Quote(alterColumnOperation.Column.Name));
            writer.Write(", Function(c)");
            Generate(alterColumnOperation.Column, writer);
            writer.WriteLine(")");
        }
コード例 #23
0
 private void AppendAlterColumn(AlterColumnOperation alterColumnOperation, StringBuilder sql)
 {
     sql.Append("ALTER TABLE ");
     AppendTableName(alterColumnOperation.Table, sql);
     sql.Append(" ALTER COLUMN \"");
     sql.Append(alterColumnOperation.Column.Name);
     sql.Append('"');
 }
コード例 #24
0
        /// <summary>
        /// Generates SQL for a <see cref="AlterColumnOperation" />.
        /// Generated SQL should be added using the Statement method.
        /// </summary>
        /// <param name="alterColumnOperation"> The operation to produce SQL for. </param>
        protected virtual void Generate(AlterColumnOperation alterColumnOperation)
        {
            Check.NotNull(alterColumnOperation, "alterColumnOperation");

            var column = alterColumnOperation.Column;

            using (var writer = Writer())
            {
                DropDefaultConstraint(alterColumnOperation.Table, column.Name, writer);

                writer.Write("ALTER TABLE ");
                writer.Write(Name(alterColumnOperation.Table));
                writer.Write(" ALTER COLUMN ");
                writer.Write(Quote(column.Name));
                writer.Write(" ");
                writer.Write(BuildColumnType(column));

                if ((column.IsNullable != null)
                    && !column.IsNullable.Value)
                {
                    writer.Write(" NOT");
                }

                writer.Write(" NULL");

                if ((column.DefaultValue != null)
                    || !string.IsNullOrWhiteSpace(column.DefaultValueSql))
                {
                    writer.WriteLine();
                    writer.Write("ALTER TABLE ");
                    writer.Write(Name(alterColumnOperation.Table));
                    writer.Write(" ADD CONSTRAINT ");
                    writer.Write(Quote("DF_" + alterColumnOperation.Table + "_" + column.Name));
                    writer.Write(" DEFAULT ");
                    writer.Write(
                        (column.DefaultValue != null)
                            ? Generate((dynamic)column.DefaultValue)
                            : column.DefaultValueSql
                        );
                    writer.Write(" FOR ");
                    writer.Write(Quote(column.Name));
                }

                Statement(writer);
            }
        }
        public void GenerateCanOutputAlterGeometryColumnOperationWithNoDefaultValue()
        {
            var operation
                = new AlterColumnOperation(
                    "T",
                    new ColumnModel(PrimitiveTypeKind.Geometry)
                    {
                        IsNullable = false,
                        Name = "C",
                    },
                    isDestructiveChange: false);

            var sql = new PostgreSqlMigrationSqlGenerator().Generate(new[] { operation }, "9.2").Join(s => s.Sql, Environment.NewLine);

            Assert.AreEqual(
               "ALTER TABLE \"T\" ALTER COLUMN \"C\" TYPE point;ALTER TABLE \"T\" ALTER COLUMN \"C\" SET NOT NULL;", sql);
        }
コード例 #26
0
        protected virtual MigrationStatement Generate(AlterColumnOperation op)
        {
            if (op == null) return null;

              ColumnModel column = op.Column;
              StringBuilder sb = new StringBuilder();
              _tableName = op.Table;

              // for existing columns
              sb.Append("alter table `" + op.Table + "` modify `" + column.Name + "` ");

              // add definition
              sb.Append(Generate(column));

              return new MigrationStatement { Sql = sb.ToString() };
        }
コード例 #27
0
        /// <summary>
        ///     Generates code to perform an <see cref = "AlterColumnOperation" />.
        /// </summary>
        /// <param name = "alterColumnOperation">The operation to generate code for.</param>
        /// <param name = "writer">Text writer to add the generated code to.</param>
        protected virtual void Generate(AlterColumnOperation alterColumnOperation, IndentedTextWriter writer)
        {
            Contract.Requires(alterColumnOperation != null);
            Contract.Requires(writer != null);

            writer.Write("AlterColumn(");
            writer.Write(Quote(alterColumnOperation.Table));
            writer.Write(", ");
            writer.Write(Quote(alterColumnOperation.Column.Name));
            writer.Write(", Function(c)");
            Generate(alterColumnOperation.Column, writer);
            writer.WriteLine(")");
        }
コード例 #28
0
    protected virtual MigrationStatement Generate(AlterColumnOperation op)
    {
      if (op == null) return null;

      ColumnModel column = op.Column;
      StringBuilder sb = new StringBuilder();
      _tableName = op.Table;

      //verify if there is any "AddPrimaryKeyOperation" related with the column that will be added and if it is defined as identity (auto_increment)
      bool uniqueAttr = (from pkOpe in _pkOperations
                         where (from col in pkOpe.Columns
                                where col == op.Column.Name
                                select col).Count() > 0
                         select pkOpe).Count() > 0 & op.Column.IsIdentity;

      // for existing columns
      sb.Append("alter table `" + TrimSchemaPrefix(op.Table) + "` modify `" + column.Name + "` ");

      // add definition
      sb.Append(Generate(column) + (uniqueAttr ? " unique " : ""));

      return new MigrationStatement { Sql = sb.ToString() };
    }
        public void Generate_can_output_alter_geography_column_operation_with_SQL_default_value()
        {
            var operation
                = new AlterColumnOperation(
                    "T",
                    new ColumnModel(PrimitiveTypeKind.Geography)
                    {
                        IsNullable = false,
                        Name = "C",
                        DefaultValueSql = "'POINT (6 7)'"
                    },
                    isDestructiveChange: false);

            var sql = new SqlServerMigrationSqlGenerator().Generate(new[] { operation }, "2008").Join(s => s.Sql, Environment.NewLine);

            Assert.Equal(
                @"ALTER TABLE [T] ADD CONSTRAINT DF_C DEFAULT 'POINT (6 7)' FOR [C]
ALTER TABLE [T] ALTER COLUMN [C] [geography] NOT NULL", sql);
        }
コード例 #30
0
        protected virtual IEnumerable<MigrationStatement> Generate(AlterColumnOperation operation)
        {
            var column = operation.Column;
            using (var writer = SqlWriter())
            {
                writer.Write("ALTER TABLE ");
                writer.Write(Quote(operation.Table));
                writer.Write(" MODIFY COLUMN ");
                writer.Write(Quote(column.Name));
                writer.Write(" ");
                writer.Write(BuildColumnType(column));
                if (column.IsNullable != null && !column.IsNullable.Value)
                {
                    writer.Write(" NOT");
                }
                writer.Write(" NULL");
                yield return Statement(writer);
            }

            if (column.DefaultValue != null || !string.IsNullOrWhiteSpace(column.DefaultValueSql))
            {
                using (var writer = SqlWriter())
                {
                    writer.Write("ALTER TABLE ");
                    writer.Write(Quote(operation.Table));
                    writer.Write(" ALTER COLUMN ");
                    writer.Write(Quote(column.Name));
                    writer.Write(" DROP DEFAULT");
                    yield return Statement(writer);
                }

                using (var writer = SqlWriter())
                {
                    writer.Write("ALTER TABLE ");
                    writer.Write(Quote(operation.Table));
                    writer.Write(" ALTER COLUMN ");
                    writer.Write(Quote(column.Name));
                    writer.Write(" SET DEFAULT ");
                    writer.Write(column.DefaultValue != null ? WriteValue((dynamic)column.DefaultValue) : column.DefaultValueSql);
                    yield return Statement(writer);
                }
            }
        }
コード例 #31
0
        /// <summary>
        ///     Generates SQL for a <see cref = "AlterColumnOperation" />.
        ///     Generated SQL should be added using the Statement method.
        /// </summary>
        /// <param name = "alterColumnOperation">The operation to produce SQL for.</param>
        protected virtual void Generate(AlterColumnOperation alterColumnOperation)
        {
            Contract.Requires(alterColumnOperation != null);

            var column = alterColumnOperation.Column;

            if ((column.DefaultValue != null)
                || !string.IsNullOrWhiteSpace(column.DefaultValueSql))
            {
                using (var writer = Writer())
                {
                    writer.Write("ALTER TABLE ");
                    writer.Write(Name(alterColumnOperation.Table));
                    writer.Write(" ADD CONSTRAINT DF_");
                    writer.Write(column.Name);
                    writer.Write(" DEFAULT ");
                    writer.Write(
                        (column.DefaultValue != null)
                            ? Generate((dynamic)column.DefaultValue)
                            : column.DefaultValueSql
                        );
                    writer.Write(" FOR ");
                    writer.Write(Quote(column.Name));

                    Statement(writer);
                }
            }

            using (var writer = Writer())
            {
                writer.Write("ALTER TABLE ");
                writer.Write(Name(alterColumnOperation.Table));
                writer.Write(" ALTER COLUMN ");
                writer.Write(Quote(column.Name));
                writer.Write(" ");
                writer.Write(BuildColumnType(column));

                if ((column.IsNullable != null)
                    && !column.IsNullable.Value)
                {
                    writer.Write(" NOT NULL");
                }

                Statement(writer);
            }
        }