Пример #1
0
        public static int Main(string[] args)
        {
            string errMessage;

            if (!TryConfigureLogger(args, out errMessage))
            {
                ConsoleLog.WriteError($"Logger cannot be initialized: {errMessage}");
                return(1);
            }
            try
            {
                Log.Debug("++ DbUp entry point");

                int result = Run(args);
                if (result == 0)
                {
                    ConsoleLog.WriteSuccess("Succeed!");
                }
                return(0);
            }
            catch (Exception ex)
            {
                Log.Error("DbUp failed", ex);
                ConsoleLog.WriteError($"Failed\r\n{ex.Message}");
                return(1);
            }
            finally
            {
                Log.Debug("-- DbUp exit point");
            }
        }
Пример #2
0
        public static DbOptions Parse(string[] args)
        {
            bool        showHelp = false, showSql = false, isForce = false, isDebugMode = false;
            DbProviders providerName = DbProviders.SqlServer;
            string      dataSource = "", initialCatalog = "", username = "", password = "";
            string      exportfilename = "", upgradepath = "";
            var         command = DbOptionCommands.None;

            showHelp = false;
            var optionSet = new OptionSet
            {
                { "d", "Drop existing database", _ => command |= DbOptionCommands.Drop },
                { "c", "Create database schema", _ => command |= DbOptionCommands.Create },
                { "e", "Export database schema", _ => command |= DbOptionCommands.ExportSchema },
                { "u", "Upgrade database", _ => command |= DbOptionCommands.Upgrade },
                { "upd=", "Upgrade database with SQL update files, {directory} containing SQL update files", dir => upgradepath = dir },
                { "exf=", "Export database schema {file path}", exf => exportfilename = exf },
                { "p=", "The database {provider} [sqlserver | sqlite. If not specified the default is sqlserver.", p => { if (p == null)
                                                                                                                          {
                                                                                                                              providerName = DbProviders.SqlServer;
                                                                                                                          }
                                                                                                                          else
                                                                                                                          {
                                                                                                                              Enum.TryParse <DbProviders>(p, out providerName);
                                                                                                                          } } },
                { "source=", "The database {source} or server host", s => dataSource = s },
                { "catalog=", "The database {catalog} name", d => initialCatalog = d },
                { "user="******"Database {user} identity", u => username = u },
                { "password="******"Database user {password}", p => password = p },

                { "f", "Forces command to run without getting confirmation from user", _ => isForce = true },
                { "debug", "Execute DbTool in debug mode", _ => isDebugMode = true },
                { "showsql", "Show executed sql commands during dbtool execution", _ => showSql = true },
                { "?|h|help", "Show this message and exit", v => showHelp = v != null },
            };

            optionSet.Parse(args);
            if (args.Length == 0)
            {
                showHelp = true;
            }

            DbOptions o = new DbOptions(
                new DatabaseInfo(providerName, dataSource, initialCatalog, username, password, false),
                command,
                exportfilename,
                upgradepath)
            {
                ShowHelp    = showHelp,
                ShowSql     = showSql,
                IsForce     = isForce,
                IsDebugMode = isDebugMode
            };

            if (showHelp)
            {
                ConsoleLog.WriteSuccess("Roham DbTool Console Application Usage\r\n");
                ConsoleLog.Write(ConsoleColor.White, "DbTool.exe [options]");
                ConsoleLog.WriteInfo("");
                optionSet.WriteOptionDescriptions(Console.Out);
                ConsoleLog.Write(ConsoleColor.White, "\r\nExamples:");
                ConsoleLog.WriteInfo(@"
DbTool -c --source=localhost --catalog=roham-dev --user=sa --password=test
Creates a new sqlserver database named roham-dev in localhost server 

DbTool -c --source=localhost --catalog=roham-dev -e --exf=c:\database\schema.sql
Creates a new sqlserver database named roham-dev and export database 
schema script into c:\database\schema.sql 

DbTool -u --upd=c:\upgrade\scripts --source=localhost --catalog=roham
Runs upgrade scripts located at c:\upgrade\scripts in roham sqlserver 
database at localhost (via IntegratedSecurity)

DbTool -d -c -u --upd:c:\upgrade\scripts --source=localhost --catalog=roham --user=sa --password=test
Drops roham sqlserver database from localhost server and creates a new one 
with same name and then upgrade database with scripts located at c:\upgrade\scripts

DbTool -p=sqlite -c -u --upd=c:\upgrade\scripts --source=c:\database\roham.sqlite --debug --showsql
Creates a new sqlite database at c:\database\roham.sqlite and then runs upgrade scripts 
located at c:\upgrade\scripts in debug mode and show executed sql commands
");
                return(o);
            }

            Validate(o);

            return(o);
        }