예제 #1
0
 /// <summary>
 /// Writes the header information.
 /// </summary>
 private void WriteHeader(CodeGenExecutorArgs args)
 {
     Console.WriteLine(App.Description);
     Console.WriteLine();
     Console.WriteLine($"  Command = {_commandArg.ParsedValue}");
     Console.WriteLine($"  ConnectionString = {_connectionStringArg.Value}");
     CodeGenConsole.LogCodeGenExecutionArgs(args, !(_commandArg.ParsedValue.HasFlag(DatabaseExecutorCommand.CodeGen)));
 }
예제 #2
0
        /// <summary>
        /// Execute the database upgrade.
        /// </summary>
        protected override async Task OnRunAsync(ExecutorRunArgs args)
        {
            var ls = new LoggerSink();

            if (_command.HasFlag(DatabaseExecutorCommand.Drop))
            {
                Logger.Default.Info(string.Empty);
                Logger.Default.Info(new string('-', 80));
                Logger.Default.Info("DB DROP: Checking database existence and dropping where found...");
                await TimeExecutionAsync(() => { DropDatabase.For.SqlDatabase(_connectionString, ls); return(Task.FromResult(true)); }).ConfigureAwait(false);
            }

            if (_command.HasFlag(DatabaseExecutorCommand.Create))
            {
                Logger.Default.Info(string.Empty);
                Logger.Default.Info(new string('-', 80));
                Logger.Default.Info("DB CREATE: Checking database existence and creating where not found...");
                await TimeExecutionAsync(() => { EnsureDatabase.For.SqlDatabase(_connectionString, ls); return(Task.FromResult(true)); }).ConfigureAwait(false);
            }

            if (_command.HasFlag(DatabaseExecutorCommand.Migrate))
            {
                Logger.Default.Info(string.Empty);
                Logger.Default.Info(new string('-', 80));
                Logger.Default.Info("DB MIGRATE: Migrating the database...");
                Logger.Default.Info($"Probing for embedded resources: {(string.Join(", ", GetNamespacesWithSuffix($"{MigrationsNamespace}.*.sql")))}");

                DatabaseUpgradeResult?result = null;
                await TimeExecutionAsync(() =>
                {
                    result = DeployChanges.To
                             .SqlDatabase(_connectionString)
                             .WithScripts(GetMigrationScripts(_assemblies))
                             .WithoutTransaction()
                             .LogTo(ls)
                             .Build()
                             .PerformUpgrade();

                    return(Task.FromResult(result.Successful));
                }).ConfigureAwait(false);

                if (!result !.Successful)
                {
                    Logger.Default.Exception(result.Error);
                    return;
                }
            }

            if (_command.HasFlag(DatabaseExecutorCommand.CodeGen))
            {
                Logger.Default.Info(string.Empty);
                Logger.Default.Info(new string('-', 80));
                Logger.Default.Info("DB CODEGEN: Code-gen database objects...");
                CodeGenConsole.LogCodeGenExecutionArgs(_codeGenArgs);

                if (!await TimeExecutionAsync(async() =>
                {
                    var em = ExecutionManager.Create(() => new CodeGenExecutor(_codeGenArgs));
                    await em.RunAsync().ConfigureAwait(false);
                    return(em.StopExecutor?.Exception == null);
                }).ConfigureAwait(false))
                {
                    return;
                }
            }

            if (_command.HasFlag(DatabaseExecutorCommand.Schema))
            {
                Logger.Default.Info(string.Empty);
                Logger.Default.Info(new string('-', 80));
                Logger.Default.Info("DB SCHEMA: Drops and creates the database objects...");

                if (!await TimeExecutionAsync(() => DropAndCreateAllObjectsAsync(new string[] { "dbo", "Ref" })).ConfigureAwait(false))
                {
                    return;
                }
            }

            if (_command.HasFlag(DatabaseExecutorCommand.Reset))
            {
                Logger.Default.Info(string.Empty);
                Logger.Default.Info(new string('-', 80));
                Logger.Default.Info("DB RESET: Resets database by dropping data from all tables...");

                if (!await TimeExecutionAsync(() => DeleteAllAndResetAsync()).ConfigureAwait(false))
                {
                    return;
                }
            }

            if (_command.HasFlag(DatabaseExecutorCommand.Data))
            {
                Logger.Default.Info(string.Empty);
                Logger.Default.Info(new string('-', 80));
                Logger.Default.Info("DB DATA: Insert or merge the embedded YAML data...");

                if (!await TimeExecutionAsync(() => InsertOrMergeYamlDataAsync()).ConfigureAwait(false))
                {
                    return;
                }
            }

            if (_command.HasFlag(DatabaseExecutorCommand.ScriptNew))
            {
                Logger.Default.Info(string.Empty);
                Logger.Default.Info(new string('-', 80));
                Logger.Default.Info("DB SCRIPTNEW: Creating a new SQL script from embedded template...");

                if (!await TimeExecutionAsync(() => CreateScriptNewAsync()).ConfigureAwait(false))
                {
                    return;
                }
            }

            ReturnCode = 0;
        }