Пример #1
0
        public void Can_upgrade_from_5_and_existing_database_migrations_still_work()
        {
            ResetDatabase();

            var migrationsConfiguration
                = new Ef5MigrationsConfiguration
                {
                TargetDatabase
                    = new DbConnectionInfo(ConnectionString, TestDatabase.ProviderName)
                };

            var migrator = new DbMigrator(migrationsConfiguration);

            migrator.Update();

            var historyRepository
                = new HistoryRepository(
                      ConnectionString, ProviderFactory, migrationsConfiguration.ContextKey, migrationsConfiguration.CommandTimeout);

            ExecuteOperations(
                new MigrationOperation[]
            {
                GetDropHistoryTableOperation(),
                GetCreateHistoryTableOperation()
            });

            var model = CreateContext <Ef5MigrationsContext>().GetModel();

            // create v5 history rows
            ExecuteOperations(
                new[]
            {
                historyRepository.CreateInsertOperation("201112202056275_InitialCreate", model),
                historyRepository.CreateInsertOperation("201112202056573_AddUrlToBlog", model)
            });

            migrator.Update("0");

            Assert.False(TableExists("dbo.Blogs"));
            Assert.False(TableExists("dbo." + HistoryContext.DefaultTableName));
        }
Пример #2
0
        public void Can_upgrade_from_5_and_existing_code_auto_migrations_still_work()
        {
            ResetDatabase();

            var migrator = CreateMigrator <ShopContext_v1>();

            migrator.Update();

            var historyRepository
                = new HistoryRepository(ConnectionString, ProviderFactory, "MyKey", null);

            // create v5 history rows
            ExecuteOperations(
                new[]
            {
                GetDropHistoryTableOperation(),
                GetCreateHistoryTableOperation(),
                historyRepository
                .CreateInsertOperation(
                    "201112202056275_NoHistoryModelAutomaticMigration",
                    CreateContext <ShopContext_v1>().GetModel())
            });

            migrator = CreateMigrator <ShopContext_v2>();

            var scaffoldedMigration
                = new MigrationScaffolder(migrator.Configuration).Scaffold("Migration_v2");

            ResetDatabase();

            migrator
                = CreateMigrator <ShopContext_v2>(
                      scaffoldedMigrations: scaffoldedMigration,
                      automaticDataLossEnabled: true);

            migrator.Update();

            Assert.True(TableExists("crm.tbl_customers"));
            Assert.True(TableExists("dbo." + HistoryContext.DefaultTableName));

            migrator.Update("0");

            Assert.False(TableExists("crm.tbl_customers"));
            Assert.False(TableExists("dbo." + HistoryContext.DefaultTableName));
        }
Пример #3
0
        private void ExecuteOperations(
            string migrationId, XDocument targetModel, IEnumerable <MigrationOperation> operations, bool downgrading, bool auto = false)
        {
            DebugCheck.NotEmpty(migrationId);
            DebugCheck.NotNull(targetModel);
            DebugCheck.NotNull(operations);

            FillInForeignKeyOperations(operations, targetModel);

            var newTableForeignKeys
                = (from ct in operations.OfType <CreateTableOperation>()
                   from afk in operations.OfType <AddForeignKeyOperation>()
                   where ct.Name.EqualsIgnoreCase(afk.DependentTable)
                   select afk)
                  .ToList();

            var orderedOperations
                = operations
                  .Except(newTableForeignKeys)
                  .Concat(newTableForeignKeys)
                  .ToList();

            var createHistoryOperation
                = operations
                  .OfType <CreateTableOperation>()
                  .SingleOrDefault(o => o.IsSystem);

            if (createHistoryOperation != null)
            {
                _historyRepository.CurrentSchema
                    = createHistoryOperation.Name.ToDatabaseName().Schema;
            }

            var moveHistoryOperation
                = operations
                  .OfType <MoveTableOperation>()
                  .SingleOrDefault(o => o.IsSystem);

            if (moveHistoryOperation != null)
            {
                _historyRepository.CurrentSchema = moveHistoryOperation.NewSchema;

                moveHistoryOperation.ContextKey = _configuration.ContextKey;
            }

            if (!downgrading)
            {
                orderedOperations.Add(_historyRepository.CreateInsertOperation(migrationId, targetModel));
            }
            else if (!operations.Any(o => o.IsSystem && o is DropTableOperation))
            {
                orderedOperations.Add(_historyRepository.CreateDeleteOperation(migrationId));
            }

            var migrationStatements = SqlGenerator.Generate(orderedOperations, _providerManifestToken);

            if (auto)
            {
                // Filter duplicates when auto-migrating. Duplicates can be caused by
                // duplicates in the model such as shared FKs.

                migrationStatements
                    = migrationStatements.Distinct((m1, m2) => string.Equals(m1.Sql, m2.Sql, StringComparison.Ordinal));
            }

            base.ExecuteStatements(migrationStatements);

            if (operations.Any(o => o.IsSystem))
            {
                _historyRepository.ResetExists();
            }
        }