Exemplo n.º 1
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);
        }
Exemplo n.º 2
0
        protected virtual IReadOnlyList <SqlStatement> 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()
                  .ToArray();
            var upgradeIndexes
                = MigrationAssembly.Migrations
                  .Select((m, i) => i)
                  .Take(targetMigrationIndex + 1)
                  .Where(i => migrationPairs[i].HistoryRow == null)
                  .ToArray();
            var database   = (RelationalDatabase)ContextConfiguration.Database;
            var statements = new List <SqlStatement>();

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

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

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

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

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

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

            return(statements);
        }