Пример #1
0
        public Migrator(
            [NotNull] DbContextConfiguration contextConfiguration,
            [NotNull] HistoryRepository historyRepository,
            [NotNull] MigrationAssembly migrationAssembly,
            [NotNull] ModelDiffer modelDiffer,
            [NotNull] IMigrationOperationSqlGeneratorFactory ddlSqlGeneratorFactory,
            [NotNull] SqlGenerator dmlSqlGenerator,
            [NotNull] SqlStatementExecutor sqlExecutor)
        {
            Check.NotNull(contextConfiguration, "contextConfiguration");
            Check.NotNull(historyRepository, "historyRepository");
            Check.NotNull(migrationAssembly, "migrationAssembly");
            Check.NotNull(modelDiffer, "modelDiffer");
            Check.NotNull(ddlSqlGeneratorFactory, "ddlSqlGeneratorFactory");
            Check.NotNull(dmlSqlGenerator, "dmlSqlGenerator");
            Check.NotNull(sqlExecutor, "sqlExecutor");

            _contextConfiguration   = contextConfiguration;
            _historyRepository      = historyRepository;
            _migrationAssembly      = migrationAssembly;
            _modelDiffer            = modelDiffer;
            _ddlSqlGeneratorFactory = ddlSqlGeneratorFactory;
            _dmlSqlGenerator        = dmlSqlGenerator;
            _sqlExecutor            = sqlExecutor;
        }
Пример #2
0
        public Migrator(
            [NotNull] HistoryRepository historyRepository,
            [NotNull] MigrationAssembly migrationAssembly,
            [NotNull] ModelDiffer modelDiffer,
            [NotNull] IMigrationOperationSqlGeneratorFactory ddlSqlGeneratorFactory,
            [NotNull] SqlGenerator dmlSqlGenerator,
            [NotNull] SqlStatementExecutor sqlExecutor,
            [NotNull] RelationalDataStoreCreator storeCreator,
            [NotNull] RelationalConnection connection,
            [NotNull] ILoggerFactory loggerFactory)
        {
            Check.NotNull(historyRepository, "historyRepository");
            Check.NotNull(migrationAssembly, "migrationAssembly");
            Check.NotNull(modelDiffer, "modelDiffer");
            Check.NotNull(ddlSqlGeneratorFactory, "ddlSqlGeneratorFactory");
            Check.NotNull(dmlSqlGenerator, "dmlSqlGenerator");
            Check.NotNull(sqlExecutor, "sqlExecutor");
            Check.NotNull(storeCreator, "storeCreator");
            Check.NotNull(connection, "connection");
            Check.NotNull(loggerFactory, "loggerFactory");

            _historyRepository      = historyRepository;
            _migrationAssembly      = migrationAssembly;
            _modelDiffer            = modelDiffer;
            _ddlSqlGeneratorFactory = ddlSqlGeneratorFactory;
            _dmlSqlGenerator        = dmlSqlGenerator;
            _sqlExecutor            = sqlExecutor;
            _storeCreator           = storeCreator;
            _connection             = connection;
            _logger = new DbContextService <ILogger>(loggerFactory.Create <Migrator>);
        }
Пример #3
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);
        }
Пример #4
0
        protected virtual IReadOnlyList <SqlBatch> DropHistoryTable(bool simulate)
        {
            var sourceModel     = HistoryRepository.HistoryModel;
            var ddlSqlGenerator = DdlSqlGeneratorFactory.Create();

            var batches = ddlSqlGenerator.Generate(ModelDiffer.DropSchema(sourceModel)).ToArray();

            if (simulate)
            {
                return(batches);
            }

            Logger.DroppingHistoryTable();

            ExecuteSqlBatches(batches);

            return(batches);
        }
Пример #5
0
        protected virtual IReadOnlyList <SqlStatement> DropHistoryTable(bool simulate)
        {
            var sourceDatabase  = ModelDiffer.DatabaseBuilder.GetDatabase(HistoryRepository.HistoryModel);
            var ddlSqlGenerator = DdlSqlGeneratorFactory.Create(sourceDatabase);

            var statements = ddlSqlGenerator.Generate(ModelDiffer.DropSchema(sourceDatabase)).ToArray();

            if (simulate)
            {
                return(statements);
            }

            Logger.DroppingHistoryTable();

            ExecuteStatements(statements);

            return(statements);
        }