From c9be9740985ea563a344b2517974a270ebd394c9 Mon Sep 17 00:00:00 2001 From: ynaydonov Date: Thu, 23 Jan 2020 14:19:26 +0200 Subject: [PATCH 01/15] Amended code formatting to improve readability --- tools/dbup-add-migration.psm1 | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tools/dbup-add-migration.psm1 b/tools/dbup-add-migration.psm1 index 689388f..e5ab99d 100644 --- a/tools/dbup-add-migration.psm1 +++ b/tools/dbup-add-migration.psm1 @@ -60,10 +60,12 @@ #generate migration file name and path $fileName = Get-Date -Format yyyyMMddHHmmss + if($Name -ne "") { $fileName = $fileName + "_" + $Name } + $fileName = $fileName + ".sql" $filePath = Join-Path $scriptsDir $fileName From a3ddb466b9a1eb34008fbd84fc200f138939ee8d Mon Sep 17 00:00:00 2001 From: ynaydonov Date: Thu, 23 Jan 2020 14:38:50 +0200 Subject: [PATCH 02/15] Added fileName option into configuration file --- tools/dbup-add-migration.psm1 | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/tools/dbup-add-migration.psm1 b/tools/dbup-add-migration.psm1 index e5ab99d..92115df 100644 --- a/tools/dbup-add-migration.psm1 +++ b/tools/dbup-add-migration.psm1 @@ -38,8 +38,7 @@ elseif (Test-Path (Join-Path $scriptsDir $scriptsFolderName) -PathType Container) { $scriptsDir = Join-Path $scriptsDir $scriptsFolderName - } - else + } else { #search for .sql files in the project $sqlFiles = @(Get-ChildItem -Path $projectDir -Filter *.sql -Recurse) @@ -65,7 +64,7 @@ { $fileName = $fileName + "_" + $Name } - + $fileName = $fileName + ".sql" $filePath = Join-Path $scriptsDir $fileName @@ -140,8 +139,7 @@ function Add-MigrationSettings if (Test-Path $settingsFilePath -PathType Leaf) { Write-Host "A settings file for Add-Migration command already exists" - } - else + } else { #create the file New-Item -Path $projectDir -Name $settingsFileName -ItemType File | Out-Null @@ -150,7 +148,11 @@ function Add-MigrationSettings $defaultSettings = @" { "folder": "Migrations", - "buildAction": "EmbeddedResource" + "buildAction": "EmbeddedResource", + "fileName": { + "segmentSeparator": "_", + "prefixFormat": "yyyyMMddHHmmss" + } } "@ From 40a3ac8a80ff2ccc747efd0b9e8032e6ec728c5e Mon Sep 17 00:00:00 2001 From: ynaydonov Date: Thu, 23 Jan 2020 14:46:24 +0200 Subject: [PATCH 03/15] Changed autogenerated datetime as a UTC timestamp --- tools/dbup-add-migration.psm1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/dbup-add-migration.psm1 b/tools/dbup-add-migration.psm1 index 92115df..68e9211 100644 --- a/tools/dbup-add-migration.psm1 +++ b/tools/dbup-add-migration.psm1 @@ -58,7 +58,7 @@ } #generate migration file name and path - $fileName = Get-Date -Format yyyyMMddHHmmss + $fileName = Get-Date([System.DateTime]::UtcNow) -Format yyyyMMddHHmmss if($Name -ne "") { From b5385bea8ddd6f0ddb48f348d97b5b7d121b946a Mon Sep 17 00:00:00 2001 From: ynaydonov Date: Thu, 23 Jan 2020 19:49:43 +0200 Subject: [PATCH 04/15] Added settings and file classes --- tools/dbup-add-migration.psm1 | 80 +++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/tools/dbup-add-migration.psm1 b/tools/dbup-add-migration.psm1 index 68e9211..ef530b7 100644 --- a/tools/dbup-add-migration.psm1 +++ b/tools/dbup-add-migration.psm1 @@ -167,6 +167,86 @@ function Add-MigrationSettings } } +class Settings +{ + [string] $Folder + [string] $BuildAction + [File] $File + + hidden static [string] $defaultFolder = "Migrations" + hidden static [string] $defaultBuildAction = "EmbeddedResource" + + static [string] getDefaultFolder() + { + return [Settings]::defaultFolder + } + + static [string] getDefaultBuildAction() + { + return [Settings]::defaultBuildAction + } + + Settings() + { + $this.Folder = [Settings]::getDefaultFolder + $this.BuildAction = [Settings]::getDefaultBuildAction + $this.File = [File]::new() + } + + Settings( + [string] $Folder, + [string] $BuildAction, + [File] $File) + { + $this.Folder = $Folder + $this.BuildAction = $BuildAction + $this.File = $File + } +} + +class File +{ + [string] $Name + [string] $SegmentSeparator + [string] $PrefixFormat + + hidden static [string] $defaultName = "dbup-add-migration.json" + hidden static [string] $defaultSegmentSeparator = "_" + hidden static [string] $defaultPrefixFormat = "yyyyMMddHHmmss" + + static [string] getDefaultName() + { + return [File]::defaultName + } + + static [string] getDefaultSegmentSeparator() + { + return [File]::defaultSegmentSeparator + } + + static [string] getDefaultPrefixFormat() + { + return [File]::defaultPrefixFormat + } + + File() + { + $this.Name = [File]::getDefaultName() + $this.SegmentSeparator = [File]::getDefaultSegmentSeparator() + $this.PrefixFormat = [File]::getDefaultPrefixFormat() + } + + File( + [string] $Name, + [string] $SegmentSeparator, + [string] $PrefixFormat) + { + $this.Name = $Name + $this.SegmentSeparator = $SegmentSeparator + $this.PrefixFormat = $PrefixFormat + } +} + enum BuildActionType { None = 0 From f9d1e274774ee6b96724300709fe28ab45207d4a Mon Sep 17 00:00:00 2001 From: ynaydonov Date: Sun, 26 Jan 2020 01:04:50 +0200 Subject: [PATCH 05/15] Completed initial implementation --- tools/dbup-add-migration.psm1 | 182 +++++++++++++++------------------- 1 file changed, 81 insertions(+), 101 deletions(-) diff --git a/tools/dbup-add-migration.psm1 b/tools/dbup-add-migration.psm1 index ef530b7..4368767 100644 --- a/tools/dbup-add-migration.psm1 +++ b/tools/dbup-add-migration.psm1 @@ -1,5 +1,4 @@ -function Add-DbUpMigration -{ +function Add-DbUpMigration { param ( [string] $Name, @@ -14,58 +13,55 @@ $projectDir = Split-Path $project.FullName $scriptsDir = $projectDir + $defaultFileName = [File]::getDefaultName() + $filePath = Join-Path $projectDir $defaultFileName + + $settings = [Settings]::readFromFile($filePath) + + #assign default settings if requested does not exist + if ($null -eq $settings) { + $settings = [Settings]::new() + } + #apply folder and build action values from settings file #(only if folder is not specified and build action is 'None') - Apply-Settings -folder ([ref]$Folder) -buildAction ([ref]$BuildAction) -projectDir $projectDir + Apply-Settings -folder ([ref]$Folder) -buildAction ([ref]$BuildAction) -settings $settings #check if the scripts folder is specified - if ($Folder -ne "") - { + if ($Folder -ne "") { $scriptsDir = Join-Path $scriptsDir $Folder #create the scripts directory if it doesn't exist yet - if (-not (Test-Path $scriptsDir -PathType Container)) - { + if (-not (Test-Path $scriptsDir -PathType Container)) { New-Item -ItemType Directory -Path $scriptsDir | Out-Null } } #check if "Migrations" folder exists - elseif (Test-Path (Join-Path $scriptsDir $migrationsFolderName) -PathType Container) - { + elseif (Test-Path (Join-Path $scriptsDir $migrationsFolderName) -PathType Container) { $scriptsDir = Join-Path $scriptsDir $migrationsFolderName } #check if "Scripts" folder exists - elseif (Test-Path (Join-Path $scriptsDir $scriptsFolderName) -PathType Container) - { + elseif (Test-Path (Join-Path $scriptsDir $scriptsFolderName) -PathType Container) { $scriptsDir = Join-Path $scriptsDir $scriptsFolderName - } else - { + } + else { #search for .sql files in the project $sqlFiles = @(Get-ChildItem -Path $projectDir -Filter *.sql -Recurse) #if no sql files are found, create a "Migrations" folder, #where the new migration file will be stored - if($sqlFiles.Count -eq 0) - { + if ($sqlFiles.Count -eq 0) { $scriptsDir = Join-Path $scriptsDir $migrationsFolderName New-Item -ItemType Directory -Path $scriptsDir | Out-Null } #get the first folder with sql files - else - { + else { $scriptsDir = $sqlFiles[0].DirectoryName } } #generate migration file name and path - $fileName = Get-Date([System.DateTime]::UtcNow) -Format yyyyMMddHHmmss - - if($Name -ne "") - { - $fileName = $fileName + "_" + $Name - } - - $fileName = $fileName + ".sql" + $fileName = [File]::buildFullName($Name, $settings.File.PrefixFormat, $settings.File.SegmentSeparator) $filePath = Join-Path $scriptsDir $fileName #create migration file @@ -75,14 +71,12 @@ $item = $project.ProjectItems.AddFromFile($filePath) #set the build action - if($BuildAction -ne [BuildActionType]::None) - { + if ($BuildAction -ne [BuildActionType]::None) { $item.Properties.Item("BuildAction").Value = $BuildAction -as [int] #if build action is set to content, then also #set 'copy to output directory' to 'copy always' - if($BuildAction -eq [BuildActionType]::Content) - { + if ($BuildAction -eq [BuildActionType]::Content) { $item.Properties.Item("CopyToOutputDirectory").Value = [uint32]1 } } @@ -93,8 +87,7 @@ $dte.ItemOperations.OpenFile($filePath) | Out-Null } -function Add-Migration -{ +function Add-Migration { param ( [string] $Name, @@ -105,59 +98,44 @@ function Add-Migration Add-DbUpMigration -Name $Name -Folder $Folder -BuildAction $BuildAction } -function Apply-Settings([ref]$folder, [ref]$buildAction, $projectDir) -{ - $settingsFilePath = Join-Path $projectDir "dbup-add-migration.json" - - #check if settings file exists - if (Test-Path $settingsFilePath -PathType Leaf) - { - $settings = Get-Content -Raw -Path $settingsFilePath | ConvertFrom-Json - - #overwrite $folder value only if it's not already set - if($folder.Value -eq "") - { - $folder.Value = $settings.folder - } +function Apply-Settings([ref]$folder, [ref]$buildAction, [Settings]$settings) { + #overwrite $folder value only if it's not already set + if ($folder.Value -eq "") { + $folder.Value = $settings.Folder + } - #overwrite $buildAction value only if it's set to 'None' - if($buildAction.Value -eq [BuildActionType]::None) - { - $buildAction.Value = [BuildActionType] $settings.buildAction - } + #overwrite $buildAction value only if it's set to 'None' + if ($buildAction.Value -eq [BuildActionType]::None) { + $buildAction.Value = [BuildActionType] $settings.BuildAction } } -function Add-MigrationSettings -{ +function Add-MigrationSettings { $project = Get-Project $projectDir = Split-Path $project.FullName - $settingsFileName = "dbup-add-migration.json" + $settingsFileName = [File]::getDefaultName() $settingsFilePath = Join-Path $projectDir $settingsFileName #create settings file only if it doesn't exist yet - if (Test-Path $settingsFilePath -PathType Leaf) - { + if (Test-Path $settingsFilePath -PathType Leaf) { Write-Host "A settings file for Add-Migration command already exists" - } else - { + } + else { #create the file New-Item -Path $projectDir -Name $settingsFileName -ItemType File | Out-Null - #prepare default settings - $defaultSettings = @" -{ - "folder": "Migrations", - "buildAction": "EmbeddedResource", - "fileName": { - "segmentSeparator": "_", - "prefixFormat": "yyyyMMddHHmmss" - } -} -"@ + #getting default file settings + $defaultFileSettings = [File]::new() | Select-Object -Property * -ExcludeProperty Name + + #composing default settings + $defaultSettings = [PSCustomObject]@{ + folder = [Settings]::getDefaultFolder() + buildAction = [Settings]::getDefaultBuildAction() + file = $defaultFileSettings + } #insert default data into the file - $defaultSettings | Out-File -FilePath $settingsFilePath + $defaultSettings | ConvertTo-Json -Depth 10 | Out-File -FilePath $settingsFilePath #add the settings file to the project $item = $project.ProjectItems.AddFromFile($settingsFilePath) @@ -167,8 +145,7 @@ function Add-MigrationSettings } } -class Settings -{ +class Settings { [string] $Folder [string] $BuildAction [File] $File @@ -176,36 +153,38 @@ class Settings hidden static [string] $defaultFolder = "Migrations" hidden static [string] $defaultBuildAction = "EmbeddedResource" - static [string] getDefaultFolder() - { + static [string] getDefaultFolder() { return [Settings]::defaultFolder } - static [string] getDefaultBuildAction() - { + static [string] getDefaultBuildAction() { return [Settings]::defaultBuildAction } - Settings() - { - $this.Folder = [Settings]::getDefaultFolder - $this.BuildAction = [Settings]::getDefaultBuildAction + static [Settings] readFromFile([string]$FilePath) { + #check if settings file exists + if (Test-Path $FilePath -PathType Leaf) { + return [Settings](Get-Content -Raw -Path $FilePath | ConvertFrom-Json) + } + else { + return $null + } + } + + Settings() { + $this.Folder = [Settings]::getDefaultFolder() + $this.BuildAction = [Settings]::getDefaultBuildAction() $this.File = [File]::new() } - Settings( - [string] $Folder, - [string] $BuildAction, - [File] $File) - { + Settings([string] $Folder, [string] $BuildAction, [File] $File) { $this.Folder = $Folder $this.BuildAction = $BuildAction $this.File = $File } } -class File -{ +class File { [string] $Name [string] $SegmentSeparator [string] $PrefixFormat @@ -214,41 +193,42 @@ class File hidden static [string] $defaultSegmentSeparator = "_" hidden static [string] $defaultPrefixFormat = "yyyyMMddHHmmss" - static [string] getDefaultName() - { + static [string] getDefaultName() { return [File]::defaultName } - static [string] getDefaultSegmentSeparator() - { + static [string] getDefaultSegmentSeparator() { return [File]::defaultSegmentSeparator } - static [string] getDefaultPrefixFormat() - { + static [string] getDefaultPrefixFormat() { return [File]::defaultPrefixFormat } - File() - { + static [string] buildFullName([string]$MainSegment, [string]$Format, [string]$Separator) { + $fullName = Get-Date([System.DateTime]::UtcNow) -Format $Format + + if ($MainSegment -ne "") { + $fullName += $Separator + $MainSegment + } + + return $fullName + ".sql" + } + + File() { $this.Name = [File]::getDefaultName() $this.SegmentSeparator = [File]::getDefaultSegmentSeparator() $this.PrefixFormat = [File]::getDefaultPrefixFormat() } - File( - [string] $Name, - [string] $SegmentSeparator, - [string] $PrefixFormat) - { + File([string] $Name, [string] $SegmentSeparator, [string] $PrefixFormat) { $this.Name = $Name $this.SegmentSeparator = $SegmentSeparator $this.PrefixFormat = $PrefixFormat } } -enum BuildActionType -{ +enum BuildActionType { None = 0 Compile = 1 Content = 2 From 9fb396a034aef51426736b2d1e82a999c92ba99a Mon Sep 17 00:00:00 2001 From: ynaydonov Date: Mon, 27 Jan 2020 14:35:26 +0200 Subject: [PATCH 06/15] Updated documentation --- README.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 75e1ce8..744886f 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ - Powershell 5 ## About -This package adds a new command **"Add-Migration"** (and **"Add-DbUpMigration"** alias) to the Package Manager Console. Running the command results in an sql file with date and time (in format _yyyyMMddHHmmss_) in the file name added to the project (e.g. `20170730185349_MyFirstMigration.sql`). +This package adds a new command **"Add-Migration"** (and **"Add-DbUpMigration"** alias) to the Package Manager Console. Running the command results in generating an empty sql file with the specified name, which is prefixed by a timestamp in UTC (by default, in a format _yyyyMMddHHmmss_). The auto-generated file (e.g. 20170730185349_MyFirstMigration.sql`) will be added to the project, which you can set by selecting respective item from the dropdown list "Default project" on the top of your package manager console window. Create a file using default behaviour (the command will decide where to put it, build action will not be set): @@ -49,7 +49,11 @@ This command will add `dbup-add-migration.json` file to your project (if it does ``` { "folder": "Migrations", - "buildAction": "EmbeddedResource" + "buildAction": "EmbeddedResource", + "file": { + "SegmentSeparator": "_", + "PrefixFormat": "yyyyMMddHHmmss" + } } ``` From 0857539f95f37078b2a9772fdf31db380750925f Mon Sep 17 00:00:00 2001 From: ynaydonov Date: Mon, 27 Jan 2020 14:46:27 +0200 Subject: [PATCH 07/15] Updated package description according to changes in implementation --- dbup-add-migration.nuspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dbup-add-migration.nuspec b/dbup-add-migration.nuspec index d2401a7..5c9c295 100644 --- a/dbup-add-migration.nuspec +++ b/dbup-add-migration.nuspec @@ -10,7 +10,7 @@ https://github.com/piotrekh/dbup-add-migration https://raw.githubusercontent.com/DbUp/DbUp/master/dbup-icon.png false - Package manager console command "Add-Migration" that creates an sql file with date and time (yyyyMMddHHmmss) in the file name (e.g. 20170730185349_Migration.sql) + Package manager console command "Add-Migration" that creates an sql file with date and time in UTC (yyyyMMddHHmmss) in the file name (e.g. 20170730185349_Migration.sql) v1.3.0 - added Add-DbUpMigration alias command Copyright 2017 dbup migrations add-migration sql From 6166812567f329f478ca8308f389c1820d0be50f Mon Sep 17 00:00:00 2001 From: ynaydonov Date: Fri, 21 Feb 2020 21:58:06 +0200 Subject: [PATCH 08/15] Added SqlScriptOptions class and related SqlScriptExecutionMode enum type --- tools/dbup-add-migration.psm1 | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/tools/dbup-add-migration.psm1 b/tools/dbup-add-migration.psm1 index 4368767..0e10b1b 100644 --- a/tools/dbup-add-migration.psm1 +++ b/tools/dbup-add-migration.psm1 @@ -228,6 +228,32 @@ class File { } } +class SqlScriptOptions { + [SqlScriptExecutionMode] $ExecutionMode + [int] $RunGroupOrder + + hidden static [SqlScriptExecutionMode] $defaultExecutionMode = [SqlScriptExecutionMode]::None + hidden static [int] $defaultRunGroupOrder = 100 # NOTE: this corresponds to the default value in DbUp sourcecode + + static [SqlScriptExecutionMode] getDefaultExecutionMode() { + return [SqlScriptOptions]::defaultExecutionMode + } + + static [int] getDefaultRunGroupOrder() { + return [SqlScriptOptions]::defaultRunGroupOrder + } + + SqlScriptOptions() { + $this.ExecutionMode = [SqlScriptOptions]::getDefault() + $this.RunGroupOrder = [SqlScriptOptions]::getDefaultRunGroupOrder() + } + + SqlScriptOptions([SqlScriptExecutionMode] $ExecutionMode, [int] $RunGroupOrder) { + $this.ExecutionMode = $ExecutionMode + $this.RunGroupOrder = $RunGroupOrder + } +} + enum BuildActionType { None = 0 Compile = 1 @@ -235,4 +261,10 @@ enum BuildActionType { EmbeddedResource = 3 } +enum SqlScriptExecutionMode { + None = 0 + RunOnce = 1 + RunAlways = 2 +} + Export-ModuleMember -Function Add-DbUpMigration, Add-Migration, Add-MigrationSettings \ No newline at end of file From fa0934ea3dfe74111e786ccd19cd52641a506bd7 Mon Sep 17 00:00:00 2001 From: ynaydonov Date: Fri, 21 Feb 2020 22:28:37 +0200 Subject: [PATCH 09/15] Added SqlScriptOptions property into Settings class --- tools/dbup-add-migration.psm1 | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/tools/dbup-add-migration.psm1 b/tools/dbup-add-migration.psm1 index 0e10b1b..3713572 100644 --- a/tools/dbup-add-migration.psm1 +++ b/tools/dbup-add-migration.psm1 @@ -3,7 +3,9 @@ ( [string] $Name, [string] $Folder = "", - [BuildActionType] $BuildAction = [BuildActionType]::None + [BuildActionType] $BuildAction = [BuildActionType]::None, + [SqlScriptType] $SqlScriptType = [SqlScriptOptions]::getDefaultSqlScriptType(), + [int] $RunGroupOrder = [SqlScriptOptions]::getDefaultRunGroupOrder() ) $migrationsFolderName = "Migrations" @@ -92,10 +94,12 @@ function Add-Migration { ( [string] $Name, [string] $Folder = "", - [BuildActionType] $BuildAction = [BuildActionType]::None + [BuildActionType] $BuildAction = [BuildActionType]::None, + [SqlScriptType] $SqlScriptType = [SqlScriptOptions]::getDefaultSqlScriptType(), + [int] $RunGroupOrder = [SqlScriptOptions]::getDefaultRunGroupOrder() ) - Add-DbUpMigration -Name $Name -Folder $Folder -BuildAction $BuildAction + Add-DbUpMigration -Name $Name -Folder $Folder -BuildAction $BuildAction -SqlScriptType $SqlScriptType -RunGroupOrder $RunGroupOrder } function Apply-Settings([ref]$folder, [ref]$buildAction, [Settings]$settings) { @@ -149,6 +153,7 @@ class Settings { [string] $Folder [string] $BuildAction [File] $File + [SqlScriptOptions] $SqlScriptOptions hidden static [string] $defaultFolder = "Migrations" hidden static [string] $defaultBuildAction = "EmbeddedResource" @@ -175,12 +180,14 @@ class Settings { $this.Folder = [Settings]::getDefaultFolder() $this.BuildAction = [Settings]::getDefaultBuildAction() $this.File = [File]::new() + $this.SqlScriptOptions = [SqlScriptOptions]::new() } - Settings([string] $Folder, [string] $BuildAction, [File] $File) { + Settings([string] $Folder, [string] $BuildAction, [File] $File, [SqlScriptOptions] $SqlScriptOptions) { $this.Folder = $Folder $this.BuildAction = $BuildAction $this.File = $File + $this.SqlScriptOptions = $SqlScriptOptions } } @@ -229,14 +236,14 @@ class File { } class SqlScriptOptions { - [SqlScriptExecutionMode] $ExecutionMode + [SqlScriptType] $SqlScriptType [int] $RunGroupOrder - hidden static [SqlScriptExecutionMode] $defaultExecutionMode = [SqlScriptExecutionMode]::None + hidden static [SqlScriptType] $defaultSqlScriptType = [SqlScriptType]::None hidden static [int] $defaultRunGroupOrder = 100 # NOTE: this corresponds to the default value in DbUp sourcecode - static [SqlScriptExecutionMode] getDefaultExecutionMode() { - return [SqlScriptOptions]::defaultExecutionMode + static [SqlScriptType] getDefaultSqlScriptType() { + return [SqlScriptOptions]::defaultSqlScriptType } static [int] getDefaultRunGroupOrder() { @@ -244,12 +251,12 @@ class SqlScriptOptions { } SqlScriptOptions() { - $this.ExecutionMode = [SqlScriptOptions]::getDefault() + $this.SqlScriptType = [SqlScriptOptions]::getDefaultSqlScriptType() $this.RunGroupOrder = [SqlScriptOptions]::getDefaultRunGroupOrder() } - SqlScriptOptions([SqlScriptExecutionMode] $ExecutionMode, [int] $RunGroupOrder) { - $this.ExecutionMode = $ExecutionMode + SqlScriptOptions([SqlScriptType] $SqlScriptType, [int] $RunGroupOrder) { + $this.SqlScriptType = $SqlScriptType $this.RunGroupOrder = $RunGroupOrder } } @@ -261,7 +268,7 @@ enum BuildActionType { EmbeddedResource = 3 } -enum SqlScriptExecutionMode { +enum SqlScriptType { None = 0 RunOnce = 1 RunAlways = 2 From dc0fb27478aea54f81e2df11e8edf62855f4b120 Mon Sep 17 00:00:00 2001 From: ynaydonov Date: Fri, 21 Feb 2020 22:52:42 +0200 Subject: [PATCH 10/15] Renamed SqlScriptType to ScriptType to preserve consistency with DbUp names --- tools/dbup-add-migration.psm1 | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/tools/dbup-add-migration.psm1 b/tools/dbup-add-migration.psm1 index 3713572..44f2fca 100644 --- a/tools/dbup-add-migration.psm1 +++ b/tools/dbup-add-migration.psm1 @@ -4,7 +4,7 @@ [string] $Name, [string] $Folder = "", [BuildActionType] $BuildAction = [BuildActionType]::None, - [SqlScriptType] $SqlScriptType = [SqlScriptOptions]::getDefaultSqlScriptType(), + [ScriptType] $ScriptType = [SqlScriptOptions]::getDefaultScriptType(), [int] $RunGroupOrder = [SqlScriptOptions]::getDefaultRunGroupOrder() ) @@ -95,11 +95,11 @@ function Add-Migration { [string] $Name, [string] $Folder = "", [BuildActionType] $BuildAction = [BuildActionType]::None, - [SqlScriptType] $SqlScriptType = [SqlScriptOptions]::getDefaultSqlScriptType(), + [ScriptType] $ScriptType = [SqlScriptOptions]::getDefaultScriptType(), [int] $RunGroupOrder = [SqlScriptOptions]::getDefaultRunGroupOrder() ) - Add-DbUpMigration -Name $Name -Folder $Folder -BuildAction $BuildAction -SqlScriptType $SqlScriptType -RunGroupOrder $RunGroupOrder + Add-DbUpMigration -Name $Name -Folder $Folder -BuildAction $BuildAction -ScriptType $ScriptType -RunGroupOrder $RunGroupOrder } function Apply-Settings([ref]$folder, [ref]$buildAction, [Settings]$settings) { @@ -131,11 +131,15 @@ function Add-MigrationSettings { #getting default file settings $defaultFileSettings = [File]::new() | Select-Object -Property * -ExcludeProperty Name + #getting default sql script options + $defaultSqlScriptOptions = [SqlScriptOptions]::new() + #composing default settings $defaultSettings = [PSCustomObject]@{ - folder = [Settings]::getDefaultFolder() - buildAction = [Settings]::getDefaultBuildAction() - file = $defaultFileSettings + folder = [Settings]::getDefaultFolder() + buildAction = [Settings]::getDefaultBuildAction() + file = $defaultFileSettings + sqlScriptOptions = $defaultSqlScriptOptions } #insert default data into the file @@ -236,14 +240,14 @@ class File { } class SqlScriptOptions { - [SqlScriptType] $SqlScriptType + [ScriptType] $ScriptType [int] $RunGroupOrder - hidden static [SqlScriptType] $defaultSqlScriptType = [SqlScriptType]::None + hidden static [ScriptType] $defaultScriptType = [ScriptType]::None hidden static [int] $defaultRunGroupOrder = 100 # NOTE: this corresponds to the default value in DbUp sourcecode - static [SqlScriptType] getDefaultSqlScriptType() { - return [SqlScriptOptions]::defaultSqlScriptType + static [ScriptType] getDefaultScriptType() { + return [SqlScriptOptions]::defaultScriptType } static [int] getDefaultRunGroupOrder() { @@ -251,12 +255,12 @@ class SqlScriptOptions { } SqlScriptOptions() { - $this.SqlScriptType = [SqlScriptOptions]::getDefaultSqlScriptType() + $this.ScriptType = [SqlScriptOptions]::getDefaultScriptType() $this.RunGroupOrder = [SqlScriptOptions]::getDefaultRunGroupOrder() } - SqlScriptOptions([SqlScriptType] $SqlScriptType, [int] $RunGroupOrder) { - $this.SqlScriptType = $SqlScriptType + SqlScriptOptions([ScriptType] $ScriptType, [int] $RunGroupOrder) { + $this.ScriptType = $ScriptType $this.RunGroupOrder = $RunGroupOrder } } @@ -268,7 +272,7 @@ enum BuildActionType { EmbeddedResource = 3 } -enum SqlScriptType { +enum ScriptType { None = 0 RunOnce = 1 RunAlways = 2 From a3585fb0c02640a0bd1a75cc9a7f6612f7da8c5c Mon Sep 17 00:00:00 2001 From: ynaydonov Date: Sat, 22 Feb 2020 16:40:51 +0200 Subject: [PATCH 11/15] Replaced SqlScriptOptions class by ExecutionMode parameter to avoid overengineering --- tools/dbup-add-migration.psm1 | 60 ++++++++++------------------------- 1 file changed, 17 insertions(+), 43 deletions(-) diff --git a/tools/dbup-add-migration.psm1 b/tools/dbup-add-migration.psm1 index 44f2fca..b375c4b 100644 --- a/tools/dbup-add-migration.psm1 +++ b/tools/dbup-add-migration.psm1 @@ -4,8 +4,7 @@ [string] $Name, [string] $Folder = "", [BuildActionType] $BuildAction = [BuildActionType]::None, - [ScriptType] $ScriptType = [SqlScriptOptions]::getDefaultScriptType(), - [int] $RunGroupOrder = [SqlScriptOptions]::getDefaultRunGroupOrder() + [ExecutionMode] $ExecutionMode = [ExecutionMode]::None ) $migrationsFolderName = "Migrations" @@ -95,11 +94,10 @@ function Add-Migration { [string] $Name, [string] $Folder = "", [BuildActionType] $BuildAction = [BuildActionType]::None, - [ScriptType] $ScriptType = [SqlScriptOptions]::getDefaultScriptType(), - [int] $RunGroupOrder = [SqlScriptOptions]::getDefaultRunGroupOrder() + [ExecutionMode] $ExecutionMode = [ExecutionMode]::None ) - Add-DbUpMigration -Name $Name -Folder $Folder -BuildAction $BuildAction -ScriptType $ScriptType -RunGroupOrder $RunGroupOrder + Add-DbUpMigration -Name $Name -Folder $Folder -BuildAction $BuildAction -ExecutionMode $ExecutionMode } function Apply-Settings([ref]$folder, [ref]$buildAction, [Settings]$settings) { @@ -131,15 +129,12 @@ function Add-MigrationSettings { #getting default file settings $defaultFileSettings = [File]::new() | Select-Object -Property * -ExcludeProperty Name - #getting default sql script options - $defaultSqlScriptOptions = [SqlScriptOptions]::new() - #composing default settings $defaultSettings = [PSCustomObject]@{ - folder = [Settings]::getDefaultFolder() - buildAction = [Settings]::getDefaultBuildAction() - file = $defaultFileSettings - sqlScriptOptions = $defaultSqlScriptOptions + folder = [Settings]::getDefaultFolder() + buildAction = [Settings]::getDefaultBuildAction() + file = $defaultFileSettings + executionMode = [Settings]::getDefaultExecutionMode() } #insert default data into the file @@ -157,10 +152,11 @@ class Settings { [string] $Folder [string] $BuildAction [File] $File - [SqlScriptOptions] $SqlScriptOptions + [ExecutionMode] $ExecutionMode hidden static [string] $defaultFolder = "Migrations" hidden static [string] $defaultBuildAction = "EmbeddedResource" + hidden static [ExecutionMode] $defaultExecutionMode = [ExecutionMode]::None static [string] getDefaultFolder() { return [Settings]::defaultFolder @@ -169,6 +165,10 @@ class Settings { static [string] getDefaultBuildAction() { return [Settings]::defaultBuildAction } + + static [ExecutionMode] getDefaultExecutionMode() { + return [Settings]::defaultExecutionMode + } static [Settings] readFromFile([string]$FilePath) { #check if settings file exists @@ -184,14 +184,14 @@ class Settings { $this.Folder = [Settings]::getDefaultFolder() $this.BuildAction = [Settings]::getDefaultBuildAction() $this.File = [File]::new() - $this.SqlScriptOptions = [SqlScriptOptions]::new() + $this.ExecutionMode = [Settings]::getDefaultExecutionMode() } - Settings([string] $Folder, [string] $BuildAction, [File] $File, [SqlScriptOptions] $SqlScriptOptions) { + Settings([string] $Folder, [string] $BuildAction, [File] $File, [ExecutionMode] $ExecutionMode) { $this.Folder = $Folder $this.BuildAction = $BuildAction $this.File = $File - $this.SqlScriptOptions = $SqlScriptOptions + $this.ExecutionMode = $ExecutionMode } } @@ -239,32 +239,6 @@ class File { } } -class SqlScriptOptions { - [ScriptType] $ScriptType - [int] $RunGroupOrder - - hidden static [ScriptType] $defaultScriptType = [ScriptType]::None - hidden static [int] $defaultRunGroupOrder = 100 # NOTE: this corresponds to the default value in DbUp sourcecode - - static [ScriptType] getDefaultScriptType() { - return [SqlScriptOptions]::defaultScriptType - } - - static [int] getDefaultRunGroupOrder() { - return [SqlScriptOptions]::defaultRunGroupOrder - } - - SqlScriptOptions() { - $this.ScriptType = [SqlScriptOptions]::getDefaultScriptType() - $this.RunGroupOrder = [SqlScriptOptions]::getDefaultRunGroupOrder() - } - - SqlScriptOptions([ScriptType] $ScriptType, [int] $RunGroupOrder) { - $this.ScriptType = $ScriptType - $this.RunGroupOrder = $RunGroupOrder - } -} - enum BuildActionType { None = 0 Compile = 1 @@ -272,7 +246,7 @@ enum BuildActionType { EmbeddedResource = 3 } -enum ScriptType { +enum ExecutionMode { None = 0 RunOnce = 1 RunAlways = 2 From ef3da79eb0fd503f3c8533547b08250a689dffa8 Mon Sep 17 00:00:00 2001 From: ynaydonov Date: Sat, 22 Feb 2020 17:51:17 +0200 Subject: [PATCH 12/15] Completed initial implementation --- tools/dbup-add-migration.psm1 | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/tools/dbup-add-migration.psm1 b/tools/dbup-add-migration.psm1 index b375c4b..c0b98f9 100644 --- a/tools/dbup-add-migration.psm1 +++ b/tools/dbup-add-migration.psm1 @@ -26,7 +26,7 @@ #apply folder and build action values from settings file #(only if folder is not specified and build action is 'None') - Apply-Settings -folder ([ref]$Folder) -buildAction ([ref]$BuildAction) -settings $settings + Apply-Settings -folder ([ref]$Folder) -buildAction ([ref]$BuildAction) -executionMode ([ref]$ExecutionMode) -settings $settings #check if the scripts folder is specified if ($Folder -ne "") { @@ -62,7 +62,7 @@ } #generate migration file name and path - $fileName = [File]::buildFullName($Name, $settings.File.PrefixFormat, $settings.File.SegmentSeparator) + $fileName = [File]::buildFullName($Name, $settings.File.PrefixFormat, $settings.File.SegmentSeparator, $ExecutionMode) $filePath = Join-Path $scriptsDir $fileName #create migration file @@ -100,7 +100,7 @@ function Add-Migration { Add-DbUpMigration -Name $Name -Folder $Folder -BuildAction $BuildAction -ExecutionMode $ExecutionMode } -function Apply-Settings([ref]$folder, [ref]$buildAction, [Settings]$settings) { +function Apply-Settings([ref]$folder, [ref]$buildAction, [ref]$executionMode, [Settings]$settings) { #overwrite $folder value only if it's not already set if ($folder.Value -eq "") { $folder.Value = $settings.Folder @@ -110,6 +110,11 @@ function Apply-Settings([ref]$folder, [ref]$buildAction, [Settings]$settings) { if ($buildAction.Value -eq [BuildActionType]::None) { $buildAction.Value = [BuildActionType] $settings.BuildAction } + + #overwrite $executionMode value only if it's set to 'None' + if ($executionMode.Value -eq [ExecutionMode]::None) { + $executionMode.Value = [ExecutionMode] $settings.ExecutionMode + } } function Add-MigrationSettings { @@ -216,12 +221,16 @@ class File { return [File]::defaultPrefixFormat } - static [string] buildFullName([string]$MainSegment, [string]$Format, [string]$Separator) { + static [string] buildFullName([string]$MainSegment, [string]$Format, [string]$Separator, [ExecutionMode]$ExecutionMode) { $fullName = Get-Date([System.DateTime]::UtcNow) -Format $Format + + if ($ExecutionMode -ne [ExecutionMode]::None) { + $fullName += $Separator + $ExecutionMode.ToString() + } if ($MainSegment -ne "") { $fullName += $Separator + $MainSegment - } + } return $fullName + ".sql" } From d535a435398c99a4113ab1f186cb2bebee31a855 Mon Sep 17 00:00:00 2001 From: ynaydonov Date: Mon, 24 Feb 2020 17:57:52 +0200 Subject: [PATCH 13/15] Updated readme file and package description --- README.md | 27 +++++++++++++++++++++++---- dbup-add-migration.nuspec | 2 +- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 744886f..44549a3 100644 --- a/README.md +++ b/README.md @@ -9,9 +9,9 @@ - Powershell 5 ## About -This package adds a new command **"Add-Migration"** (and **"Add-DbUpMigration"** alias) to the Package Manager Console. Running the command results in generating an empty sql file with the specified name, which is prefixed by a timestamp in UTC (by default, in a format _yyyyMMddHHmmss_). The auto-generated file (e.g. 20170730185349_MyFirstMigration.sql`) will be added to the project, which you can set by selecting respective item from the dropdown list "Default project" on the top of your package manager console window. +This package adds a new command **"Add-Migration"** (and **"Add-DbUpMigration"** alias) to the Package Manager Console. Running the command results in generating an empty sql file with the specified name, which is prefixed by a timestamp in UTC (by default, in a format `_yyyyMMddHHmmss_`). The auto-generated file (e.g. `20170730185349_MyFirstMigration.sql`) will be added to the project, which you can set by selecting respective item from the dropdown list "Default project" on the top of your package manager console window. -Create a file using default behaviour (the command will decide where to put it, build action will not be set): +Create a file using default behavior (the command will decide where to put it, build action will not be set): Add-Migration "MyFirstMigration" @@ -23,6 +23,10 @@ Set build action when creating a file (use tab for -BuildAction value hints): Add-Migration "MyFirstMigration" -BuildAction EmbeddedResource +Include script execution mode into the file name to support building of `ScriptOptions` and improve integration of script types available for DbUp 4.2 and higher: + + Add-Migration "MyFirstMigration" -ExecutionMode RunAlways + **If you experience issues with `Add-Migration` command being overwritten by the EF migrations' command (which has the same name), please use the alias `Add-DbUpMigration` or prefix the command with package name: `dbup-add-migration\Add-Migration`** --- @@ -39,8 +43,12 @@ When `-BuildAction` parameter is set to _Content_, the command will also set **C --- +When `-ExecutionMode` parameter is not set or equal to `None`, it is ignored and will not be included into the file name + +--- + ## Optional settings file -If you don't want to specify the `-BuildAction` or `-Folder` parameter everytime you add a new migration, you can add an optional settings file to your project by executing the following command: +If you don't want to specify the `-BuildAction`, `-Folder` or `-ExecutionMode` parameter every time you add a new migration, you can add an optional settings file to your project by executing the following command: Add-MigrationSettings @@ -50,6 +58,7 @@ This command will add `dbup-add-migration.json` file to your project (if it does { "folder": "Migrations", "buildAction": "EmbeddedResource", + "executionMode": "RunOnce", "file": { "SegmentSeparator": "_", "PrefixFormat": "yyyyMMddHHmmss" @@ -57,6 +66,8 @@ This command will add `dbup-add-migration.json` file to your project (if it does } ``` +Please, note, that `executionMode` is optional and can be omitted + --- The "buildAction" field should contain one of the following values: @@ -66,7 +77,15 @@ The "buildAction" field should contain one of the following values: - EmbeddedResource --- -However, if you have the settings file in your project and specify the `-BuildAction` or `-Folder` parameters anyway when generating a new migration, they will take precedence over the values in `dbup-add-migration.json` +The "executionMode" field should contain one of the following values: + +- None +- RunOnce +- RunAlways +- RunOnChange +--- + +However, if you have the settings file in your project and specify the `-BuildAction`, `-Folder` or `-ExecutionMode` parameters anyway when generating a new migration, they will take precedence over the values in `dbup-add-migration.json` ## How to install You can install this package from [NuGet](https://www.nuget.org/packages/dbup-add-migration/) diff --git a/dbup-add-migration.nuspec b/dbup-add-migration.nuspec index 5c9c295..00acd14 100644 --- a/dbup-add-migration.nuspec +++ b/dbup-add-migration.nuspec @@ -10,7 +10,7 @@ https://github.com/piotrekh/dbup-add-migration https://raw.githubusercontent.com/DbUp/DbUp/master/dbup-icon.png false - Package manager console command "Add-Migration" that creates an sql file with date and time in UTC (yyyyMMddHHmmss) in the file name (e.g. 20170730185349_Migration.sql) + Package manager console command "Add-Migration" creates an empty sql file with date and time in UTC as a configurable prefix (default format: yyyyMMddHHmmss) in the file name (e.g. 20170730185349_Migration.sql) v1.3.0 - added Add-DbUpMigration alias command Copyright 2017 dbup migrations add-migration sql From a3d8daba7895b2a5cfd85281197f7d5fe74ac101 Mon Sep 17 00:00:00 2001 From: ynaydonov Date: Mon, 24 Feb 2020 18:01:03 +0200 Subject: [PATCH 14/15] Added removal of null or empty properties from generation of dbup-add-migration.json file --- tools/dbup-add-migration.psm1 | 38 ++++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/tools/dbup-add-migration.psm1 b/tools/dbup-add-migration.psm1 index c0b98f9..b951e9a 100644 --- a/tools/dbup-add-migration.psm1 +++ b/tools/dbup-add-migration.psm1 @@ -133,26 +133,53 @@ function Add-MigrationSettings { #getting default file settings $defaultFileSettings = [File]::new() | Select-Object -Property * -ExcludeProperty Name + + if ([Settings]::getDefaultExecutionMode() -ne [ExecutionMode]::None) { + $defaultExecutionMode = [Settings]::getDefaultExecutionMode().ToString() + } + else { + $defaultExecutionMode = $null + } #composing default settings $defaultSettings = [PSCustomObject]@{ folder = [Settings]::getDefaultFolder() buildAction = [Settings]::getDefaultBuildAction() + executionMode = $defaultExecutionMode file = $defaultFileSettings - executionMode = [Settings]::getDefaultExecutionMode() } - #insert default data into the file - $defaultSettings | ConvertTo-Json -Depth 10 | Out-File -FilePath $settingsFilePath + #converting default settings into json-file + $defaultSettings | Remove-NullOrEmpty | ConvertTo-Json -Depth 10 | Out-File -FilePath $settingsFilePath - #add the settings file to the project + #add settings file to the project $item = $project.ProjectItems.AddFromFile($settingsFilePath) - #open the settings file + #open settings file $dte.ItemOperations.OpenFile($settingsFilePath) | Out-Null } } +function Remove-NullOrEmpty { + [cmdletbinding()] + param( + #object to remove null values from + [parameter(ValueFromPipeline, Mandatory)] + [object[]]$InputObject, + #by default, remove empty strings (""); specify -LeaveEmptyStrings to leave them + [switch]$LeaveEmptyStrings + ) + process { + foreach ($obj in $InputObject) { + $AllProperties = $obj.psobject.properties.Name + $NonNulls = $AllProperties | + where-object { $null -ne $obj.$PSItem } | + where-object { $LeaveEmptyStrings.IsPresent -or -not [string]::IsNullOrEmpty($obj.$PSItem) } + $obj | Select-Object -Property $NonNulls + } + } +} + class Settings { [string] $Folder [string] $BuildAction @@ -259,6 +286,7 @@ enum ExecutionMode { None = 0 RunOnce = 1 RunAlways = 2 + RunOnChange = 3 } Export-ModuleMember -Function Add-DbUpMigration, Add-Migration, Add-MigrationSettings \ No newline at end of file From e1e650e16002eaace38133b98d74e173da268169 Mon Sep 17 00:00:00 2001 From: ynaydonov Date: Mon, 24 Feb 2020 18:12:28 +0200 Subject: [PATCH 15/15] Corrected readme file for default settings section --- README.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/README.md b/README.md index 44549a3..34d05ee 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,6 @@ This command will add `dbup-add-migration.json` file to your project (if it does { "folder": "Migrations", "buildAction": "EmbeddedResource", - "executionMode": "RunOnce", "file": { "SegmentSeparator": "_", "PrefixFormat": "yyyyMMddHHmmss" @@ -66,7 +65,7 @@ This command will add `dbup-add-migration.json` file to your project (if it does } ``` -Please, note, that `executionMode` is optional and can be omitted +Please, note, that `executionMode` is optional and can be added if required (e.g. `"executionMode": "RunOnce"`) --- The "buildAction" field should contain one of the following values: @@ -79,7 +78,6 @@ The "buildAction" field should contain one of the following values: The "executionMode" field should contain one of the following values: -- None - RunOnce - RunAlways - RunOnChange