public MigrationResultSet Run(string connection, string workingDirectory, string createScript, string databaseName, string server, int timeout)
        {
            var logger = new RelativityRoundHouseLogger();

            //create migration agent
            var migrationAgent = new Migrate();

            //get default round house settings
            var configuration = migrationAgent.GetConfiguration();

            ApplicationConfiguraton.set_defaults_if_properties_are_not_set(configuration);

            //get the action delegate settings
            var roundHouseService = new RoundHouseSettingsService
            {
                ServerName = server
            };
            var roundhouseConfiguration = roundHouseService.GetRoundHouseSettings(connection, workingDirectory, createScript);

            //register the delegate
            migrationAgent.Set(roundhouseConfiguration);
            if (!string.IsNullOrEmpty(server))
            {
                migrationAgent.Set(x => x.DatabaseName = databaseName);
            }
            migrationAgent.SetCustomLogging(logger);
            migrationAgent.Set(x => x.CommandTimeout      = timeout);
            migrationAgent.Set(x => x.CommandTimeoutAdmin = timeout);

            // Update the database
            try
            {
                // Some googling shows that roundhouse may not be threadsafe.
                // This starts a lock context to prevent multiple threads running roundhouse deployments at the same time.
                lock (obj)
                {
                    migrationAgent.Run();
                }
            }
            catch (Exception e)
            {
                //This is theoretically already logged by RoundhousE, but if something in RH breaks rather than in SQL, I want to know
                logger.Messages.Add(new LogMessage(LogSeverity.Error, e.ToString()));
                return(new MigrationResultSet(false, logger.Messages));
            }

            return(new MigrationResultSet(true, logger.Messages));
        }
        /// <summary>
        /// Set up your migrator and call this to generate a diff file. Known limitations - will not detect size changes or renames. In other words, destructive changes will need to be done by hand.
        /// </summary>
        /// <param name="diffingType">Are you in greenfield development or have you went to production (maintenance)? Do you want it to restore during maintenance mode?</param>
        /// <param name="nameOfOutputScriptToCreateOrReplace">This is something like 0001_CreateTables.sql. This will end up in your up folder, assuming you have set up your migrator configuration correctly.</param>
        /// <param name="databaseMigrator">The Migrator to use when running.</param>
        /// <param name="mappingsAssembly">This is the assembly that contains your mapping files.</param>
        /// <param name="conventionsAssembly">This is the assembly that contains your conventions. If you do not have conventions set up, just pass null. It will use the mappingsAssembly</param>
        public void Run(RoundhousEFluentNHDiffingType diffingType, string nameOfOutputScriptToCreateOrReplace, Migrate databaseMigrator, Assembly mappingsAssembly, Assembly conventionsAssembly)
        {
            name_of_script_to_create = nameOfOutputScriptToCreateOrReplace;
            var configuration = databaseMigrator.GetConfiguration();

            configuration.Silent  = true;
            configuration.Restore = false;
            ApplicationConfiguraton.set_defaults_if_properties_are_not_set(configuration);
            path_to_sql_scripts_up_folder = Path.Combine(configuration.SqlFilesDirectory, configuration.UpFolderName);

            databaseMigrator.Set(c =>
            {
                c.Silent      = true;
                c.VersionFile = mappingsAssembly.Location;
            }
                                 );

            switch (diffingType)
            {
            case RoundhousEFluentNHDiffingType.InitialDevelopment:
                run_initial_database_setup(databaseMigrator, configuration, mappingsAssembly, conventionsAssembly);
                break;

            case RoundhousEFluentNHDiffingType.Maintenance:
                run_maintenance_database_setup(false, databaseMigrator, configuration, mappingsAssembly, conventionsAssembly, name_of_script_to_create);
                break;

            case RoundhousEFluentNHDiffingType.MaintenanceWithRestore:
                run_maintenance_database_setup(true, databaseMigrator, configuration, mappingsAssembly, conventionsAssembly, name_of_script_to_create);
                break;
            }
        }
Ejemplo n.º 3
0
        public void Dispose()
        {
            var roundhouseMigrate = new Migrate();

            roundhouseMigrate.Set(x => x.ConnectionString = database.ConnectionString);
            roundhouseMigrate.GetConfiguration().DoNotCreateDatabase = true;
            roundhouseMigrate.GetConfiguration().Drop            = true;
            roundhouseMigrate.GetConfiguration().WithTransaction = false;
            roundhouseMigrate.Run();
        }
        void rh(Roundhouse directive, string destinationDirectory)
        {
            var migrate = new Migrate();

            migrate.Set(cfg =>
            {
                cfg.CommandTimeout    = directive.GetCommandTimeout();
                cfg.ConnectionString  = directive.ConnectionString;
                cfg.SqlFilesDirectory = destinationDirectory;
                cfg.Silent            = true;
            }).Run();
        }
Ejemplo n.º 5
0
 public void SetMigrateFolders(Migrate migrateSettings, string schemaScriptsFolder)
 {
     migrateSettings.Set(x => x.SqlFilesDirectory = schemaScriptsFolder).Set(
         x => x.RunAfterCreateDatabaseFolderName  = RunAfterCreateDatabaseFolderName(schemaScriptsFolder))
     .Set(x => x.IndexesFolderName = IndexesFolderName(schemaScriptsFolder))
     .Set(
         x =>
         x.RunAfterOtherAnyTimeScriptsFolderName =
             RunAfterOtherAnyTimeScriptsFolderName(schemaScriptsFolder))
     .Set(x => x.SprocsFolderName    = SprocsFolderName(schemaScriptsFolder))
     .Set(x => x.FunctionsFolderName = FunctionsFolderName(schemaScriptsFolder))
     .Set(x => x.ViewsFolderName     = ViewsFolderName(schemaScriptsFolder))
     .Set(x => x.UpFolderName        = UpFolderName(schemaScriptsFolder));
 }
Ejemplo n.º 6
0
        public void SetRunRestoreOptions(Migrate migrateSettings)
        {
            if (!string.IsNullOrWhiteSpace(restorePath) && !File.Exists(restorePath))
            {
                throw new FileNotFoundException($"Restore Path {restorePath}\r\ndoes not exist");
            }

            if (!string.IsNullOrWhiteSpace(restorePath))
            {
                var database = Path.GetFileNameWithoutExtension(restorePath);
                migrateSettings.Set(x => x.RestoreFromPath = restorePath)
                .Set(x => x.Restore = !string.IsNullOrWhiteSpace(restorePath))
                .Set(x => x.RestoreCustomOptions = string.Format(@", MOVE '{0}' TO '{1}\{0}.mdf', MOVE '{0}_log' TO '{1}\{0}_log.LDF'", database, @"c:\Temp"));
            }
        }
Ejemplo n.º 7
0
 private void SetFolderNames(Migrate roundhouseMigrate)
 {
     roundhouseMigrate
     .Set(x => x.RunAfterCreateDatabaseFolderName      = "RunAfterCreateDatabase")
     .Set(x => x.RunAfterOtherAnyTimeScriptsFolderName = "RunAfterOtherAnyTimeScripts")
     .Set(x => x.FunctionsFolderName       = "Functions")
     .Set(x => x.SprocsFolderName          = "Sprocs")
     .Set(x => x.UpFolderName              = "Up")
     .Set(x => x.IndexesFolderName         = "Indexes")
     .Set(x => x.PermissionsFolderName     = "Permissions")
     .Set(x => x.ViewsFolderName           = "Views")
     .Set(x => x.AlterDatabaseFolderName   = "AlterDatabase")
     .Set(x => x.TriggersFolderName        = "Triggers")
     .Set(x => x.RunFirstAfterUpFolderName = "RunFirstAfterUp");
 }
Ejemplo n.º 8
0
        public void Deploy(string schemaScriptsFolder, string repository = "", bool dropDatabase = false)
        {
            if (schemaScriptsFolder == string.Empty)
            {
                schemaScriptsFolder = Assembly.GetExecutingAssembly().Directory();
            }

            if (!Directory.Exists(schemaScriptsFolder))
            {
                throw new DirectoryNotFoundException(
                          string.Format(
                              "Database schema scripts folder {0}\r\ndoes not exist", schemaScriptsFolder));
            }

            var roundhouseMigrate = new Migrate();

            if (DatabaseFolderStructure != null)
            {
                DatabaseFolderStructure.SetMigrateFolders(roundhouseMigrate, schemaScriptsFolder);
            }
            if (databaseRestoreOptions != null)
            {
                databaseRestoreOptions.SetRunRestoreOptions(roundhouseMigrate);
            }

            roundhouseMigrate.Set(x => x.ConnectionString = this.ConnectionString)
            .Set(x => x.SqlFilesDirectory          = schemaScriptsFolder)
            .Set(x => x.VersionFile                = Path.Combine(schemaScriptsFolder, "_BuildInfo.txt"))
            .Set(x => x.WithTransaction            = WithTransaction)
            .Set(x => x.Silent                     = true)
            .Set(x => x.RecoveryMode               = RecoveryMode.NoChange)
            .Set(x => x.RepositoryPath             = repository)
            .Set(x => x.WarnOnOneTimeScriptChanges = WarnOnOneTimeScriptChanges)
            .Set(x => x.DisableTokenReplacement    = true)
            .Set(x => x.Drop = dropDatabase)
            .SetCustomLogging(logger);

            if (databaseRestoreOptions != null)
            {
                roundhouseMigrate.RunRestore();
            }
            else
            {
                roundhouseMigrate.Run();
            }
        }
        /// <summary>
        /// Set up your migrator and call this to generate a diff file. Known limitations - will not detect size changes or renames. In other words, destructive changes will need to be done by hand.
        /// </summary>
        /// <param name="databaseMigrator">The Migrator to use when running.</param>
        /// <param name="migrationsAssembly">This is the assembly that contains your mapping files.</param>
        public void Run(Migrate databaseMigrator, Assembly migrationsAssembly)
        {
            var configuration = databaseMigrator.GetConfiguration();

            configuration.Silent  = true;
            configuration.Restore = false;
            ApplicationConfiguraton.set_defaults_if_properties_are_not_set(configuration);
            path_to_sql_scripts_up_folder = Path.Combine(configuration.SqlFilesDirectory, configuration.UpFolderName);

            databaseMigrator.Set(c =>
            {
                c.Silent      = true;
                c.VersionFile = migrationsAssembly.Location;
            }
                                 );

            run_changes(databaseMigrator, configuration, migrationsAssembly);
        }
Ejemplo n.º 10
0
        public static void Run(string connectionString, string scriptsLocation, string environmentName, bool dropDatabase, bool useSimpleRecoveryMode, string repositoryPath, string versionFile, string versionXPath, int commmandTimeout, int commandTimeoutAdmin, bool restore, string restorePath, int restoreTimeout, string restoreCustomOptions, string functionsFolderName, string sprocsFolderName, string viewsFolderName, string upFolderName, string versionTable, string scriptsRunTable, string scriptsRunErrorTable, bool?warnOnOneTimeScriptChanges, string outputPath)
        {
            var migrate = new Migrate();

            migrate
            .Set(p =>
            {
                p.Logger = new roundhouse.infrastructure.logging.custom.Log4NetLogger(Logging.WellKnown.DatabaseChanges);

                p.ConnectionString  = connectionString;
                p.SqlFilesDirectory = scriptsLocation;
                p.EnvironmentName   = environmentName;
                p.Drop = dropDatabase;
                p.RecoveryModeSimple   = useSimpleRecoveryMode;
                p.Restore              = restore;
                p.RestoreFromPath      = restorePath;
                p.RestoreTimeout       = restoreTimeout;
                p.RestoreCustomOptions = restoreCustomOptions;
                p.RepositoryPath       = repositoryPath;
                p.VersionFile          = versionFile;
                p.VersionXPath         = versionXPath;
                p.CommandTimeout       = commmandTimeout;
                p.CommandTimeoutAdmin  = commandTimeoutAdmin;

                p.FunctionsFolderName = functionsFolderName;
                p.SprocsFolderName    = sprocsFolderName;
                p.ViewsFolderName     = viewsFolderName;
                p.UpFolderName        = upFolderName;

                p.VersionTableName          = versionTable;
                p.ScriptsRunTableName       = scriptsRunTable;
                p.ScriptsRunErrorsTableName = scriptsRunErrorTable;
                p.OutputPath = outputPath;

                if (warnOnOneTimeScriptChanges.HasValue)
                {
                    p.WarnOnOneTimeScriptChanges = warnOnOneTimeScriptChanges.Value;
                }

                p.Silent = true;
            })
            .Run();
        }
Ejemplo n.º 11
0
        public void Deploy(string schemaScriptsFolder, string repository = "", bool dropDatabase = false, TimeSpan?commandTimeout = null)
        {
            if (commandTimeout == null)
            {
                commandTimeout = TimeSpan.FromSeconds(30);
            }

            if (schemaScriptsFolder == string.Empty)
            {
                schemaScriptsFolder = Assembly.GetExecutingAssembly().Directory();
            }

            if (!Directory.Exists(schemaScriptsFolder))
            {
                throw new DirectoryNotFoundException(
                          string.Format(
                              "Database schema scripts folder {0}\r\ndoes not exist", schemaScriptsFolder));
            }

            var roundhouseMigrate = new Migrate();

            if (DatabaseFolderStructure != null)
            {
                DatabaseFolderStructure.SetMigrateFolders(roundhouseMigrate, schemaScriptsFolder);
            }
            if (databaseRestoreOptions != null)
            {
                databaseRestoreOptions.SetRunRestoreOptions(roundhouseMigrate);
            }

            roundhouseMigrate.Set(x => x.ConnectionString = ConnectionString)
            .Set(x => x.SqlFilesDirectory = schemaScriptsFolder)
            .Set(x => x.VersionFile       = Path.Combine(schemaScriptsFolder, "_BuildInfo.txt"))
            .Set(x => x.WithTransaction   = WithTransaction)
            .Set(x => x.Silent            = true)
            .Set(x => x.CommandTimeout    = Convert.ToInt32(commandTimeout.Value.TotalSeconds))
            .Set(x =>
            {
                if (!string.IsNullOrEmpty(OutputPath))
                {
                    x.OutputPath = OutputPath;
                }
            })
            .Set(x =>
            {
                var createDatabaseCustomScript = Path.Combine(schemaScriptsFolder, "CreateDatabase.sql");
                if (File.Exists(createDatabaseCustomScript))
                {
                    x.CreateDatabaseCustomScript = createDatabaseCustomScript;
                }
            })
            .Set(x => x.RecoveryMode               = RecoveryMode.NoChange)
            .Set(x => x.RepositoryPath             = repository)
            .Set(x => x.WarnOnOneTimeScriptChanges = WarnOnOneTimeScriptChanges)
            .Set(x => x.DisableTokenReplacement    = true)
            .Set(x => x.Drop            = dropDatabase)
            .Set(x => x.DisableOutput   = true)
            .Set(x => x.DefaultEncoding = Encoding.Default)
            .SetCustomLogging(logger);

            if (databaseRestoreOptions != null)
            {
                roundhouseMigrate.RunRestore();
            }
            else
            {
                roundhouseMigrate.Run();
            }
        }