Ejemplo n.º 1
0
        private static void RunUpdate(ADataUpdater 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}\n\n{2}", method, ex.Message, ex.StackTrace);
                    if (!checkdb)
                    {
                        break;
                    }
                }
            }
        }
Ejemplo n.º 2
0
        private static void RunUpdate(ADataUpdater 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}\n\n{2}", method, ex.Message, ex.StackTrace);
                    if(!checkdb) {
                        break;
                    }
                }
            }
        }
Ejemplo n.º 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;
            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);
        }