public ScripterResult ScriptMigrationTargets(IEnumerable<SqlScript> migrationScripts) { Regex targetDbObjectRegex = new Regex(m_scrptingObjectRegEx, RegexOptions.IgnoreCase | RegexOptions.Multiline); List<ScriptObject> scriptObjects = new List<ScriptObject>(); foreach (SqlScript script in migrationScripts) { //extract db object target(s) from scripts MatchCollection matches = targetDbObjectRegex.Matches(script.Contents); foreach (Match m in matches) { string objectType = m.Groups[2].Value; ObjectTypeEnum type; if (Enum.TryParse<ObjectTypeEnum>(objectType, true, out type)) { ObjectActionEnum action = (ObjectActionEnum)Enum.Parse(typeof(ObjectActionEnum), m.Groups[1].Value, true); var scriptObject = new ScriptObject(type, action); if (string.IsNullOrEmpty(m.Groups[4].Value) && !string.IsNullOrEmpty(m.Groups[3].Value)) { //no schema specified scriptObject.ObjectName = m.Groups[3].Value; } else { scriptObject.ObjectSchema = m.Groups[3].Value; scriptObject.ObjectName = m.Groups[4].Value; } char[] removeCharacters = new char[] { '[', ']' }; scriptObject.ObjectSchema = removeCharacters.Aggregate(scriptObject.ObjectSchema, (c1, c2) => c1.Replace(c2.ToString(), "")); scriptObject.ObjectName = removeCharacters.Aggregate(scriptObject.ObjectName, (c1, c2) => c1.Replace(c2.ToString(), "")); scriptObjects.Add(scriptObject); } } } return ScriptObjects(scriptObjects); }
private void SaveScript(ScriptObject scriptObject, StringCollection script, string outputDirectory) { try { EnsureDirectoryExists(outputDirectory); StringBuilder sb = new StringBuilder(); foreach (string str in script) { sb.Append(str); sb.Append(Environment.NewLine); } m_log.WriteInformation(string.Format("Saving object definition: {0}", Path.Combine(outputDirectory, scriptObject.FileName))); File.WriteAllText(Path.Combine(outputDirectory, scriptObject.FileName), sb.ToString()); } catch (Exception ex) { m_log.WriteError(string.Format("Error when saving script file {0}: {1}", scriptObject.FullName, ex.Message)); } }
private void DeleteScript(ScriptObject scriptObject, string outputDirectory) { try { string filePath = Path.Combine(outputDirectory, scriptObject.FileName); if (File.Exists(filePath)) { m_log.WriteInformation(string.Format("Deleting object definition: {0}", filePath)); File.Delete(filePath); } } catch (Exception ex) { m_log.WriteError(string.Format("Error when deleting script file {0}: {1}", scriptObject.FullName, ex.Message)); } }
private void ScriptDefinition(ScriptObject dbObject, string outputDirectory, Func<StringCollection> scripter) { try { StringCollection script = scripter(); SaveScript(dbObject, script, outputDirectory); } catch (Exception ex) { m_log.WriteError(string.Format("Error when scripting definition for {0}: {1}", dbObject.ObjectName, ex.Message)); } }