11# Set parameters
2+ $overWriteProject = $false # Set to true to delete and recreate the project folder. Can be useful when configuring from scratch.
23$databaseType = " SqlServer" # alt values: SqlServer Oracle PostgreSql MySql
34# connection string to prodlike database
45# This POC will create a dev, test, shadow DB - this name will be in the root of all subsequent names
@@ -13,7 +14,7 @@ $projectName = "$baseDBName"
1314# This can be explicit
1415$projectPath = " ."
1516# Backup as Baseline path - must be accessible by DB server - leave empty if not needed
16- $backupPath = " C:\\Program Files\\Microsoft SQL Server\\MSSQL13.MSSQLSERVER\\MSSQL\\Backup\\Northwind .bak" # eg C:\\Program Files\\Microsoft SQL Server\\MSSQL13.MSSQLSERVER\\MSSQL\\Backup\\Northwind.bak
17+ $backupPath = " C:\\Program Files\\Microsoft SQL Server\\MSSQL13.MSSQLSERVER\\MSSQL\\Backup\\NewWorldDB_Dev .bak" # eg C:\\Program Files\\Microsoft SQL Server\\MSSQL13.MSSQLSERVER\\MSSQL\\Backup\\Northwind.bak
1718
1819# Set the schemas value
1920$Schemas = @ (" " ) # can be empty for SqlServer
@@ -26,61 +27,67 @@ $testUrl = ""
2627$shadowUrl = " "
2728
2829# Initialize project - create folders and flyway.toml - delete existing project folder if exists
29- if (Test-Path - Path " $projectPath \$projectName " ) {
30- Remove-Item - Path $projectName - Recurse - Force
31- }
32- cd $projectPath
33- mkdir $projectName
34- cd ./ $projectName
35- flyway init " -init.projectName=$projectName " " -init.databaseType=$databaseType "
36-
37- if ($backupPath -ne " " ) {
38- # Add shadow environment to flyway.toml
39- $ShadowDatabaseName = $baseDBName + ' _${env.UserName}_shadow'
40- $shadowUrl = $baselineSourceIfNoBackup -replace " databaseName=[^;]*" , " databaseName=$ShadowDatabaseName "
41- (Add-Content - Path " flyway.toml" `
42- - Value " `n`n [environments.shadow]`n url = `" $shadowUrl `"`n provisioner = `" backup`"`n`n [environments.shadow.resolvers.backup]`n backupFilePath = `" $backupPath `"`n backupVersion = `" 000`"`n`n [environments.shadow.resolvers.backup.sqlserver]`n generateWithMove = true"
43- )
44- }
30+ # For project setup, it can be helpful to overwrite as configuration may change. Set $overWriteProject = $true to force recreation
31+ if ($overWriteProject -or -not (Test-Path - Path " $projectPath \$projectName " )) {
32+ if (Test-Path - Path " $projectPath \$projectName " ) {
33+ Remove-Item - Path $projectName - Recurse - Force
34+ }
35+ Set-Location $projectPath
36+ mkdir $projectName
37+ Set-Location ./ $projectName
38+ flyway init " -init.projectName=$projectName " " -init.databaseType=$databaseType "
39+
40+ if ($backupPath -ne " " ) {
41+ # Add shadow environment to flyway.toml
42+ $ShadowDatabaseName = $baseDBName + ' _${env.UserName}_shadow'
43+ $shadowUrl = $baselineSourceIfNoBackup -replace " databaseName=[^;]*" , " databaseName=$ShadowDatabaseName "
44+ (Add-Content - Path " flyway.toml" `
45+ - Value " `n`n [environments.shadow]`n url = `" $shadowUrl `"`n provisioner = `" backup`"`n`n [environments.shadow.resolvers.backup]`n backupFilePath = `" $backupPath `"`n backupVersion = `" 000`"`n`n [environments.shadow.resolvers.backup.sqlserver]`n generateWithMove = true"
46+ )
47+ }
4548
46- <# # Modify flyway.toml to adjust comparison options
47- (Get-Content -Path "flyway.toml") `
48- -replace 'ignorePermissions\s*=\s*false', 'ignorePermissions = true' `
49- -replace 'ignoreUsersPermissionsAndRoleMemberships\s*=\s*false', 'ignoreUsersPermissionsAndRoleMemberships = true' `
50- -replace 'includeDependencies\s*=\s*true', 'includeDependencies = false' |
51- Set-Content -Path "flyway.toml"
52-
53- # Define the file path
54- $filePath = "Filter.scpf"
55-
56- # Check if the file exists
57- if (Test-Path -Path $filePath) {
58- # Load the XML file
59- $xml = [xml](Get-Content -Path $filePath)
60-
61- # Find the <None> element and update its child nodes
62- $noneElement = $xml.NamedFilter.Filter.Filters.None
63- $noneElement.Include = "False"
64- $noneElement.Expression = "((@SCHEMA LIKE 'cdc%'))"
65-
66-
67- $currentPath = Get-Location
68- $filePath = Join-Path -Path $currentPath -ChildPath $filePath
69- # Save the updated XML back to the file
70- $xml.Save($filePath)
71- Write-Host "Filter.scpf updated successfully."
72- } else {
73- Write-Host "Filter.scpf does not exist."
74- }
75- #>
49+ <# # Modify flyway.toml to adjust comparison options
50+ (Get-Content -Path "flyway.toml") `
51+ -replace 'ignorePermissions\s*=\s*false', 'ignorePermissions = true' `
52+ -replace 'ignoreUsersPermissionsAndRoleMemberships\s*=\s*false', 'ignoreUsersPermissionsAndRoleMemberships = true' `
53+ -replace 'includeDependencies\s*=\s*true', 'includeDependencies = false' |
54+ Set-Content -Path "flyway.toml"
55+
56+ # Define the file path
57+ $filePath = "Filter.scpf"
58+
59+ # Check if the file exists
60+ if (Test-Path -Path $filePath) {
61+ # Load the XML file
62+ $xml = [xml](Get-Content -Path $filePath)
63+
64+ # Find the <None> element and update its child nodes
65+ $noneElement = $xml.NamedFilter.Filter.Filters.None
66+ $noneElement.Include = "False"
67+ $noneElement.Expression = "((@SCHEMA LIKE 'cdc%'))"
68+
69+
70+ $currentPath = Get-Location
71+ $filePath = Join-Path -Path $currentPath -ChildPath $filePath
72+ # Save the updated XML back to the file
73+ $xml.Save($filePath)
74+ Write-Host "Filter.scpf updated successfully."
75+ } else {
76+ Write-Host "Filter.scpf does not exist."
77+ }
78+ #>
7679
77- # Populate SchemaModel from dev database or from backup
78- if ($backupPath -eq " " ) {
79- flyway diff model " -diff.source=dev" " -diff.target=schemaModel" " -environments.dev.url=$baselineSourceIfNoBackup " " -environments.dev.user=$User " " -environments.dev.password=$Password " " -environments.dev.schemas=$Schemas " 2>&1 | Where-Object { $_ -notmatch ' Database: jdbc:' -and $_ -notmatch ' ERROR: Skipping filesystem location:' }
80- flyway diff generate " -diff.source=schemaModel" " -diff.target=empty" " -generate.types=baseline" " -generate.description=Baseline" " -generate.version=1.0" 2>&1 | Where-Object { $_ -notmatch ' Database: jdbc:' -and $_ -notmatch ' ERROR: Skipping filesystem location:' }
80+ # Populate SchemaModel from dev database or from backup
81+ if ($backupPath -eq " " ) {
82+ flyway diff model " -diff.source=dev" " -diff.target=schemaModel" " -environments.dev.url=$baselineSourceIfNoBackup " " -environments.dev.user=$User " " -environments.dev.password=$Password " " -environments.dev.schemas=$Schemas " 2>&1 | Where-Object { $_ -notmatch ' Database: jdbc:' -and $_ -notmatch ' ERROR: Skipping filesystem location:' }
83+ flyway diff generate " -diff.source=schemaModel" " -diff.target=empty" " -generate.types=baseline" " -generate.description=Baseline" " -generate.version=1.0" 2>&1 | Where-Object { $_ -notmatch ' Database: jdbc:' -and $_ -notmatch ' ERROR: Skipping filesystem location:' }
84+ } else {
85+ Write-Output " Restoring provided backup file to server URL and Populating SchemaModel from it"
86+ flyway diff model " -diff.source=migrations" " -diff.target=schemaModel" " -diff.buildEnvironment=shadow" 2>&1 | Where-Object { $_ -notmatch ' Database: jdbc:' -and $_ -notmatch ' ERROR: Skipping filesystem location:' }
87+ }
8188} else {
82- Write-Output " Restoring provided backup file to server URL and Populating SchemaModel from it "
83- flyway diff model " -diff.source=migrations " " -diff.target=schemaModel " " -diff.buildEnvironment=shadow " 2>&1 | Where-Object { $_ -notmatch ' Database: jdbc: ' -and $_ -notmatch ' ERROR: Skipping filesystem location: ' }
89+ Write-Host " Skipping project initialization. Set \ $overWriteProject = \ $true to recreate the project. " - ForegroundColor Yellow
90+ Set-Location " $projectPath \ $projectName "
8491}
8592
8693$pocSetupMessage = " `n YDo you want to set up a full POC environment with dev and test databases?"
@@ -90,7 +97,7 @@ if ($backupPath -ne "") {
9097 $pocSetupMessage += " Provided database is assumed as test and new dev database WILL BE CREATED (Y/N)?"
9198}
9299
93- # Interactive prompt for full POC environment setup
100+ # Interactive prompt for full POC environment setup - DB and TOML setup
94101$response = Read-Host $pocSetupMessage
95102if ($response -eq " Y" -or $response -eq " y" ) {
96103 Write-Host " Setting up full POC environment..."
@@ -113,9 +120,9 @@ if ($response -eq "Y" -or $response -eq "y") {
113120 - Value " `n`n [environments.test]`n url = `" $testUrl `"`n provisioner = `" backup`"`n`n [environments.test.resolvers.backup]`n backupFilePath = `" $backupPath `"`n backupVersion = `" 000`"`n`n [environments.test.resolvers.backup.sqlserver]`n generateWithMove = true"
114121 )
115122 Write-Host " Restoring backup file to create dev database $devDBName "
116- flyway migrate - environment= development 2>&1 | Where-Object { $_ -notmatch ' Database: jdbc:' -and $_ -notmatch ' ERROR: Skipping filesystem location:' }
123+ flyway migrate repair - environment= development 2>&1 | Where-Object { $_ -notmatch ' Database: jdbc:' -and $_ -notmatch ' ERROR: Skipping filesystem location:' }
117124 Write-Host " Restoring backup file to create test database $testDatabaseName "
118- flyway migrate - environment= test 2>&1 | Where-Object { $_ -notmatch ' Database: jdbc:' -and $_ -notmatch ' ERROR: Skipping filesystem location:' }
125+ flyway migrate repair - environment= test 2>&1 | Where-Object { $_ -notmatch ' Database: jdbc:' -and $_ -notmatch ' ERROR: Skipping filesystem location:' }
119126 }
120127 Write-Host " POC environment setup completed."
121128 $skipped = $false
@@ -141,7 +148,7 @@ Write-Host "`n========================================" -ForegroundColor Cyan
141148Write-Host " STEP: Make Database Changes" - ForegroundColor Yellow - BackgroundColor DarkBlue
142149Write-Host " ========================================" - ForegroundColor Cyan
143150Write-Host " Make some changes to the dev DB ($devUrl ) for Flyway to capture and generate a deployment script." - ForegroundColor White
144- Read-Host " Press Enter when done "
151+ Read-Host " Press Enter when ready to capture your changes in the schema model and in deployment and undo scripts "
145152
146153# the Description of the migration script generated - should probably include feature/ticket information
147154$scriptName = " Jira123_YourDescription"
@@ -155,9 +162,9 @@ Write-Host "`n========================================" -ForegroundColor Cyan
155162Write-Host " DEPLOYMENT: Deploy to Test Database ($testUrl )" - ForegroundColor Yellow - BackgroundColor DarkBlue
156163Write-Host " ========================================" - ForegroundColor Cyan
157164Write-Host " You are about to deploy changes to the test DB." - ForegroundColor White
158- Read-Host " Press Enter to proceed "
165+ Read-Host " Press Enter to run flyway migrate -workingDirectory= $projectPath -environment=test -saveSnapshot=true "
159166
160- flyway migrate - workingDirectory= " $projectPath " - environment= test 2>&1 | Where-Object { $_ -notmatch ' Database: jdbc:' -and $_ -notmatch ' ERROR: Skipping filesystem location:' }
167+ flyway migrate - workingDirectory= " $projectPath " - environment= test - saveSnapshot = true 2>&1 | Where-Object { $_ -notmatch ' Database: jdbc:' -and $_ -notmatch ' ERROR: Skipping filesystem location:' }
161168
162169Write-Host " `n ========================================" - ForegroundColor Cyan
163170Write-Host " REVIEW: Check Deployment Results" - ForegroundColor Yellow - BackgroundColor DarkBlue
@@ -172,7 +179,7 @@ Write-Host "`n========================================" -ForegroundColor Cyan
172179Write-Host " INFO: View Deployment History" - ForegroundColor Yellow - BackgroundColor DarkBlue
173180Write-Host " ========================================" - ForegroundColor Cyan
174181Write-Host " You can run 'flyway info' from the command line to see the deployment history." - ForegroundColor White
175- Read-Host " Press Enter to run flyway info"
182+ Read-Host " Press Enter to run flyway info -workingDirectory= $projectPath -environment=test "
176183
177184flyway info - workingDirectory= " $projectPath " - environment= test 2>&1 | Where-Object { $_ -notmatch ' Database: jdbc:' -and $_ -notmatch ' ERROR: Skipping filesystem location:' }
178185
@@ -183,7 +190,11 @@ Write-Host "Flyway can detect drift in your databases." -ForegroundColor White
183190Write-Host " Learn more at: " - ForegroundColor White - NoNewline
184191Write-Host " Drift Detection Tutorial" - ForegroundColor Cyan
185192Write-Host " https://documentation.red-gate.com/fd/tutorial-drift-report-for-deployments-using-embedded-snapshot-317493682.html" - ForegroundColor Blue
186- Read-Host " Press Enter to continue"
193+ Read-Host " For drift to show up, you need to manually make a change to a target database outside of using Flyway. Make a schema change manually to $testUrl ."
194+ Read-Host " Press Enter to execute flyway check -drift -code -dryrun -environment=$test -check.code.failOnError=false -check.failOnDrift=false -check.deployedSnapshot=snapshothistory:current -workingDirectory=$projectPath "
195+
196+
197+ flyway check - drift - code - dryrun - environment= $test - check.code.failOnError= false - check.failOnDrift= false - check.deployedSnapshot= snapshothistory:current - workingDirectory= " $projectPath "
187198
188199
189200# # Variables to be changed by user
@@ -236,4 +247,4 @@ Read-Host "Press Enter to continue"
236247# Read-Host "Press Enter to run snapshot command"
237248# flyway snapshot -environment=$environment -filename=snapshothistory:current -configFiles="$configFiles" -workingDirectory="$workingDirectory"
238249
239- cd ..
250+ Set-Location ..
0 commit comments