public void GetLastModel_should_return_model_when_row() { ResetDatabase(); var historyRepository = new HistoryRepository(ConnectionString, ProviderFactory, "MyKey", null, HistoryContext.DefaultFactory); using (var context = CreateContext <ShopContext_v1>()) { var model1 = context.GetModel(); ExecuteOperations( GetCreateHistoryTableOperation(), historyRepository.CreateInsertOperation("Migration 1", model1)); ExecuteOperations( new[] { historyRepository.CreateInsertOperation("Migration 2", model1) }); string migrationId; var model2 = historyRepository.GetLastModel(out migrationId); Assert.NotNull(model2); Assert.True(XNode.DeepEquals(model1, model2)); Assert.Equal("Migration 2", migrationId); } }
public void Repository_should_work_gracefully_when_no_context_key_column() { ResetDatabase(); var historyRepository = new HistoryRepository(Mock.Of <InternalContextForMock>(), ConnectionString, ProviderFactory, "MyKey", null, HistoryContext.DefaultFactory); var createHistoryTableOperation = GetCreateHistoryTableOperation(); createHistoryTableOperation.Columns.Remove( createHistoryTableOperation.Columns.Single(c => c.Name == "ContextKey")); createHistoryTableOperation.PrimaryKey.Columns.Remove("ContextKey"); ExecuteOperations( createHistoryTableOperation, new SqlOperation( @"INSERT INTO [__MigrationHistory] ([MigrationId], [ProductVersion], [Model]) VALUES ('000000000000000_ExistingMigration', '1.0', 0x1F8B0800000000000400ECBD07601C499625262F6DCA7B7F4AF54AD7E074A10880601324D8904010ECC188CDE692EC1D69472329AB2A81CA6556655D661640CCED9DBCF7DE7BEFBDF7DE7BEFBDF7BA3B9D4E27F7DFFF3F5C6664016CF6CE4ADAC99E2180AAC81F3F7E7C1F3F22FEC7BFF71F7CFC7BBC5B94E9655E3745B5FCECA3DDF1CE4769BE9C56B36279F1D947EBF67CFBE0A3DFE3E8374E1E9FCE16EFD29F34EDF6D08EDE5C369F7D346FDBD5A3BB779BE93C5F64CD78514CEBAAA9CEDBF1B45ADCCD66D5DDBD9D9D83BBBB3B777302F111C14AD3C7AFD6CBB658E4FC07FD79522DA7F9AA5D67E517D52C2F1BFD9CBE79CD50D317D9226F56D934FFEC236ADB54657EBC5A95C5346B099DDD8FD2E3B2C80895D77979FE9E78ED3C045E1FD91EA9CF53C2ADBDA67EDAAC58E63577CEFDB6F9BBF6A3F4AEC3EEAEA067867177601C8FBFC8562B22A8372EFD247DAD83DA7EFDFE782F04C6DD69B3097DDB535BD5D945DEF916E39AE5CF8ABA699F666D36C99AFCA3F464B688348B0C5F61DBF177C6F958FBBCCDA4769090261FA52FEBEAB2980181D7D74D9B2FC668307EFD8BCA93B2C897AD6BF045B62CCEF3A67D53BDCDC1A044A3AFCF160FEFEEEC812DEE36CDACBC156FF468B8814BFA54797CD71788C74FF3A6B820E89E782CF32958DD01356DCE96E715D16095D7EDF5EBBCF571354DCCD78AEC17799BCD08CFE3BA2DCEB3694B5F4FF3A6A179FB28FDC9AC5C5393D3C5249F9D2DBF5CB7AB757BDC34F962525E8763DADC3F8B4288F3E32F57F8CB6383AF3F0442B3A021E45F2E9FAC8B7266F17E969521ED874160CA3ECFE9735621AF5BFA995F5C5B482FAAE52D0129F99EE6AB7C3923967C932F5625016BBE5CBECE2EF361DC6EA66148B1C74F8BECA2CE163E05E513C5E475463D7B5D5007FE1BAE3FFAF3F15D28F4A3FF270000FFFF4817137F02060000)")); Assert.True(historyRepository.Exists()); string _; Assert.NotNull(historyRepository.GetLastModel(out _, out _)); Assert.NotEmpty(historyRepository.GetMigrationsSince("0")); Assert.NotNull(historyRepository.GetModel("000000000000000_ExistingMigration", out _)); Assert.Equal("000000000000000_ExistingMigration", historyRepository.GetMigrationId("ExistingMigration")); }
public void GetLastModel_should_return_model_when_row() { ResetDatabase(); var historyRepository = new HistoryRepository(Mock.Of <InternalContextForMock>(), ConnectionString, ProviderFactory, "MyKey", null, HistoryContext.DefaultFactory); using (var context = CreateContext <ShopContext_v1>()) { var model1 = new VersionedModel(context.GetModel()); ExecuteOperations( GetCreateHistoryTableOperation(), historyRepository.CreateInsertOperation("Migration 1", model1)); ExecuteOperations( new[] { historyRepository.CreateInsertOperation("Migration 2", model1) }); string migrationId, productVersion; var model2 = historyRepository.GetLastModel(out migrationId, out productVersion); Assert.NotNull(model2); Assert.True(XNode.DeepEquals(model1.Model, model2)); Assert.Equal("Migration 2", migrationId); Assert.Equal(typeof(DbContext).Assembly().GetInformationalVersion(), productVersion); } }
public void GetLastModel_should_return_model_based_on_passed_context_key() { ResetDatabase(); var historyRepository1 = new HistoryRepository(Mock.Of <InternalContextForMock>(), ConnectionString, ProviderFactory, "Key1", null, HistoryContext.DefaultFactory); using (var context = CreateContext <ShopContext_v1>()) { var model = context.GetModel(); var versionedModel = new VersionedModel(model); ExecuteOperations( GetCreateHistoryTableOperation(), historyRepository1.CreateInsertOperation("Migration 1", versionedModel)); var historyRepository2 = new HistoryRepository(Mock.Of <InternalContextForMock>(), ConnectionString, ProviderFactory, "Key2", null, HistoryContext.DefaultFactory); ExecuteOperations( new[] { historyRepository2.CreateInsertOperation("Migration 2", versionedModel) }); string migrationId, productVersion; model = historyRepository1.GetLastModel(out migrationId, out productVersion, "Key2"); Assert.NotNull(model); Assert.Equal("Migration 2", migrationId); } }
public void GetLastModel_should_return_model_based_on_passed_context_key_when_custom_default_schema() { ResetDatabase(); var historyRepository = new HistoryRepository(ConnectionString, ProviderFactory, "LegacyKey") { CurrentSchema = "foo" }; var model = CreateContext <ShopContext_v1>().GetModel(); ExecuteOperations( GetCreateHistoryTableOperation(historyRepository.CurrentSchema), historyRepository.CreateInsertOperation("Migration", model)); historyRepository = new HistoryRepository(ConnectionString, ProviderFactory, "NewKey", new[] { "foo" }); string migrationId; model = historyRepository.GetLastModel(out migrationId, "LegacyKey"); Assert.NotNull(model); Assert.Equal("Migration", migrationId); }
public void GetLastModel_should_return_model_based_on_passed_context_key() { ResetDatabase(); var historyRepository1 = new HistoryRepository(ConnectionString, ProviderFactory, "Key1"); var model = CreateContext <ShopContext_v1>().GetModel(); ExecuteOperations( GetCreateHistoryTableOperation(), historyRepository1.CreateInsertOperation("Migration 1", model)); var historyRepository2 = new HistoryRepository(ConnectionString, ProviderFactory, "Key2"); ExecuteOperations( new[] { historyRepository2.CreateInsertOperation("Migration 2", model) }); string migrationId; model = historyRepository1.GetLastModel(out migrationId, "Key2"); Assert.NotNull(model); Assert.Equal("Migration 2", migrationId); }
public void GetLastModel_should_return_model_based_on_passed_context_key_when_custom_default_schema() { ResetDatabase(); var historyRepository = new HistoryRepository(Mock.Of <InternalContextForMock>(), ConnectionString, ProviderFactory, "LegacyKey", null, HistoryContext.DefaultFactory) { CurrentSchema = "foo" }; using (var context = CreateContext <ShopContext_v1>()) { var model = context.GetModel(); var versionedModel = new VersionedModel(model); ExecuteOperations( GetCreateHistoryTableOperation(historyRepository.CurrentSchema), historyRepository.CreateInsertOperation("Migration", versionedModel)); historyRepository = new HistoryRepository(Mock.Of <InternalContextForMock>(), ConnectionString, ProviderFactory, "NewKey", null, HistoryContext.DefaultFactory, schemas: new[] { "foo" }); string migrationId, _; model = historyRepository.GetLastModel(out migrationId, out _, "LegacyKey"); Assert.NotNull(model); Assert.Equal("Migration", migrationId); } }
public void Can_explicit_update_when_custom_history_factory() { ResetDatabase(); var migrator = CreateMigrator<ShopContext_v1>(historyContextFactory: _testHistoryContextFactoryA); var generatedMigration = new MigrationScaffolder(migrator.Configuration).Scaffold("Migration"); migrator = CreateMigrator<ShopContext_v1>( automaticMigrationsEnabled: false, scaffoldedMigrations: generatedMigration, historyContextFactory: _testHistoryContextFactoryA); migrator.Update(); Assert.True(TableExists("MigrationsCustomers")); Assert.True(TableExists("__Migrations")); migrator.Update("0"); Assert.False(TableExists("MigrationsCustomers")); Assert.False(TableExists("__Migrations")); var historyRepository = new HistoryRepository(ConnectionString, ProviderFactory, "MyKey", null); Assert.Null(historyRepository.GetLastModel()); }
public void GetLastModel_should_return_null_when_no_data() { ResetDatabase(); var historyRepository = new HistoryRepository(ConnectionString, ProviderFactory, "MyKey", null, HistoryContext.DefaultFactory); var modelBuilder = new DbModelBuilder(); modelBuilder.Entity <MigrationsCustomer>(); Assert.Null(historyRepository.GetLastModel()); }
public void GetLastModel_should_return_null_when_no_table() { ResetDatabase(); var historyRepository = new HistoryRepository(Mock.Of <InternalContextForMock>(), ConnectionString, ProviderFactory, "MyKey", null, HistoryContext.DefaultFactory); var modelBuilder = new DbModelBuilder(); modelBuilder.Entity <MigrationsCustomer>(); string _; Assert.Null(historyRepository.GetLastModel(out _, out _)); }
public void GetLastModel_should_return_null_when_no_database() { var historyRepository = new HistoryRepository( ConnectionString.Replace(DatabaseProviderFixture.DefaultDatabaseName, "NoSuchDatabase"), ProviderFactory, "MyKey"); var modelBuilder = new DbModelBuilder(); modelBuilder.Entity <MigrationsCustomer>(); Assert.Null(historyRepository.GetLastModel()); }
public void GetLastModel_should_return_model_based_on_passed_context_key() { ResetDatabase(); var historyRepository1 = new HistoryRepository(Mock.Of<InternalContextForMock>(), ConnectionString, ProviderFactory, "Key1", null, HistoryContext.DefaultFactory); using (var context = CreateContext<ShopContext_v1>()) { var model = context.GetModel(); ExecuteOperations( GetCreateHistoryTableOperation(), historyRepository1.CreateInsertOperation("Migration 1", model)); var historyRepository2 = new HistoryRepository(Mock.Of<InternalContextForMock>(), ConnectionString, ProviderFactory, "Key2", null, HistoryContext.DefaultFactory); ExecuteOperations( new[] { historyRepository2.CreateInsertOperation("Migration 2", model) }); string migrationId, _; model = historyRepository1.GetLastModel(out migrationId, out _, "Key2"); Assert.NotNull(model); Assert.Equal("Migration 2", migrationId); } }
public void Update_down_when_explicit_should_migrate_to_target_version() { ResetDatabase(); var migrator = CreateMigrator<ShopContext_v1>(); var generatedMigration = new MigrationScaffolder(migrator.Configuration).Scaffold("Migration1"); migrator = CreateMigrator<ShopContext_v1>( automaticMigrationsEnabled: false, scaffoldedMigrations: generatedMigration); migrator.Update(); Assert.True(TableExists("MigrationsCustomers")); migrator.Update(DbMigrator.InitialDatabase); Assert.False(TableExists("MigrationsCustomers")); var historyRepository = new HistoryRepository(ConnectionString, ProviderFactory); Assert.Null(historyRepository.GetLastModel()); }
public void GetLastModel_should_return_model_based_on_passed_context_key_when_custom_default_schema() { ResetDatabase(); var historyRepository = new HistoryRepository(ConnectionString, ProviderFactory, "LegacyKey", null) { CurrentSchema = "foo" }; var model = CreateContext<ShopContext_v1>().GetModel(); ExecuteOperations( GetCreateHistoryTableOperation(historyRepository.CurrentSchema), historyRepository.CreateInsertOperation("Migration", model)); historyRepository = new HistoryRepository(ConnectionString, ProviderFactory, "NewKey", null, new[] { "foo" }); string migrationId; model = historyRepository.GetLastModel(out migrationId, "LegacyKey"); Assert.NotNull(model); Assert.Equal("Migration", migrationId); }
public void Update_down_when_explicit_and_automatic_should_migrate_to_target_version() { ResetDatabase(); var historyRepository = new HistoryRepository(ConnectionString, ProviderFactory, "MyKey", null); var migrator = CreateMigrator<ShopContext_v2>(); migrator.Update(); Assert.True(TableExists("crm.tbl_customers")); migrator = CreateMigrator<ShopContext_v3>(automaticDataLossEnabled: true); var generatedMigration = new MigrationScaffolder(migrator.Configuration).Scaffold("Migration"); migrator = CreateMigrator<ShopContext_v3>( automaticDataLossEnabled: true, scaffoldedMigrations: generatedMigration); migrator.Update(); Assert.True(TableExists("crm.tbl_customers")); migrator.Update(DbMigrator.InitialDatabase); Assert.False(TableExists("MigrationsStores")); Assert.False(TableExists("tbl_customers")); Assert.Null(historyRepository.GetLastModel()); }
public void GetLastModel_should_return_null_when_no_database() { var historyRepository = new HistoryRepository( ConnectionString.Replace(DatabaseProviderFixture.DefaultDatabaseName, "NoSuchDatabase"), ProviderFactory, "MyKey", null); var modelBuilder = new DbModelBuilder(); modelBuilder.Entity<MigrationsCustomer>(); Assert.Null(historyRepository.GetLastModel()); }
public void GetLastModel_should_return_model_based_on_passed_context_key_when_custom_default_schema() { ResetDatabase(); var historyRepository = new HistoryRepository(Mock.Of<InternalContextForMock>(), ConnectionString, ProviderFactory, "LegacyKey", null, HistoryContext.DefaultFactory) { CurrentSchema = "foo" }; using (var context = CreateContext<ShopContext_v1>()) { var model = context.GetModel(); var versionedModel = new VersionedModel(model); ExecuteOperations( GetCreateHistoryTableOperation(historyRepository.CurrentSchema), historyRepository.CreateInsertOperation("Migration", versionedModel)); historyRepository = new HistoryRepository(Mock.Of<InternalContextForMock>(), ConnectionString, ProviderFactory, "NewKey", null, HistoryContext.DefaultFactory, schemas: new[] { "foo" }); string migrationId, _; model = historyRepository.GetLastModel(out migrationId, out _, "LegacyKey"); Assert.NotNull(model); Assert.Equal("Migration", migrationId); } }
public void GetLastModel_should_return_model_when_row() { ResetDatabase(); var historyRepository = new HistoryRepository(ConnectionString, ProviderFactory, "MyKey", null); var model1 = CreateContext<ShopContext_v1>().GetModel(); ExecuteOperations( GetCreateHistoryTableOperation(), historyRepository.CreateInsertOperation("Migration 1", model1)); ExecuteOperations( new[] { historyRepository.CreateInsertOperation("Migration 2", model1) }); string migrationId; var model2 = historyRepository.GetLastModel(out migrationId); Assert.NotNull(model2); Assert.True(XNode.DeepEquals(model1, model2)); Assert.Equal("Migration 2", migrationId); }
public void Update_down_when_automatic_and_multiple_steps_should_migrate_to_target_version() { ResetDatabase(); var historyRepository = new HistoryRepository(ConnectionString, ProviderFactory, "MyKey", null); var migrator = CreateMigrator<ShopContext_v2>(automaticDataLossEnabled: true); migrator.Update(); Assert.True(TableExists("crm.tbl_customers")); migrator = CreateMigrator<ShopContext_v3>(automaticDataLossEnabled: true); migrator.Update(); Assert.True(TableExists("MigrationsStores")); migrator.Update(DbMigrator.InitialDatabase); Assert.False(TableExists("crm.tbl_customers")); Assert.False(TableExists("MigrationsStores")); Assert.Null(historyRepository.GetLastModel()); }
public void GetLastModel_should_return_null_when_no_data() { ResetDatabase(); var historyRepository = new HistoryRepository(ConnectionString, ProviderFactory, "MyKey", null); var modelBuilder = new DbModelBuilder(); modelBuilder.Entity<MigrationsCustomer>(); Assert.Null(historyRepository.GetLastModel()); }
public void Can_use_per_provider_factory() { ResetDatabase(); try { MutableResolver.AddResolver<Func<DbConnection, string, HistoryContext>>(_ => _testHistoryContextFactoryA); var migrator = CreateMigrator<ShopContext_v1>(); var generatedMigration = new MigrationScaffolder(migrator.Configuration).Scaffold("Migration"); migrator = CreateMigrator<ShopContext_v1>( automaticMigrationsEnabled: false, scaffoldedMigrations: generatedMigration); migrator.Update(); Assert.True(TableExists("MigrationsCustomers")); Assert.True(TableExists("__Migrations")); migrator.Update("0"); Assert.False(TableExists("MigrationsCustomers")); Assert.False(TableExists("__Migrations")); var historyRepository = new HistoryRepository(ConnectionString, ProviderFactory, "MyKey", null); Assert.Null(historyRepository.GetLastModel()); } finally { MutableResolver.ClearResolvers(); } }
public void GetLastModel_should_return_model_based_on_passed_context_key() { ResetDatabase(); var historyRepository1 = new HistoryRepository(ConnectionString, ProviderFactory, "Key1", null); var model = CreateContext<ShopContext_v1>().GetModel(); ExecuteOperations( GetCreateHistoryTableOperation(), historyRepository1.CreateInsertOperation("Migration 1", model)); var historyRepository2 = new HistoryRepository(ConnectionString, ProviderFactory, "Key2", null); ExecuteOperations( new[] { historyRepository2.CreateInsertOperation("Migration 2", model) }); string migrationId; model = historyRepository1.GetLastModel(out migrationId, "Key2"); Assert.NotNull(model); Assert.Equal("Migration 2", migrationId); }
public void GetLastModel_should_return_null_when_no_data() { ResetDatabase(); var historyRepository = new HistoryRepository(Mock.Of<InternalContextForMock>(), ConnectionString, ProviderFactory, "MyKey", null, HistoryContext.DefaultFactory); var modelBuilder = new DbModelBuilder(); modelBuilder.Entity<MigrationsCustomer>(); string _; Assert.Null(historyRepository.GetLastModel(out _, out _)); }
public void Repository_should_work_gracefully_when_no_context_key_column() { ResetDatabase(); var historyRepository = new HistoryRepository(ConnectionString, ProviderFactory, "MyKey", null); var createHistoryTableOperation = GetCreateHistoryTableOperation(); createHistoryTableOperation.Columns.Remove( createHistoryTableOperation.Columns.Single(c => c.Name == "ContextKey")); createHistoryTableOperation.PrimaryKey.Columns.Remove("ContextKey"); ExecuteOperations( createHistoryTableOperation, new SqlOperation( @"INSERT INTO [__MigrationHistory] ([MigrationId], [ProductVersion], [Model]) VALUES ('000000000000000_ExistingMigration', '1.0', 0x1F8B0800000000000400ECBD07601C499625262F6DCA7B7F4AF54AD7E074A10880601324D8904010ECC188CDE692EC1D69472329AB2A81CA6556655D661640CCED9DBCF7DE7BEFBDF7DE7BEFBDF7BA3B9D4E27F7DFFF3F5C6664016CF6CE4ADAC99E2180AAC81F3F7E7C1F3F22FEC7BFF71F7CFC7BBC5B94E9655E3745B5FCECA3DDF1CE4769BE9C56B36279F1D947EBF67CFBE0A3DFE3E8374E1E9FCE16EFD29F34EDF6D08EDE5C369F7D346FDBD5A3BB779BE93C5F64CD78514CEBAAA9CEDBF1B45ADCCD66D5DDBD9D9D83BBBB3B777302F111C14AD3C7AFD6CBB658E4FC07FD79522DA7F9AA5D67E517D52C2F1BFD9CBE79CD50D317D9226F56D934FFEC236ADB54657EBC5A95C5346B099DDD8FD2E3B2C80895D77979FE9E78ED3C045E1FD91EA9CF53C2ADBDA67EDAAC58E63577CEFDB6F9BBF6A3F4AEC3EEAEA067867177601C8FBFC8562B22A8372EFD247DAD83DA7EFDFE782F04C6DD69B3097DDB535BD5D945DEF916E39AE5CF8ABA699F666D36C99AFCA3F464B688348B0C5F61DBF177C6F958FBBCCDA4769090261FA52FEBEAB2980181D7D74D9B2FC668307EFD8BCA93B2C897AD6BF045B62CCEF3A67D53BDCDC1A044A3AFCF160FEFEEEC812DEE36CDACBC156FF468B8814BFA54797CD71788C74FF3A6B820E89E782CF32958DD01356DCE96E715D16095D7EDF5EBBCF571354DCCD78AEC17799BCD08CFE3BA2DCEB3694B5F4FF3A6A179FB28FDC9AC5C5393D3C5249F9D2DBF5CB7AB757BDC34F962525E8763DADC3F8B4288F3E32F57F8CB6383AF3F0442B3A021E45F2E9FAC8B7266F17E969521ED874160CA3ECFE9735621AF5BFA995F5C5B482FAAE52D0129F99EE6AB7C3923967C932F5625016BBE5CBECE2EF361DC6EA66148B1C74F8BECA2CE163E05E513C5E475463D7B5D5007FE1BAE3FFAF3F15D28F4A3FF270000FFFF4817137F02060000)")); Assert.True(historyRepository.Exists()); Assert.NotNull(historyRepository.GetLastModel()); Assert.NotEmpty(historyRepository.GetMigrationsSince("0")); Assert.NotNull(historyRepository.GetModel("000000000000000_ExistingMigration")); Assert.Equal("000000000000000_ExistingMigration", historyRepository.GetMigrationId("ExistingMigration")); }
public void GetLastModel_should_return_model_when_row() { ResetDatabase(); var historyRepository = new HistoryRepository(Mock.Of<InternalContextForMock>(), ConnectionString, ProviderFactory, "MyKey", null, HistoryContext.DefaultFactory); using (var context = CreateContext<ShopContext_v1>()) { var model1 = context.GetModel(); ExecuteOperations( GetCreateHistoryTableOperation(), historyRepository.CreateInsertOperation("Migration 1", model1)); ExecuteOperations( new[] { historyRepository.CreateInsertOperation("Migration 2", model1) }); string migrationId, productVersion; var model2 = historyRepository.GetLastModel(out migrationId, out productVersion); Assert.NotNull(model2); Assert.True(XNode.DeepEquals(model1, model2)); Assert.Equal("Migration 2", migrationId); Assert.Equal(typeof(DbContext).Assembly().GetInformationalVersion(), productVersion); } }