protected override void ProcessRecord() { var rollbackDir = Configuration.GetMigratioDir(MigrationRootDir, ConfigFile, MigratioDirectory.Rollback); DatabaseProvider.SetConnectionInfo(GetConnectionInfo()); if (!DatabaseProvider.MigrationTableExists()) { throw new Exception("Migration table does not exist"); } var scripts = FileManager.GetAllFilesInFolder(rollbackDir) .OrderByDescending(f => f).ToArray(); if (scripts.Length == 0) { WriteWarning("No rollback scripts found"); WriteObject(new MgResult { Successful = false }); return; } var iteration = DatabaseProvider.GetLatestIteration(); var scriptsForLatestIteration = DatabaseProvider.GetAppliedScriptsForLatestIteration(); if (iteration == 0 || scriptsForLatestIteration?.Length == 0) { WriteWarning("No applied migrations found"); WriteObject(new MgResult { Successful = false, Details = "No applied migrations found" }); return; } WriteObject($"Found {scriptsForLatestIteration.Length} migrations applied in iteration {iteration}"); var stringBuilder = new StringBuilder(); foreach (var script in scripts) { var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(script); if (!scriptsForLatestIteration.Any(x => x.MigrationId.Contains(fileNameWithoutExtension))) { WriteObject($"Migration {fileNameWithoutExtension} was not applied in latest iteration, skipping"); continue; } var scriptContent = _migrationHelper.GetScriptContent(script, false); stringBuilder.Append(scriptContent); stringBuilder.Append(GetMigrationQuery(fileNameWithoutExtension, iteration)); WriteObject($"Adding rollback of migration: {fileNameWithoutExtension} to transaction"); } DatabaseProvider.RunTransaction(stringBuilder.ToString()); WriteObject(new MgResult { Successful = true }); }
protected override void ProcessRecord() { var rolloutDir = Configuration.GetMigratioDir(MigrationRootDir, ConfigFile, MigratioDirectory.Rollout); var replaceVariables = ReplaceVariables.IsPresent ? ReplaceVariables.ToBool() : Configuration.Resolve(Configuration?.Config?.ReplaceVariables, false, false); var cfg = GetConnectionInfo(); DatabaseProvider.SetConnectionInfo(cfg); if (!DatabaseProvider.MigrationTableExists()) { if (CreateTableIfNotExist.ToBool()) { DatabaseProvider.CreateMigrationTable(); WriteVerbose("Created migration table"); } else { throw new Exception("Migration table does not exist"); } } var scripts = FileManager.GetAllFilesInFolder(rolloutDir) .OrderBy(f => f) .ToArray(); if (scripts.Length == 0) { WriteWarning("No scripts found"); WriteObject(new MgResult { Successful = false, Details = "No scripts found" }); return; } WriteVerbose($"Found a total of {scripts.Length} migration scripts in the rollout folder"); var applied = DatabaseProvider.GetAppliedMigrations(); WriteObject($"Found {applied.Length} applied migrations"); WriteObject($"Found {scripts.Length} total migrations"); if (applied.Length == scripts.Length) { WriteObject("Number of applied migrations are the same as the total, skipping"); return; } var iteration = DatabaseProvider.GetLatestIteration(); var stringBuilder = new StringBuilder(); var currentIteration = iteration + 1; foreach (var script in scripts) { var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(script); if (applied.Any(x => x.MigrationId.Contains(fileNameWithoutExtension))) { WriteObject($"Migration {Path.GetFileNameWithoutExtension(script)} is applied, skipping"); continue; } WriteObject($"Migration {fileNameWithoutExtension} is not applied adding to transaction"); var scriptContent = _migrationHelper.GetScriptContent(script, replaceVariables ?? false); stringBuilder.Append(scriptContent); stringBuilder.Append(GetMigrationQuery(fileNameWithoutExtension, currentIteration)); } DatabaseProvider.RunTransaction(stringBuilder.ToString()); WriteObject(new MgResult { Successful = true }); }