Repository navigation
Add merge generator tool #99
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| using System.Data; | ||
| using System.Text; | ||
| using Microsoft.Data.SqlClient; | ||
|
|
||
| namespace DacFXToolLib | ||
| { | ||
| public static class MergeGenerator | ||
| { | ||
| public static void Generate(string projectPath, string connectionString, string tableName, string schema) | ||
| { | ||
| var script = ReadScript(); | ||
|
|
||
| using var connection = new SqlConnection(connectionString); | ||
| connection.Open(); | ||
| #pragma warning disable CA2100 // Review SQL queries for security vulnerabilities | ||
| using var command = new SqlCommand(script, connection); | ||
| #pragma warning restore CA2100 // Review SQL queries for security vulnerabilities | ||
| command.ExecuteNonQuery(); | ||
|
|
||
| // Sample: EXEC [#sp_generate_merge] @schema = 'dbo', @table_name = 'Album', @results_to_text = 1, @include_use_db = 0 | ||
| using var mergeCommand = new SqlCommand("[#sp_generate_merge]", connection); | ||
| mergeCommand.CommandType = CommandType.StoredProcedure; | ||
| mergeCommand.Parameters.AddWithValue("@table_name", tableName); | ||
| mergeCommand.Parameters.AddWithValue("@schema", schema); | ||
| mergeCommand.Parameters.AddWithValue("@results_to_text", 1); | ||
| mergeCommand.Parameters.AddWithValue("@include_use_db", 0); | ||
| var result = mergeCommand.ExecuteScalar(); | ||
|
|
||
| connection.Close(); | ||
|
|
||
| WriteResult(projectPath, tableName, result); | ||
| } | ||
|
|
||
| private static void WriteResult(string projectPath, string tableName, object result) | ||
| { | ||
| var projectDirectory = Path.GetDirectoryName(projectPath); | ||
|
|
||
| if (!Directory.Exists(Path.Join(projectDirectory, "Post-Deployment"))) | ||
| { | ||
| Directory.CreateDirectory(Path.Join(projectDirectory, "Post-Deployment")); | ||
| } | ||
|
|
||
| var outputPath = Path.Combine(Path.GetDirectoryName(projectPath)!, "Post-Deployment", $"{tableName}_merge.sql"); | ||
|
|
||
| if (File.Exists(outputPath)) | ||
| { | ||
| throw new InvalidOperationException($"The file {outputPath} already exists."); | ||
|
ErikEJ marked this conversation as resolved.
|
||
| } | ||
|
|
||
| File.WriteAllText(outputPath, result.ToString(), Encoding.UTF8); | ||
| } | ||
|
|
||
| private static string ReadScript() | ||
| { | ||
| var resourceName = "DacFXToolLib.sp_generate_merge.sql"; | ||
| using var stream = System.Reflection.Assembly.GetAssembly(typeof(MergeGenerator))!.GetManifestResourceStream(resourceName); | ||
| using var reader = new StreamReader(stream!); | ||
| var sql = reader.ReadToEnd(); | ||
|
|
||
| if (string.IsNullOrWhiteSpace(sql)) | ||
| { | ||
| throw new InvalidOperationException("Could not load the embedded SQL resource."); | ||
| } | ||
|
|
||
| return sql; | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.