/// <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; } }
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) { 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.Silent = true; }) .Run(); }
private static TemporarySqlLocalDbInstance ValueFactory() { var instance = TemporarySqlLocalDbInstance.Create(true); var builder = instance.CreateConnectionStringBuilder(); var databaseName = instance.Name; using (var conn = instance.CreateConnection()) { conn.Open(); var cmd = conn.CreateCommand(); var fileName = Path.Combine(Environment.CurrentDirectory, databaseName + ".mdf"); cmd.CommandText = string.Format("CREATE DATABASE [{0}] on (name='{0}', filename='{1}')", databaseName, fileName); cmd.ExecuteNonQuery(); } Console.WriteLine(instance.Name); builder.InitialCatalog = databaseName; var connectionString = builder.ConnectionString; var migrate = new Migrate().Set(c => { c.ConnectionString = connectionString; c.DatabaseName = databaseName; c.Silent = true; c.WithTransaction = true; c.SqlFilesDirectory = Path.Combine(Environment.CurrentDirectory, "DatabaseMigration"); }); migrate.Run(); return instance; }
private static void RunRoundhouseNhibernate() { var mappingsAssembly = Assembly.LoadFrom(_mappingsAssemblyPath); var conventionsAssembly = Assembly.LoadFrom(_conventionsAssemblyPath); var migrator = new Migrate().Set(c => { c.DatabaseName = _databaseName; c.RepositoryPath = _repositoryPath; c.SqlFilesDirectory = _pathToSqlScripts; c.RestoreFromPath = _pathToRestore; c.Silent = true; c.RecoveryModeSimple = true; }); var diffType = _restoreDuringMaintenance ? RoundhousEFluentNHDiffingType.MaintenanceWithRestore : RoundhousEFluentNHDiffingType.Maintenance; var scriptName = _nameOfUpdateScript; if (_isThisInitialDevelopment) { scriptName = _nameOfInitialScript; diffType = RoundhousEFluentNHDiffingType.InitialDevelopment; } new DifferenceWithFluentNHibernate().Run(diffType, scriptName, migrator, mappingsAssembly, conventionsAssembly); }
// initial database setup private void run_initial_database_setup(Migrate migrator, ConfigurationPropertyHolder configuration, Assembly mappings_assembly, Assembly conventions_assembly) { var files_directory = configuration.SqlFilesDirectory; configuration.SqlFilesDirectory = "."; migrator.Run(); generate_database_schema(configuration.DatabaseName, mappings_assembly, conventions_assembly); configuration.SqlFilesDirectory = files_directory; migrator.RunDropCreate(); }
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, string alterDatabaseFolderName, string runAfterCreateDatabaseFolderName, string runBeforeUpFolderName, string runFirstAfterUpFolderName, string indexesFolderName, string runAfterOtherAnyTimeScriptsFolderName, string permissionsFolderName) { 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.AlterDatabaseFolderName = alterDatabaseFolderName; p.RunAfterCreateDatabaseFolderName = runAfterCreateDatabaseFolderName; p.RunBeforeUpFolderName = runBeforeUpFolderName; p.UpFolderName = upFolderName; p.RunFirstAfterUpFolderName = runFirstAfterUpFolderName; p.FunctionsFolderName = functionsFolderName; p.ViewsFolderName = viewsFolderName; p.SprocsFolderName = sprocsFolderName; p.IndexesFolderName = indexesFolderName; p.RunAfterOtherAnyTimeScriptsFolderName = runAfterOtherAnyTimeScriptsFolderName; p.PermissionsFolderName = permissionsFolderName; p.VersionTableName = versionTable; p.ScriptsRunTableName = scriptsRunTable; p.ScriptsRunErrorsTableName = scriptsRunErrorTable; p.OutputPath = outputPath; if (warnOnOneTimeScriptChanges.HasValue) { p.WarnOnOneTimeScriptChanges = warnOnOneTimeScriptChanges.Value; } p.Silent = true; }) .Run(); }
public static void Run(Logger log, string connectionString, string scriptsLocation, string environmentName, bool dropDatabase, bool useSimpleRecoveryMode,string repositoryPath,string versionFile,string versionXPath, bool restore, string restorePath) { var migrate = new Migrate(); migrate .SetCustomLogging(log) .Set(p => { p.ConnectionString = connectionString; p.SqlFilesDirectory = scriptsLocation; p.EnvironmentName = environmentName; p.Drop = dropDatabase; p.RecoveryModeSimple = useSimpleRecoveryMode; p.Restore = restore; p.RestoreFromPath = restorePath; p.RepositoryPath = repositoryPath; p.VersionFile = versionFile; p.VersionXPath = versionXPath; p.Silent = true; }) .Run(); }
private static void RunRoundhouse() { var migrationsAssembly = Assembly.LoadFrom(RefreshDatabaseParameters.MigrationsAssemblyPath); var migrator = new Migrate().Set(c => { c.Logger = new roundhouse.infrastructure.logging.custom.ConsoleLogger(); c.ConnectionString = RefreshDatabaseParameters.Database.GetConnectionString(); c.RepositoryPath = RefreshDatabaseParameters.RepositoryPath; c.SqlFilesDirectory = RefreshDatabaseParameters.PathToSqlScripts; c.Restore = false; c.RestoreFromPath = RefreshDatabaseParameters.PathToRestore; if (!string.IsNullOrWhiteSpace(RefreshDatabaseParameters.RestoreCustomOptions)) { c.RestoreCustomOptions = RefreshDatabaseParameters.RestoreCustomOptions; } c.Silent = true; c.RecoveryModeSimple = true; c.VersionFile = RefreshDatabaseParameters.MigrationsAssemblyPath; }); new DifferenceWithEntityFramework().Run(migrator, migrationsAssembly); }
private static void Main(string[] args) { Console.OutputEncoding = Encoding.Default; var deployType = Environment.GetEnvironmentVariable("roundhouse-deploy-type"); DatabaseType?databaseType = null; DatabaseType t; if (Enum.TryParse(Environment.GetEnvironmentVariable("roundhouse-database-type"), out t)) { databaseType = t; } var server = Environment.GetEnvironmentVariable("roundhouse-server"); var login = Environment.GetEnvironmentVariable("roundhouse-login"); var password = Environment.GetEnvironmentVariable("roundhouse-password"); var database = Environment.GetEnvironmentVariable("roundhouse-database"); var folder = Environment.GetEnvironmentVariable("roundhouse-folder"); var environment = Environment.GetEnvironmentVariable("roundhouse-environment"); var version = Environment.GetEnvironmentVariable("roundhouse-version"); var failonNonUTF8 = Environment.GetEnvironmentVariable("roundhouse-failOnNonUTF8") == "true"; var verbose = Environment.GetEnvironmentVariable("roundhouse-verbose") == "true"; DeployType[] types; switch (deployType) { case "deploy": types = new[] { DeployType.Deploy }; break; case "drop": types = new[] { DeployType.Drop }; break; case "dropDeploy": types = new[] { DeployType.Drop, DeployType.Deploy }; break; default: throw new InvalidOperationException("Unknown deploy type"); } var filesChecked = false; foreach (var type in types) { var a = new List <string>(args); if (databaseType != null) { a.Add("-dt"); a.Add(databaseType.ToString()); } if (!string.IsNullOrWhiteSpace(server)) { a.Add("-s"); a.Add(server); } if (!string.IsNullOrWhiteSpace(database)) { a.Add("-d"); a.Add(database); } if (!string.IsNullOrWhiteSpace(folder)) { a.Add("-f"); a.Add(folder); } if (!string.IsNullOrWhiteSpace(environment)) { a.Add("-env"); a.Add(environment); } if (!string.IsNullOrWhiteSpace(version)) { a.Add("-v"); a.Add(version); } if (!string.IsNullOrWhiteSpace(login)) { switch (databaseType) { case DatabaseType.sqlserver: case DatabaseType.sql2000: a.Add("-c"); a.Add( $"data source={server};initial catalog={database};Integrated Security=false;User ID={login};Password={password};"); break; case DatabaseType.oracle: a.Add("-c"); a.Add($"Data Source={server}/{database};User Id={login};Password={password};Integrated Security=no;"); a.Add("-csa"); a.Add($"Data Source={server};User Id={login};Password={password};Integrated Security=no;"); break; case DatabaseType.mysql: a.Add("-c"); a.Add($"Server={server};Database={database};Port=3306;Uid={login};Pwd={password};"); break; case DatabaseType.postgres: a.Add("-c"); a.Add($"Server={server};Database={database};Port=5432;UserId={login};Password={password};"); break; default: throw new ArgumentOutOfRangeException(); } } if (type == DeployType.Drop) { a.Add("--drop"); } if (args.Length > 0) { a.AddRange(args); } var config = console.Program.set_up_configuration_and_build_the_container(a.ToArray()); var logger = new TeamCityServiceMessages().CreateWriter(); config.Silent = true; config.Debug = verbose; if (deployType != "drop" && !filesChecked) { var badFiles = new List <string>(); foreach ( var file in Directory.GetFiles(config.SqlFilesDirectory, "*.sql", SearchOption.AllDirectories)) { var preamble = Encoding.UTF8.GetPreamble(); using (var f = new BufferedStream(File.OpenRead(file))) { if (f.Length >= preamble.Length) { var buffer = new byte[preamble.Length]; f.Read(buffer, 0, preamble.Length); if (preamble.SequenceEqual(buffer)) { continue; } } f.Position = 0; int b; while ((b = f.ReadByte()) != -1) { if (b > 127) { badFiles.Add(file); break; } } } } if (badFiles.Count > 0) { var message = $"Found non UTF-8 files:{Environment.NewLine}{string.Join(Environment.NewLine, badFiles)}"; if (failonNonUTF8) { foreach (var badFile in badFiles) { var identity = SHA1.Create().ComputeHash(Encoding.UTF8.GetBytes(badFile)); logger.WriteBuildProblem(Convert.ToBase64String(identity), $"File {badFile} is not in UTF8 encoding"); } return; } logger.WriteWarning(message); } } using (var l = logger.OpenBlock(type.ToString())) { config.Logger = new CustomLogger(l, verbose); WriteStartupParameters(l, config); var migrate = new Migrate(); migrate.Set(c => ApplyConfig(config, c)); migrate.Run(); } } }
// maintenance database setup private void run_maintenance_database_setup(bool restoring_the_database, Migrate migrator, ConfigurationPropertyHolder configuration, Assembly mappings_assembly, Assembly conventions_assembly, string name_of_script) { var updateScriptFileName = Path.Combine(path_to_sql_scripts_up_folder, name_of_script); if (File.Exists(updateScriptFileName)) { File.Delete(updateScriptFileName); } if (restoring_the_database) { configuration.Restore = true; migrator.RunRestore(); } upgrade_database_schema(configuration.DatabaseName, mappings_assembly, conventions_assembly); configuration.Restore = false; migrator.Run(); }
// maintenance database setup private void run_maintenance_database_setup(bool restoring_the_database, Migrate migrator, ConfigurationPropertyHolder configuration, Assembly mappings_assembly, Assembly conventions_assembly) { if (restoring_the_database) { configuration.Restore = true; migrator.RunRestore(); } upgrade_database_schema(configuration.DatabaseName, mappings_assembly, conventions_assembly); configuration.Restore = false; migrator.Run(); }