Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
20 changes: 18 additions & 2 deletions src/DacFXToolLib/MergeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,30 @@
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 + "-- <ItemGroup>";
preamble += Environment.NewLine + $"-- <PostDeploy Include=\"Post-Deployment\\{tableName}_merge.sql\" />";
preamble += Environment.NewLine + "-- </ItemGroup>";

result = preamble + Environment.NewLine + result;

var commandText = $"-- EXEC [#sp_generate_merge] @schema = '{schema}', @table_name = '{tableName}', @results_to_text = 1, @include_use_db = 0";

Check warning on line 43 in src/DacFXToolLib/MergeGenerator.cs

View workflow job for this annotation

GitHub Actions / build


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);

Expand Down
27 changes: 25 additions & 2 deletions src/Vsix/Handlers/SeedHandler.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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)
{
Expand All @@ -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();

Expand All @@ -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<string> { insertStatement }, Encoding.UTF8);
}
Comment thread
ErikEJ marked this conversation as resolved.
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Vsix/VSCommandTable.vsct
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@
<CommandFlag>DynamicVisibility</CommandFlag>
<CommandFlag>TextChanges</CommandFlag>
<Strings>
<ButtonText>Script Seed Data...</ButtonText>
<ButtonText>Script Table Data...</ButtonText>
Comment thread
ErikEJ marked this conversation as resolved.
</Strings>
</Button>

Expand Down
Loading