Repository navigation
Initial seed data UI #101
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
Initial seed data UI #101
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
Binary file not shown.
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
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,209 @@ | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
| using System.Linq; | ||
| using System.Threading.Tasks; | ||
| using DacFXToolLib.Common; | ||
| using DacFXToolLib.Dab; | ||
|
|
||
| namespace SqlProjectsPowerTools | ||
| { | ||
| internal class SeedHandler | ||
| { | ||
| public async Task GenerateSeedScriptsAsync(Project project) | ||
| { | ||
| await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); | ||
|
|
||
| try | ||
| { | ||
| if (await VSHelper.IsDebugModeAsync()) | ||
| { | ||
| VSHelper.ShowError("Cannot generate code while debugging"); | ||
| return; | ||
| } | ||
|
|
||
| var options = new DataApiBuilderOptions(); | ||
|
|
||
| var info = await ChooseDataBaseConnectionAsync(options); | ||
|
|
||
| if (info.DatabaseModel == null) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| var dbInfo = await GetDatabaseInfoAsync(options); | ||
|
|
||
| if (dbInfo == null) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| options.ProjectPath = project.FullPath; | ||
|
|
||
| if (!await LoadDataBaseObjectsAsync(options)) | ||
| { | ||
| await VS.StatusBar.ClearAsync(); | ||
| return; | ||
| } | ||
|
|
||
| await GenerateScriptAsync(options, dbInfo.ConnectionString); | ||
| } | ||
| catch (AggregateException ae) | ||
| { | ||
| foreach (var innerException in ae.Flatten().InnerExceptions) | ||
| { | ||
| VSHelper.ShowError(innerException.Message); | ||
| } | ||
| } | ||
| catch (Exception exception) | ||
| { | ||
| VSHelper.ShowError(exception.Message); | ||
| } | ||
| finally | ||
| { | ||
| await VS.StatusBar.ClearAsync(); | ||
| } | ||
| } | ||
|
|
||
| private static async Task GenerateScriptAsync(DataApiBuilderOptions options, string connectionString) | ||
| { | ||
| await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); | ||
|
|
||
| await VS.StatusBar.ShowProgressAsync("Getting seed data", 1, 2); | ||
|
|
||
| var revEngRunner = new ProcessLauncher(); | ||
|
|
||
| var parts = options.Tables.FirstOrDefault()?.Name.Split('.'); | ||
|
|
||
| if (parts == null || parts.Length != 2) | ||
| { | ||
| VSHelper.ShowError("No table selected."); | ||
| return; | ||
| } | ||
|
|
||
| var tableName = parts[1]; | ||
| var schema = parts[0]; | ||
|
|
||
| var result = await revEngRunner.CreateMergeScriptAsync(options.ProjectPath, connectionString, tableName, schema); | ||
|
|
||
| await VS.StatusBar.ShowProgressAsync("Getting seed data", 2, 2); | ||
|
|
||
| if (File.Exists(result)) | ||
| { | ||
| await VS.Documents.OpenAsync(result); | ||
| } | ||
| } | ||
|
|
||
| private static async Task<List<TableModel>> GetTablesAsync(string connectionString) | ||
| { | ||
| var builder = new TableListBuilder(connectionString, DatabaseType.SQLServer, null); | ||
|
|
||
| return await builder.GetTableDefinitionsAsync(); | ||
| } | ||
|
|
||
| private static async Task<bool> LoadDataBaseObjectsAsync(DataApiBuilderOptions options) | ||
| { | ||
| await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); | ||
|
|
||
| IEnumerable<TableModel> predefinedTables = null; | ||
|
|
||
| try | ||
| { | ||
| await VS.StatusBar.StartAnimationAsync(StatusAnimation.Build); | ||
|
|
||
| predefinedTables = await GetTablesAsync(options.ConnectionString); | ||
|
|
||
| predefinedTables = predefinedTables | ||
| .Where(t => t.ObjectType == ObjectType.Table); | ||
| } | ||
| catch (InvalidOperationException ex) | ||
| { | ||
| VSHelper.ShowError($"{ex.Message}"); | ||
| return false; | ||
| } | ||
| finally | ||
| { | ||
| await VS.StatusBar.EndAnimationAsync(StatusAnimation.Build); | ||
| } | ||
|
|
||
| var preselectedTables = new List<SerializationTableModel>(); | ||
|
|
||
| await VS.StatusBar.ClearAsync(); | ||
|
|
||
| var ptd = PackageManager.Package.GetView<IPickTablesDialog>() | ||
| .AddTables(predefinedTables, new List<Schema>()) | ||
| .PreselectTables(preselectedTables); | ||
|
|
||
| var pickTablesResult = ptd.ShowAndAwaitUserResponse(true); | ||
|
|
||
| options.Tables = pickTablesResult.Payload.Objects.ToList(); | ||
| return pickTablesResult.ClosedByOK; | ||
| } | ||
|
|
||
| private async Task<(DatabaseConnectionModel DatabaseModel, int FileGenerationMode, bool ImportSettings)> ChooseDataBaseConnectionAsync(DataApiBuilderOptions options) | ||
| { | ||
| var vsDataHelper = new VsDataHelper(); | ||
| var databaseList = await vsDataHelper.GetDataConnectionsAsync(); | ||
|
|
||
| var psd = PackageManager.Package.GetView<IPickServerDatabaseDialog>(); | ||
|
|
||
| if (databaseList != null && databaseList.Any()) | ||
| { | ||
| psd.PublishConnections(databaseList.Select(m => new DatabaseConnectionModel | ||
| { | ||
| ConnectionName = m.Value.ConnectionName, | ||
| ConnectionString = m.Value.ConnectionString, | ||
| DatabaseType = m.Value.DatabaseType, | ||
| DataConnection = m.Value.DataConnection, | ||
| })); | ||
| } | ||
|
|
||
| psd.PublishFileGenerationMode(GetCodeGenerationModes()); | ||
|
|
||
| var pickDataSourceResult = psd.ShowAndAwaitUserResponse(true); | ||
| if (!pickDataSourceResult.ClosedByOK) | ||
| { | ||
| return (null, 0, false); | ||
| } | ||
|
|
||
| if (pickDataSourceResult.Payload.Connection != null) | ||
| { | ||
| options.ConnectionString = pickDataSourceResult.Payload.Connection.ConnectionString; | ||
| options.DatabaseType = pickDataSourceResult.Payload.Connection.DatabaseType; | ||
| } | ||
|
|
||
| return (pickDataSourceResult.Payload.Connection, | ||
| pickDataSourceResult.Payload.CodeGenerationMode, | ||
| pickDataSourceResult.Payload.GetDatabaseOptions); | ||
| } | ||
|
|
||
| private static async Task<DatabaseConnectionModel> GetDatabaseInfoAsync(DataApiBuilderOptions options) | ||
| { | ||
| await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); | ||
|
|
||
| var dbInfo = new DatabaseConnectionModel(); | ||
|
|
||
| if (!string.IsNullOrEmpty(options.ConnectionString)) | ||
| { | ||
| dbInfo.ConnectionString = options.ConnectionString; | ||
| dbInfo.DatabaseType = options.DatabaseType; | ||
| } | ||
|
|
||
| if (dbInfo.DatabaseType == DatabaseType.Undefined) | ||
| { | ||
| VSHelper.ShowError($"Unsupported provider"); | ||
| return null; | ||
| } | ||
|
|
||
| return dbInfo; | ||
| } | ||
|
|
||
| private static List<CodeGenerationItem> GetCodeGenerationModes() | ||
| { | ||
| var list = new List<CodeGenerationItem> | ||
| { | ||
| new CodeGenerationItem { Key = 0, Value = "Seed" }, | ||
| }; | ||
| return list; | ||
| } | ||
| } | ||
| } |
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.