Beispiel #1
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);
        }
Beispiel #2
0
        protected virtual IReadOnlyList <SqlBatch> CreateHistoryTable(bool simulate)
        {
            var targetModel     = HistoryRepository.HistoryModel;
            var ddlSqlGenerator = DdlSqlGeneratorFactory.Create(targetModel);

            var batches = ddlSqlGenerator.Generate(ModelDiffer.CreateSchema(targetModel)).ToArray();

            if (simulate)
            {
                return(batches);
            }

            Logger.CreatingHistoryTable();

            ExecuteSqlBatches(batches);

            return(batches);
        }
Beispiel #3
0
        protected virtual IReadOnlyList <SqlStatement> CreateHistoryTable(bool simulate)
        {
            var targetDatabase  = ModelDiffer.DatabaseBuilder.GetDatabase(HistoryRepository.HistoryModel);
            var ddlSqlGenerator = DdlSqlGeneratorFactory.Create();

            var statements = ddlSqlGenerator.Generate(ModelDiffer.CreateSchema(targetDatabase)).ToArray();

            if (simulate)
            {
                return(statements);
            }

            Logger.CreatingHistoryTable();

            ExecuteStatements(statements);

            return(statements);
        }