コード例 #1
0
        /// <summary>
        /// Executes the migration's down function to downgrade the database
        /// based on it's current changelog
        /// </summary>
        /// <returns></returns>
        public string Execute()
        {
            var    fileInfo   = new FileInfo(_projectFile);
            var    isMigrated = false;
            string logPath    = string.Empty;

            // check changelog for the latest migration run
            var changeLogCollection = new ChangelogCollection(_mongoDbContext);

            var changeLog    = changeLogCollection.All();
            var latestChange = changeLog.GetLatestChange();

            if (latestChange == null)
            {
                return("No changes to downgrade");
            }

            // get all classes in a namespace from project assembly
            var workingDirectory = fileInfo.Directory.FullName;

            // create build command for project where migrations are created
            var runner = CLI.DotNet(x => x.WorkingDirectory = workingDirectory)
                         .Build(x => x.BuildConfiguration = BuildConfiguration.Debug)
                         .Create();
            // run cli command
            var results = runner.Run();

            if (!results.IsSuccessful)
            {
                return($"Error: {fileInfo.Name} failed to build with the following errors: {results.Message}");
            }

            var migration = MigrationExtensions.GetMigration(latestChange.FileName, fileInfo);

            if (migration == null)
            {
                return($"Error: Unable to locate migration {latestChange.FileName}");
            }

            try
            {
                var instance = Activator.CreateInstance(migration);
                isMigrated = (bool)migration.GetMethod("Down")
                             .Invoke(instance, new[] { _mongoDbContext.Db });
            }
            catch (Exception e)
            {
                logPath = _logger.Log(migration.Name, e.ToString());
            }

            if (!isMigrated)
            {
                return($"Error: {migration.Name} was not migrated successfully. See {logPath} for more details.");
            }

            var deleteResult = changeLogCollection.Delete(latestChange);

            if (deleteResult == 0)
            {
                return($"Error: {migration.Name} was not downgraded");
            }

            return($"Downgraded: {migration.Name}");
        }
コード例 #2
0
        /// <summary>
        /// Shows the status of the database migrations
        /// </summary>
        /// <returns>A csv indicating the database status</returns>
        public string Execute()
        {
            var fileInfo = new FileInfo(_projectFile);

            // check changelog for the latest migration run
            var changeLogCollection = new ChangelogCollection(_mongoDbContext);

            var changeLog    = changeLogCollection.All();
            var latestChange = changeLog.GetLatestChange();

            // get all classes in a namespace from project assembly
            var workingDirectory = fileInfo.Directory.FullName;

            // create build command for project where migrations are created
            var runner = CLI.DotNet(x => x.WorkingDirectory = workingDirectory)
                         .Build(x => x.BuildConfiguration = BuildConfiguration.Debug)
                         .Create();
            // run command
            var results = runner.Run();

            if (!results.IsSuccessful)
            {
                return($"Error: {fileInfo.Name} failed to build with the following errors: {results.Message}");
            }

            var migrations = MigrationExtensions.GetMigrationTypes(fileInfo);

            if (migrations.Count == 0)
            {
                return("Error: No migration files found in project");
            }

            var remainingMigrations = migrations.GetRange(0, migrations.Count);

            // grab the latest changes if any migrations were previously executed
            if (latestChange != null)
            {
                remainingMigrations = migrations.GetRemainingMigrations(latestChange.FileName);
            }

            var migrationStatus = new StringBuilder();

            // add headers to migration status
            migrationStatus.AppendLine("Migration,Applied At");
            // loop through executed migrations by data applied
            foreach (var change in changeLog.OrderBy(x => x.AppliedAt))
            {
                migrationStatus.AppendLine($"{change.FileName},{change.AppliedAt.ToString("MM/dd/yyyy h:mm tt")}");
            }

            // return status if there are no remaining migrations
            if (remainingMigrations == null)
            {
                return(migrationStatus.ToString());
            }

            // loop through all pending migrations
            foreach (var migration in remainingMigrations)
            {
                migrationStatus.AppendLine($"{migration.Name},PENDING");
            }
            return(migrationStatus.ToString());
        }
コード例 #3
0
        /// <summary>
        /// Executes the migration's up function to upgrade the database
        /// based on it's current changelog
        /// </summary>
        /// <returns></returns>
        public string Execute()
        {
            var fileInfo = new FileInfo(_projectFile);

            // check changelog for the latest migration run
            var changeLogCollection = new ChangelogCollection(_mongoDbContext);

            var changeLog    = changeLogCollection.All();
            var latestChange = changeLog.GetLatestChange();

            // get all classes in a namespace from project assembly
            var workingDirectory = fileInfo.Directory.FullName;

            // create build command for project where migrations are created
            var runner = CLI.DotNet(x => x.WorkingDirectory = workingDirectory)
                         .Build(x => x.BuildConfiguration = BuildConfiguration.Debug)
                         .Create();
            // run command
            var results = runner.Run();

            if (!results.IsSuccessful)
            {
                return($"Error: {fileInfo.Name} failed to build with the following errors: {results.Message}");
            }

            var migrations = MigrationExtensions.GetMigrationTypes(fileInfo);

            if (migrations.Count == 0)
            {
                return("Error: No migration files found in project");
            }

            var remainingMigrations = migrations.GetRange(0, migrations.Count);

            // grab the latest changes if any migrations were previously executed
            if (latestChange != null)
            {
                remainingMigrations = migrations.GetRemainingMigrations(latestChange.FileName);
            }

            if (remainingMigrations.Count == 0)
            {
                return("The database is already up to date");
            }

            // string build result for multiple migrations
            var migrationResult = new StringBuilder();

            foreach (var migration in remainingMigrations)
            {
                var    isMigrated = false;
                string logPath    = string.Empty;

                try
                {
                    var instance = Activator.CreateInstance(migration);
                    isMigrated = (bool)migration.GetMethod("Up")
                                 .Invoke(instance, new[] { _mongoDbContext.Db });
                }
                catch (Exception e)
                {
                    logPath = _logger.Log(migration.Name, e.ToString());
                }

                if (!isMigrated)
                {
                    return($"Error: {migration.Name} was not migrated successfully. See {logPath} for more details.");
                }

                changeLogCollection.Insert(new Changelog
                {
                    AppliedAt = DateTime.Now,
                    FileName  = migration.Name
                });
                migrationResult.AppendLine($"Migrated: {migration.Name}");
            }

            if (migrationResult.Length > 0)
            {
                return(migrationResult.ToString());
            }

            return("Error: Unable to location migrations to be executed. Verify that a Migrations directory exists in your project");
        }