public void Can_get_default_for_name() { var dropPrimaryKeyOperation = new DropPrimaryKeyOperation { Table = "T" }; Assert.Equal("PK_T", dropPrimaryKeyOperation.Name); Assert.True(dropPrimaryKeyOperation.HasDefaultName); }
public void Can_get_default_for_name() { var dropPrimaryKeyOperation = new DropPrimaryKeyOperation { Table = "T" }; Assert.Equal("PK_T", dropPrimaryKeyOperation.Name); Assert.True(dropPrimaryKeyOperation.HasDefaultName); }
public override void Up() { AddColumn("dbo.OrderLines", "Int", c => c.Int(name: "Boo")); var dropKey = new DropPrimaryKeyOperation { Table = "dbo.OrderLines" }; dropKey.Columns.Add("Id"); this.GetOperations().Add(dropKey); CreateIndex("OrderLines", "OrderId", name: "IX_Custom_Name", clustered: true); }
public void Generate_can_output_drop_primary_key_operation() { var migrationProvider = new SqlServerMigrationSqlGenerator(); var dropPrimaryKeyOperation = new DropPrimaryKeyOperation { Table = "T" }; var sql = migrationProvider.Generate(new[] { dropPrimaryKeyOperation }, "2008").Join(s => s.Sql, Environment.NewLine); Assert.True(sql.Contains("ALTER TABLE [T] DROP CONSTRAINT [PK_T]")); }
public void Can_get_and_set_table_and_name_and_columns() { var dropPrimaryKeyOperation = new DropPrimaryKeyOperation { Table = "T", Name = "Pk" }; dropPrimaryKeyOperation.Columns.Add("pk2"); Assert.Equal("T", dropPrimaryKeyOperation.Table); Assert.Equal("Pk", dropPrimaryKeyOperation.Name); Assert.Equal("pk2", dropPrimaryKeyOperation.Columns.Single()); Assert.False(dropPrimaryKeyOperation.HasDefaultName); }
public void Can_get_and_set_create_table_operation() { var createTableOperation = new CreateTableOperation("T"); var dropPrimaryKeyOperation = new DropPrimaryKeyOperation { CreateTableOperation = createTableOperation }; Assert.Same(createTableOperation, dropPrimaryKeyOperation.CreateTableOperation); }
public void Can_get_and_set_create_table_operation() { var createTableOperation = new CreateTableOperation("T"); var dropPrimaryKeyOperation = new DropPrimaryKeyOperation { CreateTableOperation = createTableOperation }; Assert.Same(createTableOperation, dropPrimaryKeyOperation.CreateTableOperation); }
public void Inverse_should_return_drop_operation() { var dropPrimaryKeyOperation = new DropPrimaryKeyOperation { Table = "T", Name = "Pk" }; dropPrimaryKeyOperation.Columns.Add("pk2"); var inverse = (AddPrimaryKeyOperation)dropPrimaryKeyOperation.Inverse; Assert.Equal("T", inverse.Table); Assert.Equal("Pk", inverse.Name); Assert.Equal("pk2", inverse.Columns.Single()); }
public void Can_get_and_set_table_and_name_and_columns() { var dropPrimaryKeyOperation = new DropPrimaryKeyOperation { Table = "T", Name = "Pk" }; dropPrimaryKeyOperation.Columns.Add("pk2"); Assert.Equal("T", dropPrimaryKeyOperation.Table); Assert.Equal("Pk", dropPrimaryKeyOperation.Name); Assert.Equal("pk2", dropPrimaryKeyOperation.Columns.Single()); Assert.False(dropPrimaryKeyOperation.HasDefaultName); }
public void Inverse_should_return_drop_operation() { var dropPrimaryKeyOperation = new DropPrimaryKeyOperation { Table = "T", Name = "Pk" }; dropPrimaryKeyOperation.Columns.Add("pk2"); var inverse = (AddPrimaryKeyOperation)dropPrimaryKeyOperation.Inverse; Assert.Equal("T", inverse.Table); Assert.Equal("Pk", inverse.Name); Assert.Equal("pk2", inverse.Columns.Single()); }
public void Generate_can_output_drop_primary_key_with_explicit_name() { var codeGenerator = new CSharpMigrationCodeGenerator(); var dropPrimaryKeyOperation = new DropPrimaryKeyOperation { Table = "T", Name = "PK" }; dropPrimaryKeyOperation.Columns.Add("c1"); dropPrimaryKeyOperation.Columns.Add("c2"); var generatedMigration = codeGenerator.Generate( "Migration", new MigrationOperation[] { dropPrimaryKeyOperation }, "Source", "Target", "Foo", "Bar"); Assert.Equal( @"namespace Foo { using System; using System.Data.Entity.Migrations; public partial class Bar : DbMigration { public override void Up() { DropPrimaryKey(""T"", ""PK""); } public override void Down() { AddPrimaryKey(""T"", new[] { ""c1"", ""c2"" }, name: ""PK""); } } } ", generatedMigration.UserCode); }
public void Generate_can_output_drop_primary_key_with_explicit_name() { var codeGenerator = new VisualBasicMigrationCodeGenerator(); var dropPrimaryKeyOperation = new DropPrimaryKeyOperation { Table = "T", Name = "PK" }; dropPrimaryKeyOperation.Columns.Add("c1"); dropPrimaryKeyOperation.Columns.Add("c2"); var generatedMigration = codeGenerator.Generate( "Migration", new MigrationOperation[] { dropPrimaryKeyOperation }, "Source", "Target", "Foo", "Bar"); Assert.Equal( @"Imports System Imports System.Data.Entity.Migrations Namespace Foo Public Partial Class Bar Inherits DbMigration Public Overrides Sub Up() DropPrimaryKey(""T"", ""PK"") End Sub Public Overrides Sub Down() AddPrimaryKey(""T"", New String() { ""c1"", ""c2"" }, name := ""PK"") End Sub End Class End Namespace ", generatedMigration.UserCode); }
/// <summary> /// Generates code to perform a <see cref="DropPrimaryKeyOperation" />. /// </summary> /// <param name="dropPrimaryKeyOperation"> The operation to generate code for. </param> /// <param name="writer"> Text writer to add the generated code to. </param> protected virtual void Generate(DropPrimaryKeyOperation dropPrimaryKeyOperation, IndentedTextWriter writer) { Check.NotNull(dropPrimaryKeyOperation, "dropPrimaryKeyOperation"); Check.NotNull(writer, "writer"); writer.Write("DropPrimaryKey("); writer.Write(Quote(dropPrimaryKeyOperation.Table)); if (!dropPrimaryKeyOperation.HasDefaultName) { writer.Write(", name := "); writer.Write(Quote(dropPrimaryKeyOperation.Name)); } writer.WriteLine(")"); }
/// <summary> /// Generates SQL for a <see cref="DropPrimaryKeyOperation" />. /// Generated SQL should be added using the Statement method. /// </summary> /// <param name="dropPrimaryKeyOperation"> The operation to produce SQL for. </param> protected virtual void Generate(DropPrimaryKeyOperation dropPrimaryKeyOperation) { Check.NotNull(dropPrimaryKeyOperation, "dropPrimaryKeyOperation"); using (var writer = Writer()) { writer.Write("ALTER TABLE "); writer.Write(Name(dropPrimaryKeyOperation.Table)); writer.Write(" DROP CONSTRAINT "); writer.Write(Quote(dropPrimaryKeyOperation.Name)); Statement(writer); } }
private HistoryRebuildOperationSequence( AddColumnOperation addColumnOperation, DropPrimaryKeyOperation dropPrimaryKeyOperation) : base(null) { AddColumnOperation = addColumnOperation; DropPrimaryKeyOperation = dropPrimaryKeyOperation; }
private void Convert(DropPrimaryKeyOperation dropPrimaryKeyOperation) { StringBuilder sql = new StringBuilder(); sql.Append("ALTER TABLE "); AppendTableName(dropPrimaryKeyOperation.Table, sql); sql.Append(" DROP CONSTRAINT \""); sql.Append(dropPrimaryKeyOperation.Name); sql.Append('"'); AddStatment(sql); }
/// <summary> /// Generates code to perform a <see cref="DropPrimaryKeyOperation" />. /// </summary> /// <param name="dropPrimaryKeyOperation"> The operation to generate code for. </param> /// <param name="writer"> Text writer to add the generated code to. </param> protected virtual void Generate(DropPrimaryKeyOperation dropPrimaryKeyOperation, IndentedTextWriter writer) { Check.NotNull(dropPrimaryKeyOperation, "dropPrimaryKeyOperation"); Check.NotNull(writer, "writer"); writer.Write("DropPrimaryKey("); writer.Write(Quote(dropPrimaryKeyOperation.Table)); writer.Write(", "); if (!dropPrimaryKeyOperation.HasDefaultName) { writer.Write(Quote(dropPrimaryKeyOperation.Name)); } else { writer.Write("New String() { "); writer.Write(dropPrimaryKeyOperation.Columns.Join(Quote)); writer.Write(" }"); } writer.WriteLine(")"); }
private static IEnumerable<PrimaryKeyOperation> BuildChangePrimaryKeyOperations( string oldTable, string newTable, XElement oldKey, XElement newKey) { var dropPrimaryKeyOperation = new DropPrimaryKeyOperation { Table = oldTable }; oldKey.Descendants(EdmXNames.Ssdl.PropertyRefNames).Each( pr => dropPrimaryKeyOperation.Columns.Add(pr.NameAttribute())); yield return dropPrimaryKeyOperation; var addPrimaryKeyOperation = new AddPrimaryKeyOperation { Table = newTable }; newKey.Descendants(EdmXNames.Ssdl.PropertyRefNames).Each( pr => addPrimaryKeyOperation.Columns.Add(pr.NameAttribute())); yield return addPrimaryKeyOperation; }
/// <summary> /// Gera SQL para uma operação <see cref="DropPrimaryKeyOperation" />. /// </summary> /// <param name="opeDropPrimaryKey"> The operation to produce SQL for. </param> protected virtual void Generate(DropPrimaryKeyOperation opeDropPrimaryKey) { using (var ltextWriter = TextWriter()) { ltextWriter.Write("ALTER TABLE "); ltextWriter.Write(RemoveDBO(opeDropPrimaryKey.Table)); ltextWriter.Write(" DROP CONSTRAINT "); ltextWriter.Write(opeDropPrimaryKey.Name); ComandoSQL(ltextWriter); } }
public virtual IEnumerable<MigrationOperation> GetUpgradeOperations() { if (!Exists()) { yield break; } using (var connection = CreateConnection()) { const string tableName = "dbo." + HistoryContext.DefaultTableName; using (var context = CreateContext(connection)) { var productVersionExists = false; try { using (new TransactionScope(TransactionScopeOption.Suppress)) { context.History .Select(h => h.ProductVersion) .FirstOrDefault(); } productVersionExists = true; } catch (EntityException) { } if (!productVersionExists) { yield return new DropColumnOperation(tableName, "Hash"); yield return new AddColumnOperation( tableName, new ColumnModel(PrimitiveTypeKind.String) { MaxLength = 32, Name = "ProductVersion", IsNullable = false, DefaultValue = "0.7.0.0" }); } if (!_contextKeyColumnExists) { yield return new AddColumnOperation( tableName, new ColumnModel(PrimitiveTypeKind.String) { MaxLength = 512, Name = "ContextKey", IsNullable = false, DefaultValue = _contextKey }); var dropPrimaryKeyOperation = new DropPrimaryKeyOperation { Table = tableName }; dropPrimaryKeyOperation.Columns.Add("MigrationId"); yield return dropPrimaryKeyOperation; var addPrimaryKeyOperation = new AddPrimaryKeyOperation { Table = tableName }; addPrimaryKeyOperation.Columns.Add("MigrationId"); addPrimaryKeyOperation.Columns.Add("ContextKey"); yield return addPrimaryKeyOperation; } } using (var context = new LegacyHistoryContext(connection)) { var createdOnExists = false; try { using (new TransactionScope(TransactionScopeOption.Suppress)) { context.History .Select(h => h.CreatedOn) .FirstOrDefault(); } createdOnExists = true; } catch (EntityException) { } if (createdOnExists) { yield return new DropColumnOperation(tableName, "CreatedOn"); } } } }
protected override void Generate(DropPrimaryKeyOperation dropPrimaryKeyOperation) { Contract.Requires(dropPrimaryKeyOperation != null); using (var writer = Writer()) { writer.Write("ALTER TABLE "); writer.Write(Name(dropPrimaryKeyOperation.Table)); writer.Write(" DROP CONSTRAINT "); writer.Write(Quote(dropPrimaryKeyOperation.Name)); Statement(writer); } }
public void DropPrimaryKeyOperation() { var migrationOperations = new List<MigrationOperation>(); // create table where the PK exists var createTableOperation = CreateTableOperation(); migrationOperations.Add(createTableOperation); var createDropPKOperation = new DropPrimaryKeyOperation(anonymousArguments: new { DeleteAutoIncrement = true }); createDropPKOperation.Table = "Posts"; createDropPKOperation.Columns.Add("PostId"); migrationOperations.Add(createDropPKOperation); using (BlogContext context = new BlogContext()) { if (context.Database.Exists()) context.Database.Delete(); context.Database.Create(); Assert.AreEqual(true, GenerateAndExecuteMySQLStatements(migrationOperations)); using (var conn = new MySqlConnection(context.Database.Connection.ConnectionString)) { if (conn.State == System.Data.ConnectionState.Closed) conn.Open(); // check for table creation var query = new MySqlCommand("select Count(*) from information_schema.Tables WHERE `table_name` = 'Posts' and `table_schema` = '" + conn.Database + "' ", conn); int rows = Convert.ToInt32(query.ExecuteScalar()); Assert.AreEqual(1, rows); // check if PK exists query = new MySqlCommand("select Count(*) from information_schema.table_constraints where `constraint_type` = 'primary key' and `constraint_schema` = '" + conn.Database + "' and table_name= 'Posts'", conn); rows = Convert.ToInt32(query.ExecuteScalar()); Assert.AreEqual(0, rows); //check the definition of the column that was PK query = new MySqlCommand("Select Column_name, Is_Nullable, Data_Type from information_schema.Columns where table_schema ='" + conn.Database + "' and table_name = 'Posts' and column_name ='PostId'", conn); MySqlDataReader reader = query.ExecuteReader(); while (reader.Read()) { Assert.AreEqual("PostId", reader[0].ToString()); Assert.AreEqual("NO", reader[1].ToString()); Assert.AreEqual("int", reader[2].ToString()); } reader.Close(); conn.Close(); } } }
protected virtual IEnumerable<MigrationStatement> Generate(DropPrimaryKeyOperation operation) { using (var writer = SqlWriter()) { writer.Write("ALTER TABLE "); writer.Write(Quote(ExtractName(operation.Table))); writer.Write(" DROP CONSTRAINT "); writer.Write(Quote(CreateItemName(operation.Name))); yield return Statement(writer); } }
protected override void Generate(DropPrimaryKeyOperation apo) { apo.Name = GetPrimaryKeyName(apo.Table); base.Generate(apo); }
protected override void Generate(DropPrimaryKeyOperation dropPrimaryKeyOperation, IndentedTextWriter writer) { dropPrimaryKeyOperation.Table = TrimSchemaPrefix(dropPrimaryKeyOperation.Table); base.Generate(dropPrimaryKeyOperation, writer); }
public virtual IEnumerable<MigrationOperation> GetUpgradeOperations() { if (!Exists()) { yield break; } DbConnection connection = null; try { connection = CreateConnection(); var tableName = "dbo." + HistoryContext.DefaultTableName; DbProviderManifest providerManifest; if (connection.GetProviderInfo(out providerManifest).IsSqlCe()) { tableName = HistoryContext.DefaultTableName; } using (var context = new LegacyHistoryContext(connection)) { var createdOnExists = false; try { InjectInterceptionContext(context); using (new TransactionScope(TransactionScopeOption.Suppress)) { context.History .Select(h => h.CreatedOn) .FirstOrDefault(); } createdOnExists = true; } catch (EntityException) { } if (createdOnExists) { yield return new DropColumnOperation(tableName, "CreatedOn"); } } using (var context = CreateContext(connection)) { if (!_contextKeyColumnExists) { if (_historyContextFactory != HistoryContext.DefaultFactory) { throw Error.UnableToUpgradeHistoryWhenCustomFactory(); } yield return new AddColumnOperation( tableName, new ColumnModel(PrimitiveTypeKind.String) { MaxLength = _contextKeyMaxLength, Name = "ContextKey", IsNullable = false, DefaultValue = _contextKey }); var emptyModel = new DbModelBuilder().Build(connection).GetModel(); var createTableOperation = (CreateTableOperation) new EdmModelDiffer().Diff(emptyModel, context.GetModel()).Single(); var dropPrimaryKeyOperation = new DropPrimaryKeyOperation { Table = tableName, CreateTableOperation = createTableOperation }; dropPrimaryKeyOperation.Columns.Add("MigrationId"); yield return dropPrimaryKeyOperation; yield return new AlterColumnOperation( tableName, new ColumnModel(PrimitiveTypeKind.String) { MaxLength = _migrationIdMaxLength, Name = "MigrationId", IsNullable = false }, isDestructiveChange: false); var addPrimaryKeyOperation = new AddPrimaryKeyOperation { Table = tableName }; addPrimaryKeyOperation.Columns.Add("MigrationId"); addPrimaryKeyOperation.Columns.Add("ContextKey"); yield return addPrimaryKeyOperation; } } } finally { DisposeConnection(connection); } }
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 " }; }
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 }; }
public void GenerateCanOutputDropPrimaryKeyOperation() { var migrationSqlGenerator = new PostgreSqlMigrationSqlGenerator(); var dropPrimaryKeyOperation = new DropPrimaryKeyOperation { Table = "T" }; var sql = migrationSqlGenerator.Generate(new[] { dropPrimaryKeyOperation }, "9.2").Join(s => s.Sql, Environment.NewLine); Assert.True(sql.Contains("ALTER TABLE \"T\" DROP CONSTRAINT \"PK_T\"")); }
public void TestDropPrimaryKeyOperation() { var operations = new List<MigrationOperation>(); var operation = new DropPrimaryKeyOperation(); operation.Table = "someTable"; operation.Name = "somePKName"; operations.Add(operation); var statments = new NpgsqlMigrationSqlGenerator().Generate(operations, BackendVersion.ToString()); Assert.AreEqual(1, statments.Count()); Assert.AreEqual("ALTER TABLE \"someTable\" DROP CONSTRAINT \"somePKName\"", statments.ElementAt(0).Sql); }
protected override void Generate( DropPrimaryKeyOperation dropPrimaryKeyOperation ) { dropPrimaryKeyOperation.Name = dropPrimaryKeyOperation.Name.Replace( '.', '_' ); base.Generate( dropPrimaryKeyOperation ); }
/// <summary> /// Generates code to perform a <see cref = "DropPrimaryKeyOperation" />. /// </summary> /// <param name = "dropPrimaryKeyOperation">The operation to generate code for.</param> /// <param name = "writer">Text writer to add the generated code to.</param> protected virtual void Generate(DropPrimaryKeyOperation dropPrimaryKeyOperation, IndentedTextWriter writer) { Contract.Requires(dropPrimaryKeyOperation != null); Contract.Requires(writer != null); writer.Write("DropPrimaryKey("); writer.Write(Quote(dropPrimaryKeyOperation.Table)); writer.Write(", "); if (!dropPrimaryKeyOperation.HasDefaultName) { writer.Write(Quote(dropPrimaryKeyOperation.Name)); } else { writer.Write("New String() { "); writer.Write(dropPrimaryKeyOperation.Columns.Join(Quote)); writer.Write(" }"); } writer.WriteLine(")"); }
protected virtual IEnumerable<MigrationStatement> Generate(DropPrimaryKeyOperation operation) { using (var writer = SqlWriter()) { writer.Write("DROP INDEX "); writer.Write(Quote(string.Format("{0}.{1}", SchemaName(operation.Table), FixName(operation.Name)))); yield return Statement(writer); } }
protected override void Generate(DropPrimaryKeyOperation dropPrimaryKeyOperation) { dropPrimaryKeyOperation.Name = getPkName(dropPrimaryKeyOperation.Table); base.Generate(dropPrimaryKeyOperation); }