Example #1
0
        protected virtual IReadOnlyList <SqlStatement> GenerateUpdateDatabaseSql(
            IReadOnlyList <IMigrationMetadata> downgradeMigrations,
            IReadOnlyList <IMigrationMetadata> upgradeMigrations)
        {
            var sqlStatements = new List <SqlStatement>();

            foreach (var migration in downgradeMigrations)
            {
                var database        = DatabaseBuilder.GetDatabase(migration.TargetModel);
                var ddlSqlGenerator = DdlSqlGeneratorFactory.Create(database);

                sqlStatements.AddRange(
                    ddlSqlGenerator.Generate(
                        migration.DowngradeOperations, generateIdempotentSql: true));

                sqlStatements.AddRange(
                    HistoryRepository.GenerateDeleteMigrationSql(migration, DmlSqlGenerator));
            }

            foreach (var migration in upgradeMigrations)
            {
                var database        = DatabaseBuilder.GetDatabase(migration.TargetModel);
                var ddlSqlGenerator = DdlSqlGeneratorFactory.Create(database);

                sqlStatements.AddRange(
                    ddlSqlGenerator.Generate(
                        migration.UpgradeOperations, generateIdempotentSql: true));

                sqlStatements.AddRange(
                    HistoryRepository.GenerateInsertMigrationSql(migration, DmlSqlGenerator));
            }

            return(sqlStatements);
        }
Example #2
0
        protected virtual IReadOnlyList <SqlStatement> GenerateUpdateDatabaseSql(
            IReadOnlyList <IMigrationMetadata> migrations,
            IReadOnlyList <int> downgradeIndexes,
            IReadOnlyList <int> upgradeIndexes,
            bool historyRepositoryExists,
            bool removeHistoryRepository)
        {
            var sqlStatements = new List <SqlStatement>();

            if (!historyRepositoryExists &&
                upgradeIndexes.Count > 0)
            {
                var targetDatabase  = ModelDiffer.DatabaseBuilder.GetDatabase(HistoryRepository.HistoryModel);
                var ddlSqlGenerator = DdlSqlGeneratorFactory.Create();

                sqlStatements.AddRange(ddlSqlGenerator.Generate(ModelDiffer.CreateSchema(targetDatabase)));
            }

            foreach (var index in downgradeIndexes)
            {
                var migration       = migrations[index];
                var sourceDatabase  = ModelDiffer.DatabaseBuilder.GetDatabase(migration.TargetModel);
                var ddlSqlGenerator = DdlSqlGeneratorFactory.Create(sourceDatabase);

                sqlStatements.AddRange(ddlSqlGenerator.Generate(migration.DowngradeOperations));

                sqlStatements.AddRange(
                    HistoryRepository.GenerateDeleteMigrationSql(migration, DmlSqlGenerator));
            }

            foreach (var index in upgradeIndexes)
            {
                var migration       = migrations[index];
                var sourceDatabase  = ModelDiffer.DatabaseBuilder.GetDatabase(GetSourceModel(migrations, index));
                var ddlSqlGenerator = DdlSqlGeneratorFactory.Create(sourceDatabase);

                sqlStatements.AddRange(ddlSqlGenerator.Generate(migration.UpgradeOperations));

                sqlStatements.AddRange(
                    HistoryRepository.GenerateInsertMigrationSql(migration, DmlSqlGenerator));
            }

            if (historyRepositoryExists && removeHistoryRepository)
            {
                var sourceDatabase  = ModelDiffer.DatabaseBuilder.GetDatabase(HistoryRepository.HistoryModel);
                var ddlSqlGenerator = DdlSqlGeneratorFactory.Create(sourceDatabase);

                sqlStatements.AddRange(ddlSqlGenerator.Generate(ModelDiffer.DropSchema(sourceDatabase)));
            }

            return(sqlStatements);
        }
Example #3
0
        protected virtual IReadOnlyList <SqlBatch> ApplyMigration(int index, bool simulate)
        {
            var migration       = MigrationAssembly.Migrations[index];
            var targetModel     = migration.GetTargetModel();
            var ddlSqlGenerator = DdlSqlGeneratorFactory.Create(targetModel);

            var statements = ddlSqlGenerator.Generate(migration.GetUpgradeOperations())
                             .Concat(HistoryRepository.GenerateInsertMigrationSql(migration.GetMetadata(), DmlSqlGenerator))
                             .ToList();

            if (simulate)
            {
                return(statements);
            }

            Logger.ApplyingMigration(migration.GetMigrationId());

            ExecuteSqlBatches(statements);

            return(statements);
        }
Example #4
0
        protected virtual IReadOnlyList <SqlStatement> ApplyMigration(int index, bool simulate)
        {
            var migration       = MigrationAssembly.Migrations[index];
            var sourceDatabase  = ModelDiffer.DatabaseBuilder.GetDatabase(GetSourceModel(index));
            var ddlSqlGenerator = DdlSqlGeneratorFactory.Create(sourceDatabase);

            var statements = ddlSqlGenerator.Generate(migration.GetUpgradeOperations())
                             .Concat(HistoryRepository.GenerateInsertMigrationSql(migration.GetMetadata(), DmlSqlGenerator))
                             .ToArray();

            if (simulate)
            {
                return(statements);
            }

            Logger.ApplyingMigration(migration.GetMigrationId());

            ExecuteStatements(statements);

            return(statements);
        }
        public void Generate_insert_migration_sql()
        {
            using (var context = new Context())
            {
                var historyRepository = new HistoryRepository(context.Configuration);

                var sqlStatements = historyRepository.GenerateInsertMigrationSql(
                    new MigrationMetadata("000000000000001_Foo"), new DmlSqlGenerator());

                Assert.Equal(1, sqlStatements.Count);
                Assert.Equal(
                    @"INSERT INTO ""__MigrationHistory"" (""MigrationId"", ""ContextKey"") VALUES ('000000000000001_Foo', 'Microsoft.Data.Entity.Migrations.Tests.Infrastructure.HistoryRepositoryTest+Context')",
                    sqlStatements[0].Sql);
            }
        }