Exemple #1
0
        private static void RunUpdate(MysqlDataUpdater site, Assembly dllAssembly, string customMethod, string[] param, string targetVersion, bool verbose, bool dryrun, bool checkdb)
        {
            // Execute custom methods
            if (customMethod != null && !checkdb)
            {
                if (verbose)
                {
                    Console.WriteLine(String.Format("Executing custom method: {0}", customMethod));
                }
                if (!dryrun)
                {
                    site.ExecuteCustomMethod(customMethod.Trim(), dllAssembly, param);
                }
            }

            // Execute update methods
            site.LoadMethods(dllAssembly);
            List <string> methods;

            if (checkdb)
            {
                methods = site.GetDataIntegrityMethods();
            }
            else
            {
                methods = site.GetMethods();
            }

            // Execute each method
            foreach (var method in methods)
            {
                if (verbose)
                {
                    Console.WriteLine(String.Format("Executing method: {0}", method));
                }
                if (!dryrun)
                {
                    try { site.TestConnection(); } catch (Exception) {
                        System.Threading.Thread.Sleep(5000);
                        site.TestConnection();
                    }
                    try {
                        site.ExecuteMethod(method);
                    } catch (Exception ex) {
                        Console.WriteLine(string.Format("\n --- Error occured in method {0}: \n\n{1}", method, ex.StackTrace));
                        if (checkdb)
                        {
                            continue;
                        }
                        else
                        {
                            break;
                        }
                    }
                }
            }
        }
Exemple #2
0
        //--- Class Methods ---
        static int Main(string[] args)
        {
            string dbusername = "******", dbname = "wikidb", dbserver = "localhost", dbpassword = null, updateDLL = null, targetVersion = null;
            int dbport = 3306, exit = 0;
            bool showHelp = false, dryrun = false, verbose = false;

            // set command line options
            var options = new Options() {
                { "p=|dbpassword="******"Database password", p => dbpassword = p},
                { "v=|version=", "Target Version", v => targetVersion = v},
                { "u=|dbusername="******"Database user name (default: root)", u => dbusername = u},
                { "d=|dbname=", "Database name (default: wikidb)", p => dbname = p},
                { "s=|dbserver=", "Database server (default: localhost)", s => dbserver = s},
                { "n=|port=", "Database port (default: 3306)", n => {dbport = Int32.Parse(n);}},
                { "i|info", "Display verbose information (default: false)", i => {verbose = true;}},
                { "f|dryrun", "Just display the methods that will be called, do not execute any of them. (default: false)", f => { dryrun = verbose = true;} },
                { "h|?|help", "show help text", h => { verbose = true; }},
            };
            if(args == null || args.Length == 0) {
                showHelp = true;
            } else {
                try {
                    var trailingOptions = options.Parse(args).ToArray();

                    // if there are more trailing arguments display help
                    if(trailingOptions.Length != 1) {
                        showHelp = true;
                    } else {
                        updateDLL = Path.GetFullPath(trailingOptions.First());
                    }
                } catch(InvalidOperationException) {
                    exit = -3;
                    Console.Error.WriteLine("Invalid arguments");
                    showHelp = true;
                }
            }
            if(!showHelp) {

                // Check Arguments
                CheckArg(updateDLL, "No DLL file was specified");
                CheckArg(dbpassword, "No Database password specified");
                CheckArg(targetVersion, "No version specified");

                // Begin Parsing DLL
                var dllAssembly = Assembly.LoadFile(updateDLL);

                // Instatiate Mysql Upgrade class
                MysqlDataUpdater mysqlSchemaUpdater = null;
                try {
                    mysqlSchemaUpdater = new MysqlDataUpdater(dbserver, dbport, dbname, dbusername, dbpassword, targetVersion);
                } catch(VersionInfoException) {
                    PrintErrorAndExit("You entered an incorrect version numner.");
                }
                mysqlSchemaUpdater.LoadMethods(dllAssembly);
                var methods = mysqlSchemaUpdater.GetMethods();

                // Execute each method
                foreach(var method in methods) {
                    if(verbose) {
                        Console.WriteLine(String.Format("Executing Method: {0}", method));
                    }
                    if(!dryrun) {
                        try { mysqlSchemaUpdater.TestConnection(); }
                        catch (Exception) {
                            System.Threading.Thread.Sleep(5000);
                            mysqlSchemaUpdater.TestConnection();
                        }
                        mysqlSchemaUpdater.ExecuteMethod(method);
                    }
                }
            }
            else {
                ShowHelp(options);
            }
            return exit;
        }
Exemple #3
0
        //--- Class Methods ---
        static int Main(string[] args)
        {
            string dbusername = "******", dbname = "", dbserver = "localhost", dbpassword = null, updateDLL = null,
                   targetVersion = null, sourceVersion = null, customMethod = null;
            string[] param = null;
            int dbport = 3306, exitCode = 0, errors = 0;
            uint timeout = UInt32.MaxValue;
            bool showHelp = false, dryrun = false, verbose = false, listDatabases = false, checkDb = false;
            var errorStrings = new List<string>();

            // set command line options
            var options = new Options() {
                { "p=|dbpassword="******"Database password", p => dbpassword = p },
                { "v=|version=", "Target Version", v => targetVersion = v },
                { "b=|sversion=", "Source Version", b => sourceVersion = b },
                { "u=|dbusername="******"Database user name (default: root)", u => dbusername = u },
                { "d=|dbname=", "Database name (default: wikidb)", p => dbname = p },
                { "s=|dbserver=", "Database server (default: localhost)", s => dbserver = s },
                { "n=|port=", "Database port (default: 3306)", n => {dbport = Int32.Parse(n);}},
                { "o=|timeout=", "Sets the database's Default Command Timeout", o => { timeout = UInt32.Parse(o); }},
                { "c=|custom", "Custom method to invoke", c => { customMethod = c; }},
                { "i|info", "Display verbose information (default: false)", i => { verbose = true; }},
                { "noop|dryrun", "Just display the methods that will be called, do not execute any of them. (default: false)", f => { dryrun = verbose = true; }},
                { "l|listdb" , "List of databases separated by EOF", l => { listDatabases = true; }},
                { "t|checkdb", "Run database tests and nothing else", t => { checkDb = true; }},
                { "h|?|help", "show help text", h => { showHelp = true; }},
            };
            if(args == null || args.Length == 0) {
                showHelp = true;
            } else {
                try {
                    var trailingOptions = options.Parse(args).ToArray();

                    // if there are more trailing arguments display help
                    if(trailingOptions.Length < 1) {
                        showHelp = true;
                    } else {
                        updateDLL = Path.GetFullPath(trailingOptions.First());
                        param = trailingOptions.SubArray(1);
                    }
                } catch(InvalidOperationException) {
                    exitCode = -3;
                    Console.Error.WriteLine("Invalid arguments");
                    showHelp = true;
                }
            }
            if(showHelp) {
                ShowHelp(options);
                return exitCode;
            }

            // Check Arguments
            CheckArg(updateDLL, "No DLL file was specified");
            if(!listDatabases) {
                CheckArg(dbpassword, string.Format("No Database password specified for database {0}", dbname));
            }

            // If there are no custom methods specified we need a version number
            if(customMethod == null) {
                CheckArg(dbname, "No Database was specified");
            }

            // Begin Parsing DLL
            var dllAssembly = Assembly.LoadFile(updateDLL);
            MysqlDataUpdater mysqlSchemaUpdater = null;
            if(listDatabases) {

                // Read list of databases if listDatabases is true
                var databaseList = new List<DBConnection>();

                // Read the db names from input
                // format: dbname[;dbserver;dbuser;dbpassword]
                string line = null;
                while(!string.IsNullOrEmpty(line = Console.ReadLine())) {
                    var connection = DBConnection.Parse(line, dbserver, dbusername, dbpassword);
                    if(connection != null) {
                        databaseList.Add(connection);
                    }
                }
                foreach(var db in databaseList) {
                    try {
                        mysqlSchemaUpdater = new MysqlDataUpdater(db.dbServer, dbport, db.dbName, db.dbUsername, db.dbPassword, targetVersion, timeout);
                        if(sourceVersion != null) {
                            mysqlSchemaUpdater.SourceVersion = sourceVersion;
                        }
                    } catch(VersionInfoException) {
                        PrintErrorAndExit("You entered an incorrect version numbner.");
                    } catch(Exception) {

                        // If there is any problem creating the connection we will just keep going
                        errors++;
                        errorStrings.Add(string.Format("There was an error connecting to database {0} on {1}", db.dbName, db.dbServer));
                        continue;
                    }
                    if(verbose) {
                        Console.WriteLine("\n--- Updating database {0} on server {1}", db.dbName, db.dbServer);
                    }

                    // Run methods on database
                    RunUpdate(mysqlSchemaUpdater, dllAssembly, customMethod, param, verbose, dryrun, checkDb);
                }
            } else {
                try {
                    mysqlSchemaUpdater = new MysqlDataUpdater(dbserver, dbport, dbname, dbusername, dbpassword, targetVersion, timeout);
                    if(sourceVersion != null) {
                        mysqlSchemaUpdater.SourceVersion = sourceVersion;
                    }
                } catch(VersionInfoException) {
                    PrintErrorAndExit("You entered an incorrect version numner.");
                }

                // Run update
                RunUpdate(mysqlSchemaUpdater, dllAssembly, customMethod, param, verbose, dryrun, checkDb);
            }
            if(errors > 0) {
                Console.WriteLine("\nThere were {0} errors:", errors);
                foreach(var error in errorStrings) {
                    Console.WriteLine("---" + error);
                }
            }
            return exitCode;
        }
Exemple #4
0
        private static void RunUpdate(MysqlDataUpdater site, Assembly dllAssembly, string customMethod, string[] param, bool verbose, bool dryrun, bool checkdb)
        {
            // Execute custom methods
            if(customMethod != null) {
                if(verbose) {
                    Console.WriteLine("Executing custom method: {0}", customMethod);
                }
                if(!dryrun) {
                    site.ExecuteCustomMethod(customMethod.Trim(), dllAssembly, param);
                }
                return;
            }

            // Execute update methods
            site.LoadMethods(dllAssembly);
            var methods = checkdb ? site.GetDataIntegrityMethods() : site.GetMethods();

            // Execute each method
            foreach(var method in methods) {
                if(verbose) {
                    Console.WriteLine("Executing method: {0}", method);
                }
                if(dryrun) {
                    continue;
                }
                try {
                    site.TestConnection();
                } catch(Exception) {
                    System.Threading.Thread.Sleep(5000);
                    site.TestConnection();
                }
                try {
                    site.ExecuteMethod(method);
                } catch(Exception ex) {
                    Console.WriteLine("\n --- Error occured in method {0}: \n\n{1}", method, ex.StackTrace);
                    if(!checkdb) {
                        break;
                    }
                }
            }
        }
Exemple #5
0
        //--- Class Methods ---
        static int Main(string[] args)
        {
            string dbusername = "******", dbname = "", dbserver = "localhost", dbpassword = null, updateDLL = null,
                   targetVersion = null, sourceVersion = null, customMethod = null;

            string[] param = null;
            int      dbport = 3306, exitCode = 0, errors = 0;
            var      timeout = uint.MaxValue;
            bool     showHelp = false, dryrun = false, verbose = false, listDatabases = false, checkDb = false;
            var      errorStrings = new List <string>();

            // set command line options
            var options = new Options()
            {
                { "p=|dbpassword="******"Database password", p => dbpassword = p },
                { "v=|version=", "Target Version", v => targetVersion = v },
                { "b=|sversion=", "Source Version", b => sourceVersion = b },
                { "u=|dbusername="******"Database user name (default: root)", u => dbusername = u },
                { "d=|dbname=", "Database name (default: wikidb)", p => dbname = p },
                { "s=|dbserver=", "Database server (default: localhost)", s => dbserver = s },
                { "n=|port=", "Database port (default: 3306)", n => { dbport = int.Parse(n); } },
                { "o=|timeout=", "Sets the database's Default Command Timeout", o => { timeout = uint.Parse(o); } },
                { "c=|custom", "Custom method to invoke", c => { customMethod = c; } },
                { "i|info", "Display verbose information (default: false)", i => { verbose = true; } },
                { "noop|dryrun", "Just display the methods that will be called, do not execute any of them. (default: false)", f => { dryrun = verbose = true; } },
                { "l|listdb", "List of databases separated by EOF", l => { listDatabases = true; } },
                { "t|checkdb", "Run database tests and nothing else", t => { checkDb = true; } },
                { "h|?|help", "show help text", h => { showHelp = true; } },
            };

            if (args == null || args.Length == 0)
            {
                showHelp = true;
            }
            else
            {
                try {
                    var trailingOptions = options.Parse(args).ToArray();

                    // if there are more trailing arguments display help
                    if (trailingOptions.Length < 1)
                    {
                        showHelp = true;
                    }
                    else
                    {
                        updateDLL = Path.GetFullPath(trailingOptions.First());
                        param     = trailingOptions.SubArray(1);
                    }
                } catch (InvalidOperationException) {
                    exitCode = -3;
                    Console.Error.WriteLine("Invalid arguments");
                    showHelp = true;
                }
            }
            if (showHelp)
            {
                ShowHelp(options);
                return(exitCode);
            }

            // Check Arguments
            CheckArg(updateDLL, "No DLL file was specified");
            if (!listDatabases)
            {
                CheckArg(dbpassword, string.Format("No Database password specified for database {0}", dbname));
            }

            // If there are no custom methods specified we need a version number
            if (customMethod == null)
            {
                CheckArg(dbname, "No Database was specified");
            }

            // Begin Parsing DLL
            var          dllAssembly      = Assembly.LoadFile(updateDLL);
            var          upgradeAttribute = ADataUpdater.GetUpdateClassWithUpgradeAttribute(dllAssembly).Value;
            ADataUpdater schemaUpdater    = null;

            if (listDatabases)
            {
                // Read list of databases if listDatabases is true
                var databaseList = new List <DBConnection>();

                // Read the db names from input
                // format: dbname[;dbserver;dbuser;dbpassword]
                string line = null;
                while (!string.IsNullOrEmpty(line = Console.ReadLine()))
                {
                    var connection = DBConnection.Parse(line, dbserver, dbusername, dbpassword);
                    if (connection != null)
                    {
                        databaseList.Add(connection);
                    }
                }
                foreach (var db in databaseList)
                {
                    try {
                        switch (upgradeAttribute.DatabaseType)
                        {
                        case DatabaseType.Mysql:
                            schemaUpdater = new MysqlDataUpdater(db.dbServer, dbport, db.dbName, db.dbUsername, db.dbPassword, targetVersion, timeout);
                            break;

                        case DatabaseType.Redshift:
                            schemaUpdater = new RedshiftDataUpdater(db.dbServer, dbport, db.dbName, db.dbUsername, db.dbPassword, targetVersion, timeout);
                            break;

                        default:
                            throw new ShouldNeverHappenException("Unsupported database type");
                        }
                        if (sourceVersion != null)
                        {
                            schemaUpdater.SourceVersion = sourceVersion;
                        }
                    } catch (VersionInfoException) {
                        PrintErrorAndExit("You entered an incorrect version numbner.");
                    } catch (Exception) {
                        // If there is any problem creating the connection we will just keep going
                        errors++;
                        errorStrings.Add(string.Format("There was an error connecting to database {0} on {1}", db.dbName, db.dbServer));
                        continue;
                    }
                    if (verbose)
                    {
                        Console.WriteLine("\n--- Updating database {0} on server {1}", db.dbName, db.dbServer);
                    }

                    // Run methods on database
                    RunUpdate(schemaUpdater, dllAssembly, customMethod, param, verbose, dryrun, checkDb);
                }
            }
            else
            {
                try {
                    switch (upgradeAttribute.DatabaseType)
                    {
                    case DatabaseType.Mysql:
                        schemaUpdater = new MysqlDataUpdater(dbserver, dbport, dbname, dbusername, dbpassword, targetVersion, timeout);
                        break;

                    case DatabaseType.Redshift:
                        schemaUpdater = new RedshiftDataUpdater(dbserver, dbport, dbname, dbusername, dbpassword, targetVersion, timeout);
                        break;

                    default:
                        throw new ShouldNeverHappenException("Unsupported database type");
                    }
                    if (sourceVersion != null)
                    {
                        schemaUpdater.SourceVersion = sourceVersion;
                    }
                } catch (VersionInfoException) {
                    PrintErrorAndExit("You entered an incorrect version numner.");
                }

                // Run update
                RunUpdate(schemaUpdater, dllAssembly, customMethod, param, verbose, dryrun, checkDb);
            }
            if (errors > 0)
            {
                Console.WriteLine("\nThere were {0} errors:", errors);
                foreach (var error in errorStrings)
                {
                    Console.WriteLine("---" + error);
                }
            }
            return(exitCode);
        }
Exemple #6
0
        private static void RunUpdate(MysqlDataUpdater site, Assembly dllAssembly, string customMethods,string targetVersion, bool verbose, bool dryrun, bool checkdb)
        {
            // Execute custom methods
            if(customMethods != null && !checkdb) {
                var methods = customMethods.Split(',');
                foreach(var method in methods) {
                    if(verbose) {
                        Console.WriteLine(String.Format("Executing custom method: {0}", method));
                    }
                    if(!dryrun) {
                        site.ExecuteCustomMethod(method.Trim(), dllAssembly);
                    }
                }
            }

            // Execute update methods
            if(targetVersion != null) {
                site.LoadMethods(dllAssembly);
                List<string> methods;
                if(checkdb) {
                    methods = site.GetDataIntegrityMethods();
                } else {
                    methods = site.GetMethods();
                }

                // Execute each method
                foreach(var method in methods) {
                    if(verbose) {
                        Console.WriteLine(String.Format("Executing method: {0}", method));
                    }
                    if(!dryrun) {
                        try { site.TestConnection(); } catch(Exception) {
                            System.Threading.Thread.Sleep(5000);
                            site.TestConnection();
                        }
                        try {
                                site.ExecuteMethod(method);
                        } catch(Exception ex) {
                            Console.WriteLine(string.Format("\n --- Error occured in method {0}: \n\n{1}", method, ex.StackTrace));
                            if(checkdb) {
                                continue;
                            } else {
                                break;
                            }
                        }
                    }
                }
            }
        }
Exemple #7
0
        private static void RunUpdate(MysqlDataUpdater site, Assembly dllAssembly, string customMethods,string targetVersion, bool verbose, bool dryrun)
        {
            // Execute custom methods
            if(customMethods != null) {
                var methods = customMethods.Split(',');
                foreach(var method in methods) {
                    if(verbose) {
                        Console.WriteLine(String.Format("Executing custom method: {0}", method));
                    }
                    if(!dryrun) {
                        site.ExecuteCustomMethod(method.Trim(), dllAssembly);
                    }
                }
            }

            // Execute update methods
            if(targetVersion != null) {
                site.LoadMethods(dllAssembly);
                var methods = site.GetMethods();

                // Execute each method
                foreach(var method in methods) {
                    if(verbose) {
                        Console.WriteLine(String.Format("Executing method: {0}", method));
                    }
                    if(!dryrun) {
                        try { site.TestConnection(); } catch(Exception) {
                            System.Threading.Thread.Sleep(5000);
                            site.TestConnection();
                        }
                        site.ExecuteMethod(method);
                    }
                }
            }
        }