Exemplo n.º 1
0
        protected override void ProcessRecord()
        {
            DatabaseProvider.SetConnectionInfo(GetConnectionInfo());

            if (DatabaseProvider.MigrationTableExists())
            {
                WriteWarning("Migration table already exists");
                WriteObject(new MgResult {
                    Successful = false, Details = "Migration table already exists"
                });
                return;
            }

            var result = DatabaseProvider.CreateMigrationTable();

            WriteObject(new MgResult {
                Successful = result == 1
            });
        }
Exemplo n.º 2
0
        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
            });
        }