예제 #1
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>);
        }
예제 #2
0
        protected virtual IReadOnlyList <SqlBatch> ApplyMigrations(int targetMigrationIndex, bool simulate)
        {
            bool historyTableExists;
            var  migrationPairs = PairMigrations(MigrationAssembly.Migrations, HistoryRepository.GetRows(out historyTableExists));
            var  downgradeIndexes
                = MigrationAssembly.Migrations
                  .Select((m, i) => i)
                  .Skip(targetMigrationIndex + 1)
                  .Where(i => migrationPairs[i].HistoryRow != null)
                  .Reverse()
                  .ToList();
            var upgradeIndexes
                = MigrationAssembly.Migrations
                  .Select((m, i) => i)
                  .Take(targetMigrationIndex + 1)
                  .Where(i => migrationPairs[i].HistoryRow == null)
                  .ToList();

            if (!simulate &&
                !_storeCreator.Exists())
            {
                _storeCreator.Create();
            }

            var batches = new List <SqlBatch>();

            if (upgradeIndexes.Any() &&
                !historyTableExists)
            {
                batches.AddRange(CreateHistoryTable(simulate));
            }

            batches.AddRange(downgradeIndexes.SelectMany(i => RevertMigration(i, simulate)));

            batches.AddRange(upgradeIndexes.SelectMany(i => ApplyMigration(i, simulate)));

            if (targetMigrationIndex == -1 && historyTableExists)
            {
                batches.AddRange(DropHistoryTable(simulate));
            }

            if (batches.Count == 0)
            {
                Logger.UpToDate();
            }

            return(batches);
        }
예제 #3
0
        protected virtual IReadOnlyList <SqlBatch> RevertMigration(int index, bool simulate)
        {
            var migration       = MigrationAssembly.Migrations[index];
            var targetModel     = GetSourceModel(index);
            var ddlSqlGenerator = DdlSqlGeneratorFactory.Create(targetModel);

            var batches = ddlSqlGenerator.Generate(migration.GetDowngradeOperations())
                          .Concat(HistoryRepository.GenerateDeleteMigrationSql(migration.GetMetadata(), DmlSqlGenerator))
                          .ToList();

            if (simulate)
            {
                return(batches);
            }

            Logger.RevertingMigration(migration.GetMigrationId());

            ExecuteSqlBatches(batches);

            return(batches);
        }
예제 #4
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);
        }