Exemple #1
0
        private DelegatedMigrator CreateMigrator(DbMigrationsConfiguration c, int migratorPriority, DbConnection cnn)
        {
            var migrator = DelegatedMigrator.CreateFromMigrationConfig(c, cnn);

            migrator.Priority = migratorPriority;
            return(migrator);
        }
        private IEnumerable <MigrationInfo> GetAllMigrations(DelegatedMigrator migrator)
        {
            var parser = MigrationInfo.CreateParser(null);
            List <MigrationInfo> applied = migrator.GetDatabaseMigrations().Select(parser.Parse).ToList();
            var all = applied.Union(GetPendingMigrations(migrator))
                      .OrderBy(m => m.CreatedOn)
                      .ThenBy(m => migrator.Priority)
                      .ToList();

            return(all);
        }
Exemple #3
0
        public static DelegatedMigrator CreateFromMigrationConfig(DbMigrationsConfiguration c, DbConnection cnn)
        {
            var impl          = new DbMigrator(c);
            var scriptingImpl = new MigratorScriptingDecorator(new DbMigrator(c));
            var migrator      = new DelegatedMigrator(impl.GetPendingMigrations, impl.GetDatabaseMigrations, impl.Update,
                                                      scriptingImpl.ScriptUpdate, cnn)
            {
                IsAutoMigrationsEnabled = c.AutomaticMigrationsEnabled,
                ConfigurationTypeName   = c.GetType().FullName
            };

            return(migrator);
        }
        public bool Run()
        {
            // note: currently the seed method for each DbMigrationsConfiguration is being run multiple times
            // todo: work out some way to tell DbMigrationsConfiguration.Seed that it should only run once

            var migrations = (
                from migrator in _migrators
                from migration in GetPendingMigrations(migrator)
                select new { migrator, migration }
                ).ToList();

            var orderedMigrations = migrations
                                    .OrderBy(m => m.migration.CreatedOn).ThenBy(m => m.migrator.Priority)
                                    .ToList();

            var batchedMigrations = orderedMigrations
                                    .SliceWhen((prev, current) => prev != null && prev.migrator != current.migrator || current.migration.IsSkipped)
                                    .ToList();

            if (migrations.Any(m => m.migration.IsSkipped))
            {
                _allMigrations = _migrators.ToDictionary(m => m, GetAllMigrations);
            }

            batchedMigrations.ForEach(batch =>
            {
                batch = batch.ToList();
                DelegatedMigrator migator   = batch.First().migrator;
                MigrationInfo lastMigration = batch.Last().migration;
                if (lastMigration.IsSkipped)
                {
                    // Q: Why do we need to insert a skipped migration into the migration history table?
                    // A: the standard behaviour when calling migator.Update(lastMigration.FullName) is to execute
                    //    ALL migrations after the last inserted migration in the db and the migration identified
                    //    by lastMigration.FullName
                    //    This will result in running migration that was skipped in the previous batch
                    var previousMigration =
                        _allMigrations[migator].TakeWhile(m => m.CreatedOn < lastMigration.CreatedOn).LastOrDefault();
                    migator.InsertMigrationHistory(previousMigration != null ? previousMigration.FullName : null, lastMigration.FullName);
                }
                else
                {
                    migator.Update(lastMigration.FullName);
                }
            });

            bool migrationsRun = migrations.Any(m => !m.migration.IsSkipped);

            return(migrationsRun);
        }
        private IEnumerable <MigrationInfo> GetPendingMigrations(DelegatedMigrator migrator)
        {
            var parser = MigrationInfo.CreateParser(SkippedMigrations);
            List <MigrationInfo> migrations = migrator.GetPendingMigrations().Select(parser.Parse).ToList();

            if (migrator.IsAutoMigrationsEnabled)
            {
                return(migrations.Union(new[] { MigrationInfo.Auto }));
            }
            else
            {
                return(migrations);
            }
        }
Exemple #6
0
        private DbConnection CreateDbConnection()
        {
            ConnectionStringSettings connectionString = ConfigurationManager.ConnectionStrings[_connectionStringName];

            return(DelegatedMigrator.CreateDbConnection(connectionString));
        }