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

Simplify scripts to use powershell switch args over booleans. #27

Merged
merged 2 commits into from
May 22, 2024
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 .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:

- name: Check lint
run: |
& .\format.ps1 -verbose_output $true
& .\format.ps1 -verbose_output
$git_diff = (git diff --stat -- "*.sql")
if ($git_diff -ne $null) {
Write-Host "Code formatting issues found! Please run the format.ps1 script, commit the changes and push."
Expand Down
14 changes: 8 additions & 6 deletions export.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,20 @@
param (
# change server_name if you installed your sql server as a Named Instance.
# If you installed on the Default Instance, then you can leave this as-is.
# If you're still not sure what is your sql server names, you can run the following powershell command:
# (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server').InstalledInstances
[string][Parameter(Mandatory = $false)]
$server_name = "localhost",

[string][Parameter(Mandatory = $false)]
$db_name = "kodb",

# skip code formatting and linting
[switch][Parameter(Mandatory = $false)]
$skip_format,

[bool][Parameter(Mandatory = $false)]
$apply_format = $true,

[bool][Parameter(Mandatory = $false)]
$quiet = $false
[switch][Parameter(Mandatory = $false)]
$quiet
)

. "$PSScriptRoot\logger.ps1"
Expand Down Expand Up @@ -95,7 +97,7 @@ function Main {

Remove-Item tmp -Recurse

if ($apply_format) {
if (-not $skip_format) {
# Note that we intentionally don't format schema and data, because they're not written
# by the user and also because it takes a while to format big data.
sqlfluff format `
Expand Down
4 changes: 2 additions & 2 deletions format.ps1
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Check the .sqlfluff config file for more info
param (
[bool][Parameter(Mandatory = $false)]
$verbose_output = $false
[switch][Parameter(Mandatory = $false)]
$verbose_output
)

$dirs_to_format = @(
Expand Down
27 changes: 14 additions & 13 deletions import.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,24 @@
param (
# change server_name if you installed your sql server as a Named Instance.
# If you installed on the Default Instance, then you can leave this as-is.
# If you're still not sure what is your sql server names, you can run the following powershell command:
# (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server').InstalledInstances
[string][Parameter(Mandatory = $false)]
$server_name = "localhost",

[string][Parameter(Mandatory = $false)]
$db_name = "kodb",

# Set this flag to false if you want to skip migration scripts
[bool][Parameter(Mandatory = $false)]
$run_migration_scripts = $true,
[switch][Parameter(Mandatory = $false)]
$skip_migration_scripts,

# Set this flag to true if you're creating a db release or want to see the current diffs of each migration script
# Generate diffs for each migration script that is not archived.
# Warning: make sure to commit your changes before running the script with this enabled, or you may lose work
[bool][Parameter(Mandatory = $false)]
$generate_diffs = $false,
[switch][Parameter(Mandatory = $false)]
$generate_diffs,

[bool][Parameter(Mandatory = $false)]
$quiet = $false
[switch][Parameter(Mandatory = $false)]
$quiet
)

. "$PSScriptRoot\logger.ps1"
Expand Down Expand Up @@ -114,15 +115,15 @@ function RunMigrationScriptsAndGenerateDiffs {
foreach ($script In $scripts) {
Message $script.FullName
InvokeSqlScript -script_path $script.FullName
.\export.ps1 -server_name $server_name -db_name $db_name -apply_format $false -quiet $true
.\export.ps1 -server_name $server_name -db_name $db_name -skip_format -quiet
git add $targetDirs
$diffOutputFile = $script.FullName + ".diff"
# Note that powershell messes up with the output and corrupts the patch, hence we use cmd here.
cmd.exe /c "git diff --staged $targetDirs > $diffOutputFile"
git commit -m $tempUniqueCommitMessage
}
# Note that we're intentionally doing it this way, to be sure that we're not deleting commits we shouldn't
$commits = git rev-list --grep="$tempUniqueCommitMessage" --reverse HEAD
$commits = @(git rev-list --grep="$tempUniqueCommitMessage" --reverse HEAD)
if ($commits.Count) {
git reset --hard "$($commits[0])^1"
}
Expand All @@ -147,14 +148,14 @@ function Main {
RecreateDb -server_instance $server
RunInitialDbScripts

if ($run_migration_scripts) {
if ($skip_migration_scripts) {
MessageInfo "Skipping migration scripts..."
} else {
if ($generate_diffs) {
RunMigrationScriptsAndGenerateDiffs
} else {
RunMigrationScripts
}
} else {
MessageInfo "Skipping migration scripts..."
}

CreateDbCredentials
Expand Down
4 changes: 2 additions & 2 deletions src/migration/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Add here scripts that alters the existing database's schema, procedures, data an
Couple of rules and notes when writing migration scripts:
- Always assume the database containing data, even if you just modify an empty table
- When altering the schema, adding comments (`--` for tsql) are encouraged
- When submitting a PR, please generate a `*.diff` file and commit it (`.\import.ps1 -generate_diffs $true`)
- When submitting a PR, please generate a `*.diff` file and commit it (`.\import.ps1 -generate_diffs`)
- Every migration script should start with max 4 leading zeros (example `0001_insert_user.sql`)

## Creating a new release
Expand All @@ -16,7 +16,7 @@ Below are instructions for the release engineer in order to create a new db rele
- Run the export script, to be sure that no diff is produced (`.\export.ps1` and then `git status`)
- If there are local changes, something is probably off. Repeat the steps above
- If you're sure all in order, best is if you create a new separate PR with the changes, in case empty spaces and such were added
- Run the import script again with the migration scripts and produce diffs (`.\import.ps1 -generate_diffs $true`)
- Run the import script again with the migration scripts and produce diffs (`.\import.ps1 -generate_diffs`)
- Move all migration scripts and its `*.diff` files to the `archived` directory
- Lastly run the export script once more, but this time you'll have the actual changes from the migration scripts affecting the actual schema
- Git commit all the changes (`Bump version from 1.0.0 to 1.0.1.`)
Expand Down