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");
        }
Exemple #2
0
 /// <summary>
 /// Initializes a new instance of the MigratorLoggingDecorator class.
 /// </summary>
 /// <param name="innerMigrator"> The migrator that this decorator is wrapping. </param>
 /// <param name="logger"> The logger to write messages to. </param>
 public MigratorLoggingDecorator(MigratorBase innerMigrator, MigrationsLogger logger)
     : base(innerMigrator)
 {
     Check.NotNull <MigratorBase>(innerMigrator, nameof(innerMigrator));
     Check.NotNull <MigrationsLogger>(logger, nameof(logger));
     this._logger = logger;
     this._logger.Verbose(Strings.LoggingTargetDatabase((object)this.TargetDatabase));
 }
        /// <summary>
        ///     Initializes a new instance of the MigratorLoggingDecorator class.
        /// </summary>
        /// <param name = "innerMigrator">The migrator that this decorator is wrapping.</param>
        /// <param name = "logger">The logger to write messages to.</param>
        public MigratorLoggingDecorator(MigratorBase innerMigrator, MigrationsLogger logger)
            : base(innerMigrator)
        {
            //Contract.Requires(innerMigrator != null);
            //Contract.Requires(logger != null);

            _logger = logger;
            _logger.Verbose(Strings.LoggingTargetDatabase(base.TargetDatabase));
        }
        /// <summary>
        ///     Initializes a new instance of the MigratorLoggingDecorator class.
        /// </summary>
        /// <param name="innerMigrator"> The migrator that this decorator is wrapping. </param>
        /// <param name="logger"> The logger to write messages to. </param>
        public MigratorLoggingDecorator(MigratorBase innerMigrator, MigrationsLogger logger)
            : base(innerMigrator)
        {
            Check.NotNull(innerMigrator, "innerMigrator");
            Check.NotNull(logger, "logger");

            _logger = logger;
            _logger.Verbose(Strings.LoggingTargetDatabase(base.TargetDatabase));
        }
        /// <summary>
        ///     Initializes a new instance of the MigratorLoggingDecorator class.
        /// </summary>
        /// <param name="innerMigrator"> The migrator that this decorator is wrapping. </param>
        /// <param name="logger"> The logger to write messages to. </param>
        public MigratorLoggingDecorator(MigratorBase innerMigrator, MigrationsLogger logger)
            : base(innerMigrator)
        {
            Check.NotNull(innerMigrator, "innerMigrator");
            Check.NotNull(logger, "logger");

            _logger = logger;
            _logger.Verbose(Strings.LoggingTargetDatabase(base.TargetDatabase));
        }
 protected override void ExecuteCommandCore(MigratorBase migrator)
 {
     if (Sql)
     {
         ScriptMigrations(migrator);
     }
     else
     {
         RunMigrations(migrator);
     }
 }
Exemple #7
0
 /// <summary>Initializes a new instance of the MigratorBase class.</summary>
 /// <param name="innerMigrator"> The migrator that this decorator is wrapping. </param>
 protected MigratorBase(MigratorBase innerMigrator)
 {
     if (innerMigrator == null)
     {
         this._this = this;
     }
     else
     {
         this._this = innerMigrator;
         MigratorBase migratorBase = innerMigrator;
         while (migratorBase._this != innerMigrator)
         {
             migratorBase = migratorBase._this;
         }
         migratorBase._this = this;
     }
 }
        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");
        }
Exemple #9
0
        /// <summary>
        /// Initializes a new instance of the MigratorBase class.
        /// </summary>
        /// <param name="innerMigrator"> The migrator that this decorator is wrapping. </param>
        protected MigratorBase(MigratorBase innerMigrator)
        {
            if (innerMigrator == null)
            {
                _this = this;
            }
            else
            {
                _this = innerMigrator;

                var nextMigrator = innerMigrator;

                while (nextMigrator._this != innerMigrator)
                {
                    nextMigrator = nextMigrator._this;
                }

                nextMigrator._this = this;
            }
        }
        /// <summary>
        /// Initializes a new instance of the MigratorBase class.
        /// </summary>
        /// <param name="innerMigrator"> The migrator that this decorator is wrapping. </param>
        protected MigratorBase(MigratorBase innerMigrator)
        {
            if (innerMigrator == null)
            {
                _this = this;
            }
            else
            {
                _this = innerMigrator;

                var nextMigrator = innerMigrator;

                while (nextMigrator._this != innerMigrator)
                {
                    nextMigrator = nextMigrator._this;
                }

                nextMigrator._this = this;
            }
        }
        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);
        }
Exemple #12
0
 /// <summary>
 /// Initializes a new instance of the  MigratorScriptingDecorator class.
 /// </summary>
 /// <param name="innerMigrator"> The migrator that this decorator is wrapping. </param>
 public MigratorScriptingDecorator(MigratorBase innerMigrator)
     : base(innerMigrator)
 {
     Check.NotNull <MigratorBase>(innerMigrator, nameof(innerMigrator));
 }
 protected abstract void ExecuteCommandCore(MigratorBase migrator);
 /// <summary>
 /// Initializes a new instance of the  MigratorScriptingDecorator class.
 /// </summary>
 /// <param name="innerMigrator"> The migrator that this decorator is wrapping. </param>
 public MigratorScriptingDecorator(MigratorBase innerMigrator)
     : base(innerMigrator)
 {
     Check.NotNull(innerMigrator, "innerMigrator");
 }
 /// <summary>
 ///     Initializes a new instance of the  MigratorScriptingDecorator class.
 /// </summary>
 /// <param name="innerMigrator"> The migrator that this decorator is wrapping. </param>
 public MigratorScriptingDecorator(MigratorBase innerMigrator)
     : base(innerMigrator)
 {
     Check.NotNull(innerMigrator, "innerMigrator");
 }
 /// <summary>
 ///     Initializes a new instance of the  MigratorScriptingDecorator class.
 /// </summary>
 /// <param name = "innerMigrator">The migrator that this decorator is wrapping.</param>
 public MigratorScriptingDecorator(MigratorBase innerMigrator)
     : base(innerMigrator)
 {
     Contract.Requires(innerMigrator != null);
 }
 private static void GetValue(MigratorBase migrator)
 {
     migrator.Update();
 }