Example #1
0
        private static RunContext InitializeContext(RunArgs run)
        {
            // init DB connection
            var ctx = new RunContext
            {
                DbType = run.DbType,
                Connection = run.Connection
            };

            if (ctx.Connection != null)
            {
                Verbose.WriteLine($"DB type: {ctx.DbType.ToString().ToUpper()}");
                Verbose.WriteLine($"DB connection: {ctx.Connection}");
            }

            // choose target path
            ctx.TargetPath = Environment.CurrentDirectory;
            if (!String.IsNullOrWhiteSpace(run.CustomPath))
                ctx.TargetPath = run.CustomPath;

            if (!Directory.Exists(ctx.TargetPath))
            {
                XConsole.NewPara().Warning.WriteLine("Cannot locate target path");
                XConsole.Write("The following target path was not found: ").Cyan.WriteLine(ctx.TargetPath);
                XConsole.Write("When you use ").Yellow.Write("-p").Default.WriteLine(" parameter to specify custom path, make sure it points to existing location");
                return null;
            }

            Verbose.WriteLine($"Target path: {ctx.TargetPath}");

            // get log path
            ctx.LogPath = Path.Combine(ctx.TargetPath, "log");
            Verbose.Write($"Log path: {ctx.LogPath}");

            if (!Directory.Exists(ctx.LogPath))
            {
                Directory.CreateDirectory(ctx.LogPath);
                Verbose.Write(" (created)");
            }

            Verbose.WriteLine();

            // optionally use "soft" reset
            ctx.UseSoftReset = run.UseSoftReset;

            return ctx;
        }
Example #2
0
        private static void ReportSqlError(DbExecutorException e)
        {
            var lines = e.SqlScript
                .Replace("\r\n", "\n")
                .Replace("\r", "\n")
                .Split('\n');

            var from = Math.Max(e.LineNumber - 5, 0);
            var to = Math.Min(e.LineNumber + 5, lines.Length - 1);

            XConsole.NewPara();
            for (var i = from; i <= to; i++)
            {
                if (i == e.LineNumber)
                    XConsole.Error.Write(">>> ").Gold.Write(lines[i]).Error.WriteLine(" <<<");
                else
                    XConsole.Gold.WriteLine(lines[i]);
            }
        }
Example #3
0
        public void Run()
        {
            XConsole.NewPara();

            var updatedFile = Path.Combine(_context.LogPath, FILE_NAME);

            Verbose.WriteLine("Checking if update is needed...");
            if (File.Exists(updatedFile))
            {
                var updatedTime = File.GetCreationTimeUtc(updatedFile);
                if (DateTime.UtcNow.Subtract(updatedTime).TotalDays < UPDATE_EVERY_DAYS)
                {
                    Verbose.WriteLine("Update is not required");
                    return;
                }
            }

            Verbose.WriteLine($"Update is required, so {FILE_NAME} file will be deleted");
            File.Delete(updatedFile);
        }
Example #4
0
    public static void Main()
    {
        //_prerelease = true;

        if (_prerelease)
        {
            XConsole
            .NewPara()
            .Warning.WriteLine("Prerelease mode is used")
            .Yellow.WriteLine("Don't forget to turn it off and build packages again")
            .NewPara();
        }

        var modules = Modules.Load();

        foreach (var module in modules)
        {
            XConsole.Write($"Process \"{module.Key}\"... ");
            var updated = ProcessModule(module.Key, module.Value);

            if (updated)
            {
                XConsole.Warning.WriteLine("UPDATED");
            }
            else
            {
                XConsole.Green.WriteLine("OK");
            }
        }

        XConsole.Write("Update pack scripts... ");
        UpdatePackBat(modules);
        XConsole.Green.WriteLine("OK");

        Modules.Save(modules);
        XConsole.NewPara().WriteLine("Done.");
    }
Example #5
0
 public void Run()
 {
     XConsole.NewPara().Write("Opening URL ").Cyan.Write(URL).Default.WriteLine("...");
     OpenBrowser(URL);
 }
Example #6
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;
        }
Example #7
0
        public void Run()
        {
            if (!Validate())
            {
                return;
            }

            var engine = GetDbEngine();

            if (!Validate(engine))
            {
                return;
            }

            if (!Proceed(engine))
            {
                return;
            }

            // prepare scripts
            XConsole.NewPara();
            CleanLogFiles(engine);

            var scripts = BuildScripts();

            if (scripts == null)
            {
                return;
            }

            var sw = Stopwatch.StartNew();

            // run scripts
            XConsole.NewPara();
            Execute($"Drop {(_context.UseSoftReset ? "only programmatic" : "all")} objects...", () => engine.DropAll(_context.UseSoftReset));

            foreach (var script in scripts)
            {
                Verbose.WriteLine();
                foreach (var file in script.Files)
                {
                    Verbose.WriteLine($"{script.CategoryName.ToUpper()} : {file.BasePath}");
                }

                if (_context.UseSoftReset && !KnownScripts.UsedWhenSoftReset(script.CategoryName))
                {
                    XConsole.Write($"Creating {script.CategoryName.ToLower()}... ").Gold.Write("Skipped");
                    Verbose.Write(" (due to \"soft\" reset)");
                    XConsole.WriteLine();
                    continue;
                }

                Execute($"Creating {script.CategoryName.ToLower()}...", () => engine.ExecuteCreation(script, _context.UseSoftReset));
            }

            sw.Stop();
            Verbose.WriteLine();
            Verbose.WriteLine($"Database reset took {sw.ElapsedMilliseconds:N0} ms");

            Report(engine);
        }