From 92632050d7613a7e4b5e2dc5ade4fa4c3106bb78 Mon Sep 17 00:00:00 2001 From: Tudor Stanciu Date: Sat, 22 Jan 2022 01:54:19 +0200 Subject: [PATCH] MigrationService fix --- NDB.Test.Api/appsettings.json | 1 + .../NDB.Infrastructure.DatabaseMigration.csproj | 2 +- .../Repositories/IMigrationRepository.cs | 2 +- .../Repositories/MigrationRepository.cs | 9 ++------- .../Services/MigrationService.cs | 6 +++++- .../Scripts/1.0.1/04.Create table Settings.sql | 4 ++-- 6 files changed, 12 insertions(+), 12 deletions(-) diff --git a/NDB.Test.Api/appsettings.json b/NDB.Test.Api/appsettings.json index 1be2f95..c2c97f5 100644 --- a/NDB.Test.Api/appsettings.json +++ b/NDB.Test.Api/appsettings.json @@ -1,6 +1,7 @@ { "ConnectionStrings": { "DatabaseConnection": "Data Source={Workspace}\\TesterDb.db" + //"DatabaseConnection": "***REMOVED***" }, "Logging": { "LogLevel": { diff --git a/infrastructure/NDB.Infrastructure.DatabaseMigration/NDB.Infrastructure.DatabaseMigration.csproj b/infrastructure/NDB.Infrastructure.DatabaseMigration/NDB.Infrastructure.DatabaseMigration.csproj index bb28bc4..bd4ee51 100644 --- a/infrastructure/NDB.Infrastructure.DatabaseMigration/NDB.Infrastructure.DatabaseMigration.csproj +++ b/infrastructure/NDB.Infrastructure.DatabaseMigration/NDB.Infrastructure.DatabaseMigration.csproj @@ -7,7 +7,7 @@ https://dev.azure.com/tstanciu94/NDB Git NDB Database migration - 1.0.1 + 1.0.2 diff --git a/infrastructure/NDB.Infrastructure.DatabaseMigration/Repositories/IMigrationRepository.cs b/infrastructure/NDB.Infrastructure.DatabaseMigration/Repositories/IMigrationRepository.cs index a67cf3b..76ef0df 100644 --- a/infrastructure/NDB.Infrastructure.DatabaseMigration/Repositories/IMigrationRepository.cs +++ b/infrastructure/NDB.Infrastructure.DatabaseMigration/Repositories/IMigrationRepository.cs @@ -4,6 +4,6 @@ namespace NDB.Infrastructure.DatabaseMigration.Repositories { public interface IMigrationRepository { - Task ExecuteSqlRaw(string commandText); + Task ExecuteSqlRaw(string sqlRaw); } } diff --git a/infrastructure/NDB.Infrastructure.DatabaseMigration/Repositories/MigrationRepository.cs b/infrastructure/NDB.Infrastructure.DatabaseMigration/Repositories/MigrationRepository.cs index be56474..97aa65c 100644 --- a/infrastructure/NDB.Infrastructure.DatabaseMigration/Repositories/MigrationRepository.cs +++ b/infrastructure/NDB.Infrastructure.DatabaseMigration/Repositories/MigrationRepository.cs @@ -13,14 +13,9 @@ namespace NDB.Infrastructure.DatabaseMigration.Repositories _dbContext = dbContext; } - public async Task ExecuteSqlRaw(string commandText) + public async Task ExecuteSqlRaw(string sqlRaw) { - using (var command = _dbContext.Database.GetDbConnection().CreateCommand()) - { - command.CommandText = commandText; - await _dbContext.Database.OpenConnectionAsync(); - await command.ExecuteNonQueryAsync(); - } + await _dbContext.Database.ExecuteSqlRawAsync(sqlRaw); } } } diff --git a/infrastructure/NDB.Infrastructure.DatabaseMigration/Services/MigrationService.cs b/infrastructure/NDB.Infrastructure.DatabaseMigration/Services/MigrationService.cs index e8bbfb6..4efd680 100644 --- a/infrastructure/NDB.Infrastructure.DatabaseMigration/Services/MigrationService.cs +++ b/infrastructure/NDB.Infrastructure.DatabaseMigration/Services/MigrationService.cs @@ -6,6 +6,7 @@ using System; using System.Collections.Generic; using System.IO; using System.Linq; +using System.Threading.Tasks; using System.Xml; using System.Xml.Serialization; @@ -112,6 +113,9 @@ namespace NDB.Infrastructure.DatabaseMigration.Services } private void RunScript(string path) + => RunScriptAsync(path).GetAwaiter().GetResult(); + + private async Task RunScriptAsync(string path) { _logger.LogInformation($"Running sql script: '{path}'"); var sqlContent = File.ReadAllText(path); @@ -121,7 +125,7 @@ namespace NDB.Infrastructure.DatabaseMigration.Services using (var scope = _serviceProvider.CreateScope()) { var _repository = scope.ServiceProvider.GetRequiredService(); - _repository.ExecuteSqlRaw(sqlContent); + await _repository.ExecuteSqlRaw(sqlContent); } } } diff --git a/test/NDB.Test.Application/Scripts/1.0.1/04.Create table Settings.sql b/test/NDB.Test.Application/Scripts/1.0.1/04.Create table Settings.sql index 40d5c0b..992e517 100644 --- a/test/NDB.Test.Application/Scripts/1.0.1/04.Create table Settings.sql +++ b/test/NDB.Test.Application/Scripts/1.0.1/04.Create table Settings.sql @@ -3,11 +3,11 @@ "Code" TEXT NOT NULL UNIQUE, "Name" TEXT NOT NULL, "Description" TEXT, - "Context" TEXT NOT NULL + "Context" TEXT NOT NULL, CONSTRAINT "PK_Settings" PRIMARY KEY("Id" AUTOINCREMENT) ); INSERT INTO Settings(Id, Code, Name, Context) SELECT 1, 'THEME', 'Application theme', 'USER' UNION SELECT 2, 'LANGUAGE', 'Application language', 'USER' UNION -SELECT 3, 'NOTIFICATIONS_POSITION', 'Notifications position', 'USER' \ No newline at end of file +SELECT 3, 'NOTIFICATIONS_POSITION', 'Notifications position', 'USER'; \ No newline at end of file