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)); }
public ScriptObject(ObjectTypeEnum type, ObjectActionEnum action) { this.ObjectSchema = "dbo"; this.ObjectType = type; this.ObjectAction = action; }
private IEnumerable <ScriptObject> GetObjectsFromMigrationScripts(SqlScript script) { //extract db object target(s) from scripts MatchCollection matches = this.m_targetDbObjectRegex.Matches(script.Contents); foreach (Match m in matches) { //if this group is empty, it means the second part of the regex matched (sp_rename) if (!string.IsNullOrEmpty(m.Groups[REGEX_INDEX_ACTION_TYPE].Value)) { if (Enum.TryParse <ObjectTypeEnum>(m.Groups[REGEX_INDEX_OBJECT_TYPE].Value, true, out var type)) { //replace CREATE OR ALTER by CREATE var actionString = m.Groups[REGEX_INDEX_ACTION_TYPE].Value.StartsWith(ObjectActionEnum.Create.ToString(), StringComparison.OrdinalIgnoreCase) ? ObjectActionEnum.Create.ToString() : m.Groups[REGEX_INDEX_ACTION_TYPE].Value; ObjectActionEnum action = (ObjectActionEnum)Enum.Parse(typeof(ObjectActionEnum), actionString, true); var scriptObject = new ScriptObject(type, action); if (string.IsNullOrEmpty(m.Groups[REGEX_INDEX_OBJECT_NAME].Value) && !string.IsNullOrEmpty(m.Groups[REGEX_INDEX_SCHEMA_NAME].Value)) { //no schema specified. in that case, object name is in the schema group scriptObject.ObjectName = RemoveBrackets(m.Groups[REGEX_INDEX_SCHEMA_NAME].Value); } else { scriptObject.ObjectSchema = RemoveBrackets(m.Groups[REGEX_INDEX_SCHEMA_NAME].Value); scriptObject.ObjectName = RemoveBrackets(m.Groups[REGEX_INDEX_OBJECT_NAME].Value); } yield return(scriptObject); } } else { string schemaName; string oldObjectName; string newObjectName; if (string.IsNullOrEmpty(m.Groups[REGEX_INDEX_OBJECT_RENAME_OLD_NAME].Value) && !string.IsNullOrEmpty(m.Groups[REGEX_INDEX_OBJECT_RENAME_SCHEMA].Value)) { //no schema specified. in that case, object name is in the schema group schemaName = "dbo"; oldObjectName = RemoveBrackets(m.Groups[REGEX_INDEX_OBJECT_RENAME_SCHEMA].Value); newObjectName = RemoveBrackets(m.Groups[REGEX_INDEX_OBJECT_RENAME_OLD_NAME].Value); } else { schemaName = m.Groups[REGEX_INDEX_OBJECT_RENAME_SCHEMA].Value; oldObjectName = RemoveBrackets(m.Groups[REGEX_INDEX_OBJECT_RENAME_OLD_NAME].Value); newObjectName = RemoveBrackets(m.Groups[REGEX_INDEX_OBJECT_RENAME_NEW_NAME].Value); } var type = GetObjectTypeFromDb(schemaName, newObjectName); var scriptObjectDrop = new ScriptObject(type, ObjectActionEnum.Drop); scriptObjectDrop.ObjectSchema = schemaName; scriptObjectDrop.ObjectName = oldObjectName; yield return(scriptObjectDrop); var scriptObjectCreate = new ScriptObject(type, ObjectActionEnum.Create); scriptObjectCreate.ObjectSchema = schemaName; scriptObjectCreate.ObjectName = newObjectName; yield return(scriptObjectCreate); } } }
public ScriptObject(ObjectTypeEnum type, ObjectActionEnum action) { this.ObjectSchema = "dbo"; this.ObjectType = type; this.ObjectAction = action; }