Example #1
0
        private void RunMigrations(MigratorBase migrator)
        {
            // We only support UP right now.
            // Find the target migration and collect everything between the start and it
            var toApply = new List <string>();

            // TakeWhile won't work because it doesn't include the actual target :(
            foreach (var migration in migrator.GetPendingMigrations())
            {
                toApply.Add(migration);
                if (IsMigration(migration, TargetMigration))
                {
                    break;
                }
            }

            if (!toApply.Any(s => IsMigration(s, TargetMigration)))
            {
                Log.Error("{0} is not a pending migration. Only the UP direction can be run in this way. Use the -Sql option to script downwards migrations.", TargetMigration);
                return;
            }

            // We have a list of migrations to apply, apply them one-by-one
            foreach (var migration in toApply)
            {
                Log.Info("Applying {0}", migration);
                if (!WhatIf)
                {
                    migrator.Update(migration);
                }
            }
            Log.Info("All requested migrations applied");
        }
Example #2
0
        protected string GetMigrationHistory(DbMigrationsConfiguration migrationConfiguration, string migrationName)
        {
            // I would rather not use reflection to get the needed DbConnection but the alternative is requiring
            // more configuraton or hardcoding to SQL Server. This looks to be the lesser of multiple evils

            MigratorBase migrator = CreateMigrator(migrationConfiguration);
            MethodInfo   method   = migrator.GetType().GetMethod("CreateConnection", BindingFlags.NonPublic | BindingFlags.Instance);

            using (DbConnection dbConnection = (DbConnection)method.Invoke(migrator, null))
            {
                dbConnection.Open();

                using (HistoryContext historyContext = new HistoryContext(dbConnection, null))
                {
                    HistoryRow history = historyContext.History.SingleOrDefault(x => x.MigrationId == migrationName);
                    if (history != null)
                    {
                        using (MemoryStream stream = new MemoryStream(history.Model))
                        {
                            using (GZipStream gzip = new GZipStream(stream, CompressionMode.Decompress))
                            {
                                return(XDocument.Load(gzip).ToString());
                            }
                        }
                    }
                    else
                    {
                        return("Migration name not found");
                    }
                }
            }
        }
Example #3
0
        protected void RollbackAll(DbMigrationsConfiguration migrationConfiguration)
        {
            MigratorBase migrator = CreateLoggingMigrator(migrationConfiguration);

            migrator.Update(DbMigrator.InitialDatabase);
            OnMigrationCompleted(new DbMigratorEventArgs(migrationConfiguration));
        }
Example #4
0
        protected void ExecuteMigration(DbMigrationsConfiguration migrationConfiguration, string migrationName)
        {
            MigratorBase migrator = CreateLoggingMigrator(migrationConfiguration);

            migrator.Update(migrationName);
            OnMigrationCompleted(new DbMigratorEventArgs(migrationConfiguration));
        }
Example #5
0
        protected void Reseed(DbMigrationsConfiguration migrationConfiguration)
        {
            // I would rather not use reflection for this but the alternatives look to be even worse
            // Sticking with the lesser evil for now

            MigratorBase migrator = CreateMigrator(migrationConfiguration);
            MethodInfo   method   = migrator.GetType().GetMethod("SeedDatabase", BindingFlags.NonPublic | BindingFlags.Instance);

            method.Invoke(migrator, null);
        }
Example #6
0
 protected override void ExecuteCommandCore(MigratorBase migrator)
 {
     if (Sql)
     {
         ScriptMigrations(migrator);
     }
     else
     {
         RunMigrations(migrator);
     }
 }
        protected override void ExecuteCommandCore(MigratorBase migrator)
        {
            Log.Info("Migration Status for {0} on {1}",
                     ConnectionString.InitialCatalog,
                     ConnectionString.DataSource);

            foreach (var migration in migrator.GetDatabaseMigrations().Reverse())
            {
                Log.Info("A {0}", migration);
            }

            foreach (var migration in migrator.GetPendingMigrations())
            {
                Log.Info("  {0}", migration);
            }
            Log.Info("A = Applied");
        }
Example #8
0
        private void ScriptMigrations(MigratorBase migrator)
        {
            var scriptingMigrator = new MigratorScriptingDecorator(migrator);

            string start;
            string target;

            if (migrator.GetDatabaseMigrations().Any(s => IsMigration(s, TargetMigration)))
            {
                // Down migration, start is null, target is the target
                start  = null;
                target = migrator.GetDatabaseMigrations().Single(s => IsMigration(s, TargetMigration));
            }
            else
            {
                // Up migration, go from start to target.
                start  = migrator.GetDatabaseMigrations().FirstOrDefault();
                target = migrator.GetLocalMigrations().Single(s => IsMigration(s, TargetMigration));
            }

            string startName      = start ?? migrator.GetDatabaseMigrations().FirstOrDefault();
            string scriptFileName = String.Format("{0}-{1}.sql", startName, target);

            if (File.Exists(scriptFileName))
            {
                Log.Error("File already exists: {0}", scriptFileName);
                return;
            }

            // Generate script
            Log.Info("Scripting migration from {0} to {1}", startName, target);
            if (!WhatIf)
            {
                string script = scriptingMigrator.ScriptUpdate(start, target);

                // Write the script
                File.WriteAllText(scriptFileName, script);
            }
            Log.Info("Wrote script to {0}", scriptFileName);
        }
Example #9
0
 protected abstract void ExecuteCommandCore(MigratorBase migrator);