/// <summary>
        /// Retrieves the latest migration script version from the migration directory.
        /// </summary>
        /// <returns>The latest script version</returns>
        private long GetLatestScriptVersion(DatabaseCommandArguments args)
        {
            IOrderedEnumerable <IMigrationScriptFile> files = _migrationDirectory.GetScripts(args)
                                                              .OrderByDescending(x => x.Version);

            IMigrationScriptFile latestFile = files.FirstOrDefault();

            if (latestFile != null)
            {
                return(latestFile.Version);
            }

            return(0);
        }
        public long GetNewVersionNumber(IMigrationDirectory migrationDirectory, IScriptsDirectoryPathArgs args)
        {
            long lastNumber = 0;

            // get the latest number in use
            IEnumerable <IMigrationScriptFile> scripts = migrationDirectory.GetScripts(args);

            if (scripts.Count() > 0)
            {
                lastNumber = scripts.Max(script => script.Version);
            }

            // increment and return
            return(lastNumber + 1);
        }
        public long GetNewVersionNumber(IMigrationDirectory migrationDirectory)
        {
            long lastNumber = 0;

            // get the latest number in use
            IEnumerable<IMigrationScriptFile> scripts = migrationDirectory.GetScripts();

            if (scripts.Count() > 0)
            {
                lastNumber = scripts.Max(script => script.Version);
            }

            // increment and return
            return lastNumber + 1;
        }
Beispiel #4
0
        /// <summary>
        /// Executes the Command's logic.
        /// </summary>
        protected override void Execute(MigrateCommandArgs args)
        {
            IOrderedEnumerable <IMigrationScriptFile> files = _migrationDirectory.GetScripts(args)
                                                              .OrderByDescending(x => x.Version);

            if (files.Count() == 0)
            {
                Log.WriteLine("No migration scripts were found.");
                return;
            }

            long currentVersion = GetDatabaseVersion();
            long targetVersion  = args.TargetVersion;

            if (targetVersion == -1)
            {
                //  if version not provided, assume we want to migrate to the latest migration script version
                targetVersion = files.Select(x => x.Version).First();
            }

            Log.WriteLine("Database is at version:".PadRight(30) + currentVersion);

            MigrationDirection direction;

            if (currentVersion < targetVersion)
            {
                direction = MigrationDirection.Up;
                MigrateUp(currentVersion, targetVersion, files);
                Log.WriteLine("Migrated up to version:".PadRight(30) + targetVersion);
            }
            else if (currentVersion > targetVersion)
            {
                direction = MigrationDirection.Down;
                MigrateDown(currentVersion, targetVersion, files);
                Log.WriteLine("Migrated down to version:".PadRight(30) + targetVersion);
            }
            else
            {
                return;
            }

            // execute the post migration actions
            var postMigrationHooks = Program.Current.CommandRepository.Commands
                                     .Where(cmd => cmd is IPostMigrationHook)
                                     .Cast <IPostMigrationHook>()
                                     .Where(hook => hook.ShouldRun(direction));

            if (postMigrationHooks.Count() > 0)
            {
                Log.WriteLine("Executing post migration hooks...");

                foreach (var hook in postMigrationHooks)
                {
                    Log.WriteLine("  {0}", hook.CommandName);
                    hook.Log = Log;
                    hook.OnPostMigration(args, direction);
                }
            }
            else
            {
                Log.WriteLine("No post migration hooks were run.");
            }
        }