예제 #1
0
        /// <summary>
        /// Instantiates a new instance of the <see cref="ConsoleLogger"/> class
        /// </summary>
        /// <param name="migrator">Migrator to use</param>
        public ConsoleRunner(ISimpleMigrator migrator)
        {
            this.migrator = migrator;

            // If they haven't assigned a logger, do so now
            if (this.migrator.Logger == null)
                this.migrator.Logger = new ConsoleLogger();

            this.CreateSubCommands();
        }
예제 #2
0
        /// <summary>
        /// Instantiates a new instance of the <see cref="ConsoleLogger"/> class
        /// </summary>
        /// <param name="migrator">Migrator to use</param>
        public ConsoleRunner(ISimpleMigrator migrator)
        {
            this.migrator = migrator;

            // If they haven't assigned a logger, do so now
            if (this.migrator.Logger == null)
            {
                this.migrator.Logger = new ConsoleLogger();
            }

            this.CreateSubCommands();
        }
 public MigrationRunner(
     ISimpleMigrator migrator,
     IVersionValidator versionValidator,
     INoOpProcess noOpProcess,
     IApplyProcess applyProcess,
     IVersionOutputHelper versionOutputHelper)
 {
     _migrator            = migrator;
     _versionValidator    = versionValidator;
     _noOpProcess         = noOpProcess;
     _applyProcess        = applyProcess;
     _versionOutputHelper = versionOutputHelper;
 }
        private void WriteVersionsToBeApplied(ISimpleMigrator migrator, long targetVersion)
        {
            var versionsToBeApplied = migrator.Migrations.VersionsToBeAppliedNoop(migrator.CurrentMigration.Version, targetVersion);

            _outputWriter.WriteLine("The following migrations will be applied:");

            var action = migrator.IsRollBack(targetVersion) ? "Down" : "Up";

            foreach (var versionToApply in versionsToBeApplied)
            {
                _outputWriter.WriteLine($"\t{versionToApply.Version} ({action}) - {versionToApply.Description}");
            }

            _outputWriter.WriteLine();
        }
        private void WriteAvailableVersions(ISimpleMigrator migrator, long targetVersion)
        {
            var higherVersions = migrator.Migrations.VersionsHigherThan(targetVersion);

            if (!higherVersions.Any())
            {
                return;
            }

            _outputWriter.WriteLine("The following migrations are available but will NOT be applied:");

            foreach (var laterVersion in higherVersions)
            {
                _outputWriter.WriteLine($"\t{laterVersion.Version} - {laterVersion.Description}");
            }

            _outputWriter.WriteLine();
        }
예제 #6
0
        public void WriteVersionSection(ISimpleMigrator migrator, Settings settings)
        {
            _outputWriter.WriteLine($"Current database version is {migrator.CurrentMigration.Version}");
            _outputWriter.WriteLine();

            var migrationAction = migrator.IsRollBack(settings.TargetVersion) ? "Rolling back" : "Updating";
            var targetMigration = migrator.Migrations.Single(m => m.Version == settings.TargetVersion);

            if (targetMigration == null)
            {
                return;
            }

            _outputWriter.WriteLine($"{migrationAction} database");
            _outputWriter.WriteLine($"\tfrom:\t{migrator.CurrentMigration.Version} - {migrator.CurrentMigration.Description}");
            _outputWriter.WriteLine($"\tto:\t{targetMigration.Version} - {targetMigration.Description}");
            _outputWriter.WriteLine();
        }
        public void Run(ISimpleMigrator migrator, long targetVersion)
        {
            var currentVersion = migrator.CurrentMigration.Version;

            var migrationsToApply = migrator.Migrations.VersionsToBeApplied(currentVersion, targetVersion);

            var action = migrator.IsRollBack(targetVersion) ? "Down" : "Up";

            foreach (var migration in migrationsToApply)
            {
                if (migrator.IsRollBack(targetVersion) && migration == migrationsToApply.Last())
                {
                    migrator.MigrateTo(migration.Version);
                }
                else
                {
                    _outputWriter.Write($"Applying version {migration.Version} ({action}) - {migration.Description}...");
                    migrator.MigrateTo(migration.Version);
                    _outputWriter.WriteLine("Done");
                }
            }
        }
예제 #8
0
        public VersionValidation Validate(ISimpleMigrator migrator, Settings settings)
        {
            if (settings.TargetVersion > migrator.LatestMigration.Version)
            {
                _outputWriter.WriteLine($"Target database version ({settings.TargetVersion}) is higher than the latest migration: {migrator.LatestMigration.Version} - {migrator.LatestMigration.Description}");
                return(VersionValidation.TargetVersionHigherThanLatest);
            }

            if (settings.TargetVersion == migrator.CurrentMigration.Version)
            {
                _outputWriter.WriteLine($"Target version is same as the current: {migrator.CurrentMigration.Version} - {migrator.CurrentMigration.Description}");
                return(VersionValidation.TargetVersionIsSameAsCurrent);
            }

            if (migrator.Migrations.All(m => m.Version != settings.TargetVersion))
            {
                _outputWriter.WriteLine($"Could not find migration number {settings.TargetVersion}");
                return(VersionValidation.CannotFindTargetVersion);
            }

            return(VersionValidation.Valid);
        }
        public void Run(ISimpleMigrator migrator, long targetVersion)
        {
            WriteVersionsToBeApplied(migrator, targetVersion);

            WriteAvailableVersions(migrator, targetVersion);
        }
예제 #10
0
 public static bool IsRollBack(this ISimpleMigrator migrator, long targetVersion)
 {
     return(targetVersion < migrator.CurrentMigration.Version);
 }