public StringBuilder ToDiff(DomainDumpTransformer source) { StringBuilder alters = new(); BuildAlterNullsAndDefaults(source, alters); BuildAlterConstraints(source, alters); return alters; }
private void BuildAlterConstraints(DomainDumpTransformer source, StringBuilder sb) { var skipSourceKeys = new HashSet<string>(); var skipTargetKeys = new HashSet<string>(); foreach (var (sourceKey, sourceValue) in source.Constraints) { foreach (var (targetKey, targetValue) in this.Constraints) { if (Equals(sourceValue, targetValue) && !Equals(sourceKey, targetKey)) { sb.AppendLine($"ALTER DOMAIN {this.Item.Schema}.\"{this.Item.Name}\" RENAME CONSTRAINT \"{targetKey.Trim('"')}\" TO \"{sourceKey.Trim('"')}\";"); skipSourceKeys.Add(sourceKey); skipTargetKeys.Add(targetKey);; } } } foreach (var targetKey in Constraints.Keys.Where(k => !skipTargetKeys.Contains(k) && !source.Constraints.Keys.Contains(k))) { sb.AppendLine($"ALTER DOMAIN {this.Item.Schema}.\"{this.Item.Name}\" DROP CONSTRAINT \"{targetKey.Trim('"')}\" RESTRICT;"); } foreach (var sourceKey in source.Constraints.Keys.Where(k => !skipSourceKeys.Contains(k) && !Constraints.Keys.Contains(k))) { var sourceValue = source.Constraints[sourceKey]; sb.AppendLine($"ALTER DOMAIN {this.Item.Schema}.\"{this.Item.Name}\" ADD CONSTRAINT \"{sourceKey.Trim('"')}\" {sourceValue};"); } }
private void BuildAlterDomains(StringBuilder sb) { var header = false; foreach (var sourceKey in targetDomains.Keys.Where(k => sourceDomains.Keys.Contains(k))) { var sourceItem = sourceDomains[sourceKey]; var targetItem = targetDomains[sourceKey]; var sourceTransformer = new DomainDumpTransformer(sourceItem, SourceLines).BuildLines(ignorePrepend: true); var targetTransformer = new DomainDumpTransformer(targetItem, TargetLines).BuildLines(ignorePrepend: true); var alters = targetTransformer.ToDiff(sourceTransformer); if (alters.Length > 0) { if (!header) { AddComment(sb, "#region ALTER DOMAINS"); header = true; } sb.Append(alters); } } if (header) { AddComment(sb, "#endregion ALTER DOMAINS"); } }
private void BuildAlterNullsAndDefaults(DomainDumpTransformer source, StringBuilder sb) { if (source.IsNull != this.IsNull) { sb.AppendLine($"ALTER DOMAIN {this.Item.Schema}.\"{this.Item.Name}\" {(source.IsNull ? "DROP NOT NULL" : "SET NOT NULL")};"); } if (source.Default != this.Default) { if (source.Default == null) { sb.AppendLine($"ALTER DOMAIN {this.Item.Schema}.\"{this.Item.Name}\" DROP DEFAULT;"); } else { sb.AppendLine($"ALTER DOMAIN {this.Item.Schema}.\"{this.Item.Name}\" SET DEFAULT {source.Default};"); } } }
private void BuildCreateDomainsNotInTarget(StringBuilder sb) { var header = false; foreach (var domainKey in sourceDomains.Keys.Where(k => !targetDomains.Keys.Contains(k))) { if (!header) { AddComment(sb, "#region CREATE NON EXISTING DOMAINS"); header = true; } var item = sourceDomains[domainKey]; var content = new DomainDumpTransformer(item, SourceLines).BuildLines(ignorePrepend: true); sb.AppendLine(content.ToString()); } if (header) { AddComment(sb, "#endregion CREATE NON EXISTING DOMAINS"); } }
public IEnumerable <(string name, string content, PgType type, string schema)> GetDatabaseObjects() { var args = string.Concat( baseArg, " --schema-only", settings.DbObjectsOwners ? "" : " --no-owner", settings.DbObjectsPrivileges ? "" : " --no-acl", settings.DbObjectsDropIfExists ? " --clean --if-exists" : ""); foreach (var table in Connection.GetTables(settings)) { var name = table.GetFileName(); string content = null; try { #pragma warning disable CS8509 content = table.Type switch #pragma warning restore CS8509 { PgType.Table => GetTableContent(table, args), PgType.View => GetViewContent(table, args) }; } catch (Exception e) { Program.WriteLine(ConsoleColor.Red, $"Could not write dump file {name}", $"ERROR: {e.Message}"); continue; } yield return(name, content, table.Type, table.Schema); } foreach (var seq in Connection.GetSequences(settings)) { var name = seq.GetFileName(); string content; try { content = GetSeqContent(seq, args); } catch (Exception e) { Program.WriteLine(ConsoleColor.Red, $"Could not write dump file {name}", $"ERROR: {e.Message}"); continue; } yield return(name, content, seq.Type, seq.Schema); } List <string> lines = null; List <PgItem> types = new(); try { lines = GetDumpLines(args, excludeTables, out types); } catch (Exception e) { Program.WriteLine(ConsoleColor.Red, $"Could not create pg_dump for functions and procedures", $"ERROR: {e.Message}"); } if (lines != null) { foreach (var routine in Connection.GetRoutines(settings)) { var name = routine.GetFileName(); string content; try { content = new RoutineDumpTransformer(routine, lines) .BuildLines(dbObjectsCreateOrReplace: settings.DbObjectsCreateOrReplace) .ToString(); } catch (Exception e) { Program.WriteLine(ConsoleColor.Red, $"Could not write dump file {name}", $"ERROR: {e.Message}"); continue; } yield return(name, content, routine.Type, routine.Schema); } foreach (var domain in Connection.GetDomains(settings)) { var name = domain.GetFileName(); string content; try { content = new DomainDumpTransformer(domain, lines) .BuildLines() .ToString(); } catch (Exception e) { Program.WriteLine(ConsoleColor.Red, $"Could not write dump file {name}", $"ERROR: {e.Message}"); continue; } yield return(name, content, domain.Type, domain.Schema); } foreach (var type in Connection.FilterTypes(types, settings)) { var name = type.GetFileName(); string content; try { content = new TypeDumpTransformer(type, lines) .BuildLines() .ToString(); } catch (Exception e) { Program.WriteLine(ConsoleColor.Red, $"Could not write dump file {name}", $"ERROR: {e.Message}"); continue; } yield return(name, content, type.Type, type.Schema); } foreach (var schema in Connection.GetSchemas(settings)) { if (string.Equals(schema, "public")) { continue; } var name = $"{schema}.sql"; string content; try { content = new SchemaDumpTransformer(schema, lines) .BuildLines() .ToString(); } catch (Exception e) { Program.WriteLine(ConsoleColor.Red, $"Could not write dump file {name}", $"ERROR: {e.Message}"); continue; } yield return(name, content, PgType.Schema, null); } } }