private void EnsureDbAndMigrationTableExists(string projectdllPath, Type repoType, string configFilePath) { var repoBase = TypeHandler.CreateRepoBase(projectdllPath, repoType); //this might blow up.. might need "args" var connectionString = ConnectionStringHandler.FindConnectionString(repoBase, configFilePath); CreateRepoDatabase(projectdllPath, repoType, configFilePath); //create migration log table, if it doesn't exist. var updateMigration = CreateSchemaUpdater(projectdllPath, typeof(MigrationRepo), configFilePath, connectionString); updateMigration.Execute(true, true); }
/// <summary> /// Creates an instance of the NHibernate SchemaUpdate class using the repository found. /// </summary> /// <param name="projectdllPath"></param> /// <param name="repoType"></param> /// <param name="args">optional constructor arguments</param> /// <returns></returns> private SchemaUpdate CreateSchemaUpdater(string projectdllPath, Type repoType, string configFilePath, params object[] args) { var repoBase = TypeHandler.CreateRepoBase(projectdllPath, repoType, args); MethodInfo createRepoSetupMethod = repoType.GetMethod("CreateRepoSetup", BindingFlags.Instance | BindingFlags.NonPublic); var connectionString = ConnectionStringHandler.FindConnectionString(repoBase, configFilePath); var repoSetup = createRepoSetupMethod.Invoke(repoBase, new object[] { connectionString }); var createConfigMethod = repoSetup.GetType().GetMethod("CreateConfiguration", BindingFlags.Instance | BindingFlags.NonPublic); var config = createConfigMethod.Invoke(repoSetup, null) as FluentConfiguration; var updater = new SchemaUpdate(config.BuildConfiguration()); return(updater); }
public void ApplyMigrations(ApplyMigrationCriteria criteria) { var repoInfo = TypeHandler.FindSingleRepo(criteria.ProjectPath, criteria.RepoName); if (repoInfo == null) { return; } var repoBase = TypeHandler.CreateRepoBase(repoInfo.Assembly.Location, repoInfo.RepoType); var connectionString = ConnectionStringHandler.FindConnectionString(repoBase, criteria.ConfigFilePath); ConnectionStringHandler.OverrideConnectionString(repoBase, connectionString); var migrationTypes = TypeHandler.FindAllMigrations(criteria.ProjectPath, repoInfo.RepoType); if (migrationTypes == null || !migrationTypes.Any()) { LoggerBase.Log("No migrations to run."); return; } var migrationRepo = new MigrationRepo(connectionString); var doneMigrations = migrationRepo.GetMigrationsThatHaveRun(repoInfo.RepoType.Name); //only run on migrations that have not been run yet. //order by name, ie. datestamp. foreach (var migrationType in migrationTypes.Where(m => !doneMigrations.Contains(m.Name)).OrderBy(m => m.Name)) { var migration = Activator.CreateInstance(migrationType) as AbstractBaseMigration; if (migration == null) { LoggerBase.Log("Error creating migration: " + migrationType.Name); throw new ApplicationException("Error creating migration: " + migrationType.Name); } //use reflection to set the internal base repo PropertyInfo baseRepoPropertyInfo = migrationType.GetProperty("BaseRepo", BindingFlags.Instance | BindingFlags.NonPublic); baseRepoPropertyInfo.SetValue(migration, repoBase); LoggerBase.Log(string.Format("Executing Migration: {0}", migrationType.Name)); migration.Execute(); migrationRepo.LogMigrationRan(migrationType.Name, repoInfo.RepoType.Name); } }
/// <summary> /// Ensures that the migration database exists. /// </summary> /// <param name="projectdllPath"></param> /// <param name="repoType"></param> /// <param name="configFilePath"></param> /// <param name="args"></param> private void CreateRepoDatabase(string projectdllPath, Type repoType, string configFilePath, params object[] args) { var repoBase = TypeHandler.CreateRepoBase(projectdllPath, repoType, args); var connectionString = ConnectionStringHandler.FindConnectionString(repoBase, configFilePath); var builder = new System.Data.SqlClient.SqlConnectionStringBuilder { ConnectionString = connectionString }; string databaseName = builder.InitialCatalog; var migrationDbExists = DatabaseChecking.CheckDatabaseExists(connectionString, databaseName); if (!migrationDbExists) { DatabaseCreation.CreateDatabase(connectionString, databaseName); } }