/// <summary> /// Creates a CS file that inherits from BaseMigration that will execute the SQL schema changes. /// </summary> /// <param name="criteria">description of what to do and where to put the file</param> public void CreateScript(CreationCriteria criteria) { var configurationStatus = GetMigrationConfigurationStatus(criteria.ProjectFileLocation, criteria.RepoName); if (!configurationStatus.Enabled) { LoggerBase.Log("Migrations are not enabled, can not create any migrations."); return; } if (configurationStatus.MigrationType != MigrationToUse.Manual) { LoggerBase.Log("Manual Migrations are not enabled, can not create manual migrations."); return; } var repoInfo = TypeHandler.FindSingleRepo(criteria.ProjectFileLocation, criteria.RepoName); //if null we need to drop out. if (repoInfo == null) { LoggerBase.Log("Unable to find repo"); return; } AssertRepoHasEmptyConstructor(repoInfo.RepoType); EnsureDbAndMigrationTableExists(criteria.ProjectFileLocation, repoInfo.RepoType, criteria.ConfigFilePath); //ensure that we have the case correct. criteria.RepoName = repoInfo.RepoType.Name; var configuration = TypeHandler.FindConfiguration(criteria.ProjectFileLocation, repoInfo.RepoType); //if null something bad happend, drop out if (configuration == null) { LoggerBase.Log("unable to find configuration file"); return; } var updater = CreateSchemaUpdater(repoInfo.Assembly.Location, repoInfo.RepoType, criteria.ConfigFilePath); var fileMigrationHandler = new MigrationFileHandler(updater); var projectFileHandler = new ProjectDteHelper(); //set the mgiration folder form config: criteria.MigrationPath = configuration.RootMigrationFolder; var filePath = fileMigrationHandler.CreateFile(criteria); projectFileHandler.AddFile(criteria.ProjectFileLocation, configuration.RootMigrationFolder, filePath, showFile: true); LoggerBase.Log("Created migration file."); //clean up ProjectEvalutionHelper.FinishedWithProject(criteria.ProjectFileLocation); }
private void AddMigration(string[] args) { var criteriaParmas = ParseParams(args); var criteria = new CreationCriteria { ProjectFileLocation = criteriaParmas.ProjectPath, FileName = criteriaParmas.MigrationName, RepoName = criteriaParmas.OptionalRepoName, ConfigFilePath = criteriaParmas.ConfigFilePath, }; var setup = new SchemaSetup(); setup.CreateScript(criteria); }
public string CreateFile(CreationCriteria criteria) { var projectNamespace = new ProjectEvalutionHelper().RootNameSpace(criteria.ProjectFileLocation); var migrationFileInfo = GenerateFileLocation(criteria); if (!Directory.Exists(migrationFileInfo.Folder)) { Directory.CreateDirectory(migrationFileInfo.Folder); } //if it already exists delete it.. it should not at this point. this is a just incase. if (File.Exists(migrationFileInfo.FullFilePath)) { File.Delete(migrationFileInfo.FullFilePath); } _schemaUpdater.Execute(_logScript, false); var fileBuilder = new StringBuilder(); fileBuilder.AppendLine("using NHibernateRepo.Migrations;"); fileBuilder.AppendLine("using System;"); fileBuilder.AppendLine(""); fileBuilder.AppendLine(string.Format("namespace {0}.{1}", projectNamespace, criteria.MigrationPath.Replace("\\", ".").Replace("-", "."))); fileBuilder.AppendLine("{"); fileBuilder.AppendLine(" public class " + migrationFileInfo.ClassName + " : BaseMigration<" + criteria.RepoName + ">"); fileBuilder.AppendLine(" {"); fileBuilder.AppendLine(" public override void Execute()"); fileBuilder.AppendLine(" {"); foreach (var script in Scripts) { fileBuilder.AppendLine(" "); fileBuilder.AppendLine(" ExecuteSql(@\"" + script + "\");"); } fileBuilder.AppendLine(" "); fileBuilder.AppendLine(" }"); fileBuilder.AppendLine(" }"); fileBuilder.AppendLine("}"); File.WriteAllText(migrationFileInfo.FullFilePath, fileBuilder.ToString()); return(migrationFileInfo.FullFilePath); }
/// <summary> /// Determine the path for the file to be created at. /// </summary> /// <param name="criteria"></param> /// <returns></returns> private MigrationFileInfo GenerateFileLocation(CreationCriteria criteria) { if (string.IsNullOrWhiteSpace(criteria.FileName)) { throw new ArgumentNullException("criteria", "File name can not be null"); } /* * * get fully qualifed folder location. * * from folder location use regex to find how many files with that name exist (ignore date stamp at the begninning and possible brackets and number at the end) * * if zero then datastamp and name = name * * else datastamp, name and brackets with count plus one = name * * * */ string folder = Path.Combine(criteria.ProjectFileLocation.Substring(0, criteria.ProjectFileLocation.LastIndexOf("\\")), criteria.MigrationPath); criteria.FileName = criteria.FileName.Replace("-", "_"); var rgx = new Regex("[^a-zA-Z0-9_]"); string safeName = rgx.Replace(criteria.FileName, "_"); string pattern = @"\\\d+_" + criteria.FileName + @"(\(\d+\)){0,1}.cs"; var reg = new Regex(pattern); //only if the folder exists do we need to check if files already exist. var files = new List <string>(); if (Directory.Exists(folder)) { files = Directory.GetFiles(folder, "*.cs", SearchOption.AllDirectories) .Where(path => reg.IsMatch(path)) .ToList(); } var now = DateTime.Now; var dateStamp = now.ToString("yyyyMMddHHmmss"); var result = new MigrationFileInfo(); result.Extension = ".cs"; result.Folder = folder; const string fileNamePre = "Migration"; if (files.Count == 0) { result.Name = dateStamp + "_" + criteria.FileName; result.ClassName = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(string.Format("{0}_{1}_{2}", fileNamePre, dateStamp, safeName)); } else { result.Name = string.Format("{0}_{1}({2})", dateStamp, criteria.FileName, files.Count); result.ClassName = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(string.Format("{0}_{1}_{2}{3}", fileNamePre, dateStamp, safeName, files.Count)); } result.FullFilePath = Path.Combine(folder, result.Name + result.Extension); return(result); }