protected override void Generate(AddColumnOperation addColumnOperation)
        {
            SetCreatedUtcColumn(addColumnOperation.Column);
            AddDefaultValue(addColumnOperation.Column);

            base.Generate(addColumnOperation);
        }
        protected virtual IEnumerable<MigrationStatement> Generate(AddColumnOperation operation)
        {
            using (var writer = SqlWriter())
            {
                writer.Write("ALTER TABLE ");
                writer.Write(Quote(operation.Table));
                writer.Write(" ADD ");
                var column = operation.Column;
                writer.Write(Generate(column));
                if (column.IsNullable != null
                    && !column.IsNullable.Value
                    && column.DefaultValue == null
                    && string.IsNullOrWhiteSpace(column.DefaultValueSql)
                    && !column.IsIdentity
                    && !column.IsTimestamp)
                {
                    writer.Write(" DEFAULT ");

                    if (column.Type == PrimitiveTypeKind.DateTime)
                    {
                        writer.Write(WriteValue(DateTime.Parse("1970-01-01 00:00:00", CultureInfo.InvariantCulture)));
                    }
                    else
                    {
                        writer.Write(WriteValue((dynamic)column.ClrDefaultValue));
                    }
                }
                yield return Statement(writer);
            }
        }
        public void Generate_should_output_invariant_decimals_when_non_invariant_culture()
        {
            var migrationProvider = new SqlServerMigrationSqlGenerator();

            var addColumnOperation
                = new AddColumnOperation(
                    "T",
                    new ColumnModel(PrimitiveTypeKind.Binary)
                        {
                            Name = "C",
                            DefaultValue = 123.45m
                        });

            var lastCulture = Thread.CurrentThread.CurrentCulture;

            try
            {
                Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("nl-NL");

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

                Assert.Contains("ALTER TABLE [T] ADD [C] [varbinary](max) DEFAULT 123.45", sql);
            }
            finally
            {
                Thread.CurrentThread.CurrentCulture = lastCulture;
            }
        }
        public void Inverse_should_produce_drop_column_operation()
        {
            var inverse = new AddColumnOperation("T", new ColumnModel(PrimitiveTypeKind.Binary));
            var dropColumnOperation = new DropColumnOperation("T", "c", inverse);

            Assert.Same(inverse, dropColumnOperation.Inverse);
        }
        public void Can_get_and_set_table_and_column_info()
        {
            var column = new ColumnModel(PrimitiveTypeKind.Decimal);
            var addColumnOperation = new AddColumnOperation("T", column);

            Assert.Equal("T", addColumnOperation.Table);
            Assert.Same(column, addColumnOperation.Column);
        }
    protected virtual MigrationStatement Generate(AddColumnOperation op)
    {
      if (op == null) return null;

      MigrationStatement stmt = new MigrationStatement();
      stmt.Sql = string.Format("alter table `{0}` add column `{1}`",
        op.Table, op.Column.Name) + " " + Generate(op.Column);
      return stmt;
    }
        public void Inverse_should_produce_drop_column_operation()
        {
            var column = new ColumnModel(PrimitiveTypeKind.Decimal) { Name = "C" };
            var addColumnOperation = new AddColumnOperation("T", column);

            var dropColumnOperation = (DropColumnOperation)addColumnOperation.Inverse;

            Assert.Equal("C", dropColumnOperation.Name);
            Assert.Equal("T", dropColumnOperation.Table);
        }
        public DropColumnOperation(
            string table, string name, AddColumnOperation inverse, object anonymousArguments = null)
            : base(anonymousArguments)
        {
            //Contract.Requires(!string.IsNullOrWhiteSpace(table));
            //Contract.Requires(!string.IsNullOrWhiteSpace(name));

            _table = table;
            _name = name;
            _inverse = inverse;
        }
        public DropColumnOperation(
            string table, string name, AddColumnOperation inverse, object anonymousArguments = null)
            : base(anonymousArguments)
        {
            Check.NotEmpty(table, "table");
            Check.NotEmpty(name, "name");

            _table = table;
            _name = name;
            _inverse = inverse;
        }
        public void Inverse_should_return_given_AddColumnOperation()
        {
            var inverse = new AddColumnOperation("T", new ColumnModel(PrimitiveTypeKind.Binary));
            var dropColumnOperation = new DropColumnOperation("T", "c", inverse);

            Assert.Same(inverse, dropColumnOperation.Inverse);

            dropColumnOperation = new DropColumnOperation("T", "c", null, inverse);

            Assert.Same(inverse, dropColumnOperation.Inverse);
        }
        public void GenerateCanOutputAddColumnStatementWhenNonNullableAndNoDefaultProvided()
        {
            var migrationSqlGenerator = new PostgreSqlMigrationSqlGenerator();

            var column = new ColumnModel(PrimitiveTypeKind.Int32)
            {
                Name = "Bar",
                IsNullable = false
            };
            var addColumnOperation = new AddColumnOperation("Foo", column);

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

            Assert.True(sql.Contains("ALTER TABLE \"Foo\" ADD \"Bar\" integer NOT NULL DEFAULT 0"));
        }
Exemple #12
0
        public void Generate_can_output_add_column_statement_for_GUID_and_uses_newid()
        {
            var migrationSqlGenerator = new SqlCeMigrationSqlGenerator();

            var column = new ColumnModel(PrimitiveTypeKind.Guid)
                             {
                                 Name = "Bar",
                                 IsIdentity = true
                             };
            var addColumnOperation = new AddColumnOperation("Foo", column);

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

            Assert.Contains("ALTER TABLE [Foo] ADD [Bar] [uniqueidentifier] DEFAULT newid()", sql);
        }
        public void GenerateCanOutputAddColumnStatement(PrimitiveTypeKind type, string typeName)
        {
            var migrationSqlGenerator = new PostgreSqlMigrationSqlGenerator();

            var column = new ColumnModel(type)
            {
                Name = "Bar",
                IsIdentity = true
            };
            var addColumnOperation = new AddColumnOperation("Foo", column);

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

            Assert.True(sql.Contains(string.Format("ALTER TABLE \"Foo\" ADD \"Bar\" {0}", typeName)));
        }
        public void Inverse_should_produce_drop_column_operation()
        {
            var column = new ColumnModel(PrimitiveTypeKind.Decimal)
            {
                Name = "C",
                Annotations = new Dictionary<string, AnnotationValues> { { "A1", new AnnotationValues(null, "V1") } }
            };

            var addColumnOperation
                = new AddColumnOperation("T", column);

            var dropColumnOperation = (DropColumnOperation)addColumnOperation.Inverse;

            Assert.Equal("C", dropColumnOperation.Name);
            Assert.Equal("T", dropColumnOperation.Table);
            Assert.Equal("V1", dropColumnOperation.RemovedAnnotations["A1"]);
        }
        public void Generate_can_output_add_rowversion_store_type_column_operation()
        {
            var migrationSqlGenerator = new SqlServerMigrationSqlGenerator();

            var addColumnOperation
                = new AddColumnOperation(
                    "T",
                    new ColumnModel(PrimitiveTypeKind.Binary)
                        {
                            IsNullable = false,
                            Name = "C",
                            StoreType = "RowVersion"
                        });

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

            Assert.Contains("ALTER TABLE [T] ADD [C] [RowVersion] NOT NULL", sql);
        }
        public void Generate_can_output_add_timestamp_store_type_column_operation()
        {
            var migrationProvider = new SqlServerMigrationSqlGenerator();

            var addColumnOperation
                = new AddColumnOperation(
                    "T",
                    new ColumnModel(PrimitiveTypeKind.Binary)
                    {
                        IsNullable = false,
                        Name = "C",
                        StoreType = "timestamp"
                    });

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

            Assert.True(sql.Contains("ALTER TABLE [T] ADD [C] [timestamp] NOT NULL"));
        }
    public void AddColumnOperationMigration()
    {
      var migrationOperations = new List<MigrationOperation>();

      if (ProviderManifest == null)
      ProviderManifest = new MySqlProviderManifest(Version.ToString());

      TypeUsage tu = TypeUsage.CreateDefaultTypeUsage(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32));
      TypeUsage result = ProviderManifest.GetStoreType(tu);

      var intColumn = new ColumnModel(PrimitiveTypeKind.Int32, result)
      {
        Name = "TotalPosts",
        IsNullable = false
      };

      var addColumnMigratioOperation = new AddColumnOperation("Blogs", intColumn);
      migrationOperations.Add(addColumnMigratioOperation);

      using (BlogContext context = new BlogContext())
      {
        if (context.Database.Exists()) context.Database.Delete();
        context.Database.Create();        
        
        using (MySqlConnection conn = new MySqlConnection(context.Database.Connection.ConnectionString))
        {
          if (conn.State == System.Data.ConnectionState.Closed) conn.Open();
          Assert.AreEqual(true, GenerateAndExecuteMySQLStatements(migrationOperations));
          
          MySqlCommand query = new MySqlCommand("Select Column_name, Is_Nullable, Data_Type from information_schema.Columns where table_schema ='" + conn.Database + "' and table_name = 'Blogs' and column_name ='TotalPosts'" , conn);
          MySqlDataReader reader = query.ExecuteReader();
          while (reader.Read())
          {
            Assert.AreEqual("TotalPosts", reader[0].ToString());
            Assert.AreEqual("NO", reader[1].ToString());
            Assert.AreEqual("int", reader[2].ToString());
          }
          reader.Close();
          conn.Close();
        }
      }        
    }
        public void Generate_can_output_add_geography_column_operation_with_implicit_default_value()
        {
            var operation
                = new AddColumnOperation(
                    "T",
                    new ColumnModel(PrimitiveTypeKind.Geography)
                        {
                            IsNullable = false,
                            Name = "C"
                        });

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

            Assert.Equal("ALTER TABLE [T] ADD [C] [geography] NOT NULL DEFAULT 'SRID=4326;POINT (0 0)'", sql);
        }
 /// <summary>
 /// Generates code to perform an <see cref="T:System.Data.Entity.Migrations.Model.AddColumnOperation" />.
 /// </summary>
 /// <param name="addColumnOperation">The operation to generate code for.</param>
 /// <param name="writer">Text writer to add the generated code to.</param>
 protected override void Generate(System.Data.Entity.Migrations.Model.AddColumnOperation addColumnOperation, System.Data.Entity.Migrations.Utilities.IndentedTextWriter writer)
 {
     base.Generate(addColumnOperation, writer);
     GenerateCustomColumnCode(writer, addColumnOperation.Table, addColumnOperation.Column.Name);
 }
Exemple #20
0
        public void Generate_can_output_add_column_statement_when_non_nullable_and_no_default_provided()
        {
            var migrationSqlGenerator = new SqlCeMigrationSqlGenerator();

            var column = new ColumnModel(PrimitiveTypeKind.Int32)
                             {
                                 Name = "Bar",
                                 IsNullable = false
                             };
            var addColumnOperation = new AddColumnOperation("Foo", column);

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

            Assert.Contains("ALTER TABLE [Foo] ADD [Bar] [int] NOT NULL DEFAULT 0", sql);
        }
Exemple #21
0
        public void Generate_can_output_add_column_statement_with_explicit_default_value_sql()
        {
            var migrationSqlGenerator = new SqlCeMigrationSqlGenerator();

            var column = new ColumnModel(PrimitiveTypeKind.Guid)
                             {
                                 Name = "Bar",
                                 IsNullable = false,
                                 DefaultValueSql = "42"
                             };
            var addColumnOperation = new AddColumnOperation("Foo", column);

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

            Assert.Contains("ALTER TABLE [Foo] ADD [Bar] [uniqueidentifier] NOT NULL DEFAULT 42", sql);
        }
Exemple #22
0
        public void Generate_can_output_add_column_statement_with_custom_store_type()
        {
            var migrationSqlGenerator = new SqlCeMigrationSqlGenerator();

            var column = new ColumnModel(PrimitiveTypeKind.String)
                             {
                                 Name = "Bar",
                                 StoreType = "varchar",
                                 MaxLength = 15
                             };
            var addColumnOperation = new AddColumnOperation("Foo", column);

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

            Assert.Contains("ALTER TABLE [Foo] ADD [Bar] [varchar](15)", sql);
        }
Exemple #23
0
        public void Generate_can_output_add_timestamp_column_operation()
        {
            var migrationSqlGenerator = new SqlCeMigrationSqlGenerator();

            var addColumnOperation
                = new AddColumnOperation(
                    "T",
                    new ColumnModel(PrimitiveTypeKind.Binary)
                        {
                            IsNullable = false,
                            Name = "C",
                            IsTimestamp = true
                        });

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

            Assert.Contains("ALTER TABLE [T] ADD [C] rowversion NOT NULL", sql);
        }
 protected override void Generate(AddColumnOperation addColumnOperation, IndentedTextWriter writer)
 {
     var add = new AddColumnOperation(TrimSchemaPrefix(addColumnOperation.Table), addColumnOperation.Column);
       base.Generate(add, writer);
 }
        /// <summary>
        /// Generates code to perform an <see cref="AddColumnOperation" />.
        /// </summary>
        /// <param name="addColumnOperation"> The operation to generate code for. </param>
        /// <param name="writer"> Text writer to add the generated code to. </param>
        protected virtual void Generate(AddColumnOperation addColumnOperation, IndentedTextWriter writer)
        {
            Check.NotNull(addColumnOperation, "addColumnOperation");
            Check.NotNull(writer, "writer");

            writer.Write("AddColumn(");
            writer.Write(Quote(addColumnOperation.Table));
            writer.Write(", ");
            writer.Write(Quote(addColumnOperation.Column.Name));
            writer.Write(", Function(c)");
            Generate(addColumnOperation.Column, writer);
            writer.WriteLine(")");
        }
        /// <summary>
        /// Generates SQL for a <see cref="AddColumnOperation" />.
        /// Generated SQL should be added using the Statement method.
        /// </summary>
        /// <param name="addColumnOperation"> The operation to produce SQL for. </param>
        protected virtual void Generate(AddColumnOperation addColumnOperation)
        {
            Check.NotNull(addColumnOperation, "addColumnOperation");

            using (var writer = Writer())
            {
                writer.Write("ALTER TABLE ");
                writer.Write(Name(addColumnOperation.Table));
                writer.Write(" ADD ");

                var column = addColumnOperation.Column;

                Generate(column, writer);

                if ((column.IsNullable != null)
                    && !column.IsNullable.Value
                    && (column.DefaultValue == null)
                    && (string.IsNullOrWhiteSpace(column.DefaultValueSql))
                    && !column.IsIdentity
                    && !column.IsTimestamp
                    && !column.StoreType.EqualsIgnoreCase("rowversion")
                    && !column.StoreType.EqualsIgnoreCase("timestamp"))
                {
                    writer.Write(" DEFAULT ");

                    if (column.Type == PrimitiveTypeKind.DateTime)
                    {
                        writer.Write(Generate(DateTime.Parse("1900-01-01 00:00:00", CultureInfo.InvariantCulture)));
                    }
                    else
                    {
                        writer.Write(Generate((dynamic)column.ClrDefaultValue));
                    }
                }

                Statement(writer);
            }
        }
 private HistoryRebuildOperationSequence(
     AddColumnOperation addColumnOperation,
     DropPrimaryKeyOperation dropPrimaryKeyOperation)
     : base(null)
 {
     AddColumnOperation = addColumnOperation;
     DropPrimaryKeyOperation = dropPrimaryKeyOperation;
 }
 private void Convert(AddColumnOperation addColumnOperation)
 {
     StringBuilder sql = new StringBuilder();
     sql.Append("ALTER TABLE ");
     AppendTableName(addColumnOperation.Table, sql);
     sql.Append(" ADD ");
     AppendColumn(addColumnOperation.Column, sql);
     AddStatment(sql);
 }
        public DropColumnOperation(
            string table, string name, IDictionary<string, object> removedAnnotations, AddColumnOperation inverse, object anonymousArguments = null)
            : base(anonymousArguments)
        {
            Check.NotEmpty(table, "table");
            Check.NotEmpty(name, "name");

            _table = table;
            _name = name;
            _removedAnnotations = removedAnnotations ?? new Dictionary<string, object>();
            _inverse = inverse;
        }
 public DropColumnOperation(
     string table, string name, AddColumnOperation inverse, object anonymousArguments = null)
     : this(table, name, null, inverse, anonymousArguments)
 {
 }
        public void Generate_can_output_add_column_statement_for_GUID(string providerManifestToken, string expectedGuidDefault)
        {
            var migrationSqlGenerator = new SqlServerMigrationSqlGenerator();

            var column = new ColumnModel(PrimitiveTypeKind.Guid)
                             {
                                 Name = "Bar",
                                 IsIdentity = true
                             };
            var addColumnOperation = new AddColumnOperation("Foo", column);

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

            Assert.Contains(string.Format("ALTER TABLE [Foo] ADD [Bar] [uniqueidentifier] DEFAULT {0}", expectedGuidDefault), sql);
        }