/// <summary> /// Applies all migrations for the given repository. /// </summary> /// <param name="criteria"></param> public void ApplyMigrations(ApplyMigrationCriteria criteria) { var configurationStatus = GetMigrationConfigurationStatus(criteria.ProjectPath, criteria.RepoName); if (!configurationStatus.Enabled) { LoggerBase.Log("Migrations are not enabled, can not apply any migrations."); return; } if (configurationStatus.MigrationType != MigrationToUse.Manual) { LoggerBase.Log("Manual Migrations are not enabled, can not apply any migrations."); return; } var repoInfo = TypeHandler.FindSingleRepo(criteria.ProjectPath, criteria.RepoName); //if null we need to drop out. if (repoInfo == null) { return; } AssertRepoHasEmptyConstructor(repoInfo.RepoType); EnsureDbAndMigrationTableExists(criteria.ProjectPath, repoInfo.RepoType, criteria.ConfigFilePath); var runner = new MigrationRunner(); runner.ApplyMigrations(criteria); //clean up ProjectEvalutionHelper.FinishedWithProject(criteria.ProjectPath); }
private void UpdateDatabase(string[] args) { var criteriaParmas = ParseParams(args); var criteria = new ApplyMigrationCriteria { ProjectPath = criteriaParmas.ProjectPath, RepoName = criteriaParmas.OptionalRepoName, ConfigFilePath = criteriaParmas.ConfigFilePath, }; var setup = new SchemaSetup(); setup.ApplyMigrations(criteria); }
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); } }