Example #1
0
        public static int Main(string[] args)
        {
            XConsole.NewLine()
                .Graphite.Write(" DevDB ")
                .Default.Write(" version ")
                .Green.WriteLine(GetVersion());

            try
            {
                XConsole.NewPara();
                var run = ParseArguments(args);
                if (run == null)
                {
                    XConsole.WriteLine("Usage:")
                        .Write("  devdb ").Yellow.Write("reset").Default.WriteLine(" <CONNECTION_STRING> [options]     Resets specified DB and recreates it from scripts")
                        .Write("  devdb ").Yellow.Write("migrate").Default.WriteLine(" <CONNECTION_STRING> [options]   Applies all available migrations to specified DB")
                        .Write("  devdb ").Yellow.Write("help").Default.WriteLine("                                    Opens GitHub URL with detailed help");

                    XConsole.NewPara().WriteLine("Options:")
                        .WriteLine("  -db <DB_TYPE>      Specifies DB engine type: 'mssql' or 'pgsql' (default is 'mssql')")
                        .WriteLine("  -p <TARGET_PATH>   Specifies custom target path (current folder will be used by default)")
                        .WriteLine("  -s                 Performs \"soft\" reset by dropping and recreating only procedures, types, functions, and views.")
                        .WriteLine("                     Does not reset tables, schemas, users, roles, etc. or anything related to modifying the data.")
                        .WriteLine("                     Applies only to 'reset' command.")
                        .WriteLine("  -y                 Automatically submits 'yes' to all Y/N prompts")
                        .WriteLine("  -v                 Enables verbose output");
                }
                else
                {
                    XConsole.NewPara();
                    var ctx = InitializeContext(run);
                    if (ctx != null)
                    {
                        // check if update is required
                        new Update(ctx).Run();

                        // run the command
                        switch (run.Command)
                        {
                            case RunCommand.Reset:
                                new ResetDb(ctx).Run();
                                break;

                            case RunCommand.Migrate:
                                new MigrateDb(ctx).Run();
                                break;

                            case RunCommand.Help:
                                new Help().Run();
                                break;

                            default:
                                throw new ArgumentException($"Unknown command: {run.Command}");
                        }
                    }
                }
            }
            catch (Exception e)
            {
                if (e is DbExecutorException sqlError)
                    ReportSqlError(sqlError);

                XConsole.NewPara().Error.WriteLine(e.Message);
                XConsole.Red.WriteLine(e.ToString());
                XConsole.PressAnyKeyWhenDebug();
                return -1;
            }

            return 0;
        }