コード例 #1
0
        public void run()
        {
            database_migrator.initialize_connections();

            Log.bound_to(this).log_an_info_event_containing("Running {0} v{1} against {2} - {3}.",
                                                            ApplicationParameters.name,
                                                            VersionInformation.get_current_assembly_version(),
                                                            database_migrator.database.server_name,
                                                            database_migrator.database.database_name);

            Log.bound_to(this).log_an_info_event_containing("Looking in {0} for scripts to run.", known_folders.up.folder_path);
            if (!silent)
            {
                Log.bound_to(this).log_an_info_event_containing("Please press enter when ready to kick...");
                Console.ReadLine();
            }

            if (run_in_a_transaction && !database_migrator.database.supports_ddl_transactions)
            {
                Log.bound_to(this).log_a_warning_event_containing("You asked to run in a transaction, but this dabasetype doesn't support DDL transactions.");
                if (!silent)
                {
                    Log.bound_to(this).log_an_info_event_containing("Please press enter to continue without transaction support...");
                    Console.ReadLine();
                }
                run_in_a_transaction = false;
            }

            create_change_drop_folder();
            Log.bound_to(this).log_a_debug_event_containing("The change_drop (output) folder is: {0}", known_folders.change_drop.folder_full_path);
            Log.bound_to(this).log_a_debug_event_containing("Using SearchAllSubdirectoriesInsteadOfTraverse execution: {0}", configuration.SearchAllSubdirectoriesInsteadOfTraverse);

            try
            {
                Log.bound_to(this).log_an_info_event_containing("{0}", "=".PadRight(50, '='));
                Log.bound_to(this).log_an_info_event_containing("Setup, Backup, Create/Restore/Drop");
                Log.bound_to(this).log_an_info_event_containing("{0}", "=".PadRight(50, '='));
                create_share_and_set_permissions_for_change_drop_folder();
                //database_migrator.backup_database_if_it_exists();
                remove_share_from_change_drop_folder();

                bool database_was_created = false;

                if (!dropping_the_database)
                {
                    if (!dont_create_the_database)
                    {
                        database_was_created = database_migrator.create_or_restore_database(get_custom_create_database_script());
                    }

                    if (configuration.RecoveryMode != RecoveryMode.NoChange)
                    {
                        database_migrator.set_recovery_mode(configuration.RecoveryMode == RecoveryMode.Simple);
                    }


                    database_migrator.open_connection(run_in_a_transaction);
                    Log.bound_to(this).log_an_info_event_containing("{0}", "=".PadRight(50, '='));
                    Log.bound_to(this).log_an_info_event_containing("RoundhousE Structure");
                    Log.bound_to(this).log_an_info_event_containing("{0}", "=".PadRight(50, '='));
                    database_migrator.run_roundhouse_support_tasks();

                    Log.bound_to(this).log_an_info_event_containing("{0}", "=".PadRight(50, '='));
                    Log.bound_to(this).log_an_info_event_containing("Versioning");
                    Log.bound_to(this).log_an_info_event_containing("{0}", "=".PadRight(50, '='));
                    string current_version = database_migrator.get_current_version(repository_path);
                    string new_version     = version_resolver.resolve_version();
                    Log.bound_to(this).log_an_info_event_containing(" Migrating {0} from version {1} to {2}.", database_migrator.database.database_name, current_version, new_version);
                    long version_id = database_migrator.version_the_database(repository_path, new_version);

                    Log.bound_to(this).log_an_info_event_containing("{0}", "=".PadRight(50, '='));
                    Log.bound_to(this).log_an_info_event_containing("Migration Scripts");
                    Log.bound_to(this).log_an_info_event_containing("{0}", "=".PadRight(50, '='));

                    database_migrator.open_admin_connection();
                    log_and_traverse(known_folders.alter_database, version_id, new_version, ConnectionType.Admin);
                    database_migrator.close_admin_connection();

                    if (database_was_created)
                    {
                        log_and_traverse(known_folders.run_after_create_database, version_id, new_version, ConnectionType.Default);
                    }

                    log_and_traverse(known_folders.run_before_up, version_id, new_version, ConnectionType.Default);
                    log_and_traverse(known_folders.up, version_id, new_version, ConnectionType.Default);

                    //int last_errors = -1;
                    //int new_errors = 0;
                    //while (last_errors != new_errors || last_errors !=0)
                    //{

                    //}
                    log_and_traverse(known_folders.run_first_after_up, version_id, new_version, ConnectionType.Default);
                    log_and_traverse(known_folders.functions, version_id, new_version, ConnectionType.Default);
                    log_and_traverse(known_folders.views, version_id, new_version, ConnectionType.Default);
                    log_and_traverse(known_folders.sprocs, version_id, new_version, ConnectionType.Default);
                    log_and_traverse(known_folders.indexes, version_id, new_version, ConnectionType.Default);
                    log_and_traverse(known_folders.run_after_other_any_time_scripts, version_id, new_version, ConnectionType.Default);

                    if (run_in_a_transaction)
                    {
                        database_migrator.close_connection();
                        database_migrator.open_connection(false);
                    }
                    log_and_traverse(known_folders.permissions, version_id, new_version, ConnectionType.Default);

                    Log.bound_to(this).log_an_info_event_containing(
                        "{0}{0}{1} v{2} has kicked your database ({3})! You are now at version {4}. All changes and backups can be found at \"{5}\".",
                        System.Environment.NewLine,
                        ApplicationParameters.name,
                        VersionInformation.get_current_assembly_version(),
                        database_migrator.database.database_name,
                        new_version,
                        known_folders.change_drop.folder_full_path);
                    database_migrator.close_connection();
                }
                else
                {
                    database_migrator.open_admin_connection();
                    database_migrator.delete_database();
                    database_migrator.close_admin_connection();
                    database_migrator.close_connection();
                    Log.bound_to(this).log_an_info_event_containing("{0}{0}{1} has removed database ({2}). All changes and backups can be found at \"{3}\".",
                                                                    System.Environment.NewLine,
                                                                    ApplicationParameters.name,
                                                                    database_migrator.database.database_name,
                                                                    known_folders.change_drop.folder_full_path);
                }
            }
            catch (Exception ex)
            {
                Log.bound_to(this).log_an_error_event_containing("{0} encountered an error.{1}{2}{3}",
                                                                 ApplicationParameters.name,
                                                                 run_in_a_transaction
                                                                     ? " You were running in a transaction though, so the database should be in the state it was in prior to this piece running. This does not include a drop/create or any creation of a database, as those items can not run in a transaction."
                                                                     : string.Empty,
                                                                 System.Environment.NewLine,
                                                                 ex.to_string());

                throw;
            }
            finally
            {
                database_migrator.database.Dispose();
                //copy_log_file_to_change_drop_folder();
            }
        }
コード例 #2
0
        public void run()
        {
            database_migrator.initialize_connections();

            Log.bound_to(this).log_an_info_event_containing("Running {0} v{1} against {2} - {3}.",
                                                            ApplicationParameters.name,
                                                            infrastructure.VersionInformation.get_current_assembly_version(),
                                                            database_migrator.database.server_name,
                                                            database_migrator.database.database_name);

            Log.bound_to(this).log_an_info_event_containing("Looking in {0} for scripts to run.", known_folders.up.folder_path);
            if (!silent)
            {
                Log.bound_to(this).log_an_info_event_containing("Please press enter when ready to kick...");
                Console.ReadLine();
            }

            if (run_in_a_transaction && !database_migrator.database.supports_ddl_transactions)
            {
                Log.bound_to(this).log_a_warning_event_containing("You asked to run in a transaction, but this dabasetype doesn't support DDL transactions.");
                if (!silent)
                {
                    Log.bound_to(this).log_an_info_event_containing("Please press enter to continue without transaction support...");
                    Console.ReadLine();
                }
                run_in_a_transaction = false;
            }

            create_change_drop_folder();
            Log.bound_to(this).log_a_debug_event_containing("The change_drop (output) folder is: {0}", known_folders.change_drop.folder_full_path);

            try
            {
                Log.bound_to(this).log_an_info_event_containing("{0}", "=".PadRight(50, '='));
                Log.bound_to(this).log_an_info_event_containing("Setup, Backup, Create/Restore/Drop");
                Log.bound_to(this).log_an_info_event_containing("{0}", "=".PadRight(50, '='));
                create_share_and_set_permissions_for_change_drop_folder();
                //database_migrator.backup_database_if_it_exists();
                remove_share_from_change_drop_folder();

                if (!dropping_the_database)
                {
                    if (!dont_create_the_database)
                    {
                        database_migrator.open_admin_connection();
                        database_migrator.create_or_restore_database();
                        database_migrator.set_recovery_mode(use_simple_recovery);
                        database_migrator.close_admin_connection();
                    }
                    database_migrator.open_connection(run_in_a_transaction);
                    Log.bound_to(this).log_an_info_event_containing("{0}", "=".PadRight(50, '='));
                    Log.bound_to(this).log_an_info_event_containing("RoundhousE Structure");
                    Log.bound_to(this).log_an_info_event_containing("{0}", "=".PadRight(50, '='));
                    database_migrator.run_roundhouse_support_tasks();

                    Log.bound_to(this).log_an_info_event_containing("{0}", "=".PadRight(50, '='));
                    Log.bound_to(this).log_an_info_event_containing("Versioning");
                    Log.bound_to(this).log_an_info_event_containing("{0}", "=".PadRight(50, '='));
                    string current_version = database_migrator.get_current_version(repository_path);
                    string new_version     = version_resolver.resolve_version();
                    Log.bound_to(this).log_an_info_event_containing(" Migrating {0} from version {1} to {2}.", database_migrator.database.database_name, current_version, new_version);
                    long version_id = database_migrator.version_the_database(repository_path, new_version);

                    Log.bound_to(this).log_an_info_event_containing("{0}", "=".PadRight(50, '='));
                    Log.bound_to(this).log_an_info_event_containing("Migration Scripts");
                    Log.bound_to(this).log_an_info_event_containing("{0}", "=".PadRight(50, '='));
                    Log.bound_to(this).log_an_info_event_containing("Looking for {0} scripts in \"{1}\". These should be one time only scripts.", "Update", known_folders.up.folder_full_path);
                    Log.bound_to(this).log_an_info_event_containing("{0}", "-".PadRight(50, '-'));
                    traverse_files_and_run_sql(known_folders.up.folder_full_path, version_id, known_folders.up, environment, new_version);

                    //todo: remember when looking through all files below here, change CREATE to ALTER
                    // we are going to create the create if not exists script

                    Log.bound_to(this).log_an_info_event_containing("{0}", "-".PadRight(50, '-'));
                    Log.bound_to(this).log_an_info_event_containing("Looking for {0} scripts in \"{1}\".", "Run First After Update", known_folders.run_first_after_up.folder_full_path);
                    Log.bound_to(this).log_an_info_event_containing("{0}", "-".PadRight(50, '-'));
                    traverse_files_and_run_sql(known_folders.run_first_after_up.folder_full_path, version_id, known_folders.run_first_after_up, environment, new_version);
                    Log.bound_to(this).log_an_info_event_containing("{0}", "-".PadRight(50, '-'));
                    Log.bound_to(this).log_an_info_event_containing("Looking for {0} scripts in \"{1}\".", "Function", known_folders.functions.folder_full_path);
                    Log.bound_to(this).log_an_info_event_containing("{0}", "-".PadRight(50, '-'));
                    traverse_files_and_run_sql(known_folders.functions.folder_full_path, version_id, known_folders.functions, environment, new_version);
                    Log.bound_to(this).log_an_info_event_containing("{0}", "-".PadRight(50, '-'));
                    Log.bound_to(this).log_an_info_event_containing("Looking for {0} scripts in \"{1}\".", "View", known_folders.views.folder_full_path);
                    Log.bound_to(this).log_an_info_event_containing("{0}", "-".PadRight(50, '-'));
                    traverse_files_and_run_sql(known_folders.views.folder_full_path, version_id, known_folders.views, environment, new_version);
                    Log.bound_to(this).log_an_info_event_containing("{0}", "-".PadRight(50, '-'));
                    Log.bound_to(this).log_an_info_event_containing("Looking for {0} scripts in \"{1}\".", "Stored Procedure", known_folders.sprocs.folder_full_path);
                    Log.bound_to(this).log_an_info_event_containing("{0}", "-".PadRight(50, '-'));
                    traverse_files_and_run_sql(known_folders.sprocs.folder_full_path, version_id, known_folders.sprocs, environment, new_version);
                    Log.bound_to(this).log_an_info_event_containing("{0}", "-".PadRight(50, '-'));
                    Log.bound_to(this).log_an_info_event_containing("Looking for {0} scripts in \"{1}\". These scripts will run every time.", "Permission", known_folders.permissions.folder_full_path);
                    Log.bound_to(this).log_an_info_event_containing("{0}", "-".PadRight(50, '-'));
                    if (run_in_a_transaction)
                    {
                        database_migrator.close_connection();
                        database_migrator.open_connection(false);
                    }

                    traverse_files_and_run_sql(known_folders.permissions.folder_full_path, version_id, known_folders.permissions, environment, new_version);

                    Log.bound_to(this).log_an_info_event_containing("{0}{0}{1} v{2} has kicked your database ({3})! You are now at version {4}. All changes and backups can be found at \"{5}\".",
                                                                    System.Environment.NewLine,
                                                                    ApplicationParameters.name,
                                                                    infrastructure.VersionInformation.get_current_assembly_version(),
                                                                    database_migrator.database.database_name,
                                                                    new_version,
                                                                    known_folders.change_drop.folder_full_path);
                    database_migrator.close_connection();
                }
                else
                {
                    database_migrator.open_admin_connection();
                    database_migrator.delete_database();
                    database_migrator.close_admin_connection();
                    Log.bound_to(this).log_an_info_event_containing("{0}{0}{1} has removed database ({2}). All changes and backups can be found at \"{3}\".",
                                                                    System.Environment.NewLine,
                                                                    ApplicationParameters.name,
                                                                    database_migrator.database.database_name,
                                                                    known_folders.change_drop.folder_full_path);
                }
            }
            catch (Exception ex)
            {
                Log.bound_to(this).log_an_error_event_containing("{0} encountered an error.{1}{2}{3}",
                                                                 ApplicationParameters.name,
                                                                 run_in_a_transaction ? " You were running in a transaction though, so the database should be in the state it was in prior to this piece running. This does not include a drop/create or any creation of a database, as those items can not run in a transaction." : string.Empty,
                                                                 System.Environment.NewLine,
                                                                 ex.ToString());
                throw;
            }
            finally
            {
                copy_log_file_to_change_drop_folder();
            }
        }
コード例 #3
0
        public void run()
        {
            initialize_database_connections();

            log_initial_events();

            if (configuration.DryRun)
            {
                this.log_info_event_on_bound_logger("This is a dry run, nothing will be done to the database.");
                //WaitForKeypress();
            }

            handle_invalid_transaction_argument();
            create_change_drop_folder_and_log();

            try
            {
                this.log_action_starting();

                if (configuration.SimpleOutput)
                {
                    create_default_combined_script_path_file_if_needed(get_combined_script_file_path());
                }

                create_share_and_set_permissions_for_change_drop_folder();

                if (configuration.DryRun && !database_migrator.database.has_roundhouse_support_tables())
                {
                    log_warning_event_on_bound_logger("Database {1} on server {2} does not have the RoundhousE support{0}" +
                                                      "tables (ScriptsRun, ScriptsRunErrors, Version) created.{0}{0}" +
                                                      "You can not run RoundhousE in dry run mode against this database. To fix this: {0}" +
                                                      "    (A) -EITHER- Run RoundhousE in regular mode (without the --dryrun switch){0}" +
                                                      "    (B) -OR- Run the '{3}' script located in the drop folder manually.{0}{0}",
                                                      System.Environment.NewLine,
                                                      database_migrator.database.database_name,
                                                      database_migrator.database.server_name,
                                                      "CreateRoundhouseSupportTables.sql");
                    var script = database_migrator.database.generate_support_tables_script();
                    put_script_in_change_drop_for_database_command("CreateRoundhouseSupportTables", script, true);
                    return;
                }

                // TODO (PMO): This was commented out, figure out if we can turn it on again?
                //database_migrator.backup_database_if_it_exists();
                remove_share_from_change_drop_folder();

                bool database_was_created = false;

                if (!dropping_the_database)
                {
                    if (!dont_create_the_database)
                    {
                        database_was_created = create_or_restore_the_database();
                    }

                    set_database_recovery_mode();

                    open_connection_in_transaction_if_needed();

                    log_and_run_support_tasks();

                    string new_version = version_resolver.resolve_version();
                    long   version_id  = 0;
                    try
                    {
                        version_id = log_and_run_version_the_database(new_version);
                    }
                    catch (InvalidOperationException ex)
                    {
                        // this is where we bug out if in dry run and no support tables
                        return;
                    }

                    run_out_side_of_transaction_folder(known_folders.before_migration, version_id, new_version);

                    log_migration_scripts();
                    log_and_traverse_known_folders(version_id, new_version, database_was_created);

                    if (run_in_a_transaction)
                    {
                        if (configuration.DryRun)
                        {
                            this.log_info_event_on_bound_logger("{0}-DryRun-Would have committed the transaction on database {1}",
                                                                System.Environment.NewLine,
                                                                database_migrator.database.database_name);
                        }
                        else
                        {
                            database_migrator.close_connection();
                            database_migrator.open_connection(false);
                        }
                    }
                    log_and_traverse(known_folders.permissions, version_id, new_version, ConnectionType.Default);
                    run_out_side_of_transaction_folder(known_folders.after_migration, version_id, new_version);

                    if (configuration.DryRun)
                    {
                        log_info_event_on_bound_logger(
                            "{0}{0}-DryRun-{1} v{2} would have kicked your database ({3})! You would be at version {4}. All changes and backups can be found at \"{5}\".",
                            System.Environment.NewLine,
                            ApplicationParameters.name,
                            VersionInformation.get_current_assembly_version(),
                            database_migrator.database.database_name,
                            new_version,
                            known_folders.change_drop.folder_full_path);
                    }
                    else
                    {
                        log_info_event_on_bound_logger(
                            "{0}{0}{1} v{2} has kicked your database ({3})! You are now at version {4}. All changes and backups can be found at \"{5}\".",
                            System.Environment.NewLine,
                            ApplicationParameters.name,
                            VersionInformation.get_current_assembly_version(),
                            database_migrator.database.database_name,
                            new_version,
                            known_folders.change_drop.folder_full_path);
                    }
                    database_migrator.close_connection();
                }
                else
                {
                    drop_the_database();
                }
            }
            catch (Exception ex)
            {
                this.log_exception_and_throw(ex);
            }
            finally
            {
                database_migrator.database.Dispose();
                //copy_log_file_to_change_drop_folder();

                if (configuration.ExploreChangeDrop)
                {
                    Process.Start(known_folders.change_drop.folder_full_path);
                }
            }
        }