From 60d376f6f6a1d5881d390a9a8d51c8c9e0bf7d02 Mon Sep 17 00:00:00 2001 From: crimcol Date: Sat, 23 Jul 2022 23:19:16 -0400 Subject: [PATCH 1/2] Read connection string directly from config file or from appsettings.json file. Added Sample projects. --- .../DbMigrationsNet6/DbMigrationsNet6.csproj | 15 --- .../DbMigrationsNet6/migrations.json | 2 +- sample/ProjectExample/ProjectExample.sln | 10 +- .../WebApplicationNet6/Program.cs | 6 ++ .../Properties/launchSettings.json | 28 +++++ .../WebApplicationNet6.csproj | 13 +++ .../appsettings.Development.json | 8 ++ .../WebApplicationNet6/appsettings.json | 12 +++ src/Alt.FluentMigrator.VStudio.psm1 | 101 ++++++++++++++++-- 9 files changed, 171 insertions(+), 24 deletions(-) create mode 100644 sample/ProjectExample/WebApplicationNet6/Program.cs create mode 100644 sample/ProjectExample/WebApplicationNet6/Properties/launchSettings.json create mode 100644 sample/ProjectExample/WebApplicationNet6/WebApplicationNet6.csproj create mode 100644 sample/ProjectExample/WebApplicationNet6/appsettings.Development.json create mode 100644 sample/ProjectExample/WebApplicationNet6/appsettings.json diff --git a/sample/ProjectExample/DbMigrationsNet6/DbMigrationsNet6.csproj b/sample/ProjectExample/DbMigrationsNet6/DbMigrationsNet6.csproj index 41baf9d..addc29d 100644 --- a/sample/ProjectExample/DbMigrationsNet6/DbMigrationsNet6.csproj +++ b/sample/ProjectExample/DbMigrationsNet6/DbMigrationsNet6.csproj @@ -15,19 +15,4 @@ - - - PreserveNewest - - - PreserveNewest - - - PreserveNewest - - - PreserveNewest - - - diff --git a/sample/ProjectExample/DbMigrationsNet6/migrations.json b/sample/ProjectExample/DbMigrationsNet6/migrations.json index bb0368c..d482349 100644 --- a/sample/ProjectExample/DbMigrationsNet6/migrations.json +++ b/sample/ProjectExample/DbMigrationsNet6/migrations.json @@ -1,5 +1,5 @@ { - "ConnectionProjectName": "ConsoleApp", + "ConnectionProjectName": "WebApplicationNet6", "ConnectionName": "TestDb", "FluentMigrationToolPath": "%USERPROFILE%\\.nuget\\packages\\fluentmigrator.console\\3.3.2\\net461\\any\\Migrate.exe", "DbProvider": "SqlServer", diff --git a/sample/ProjectExample/ProjectExample.sln b/sample/ProjectExample/ProjectExample.sln index 5077c84..3091680 100644 --- a/sample/ProjectExample/ProjectExample.sln +++ b/sample/ProjectExample/ProjectExample.sln @@ -13,9 +13,11 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleApp", "ConsoleApp\Co EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DbMigrationsNetFramework48", "DbMigrationsNetFramework48\DbMigrationsNetFramework48.csproj", "{FB887B25-741F-43CE-A96E-F50D072E1754}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DbMigrationsNet31", "DbMigrationsNet31\DbMigrationsNet31.csproj", "{0E5D4059-0B0A-4AE0-B2E7-EE5432C21844}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DbMigrationsNet31", "DbMigrationsNet31\DbMigrationsNet31.csproj", "{0E5D4059-0B0A-4AE0-B2E7-EE5432C21844}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DbMigrationsNet6", "DbMigrationsNet6\DbMigrationsNet6.csproj", "{280229EE-A160-404E-8F27-78D1D100DCB9}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DbMigrationsNet6", "DbMigrationsNet6\DbMigrationsNet6.csproj", "{280229EE-A160-404E-8F27-78D1D100DCB9}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebApplicationNet6", "WebApplicationNet6\WebApplicationNet6.csproj", "{A67D7B67-6F43-403F-B096-CE046E0EAF2A}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -39,6 +41,10 @@ Global {280229EE-A160-404E-8F27-78D1D100DCB9}.Debug|Any CPU.Build.0 = Debug|Any CPU {280229EE-A160-404E-8F27-78D1D100DCB9}.Release|Any CPU.ActiveCfg = Release|Any CPU {280229EE-A160-404E-8F27-78D1D100DCB9}.Release|Any CPU.Build.0 = Release|Any CPU + {A67D7B67-6F43-403F-B096-CE046E0EAF2A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A67D7B67-6F43-403F-B096-CE046E0EAF2A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A67D7B67-6F43-403F-B096-CE046E0EAF2A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A67D7B67-6F43-403F-B096-CE046E0EAF2A}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/sample/ProjectExample/WebApplicationNet6/Program.cs b/sample/ProjectExample/WebApplicationNet6/Program.cs new file mode 100644 index 0000000..79af670 --- /dev/null +++ b/sample/ProjectExample/WebApplicationNet6/Program.cs @@ -0,0 +1,6 @@ +var builder = WebApplication.CreateBuilder(args); +var app = builder.Build(); + +app.MapGet("/items", () => "Hello world"); + +app.Run(); diff --git a/sample/ProjectExample/WebApplicationNet6/Properties/launchSettings.json b/sample/ProjectExample/WebApplicationNet6/Properties/launchSettings.json new file mode 100644 index 0000000..4d2e2d6 --- /dev/null +++ b/sample/ProjectExample/WebApplicationNet6/Properties/launchSettings.json @@ -0,0 +1,28 @@ +{ + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:61753", + "sslPort": 44393 + } + }, + "profiles": { + "WebApplicationNet6": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "https://localhost:7282;http://localhost:5282", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/sample/ProjectExample/WebApplicationNet6/WebApplicationNet6.csproj b/sample/ProjectExample/WebApplicationNet6/WebApplicationNet6.csproj new file mode 100644 index 0000000..3eef2ad --- /dev/null +++ b/sample/ProjectExample/WebApplicationNet6/WebApplicationNet6.csproj @@ -0,0 +1,13 @@ + + + + net6.0 + enable + enable + + + + + + + diff --git a/sample/ProjectExample/WebApplicationNet6/appsettings.Development.json b/sample/ProjectExample/WebApplicationNet6/appsettings.Development.json new file mode 100644 index 0000000..0c208ae --- /dev/null +++ b/sample/ProjectExample/WebApplicationNet6/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/sample/ProjectExample/WebApplicationNet6/appsettings.json b/sample/ProjectExample/WebApplicationNet6/appsettings.json new file mode 100644 index 0000000..07eaa71 --- /dev/null +++ b/sample/ProjectExample/WebApplicationNet6/appsettings.json @@ -0,0 +1,12 @@ +{ + "ConnectionStrings": { + "TestDb": "server=localhost\\SQLEXPRESS; database=TestDb; Integrated Security=SSPI" + }, + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/src/Alt.FluentMigrator.VStudio.psm1 b/src/Alt.FluentMigrator.VStudio.psm1 index b2d985a..bc8d972 100644 --- a/src/Alt.FluentMigrator.VStudio.psm1 +++ b/src/Alt.FluentMigrator.VStudio.psm1 @@ -14,8 +14,81 @@ function GetMigrationSettings($projectName) return $migrationSettings } -function GetProjectProperties($projectName) +function ReadConnectionString { + [CmdletBinding()] + param($connectionStringPath, $connectionName) + + if ([string]::IsNullOrEmpty($connectionProject.ConfigFilePath)) + { + Write-Error -Message "Config file path is empty." -ErrorAction Stop + } + + $connectionString = "" + try { + $connectionString = ReadConnectionStringFromJsonConfig $connectionStringPath $connectionName + } + catch { + Write-Verbose "probably it is not a json file" + Write-Verbose $_ + } + + if ([string]::IsNullOrEmpty($connectionString)) + { + $connectionString = ReadConnectionStringFromXmlConfig $connectionStringPath $connectionName + } + else + { + return $connectionString + } + + if ([string]::IsNullOrEmpty($connectionString)) + { + Write-Error -Message "ConnectionString '$($connectionName)' not found in file '$($connectionStringPath)'." -ErrorAction Stop + } + + return $connectionString +} + +function ReadConnectionStringFromJsonConfig +{ + [CmdletBinding()] + param($connectionStringPath, $connectionName) + + Write-Verbose "Read connection string from JSON config: $connectionStringPath" + $json = Get-Content -Path $connectionStringPath | ConvertFrom-Json + $connectionStrings = $json.ConnectionStrings + + foreach($item in $connectionStrings.PSObject.Properties){ + if($item.Name -match $connectionName){ + return $item.Value + }else{ + Write-Verbose "Skip connection string $($item.Name) : $($item.Value)" + } + } +} + +function ReadConnectionStringFromXmlConfig +{ + [CmdletBinding()] + param($connectionStringPath, $connectionName) + + Write-Verbose "Read connection string from XML config: $connectionStringPath" + $cfg = [xml](Get-Content -Path $connectionStringPath) + $connectionStrings = $cfg.SelectNodes("//connectionStrings/add") + foreach($cs in $connectionStrings){ + if($cs.name -match $connectionName){ + return $cs.connectionString + }else{ + Write-Verbose "Skip connection string $($cs.name) : $($cs.connectionString)" + } + } +} + +function GetProjectProperties +{ + [CmdletBinding()] + param($projectName) $p = GetProject $projectName $fullPath = Split-Path -Path $p.FullName @@ -24,6 +97,17 @@ function GetProjectProperties($projectName) $outputFullPath = [IO.Path]::Combine($fullPath, $outputPath) $outputFileFullPath = [IO.Path]::Combine($outputFullPath, $outputFileName) $configFilePath = $outputFileFullPath + ".config" + + if (-not(Test-Path $configFilePath)) + { + $prevConfigPath = $configFilePath + $configFilePath = [IO.Path]::Combine($outputFullPath, "appsettings.json") + if (-not(Test-Path $configFilePath)) + { + Write-Verbose -Message "Config file was not found:`r`n$($prevConfigPath)`r`n$($configFilePath)" + $configFilePath = "" + } + } $properties = @{ Name = $p.Name @@ -41,19 +125,23 @@ function GetProjectProperties($projectName) return $o } -function Update-FluentDatabase([String]$ProjectName, [Int] $Timeout = 30) +function Update-FluentDatabase { + [CmdletBinding()] + param ([String]$ProjectName, [Int] $Timeout = 30) + $migrationProject = GetProjectProperties $ProjectName FluentBuild $migrationProject.Project $migrationSettings = GetMigrationSettings $migrationProject.Name $connectionProject = GetProjectProperties $migrationSettings.ConnectionProjectName + $connectionString = ReadConnectionString $connectionProject.ConfigFilePath $migrationSettings.ConnectionName $params = @( "-t:migrate", "-db $($migrationSettings.DbProvider)", - "-configPath ""$($connectionProject.ConfigFilePath)""", - "-c ""$($migrationSettings.ConnectionName)""", + #"-configPath ""$($connectionProject.ConfigFilePath)""", + "-c ""$($connectionString)""", "-a ""$($migrationProject.OutputFileFullPath)""", "-wd ""$($migrationProject.OutputFullPath)""", "-timeout $($Timeout)", @@ -77,13 +165,14 @@ function Rollback-FluentDatabase $migrationSettings = GetMigrationSettings $migrationProject.Name $connectionProject = GetProjectProperties $migrationSettings.ConnectionProjectName + $connectionString = ReadConnectionString $connectionProject.ConfigFilePath $migrationSettings.ConnectionName $params = @( "-t rollback:toversion", "-version $MigrationNumber", "-db $($migrationSettings.DbProvider)", - "-configPath ""$($connectionProject.ConfigFilePath)""", - "-c ""$($migrationSettings.ConnectionName)""", + #"-configPath ""$($connectionProject.ConfigFilePath)""", + "-c ""$($connectionString)""", "-a ""$($migrationProject.OutputFileFullPath)""", "-wd ""$($migrationProject.OutputFullPath)""", "-timeout $($Timeout)") From 95245a4d7702aa1b91ab7607e4a446ae004cac37 Mon Sep 17 00:00:00 2001 From: crimcol Date: Sat, 23 Jul 2022 23:22:22 -0400 Subject: [PATCH 2/2] Increase version --- appveyor.yml | 2 +- sample/ProjectExample/ManuallTests.md | 2 +- src/Alt.FluentMigrator.VStudio.nuspec | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index c3ea417..95fdce7 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -1,4 +1,4 @@ -version: 1.3.2.{build} +version: 1.3.3.{build} cache: - tools -> build.cake diff --git a/sample/ProjectExample/ManuallTests.md b/sample/ProjectExample/ManuallTests.md index 6c7ed52..1ef7933 100644 --- a/sample/ProjectExample/ManuallTests.md +++ b/sample/ProjectExample/ManuallTests.md @@ -10,7 +10,7 @@ Import-Module ..\..\src\Alt.FluentMigrator.VStudio.psm1 -Force Find-Package Alt.FluentMigrator.VStudio Uninstall-Package Alt.FluentMigrator.VStudio -verbose Install-Package Alt.FluentMigrator.VStudio -verbose -Install-Package Alt.FluentMigrator.VStudio -Source LocalSource -Version 1.3.2 -verbose +Install-Package Alt.FluentMigrator.VStudio -Source LocalSource -Version 1.3.3 -verbose ``` diff --git a/src/Alt.FluentMigrator.VStudio.nuspec b/src/Alt.FluentMigrator.VStudio.nuspec index db56365..68aea31 100644 --- a/src/Alt.FluentMigrator.VStudio.nuspec +++ b/src/Alt.FluentMigrator.VStudio.nuspec @@ -2,7 +2,7 @@ Alt.FluentMigrator.VStudio - 1.3.2 + 1.3.3 Volodymyr Liashenko https://github.com/crimcol/Alt.FluentMigrator.VStudio MIT