Skip to content
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

Read connection string from file #11

Merged
merged 2 commits into from
Jul 24, 2022
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
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: 1.3.2.{build}
version: 1.3.3.{build}

cache:
- tools -> build.cake
Expand Down
15 changes: 0 additions & 15 deletions sample/ProjectExample/DbMigrationsNet6/DbMigrationsNet6.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,4 @@
<PackageReference Include="FluentMigrator.Console" Version="3.3.2" />
</ItemGroup>

<ItemGroup>
<None Update="Scripts\2022041812260753_M1.sql">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Scripts\2022041812260753_M1_Down.sql">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Scripts\2022041812261210_M4.sql">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Scripts\2022041812261210_M4_Down.sql">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion sample/ProjectExample/DbMigrationsNet6/migrations.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion sample/ProjectExample/ManuallTests.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```


Expand Down
10 changes: 8 additions & 2 deletions sample/ProjectExample/ProjectExample.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions sample/ProjectExample/WebApplicationNet6/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

app.MapGet("/items", () => "Hello world");

app.Run();
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
}
13 changes: 13 additions & 0 deletions sample/ProjectExample/WebApplicationNet6/WebApplicationNet6.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<None Include="..\..\..\src\Alt.FluentMigrator.VStudio.psm1" Link="Alt.FluentMigrator.VStudio.psm1" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
12 changes: 12 additions & 0 deletions sample/ProjectExample/WebApplicationNet6/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"ConnectionStrings": {
"TestDb": "server=localhost\\SQLEXPRESS; database=TestDb; Integrated Security=SSPI"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
2 changes: 1 addition & 1 deletion src/Alt.FluentMigrator.VStudio.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package>
<metadata minClientVersion="3.3.0">
<id>Alt.FluentMigrator.VStudio</id>
<version>1.3.2</version>
<version>1.3.3</version>
<authors>Volodymyr Liashenko</authors>
<projectUrl>https://github.com/crimcol/Alt.FluentMigrator.VStudio</projectUrl>
<license type="expression">MIT</license>
Expand Down
101 changes: 95 additions & 6 deletions src/Alt.FluentMigrator.VStudio.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)",
Expand All @@ -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)")
Expand Down