From a20c38776c6ddbf970f17c4af951f3bbeedad61a Mon Sep 17 00:00:00 2001 From: Vedran B Date: Tue, 1 Aug 2023 13:29:04 +0200 Subject: [PATCH] ## 5.3.6 --- PgRoutiner/Builder/Dump/DumpBuilder.cs | 69 +++++++++++++++++++---- PgRoutiner/Builder/Md/MarkdownDocument.cs | 4 +- PgRoutiner/Extensions.cs | 9 ++- PgRoutiner/PgRoutiner.csproj | 6 +- changelog.md | 38 +++++++++++++ 5 files changed, 108 insertions(+), 18 deletions(-) diff --git a/PgRoutiner/Builder/Dump/DumpBuilder.cs b/PgRoutiner/Builder/Dump/DumpBuilder.cs index 0721f73..523f249 100644 --- a/PgRoutiner/Builder/Dump/DumpBuilder.cs +++ b/PgRoutiner/Builder/Dump/DumpBuilder.cs @@ -102,6 +102,17 @@ string GetDir(DumpType type) var baseSequencesDir = GetDir(DumpType.Sequences); var baseExtensionsDir = GetDir(DumpType.Extensions); + int tableCount = 0; + int viewsCount = 0; + int functionsCount = 0; + int proceduresCount = 0; + int domainsCount = 0; + int typesCount = 0; + int schemasCount = 0; + int sequencesCount = 0; + int extensionsCount = 0; + + /* static string ParseSchema(string dir, string schema) { if (dir == null) @@ -110,7 +121,7 @@ static string ParseSchema(string dir, string schema) } return string.Format(dir, schema == null || schema == "public" ? "" : schema).Replace("//", "/").Replace("\\\\", "\\"); } - + */ HashSet dirs = new(); @@ -120,17 +131,33 @@ static string ParseSchema(string dir, string schema) { continue; } + var schemaDir = string.Concat(schema == null || schema == "public" ? "" : schema, "/"); + var dir = type switch { - PgType.Table => ParseSchema(baseTablesDir, schema), - PgType.View => ParseSchema(baseViewsDir, schema), - PgType.Function => ParseSchema(baseFunctionsDir, schema), - PgType.Procedure => ParseSchema(baseProceduresDir, schema), - PgType.Domain => ParseSchema(baseDomainsDir, schema), - PgType.Type => ParseSchema(baseTypesDir, schema), - PgType.Schema => ParseSchema(baseSchemasDir, schema), - PgType.Sequence => ParseSchema(baseSequencesDir, schema), - PgType.Extension => ParseSchema(baseExtensionsDir, schema), + PgType.Table => baseTablesDir, //ParseSchema(baseTablesDir, schema), + PgType.View => baseViewsDir, //ParseSchema(baseViewsDir, schema), + PgType.Function => baseFunctionsDir, //ParseSchema(baseFunctionsDir, schema), + PgType.Procedure => baseProceduresDir, //ParseSchema(baseProceduresDir, schema), + PgType.Domain => baseDomainsDir, //ParseSchema(baseDomainsDir, schema), + PgType.Type => baseTypesDir, //ParseSchema(baseTypesDir, schema), + PgType.Schema => baseSchemasDir, //ParseSchema(baseSchemasDir, schema), + PgType.Sequence => baseSequencesDir, //ParseSchema(baseSequencesDir, schema), + PgType.Extension => baseExtensionsDir, //ParseSchema(baseExtensionsDir, schema), + _ => null + }; + + int? count = type switch + { + PgType.Table => ++tableCount, + PgType.View => ++viewsCount, + PgType.Function => ++functionsCount, + PgType.Procedure => ++proceduresCount, + PgType.Domain => ++domainsCount, + PgType.Type => ++typesCount, + PgType.Schema => ++schemasCount, + PgType.Sequence => ++sequencesCount, + PgType.Extension => ++extensionsCount, _ => null }; @@ -160,7 +187,25 @@ static string ParseSchema(string dir, string schema) } } - var file = string.Format(Path.GetFullPath(Path.Combine(dir, shortFilename)), connectionName); + var shortName = $"{schemaDir}{shortFilename}"; + var file = Path.GetFullPath(dir.FormatByName( + ("0", shortName), + ("file", shortName), + ("fileName", shortName), + ("1", schema), + ("schema", schema), + ("2", objectName), + ("name", objectName), + ("3", connectionName), + ("connection", connectionName), + ("4", count), + ("count", count), + ("number", count))) + .Replace("//", "/") + .Replace("\\\\", "\\") + .SanitazePath(); + + dir = Path.GetDirectoryName(file); var relative = file.GetRelativePath(); if (!dirs.Contains(dir.TrimEnd('/').TrimEnd('\\'))) @@ -234,6 +279,7 @@ private static void CreateDir(string dir, bool skipDelete = false) } } + /* private static void RemoveDir(string dir) { if (!Current.Value.DumpConsole) @@ -253,4 +299,5 @@ private static void RemoveDir(string dir) Directory.Delete(dir, true); } } + */ } diff --git a/PgRoutiner/Builder/Md/MarkdownDocument.cs b/PgRoutiner/Builder/Md/MarkdownDocument.cs index c79a896..d0a143f 100644 --- a/PgRoutiner/Builder/Md/MarkdownDocument.cs +++ b/PgRoutiner/Builder/Md/MarkdownDocument.cs @@ -338,7 +338,7 @@ private void BuildViews(StringBuilder content, StringBuilder header, List t.Table).Count(); if (count > 0) { header.AppendLine(); @@ -480,7 +480,7 @@ void WriteStats(string schema, string table) Dictionary additionalColumnComments = new(); var tableComments = connection.GetTableComments(settings, schema).ToList(); - var count = tableComments.Count(); + var count = tableComments.GroupBy(t => t.Table).Count(); if (count > 0) { header.AppendLine(); diff --git a/PgRoutiner/Extensions.cs b/PgRoutiner/Extensions.cs index e3968e5..f372b05 100644 --- a/PgRoutiner/Extensions.cs +++ b/PgRoutiner/Extensions.cs @@ -113,7 +113,7 @@ public static string GetFrom(this string value) } index1 = index1 + seq.Length; var index2 = 0; - for(var i = index1; i < value.Length; i++) + for (var i = index1; i < value.Length; i++) { var ch = value[i]; if (ch.IsSqlChar()) @@ -382,4 +382,9 @@ public static string GetAssumedNamespace(this Current settings) } return ns; } -} + + public static string FormatByName(this string input, params (string key, object value)[] parameters) + { + return parameters.Aggregate(input, (current, parameter) => current.Replace(string.Concat("{", parameter.key, "}"), parameter.value?.ToString() ?? "")); + } +} \ No newline at end of file diff --git a/PgRoutiner/PgRoutiner.csproj b/PgRoutiner/PgRoutiner.csproj index 888319e..6b59f92 100644 --- a/PgRoutiner/PgRoutiner.csproj +++ b/PgRoutiner/PgRoutiner.csproj @@ -14,10 +14,10 @@ https://github.com/vb-consulting/PgRoutiner PostgreSQL true - 5.3.5.0 + 5.3.6.0 dotnet-pgroutiner - 5.3.5.0 - 5.3.5.0 + 5.3.6.0 + 5.3.6.0 Debug;Release;SelfContained LICENSE diff --git a/changelog.md b/changelog.md index 8e2c37f..7fa0d82 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,43 @@ # VERSION HISTORY +## 5.3.6 + +### Fix wrong counts in TOC of markdown document. + +### Improve `DbObjects` section name handling. + +Now, dir names can accept named formats (instead of just numbers). This applies to `DbObjectsDir`˙and all `DbObjectsDirNames` (`Tables`, `Views`, `Enums`, `Sequences`, `Functions`, `Procedures`, `Extensions`). + +Available named formats are: + +- "0" - constructed file name: `subdir/schema_name_object_name.sql` where sibdir is schema name if not public, otherwise it's empty (base dir). This is for compatibility with previous versions. +- "file" - same. +- "fileName" - same. +- "1" - schema name. +- "schema" - same. +- "2" - object name. +- "name" - same. +- "3" - connection name +- "connection" - same. +- "4" - object count number (for this type). +- "count" - same. +- "number" - same. + +This feature is added to support flyway migrations name format for repeatable migrations. + +For example, if we have `DbObjectsDirNames` set to `{"Functions": "R__F_{count}_{schema}.{name}.sql"}` then we will have following directory files: + +- `/R__F_1_public.func1.sql` +- `/R__F_1_public.func2.sql` + +This repeatable flyway migration format. + +Default name settings are the same as before. + +``` + +```json + ## 5.3.5 - Massive improvements to markdown documentation generation.