public virtual void Install(Guid modelKey, string connectionString) { if (this.Options == null) { throw new Exception("The 'Options' parameter cannot be null."); } this.ConnectionString = connectionString; //Ensure that there is a database using (var context = new DbManagementContext(this.Options)) { context.Database.EnsureCreated(); var version = context.VersionModel.FirstOrDefault(x => x.ModelKey == modelKey); if (version != null) { this.CurrentVersion = new Version(version.Version); } this.LastestVersion = this.CurrentVersion; } //Run all scripts this.RunScripts(); //Update management tables using (var context = new DbManagementContext(this.Options)) { var version = context.VersionModel.FirstOrDefault(x => x.ModelKey == modelKey); if (version == null) { version = new VersionModel { ModelKey = modelKey }; context.Add(version); } version.LastUpdated = DateTime.Now; version.Version = this.LastestVersion.ToString(); context.SaveChanges(); } }
private void ExecuteScript(string sql, DbManagementContext context) { if (string.IsNullOrEmpty(sql)) { return; } //Break into lines var lines = BreakLines(sql); var blocks = new List <string>(); var lastBlock = new List <string>(); foreach (var ss in lines) { if (ss == this.BreakLine) { blocks.Add(string.Join("\r\n", lastBlock)); lastBlock.Clear(); } else { lastBlock.Add(ss); } } blocks.RemoveAll(x => string.IsNullOrEmpty(x)); foreach (var line in blocks) { using (var command = context.Database.GetDbConnection().CreateCommand()) { command.CommandText = line; context.Database.OpenConnection(); command.ExecuteNonQuery(); } } }
protected virtual void RunScripts() { var scripts = GetScripts(this.Assembly); var newList = new List <string>(); newList.AddRange(scripts.Where(x => x.Contains(Folder1)).OrderBy(x => x)); //Only find those that are versioned. TODO: If any others then error var migrations = new Dictionary <Version, string>(); foreach (var script in scripts.Where(x => x.Contains(Folder2))) { if (script.Contains(Folder2, StringComparison.CurrentCultureIgnoreCase)) { var index = script.IndexOf(Folder2, StringComparison.CurrentCultureIgnoreCase); if (index != -1) { var fragment = script.Substring(index + Folder2.Length); if (fragment.EndsWith(".sql")) { fragment = fragment.Substring(0, fragment.Length - 4); if (Version.TryParse(fragment, out Version v)) { migrations.Add(v, script); } } } } } //Sort migration scripts foreach (var migrationKey in migrations.Keys.OrderBy(x => x)) { newList.Add(migrations[migrationKey]); if (this.LastestVersion < migrationKey) { this.LastestVersion = migrationKey; } } newList.AddRange(scripts.Where(x => x.Contains(Folder3)).OrderBy(x => x)); newList.AddRange(scripts.Where(x => x.Contains(Folder4)).OrderBy(x => x)); newList.AddRange(scripts.Where(x => x.Contains(Folder5)).OrderBy(x => x)); //TODO: Transaction using (var context = new DbManagementContext(this.Options)) { foreach (var resourceFileName in newList) { var sql = string.Empty; var manifestStream = this.Assembly.GetManifestResourceStream(resourceFileName); using (var sr = new System.IO.StreamReader(manifestStream)) { sql = sr.ReadToEnd(); } this.ExecuteScript(sql, context); var hash = sql.GetHash(); var logItem = context.VersionObject.FirstOrDefault(x => x.Hash == hash); if (logItem == null) { logItem = new VersionObject { Hash = hash }; context.Add(logItem); } logItem.Name = resourceFileName; logItem.CreatedDate = DateTime.Now; logItem.ModifiedDate = logItem.CreatedDate; } context.SaveChanges(); } }