diff --git a/README.md b/README.md index 5493e56..7af3076 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,7 @@ The tool contains the following features. - **Analyze** - report with static code analysis result of your database project - **Create Mermaid E/R diagram** - create an Entity/Relationship diagram of selected tables from your database project - **Script Database Model** - extract a .dacpac into a single .sql file +- **Script Table Data** - generate insert statements for table data in your database project, based on [generate-sql-merge](https://github.com/dnlnln/generate-sql-merge) ### Power Pack diff --git a/src/DacFXToolLib/MergeGenerator.cs b/src/DacFXToolLib/MergeGenerator.cs index ccc5f16..146764b 100644 --- a/src/DacFXToolLib/MergeGenerator.cs +++ b/src/DacFXToolLib/MergeGenerator.cs @@ -24,14 +24,30 @@ public static string Generate(string projectPath, string connectionString, strin mergeCommand.Parameters.AddWithValue("@schema", schema); mergeCommand.Parameters.AddWithValue("@results_to_text", 1); mergeCommand.Parameters.AddWithValue("@include_use_db", 0); - var result = mergeCommand.ExecuteScalar(); + var result = (string?)mergeCommand.ExecuteScalar(); connection.Close(); + if (result == null) + { + throw new InvalidOperationException("Merge script generation failed."); + } + + var preamble = "-- Add the following ItemGroup to your project file to include the merge script in your project:"; + preamble += Environment.NewLine + "-- "; + preamble += Environment.NewLine + $"-- "; + preamble += Environment.NewLine + "-- "; + + result = preamble + Environment.NewLine + result; + + var commandText = $"-- EXEC [#sp_generate_merge] @schema = '{schema}', @table_name = '{tableName}', @results_to_text = 1, @include_use_db = 0"; + + result = commandText + Environment.NewLine + result; + return WriteResult(projectPath, tableName, result); } - private static string WriteResult(string projectPath, string tableName, object result) + private static string WriteResult(string projectPath, string tableName, string result) { var projectDirectory = Path.GetDirectoryName(projectPath); diff --git a/src/Vsix/Handlers/SeedHandler.cs b/src/Vsix/Handlers/SeedHandler.cs index 4581f66..d7a752a 100644 --- a/src/Vsix/Handlers/SeedHandler.cs +++ b/src/Vsix/Handlers/SeedHandler.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using System.Text; using System.Threading.Tasks; using DacFXToolLib.Common; using DacFXToolLib.Dab; @@ -45,7 +46,7 @@ public async Task GenerateSeedScriptsAsync(Project project) return; } - await GenerateScriptAsync(options, dbInfo.ConnectionString); + await GenerateScriptAsync(options, dbInfo.ConnectionString, project); } catch (AggregateException ae) { @@ -64,7 +65,7 @@ public async Task GenerateSeedScriptsAsync(Project project) } } - private static async Task GenerateScriptAsync(DataApiBuilderOptions options, string connectionString) + private static async Task GenerateScriptAsync(DataApiBuilderOptions options, string connectionString, Project project) { await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); @@ -90,6 +91,28 @@ private static async Task GenerateScriptAsync(DataApiBuilderOptions options, str if (File.Exists(result)) { await VS.Documents.OpenAsync(result); + + var projectDirectory = Path.GetDirectoryName(project.FullPath); + + var insertStatement = $":r ./{Path.GetFileName(result)}"; + + var postDeployFilePath = Path.Combine(projectDirectory, "Post-Deployment", "postdeploy.sql"); + + if (File.Exists(postDeployFilePath)) + { + var textLines = File.ReadAllLines(postDeployFilePath).ToList(); + + if (!textLines.Any(line => line.Trim().Equals(insertStatement, StringComparison.OrdinalIgnoreCase))) + { + textLines.Add(string.Empty); + textLines.Add(insertStatement); + File.WriteAllLines(postDeployFilePath, textLines, Encoding.UTF8); + } + } + else + { + File.WriteAllLines(postDeployFilePath, new List { insertStatement }, Encoding.UTF8); + } } } diff --git a/src/Vsix/VSCommandTable.vsct b/src/Vsix/VSCommandTable.vsct index e1a8795..40b57f1 100644 --- a/src/Vsix/VSCommandTable.vsct +++ b/src/Vsix/VSCommandTable.vsct @@ -121,7 +121,7 @@ DynamicVisibility TextChanges - Script Seed Data... + Script Table Data...