generated from bcgov/quickstart-openshift
-
Notifications
You must be signed in to change notification settings - Fork 1
chore: postgres 17 upgrade #819
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
Merged
Merged
Changes from all commits
Commits
Show all changes
49 commits
Select commit
Hold shift + click to select a range
0f0f037
chore: cleanup file, trigger deployment
DerekRoberts 1c0f1a0
Postgres 17 db with new name and pvc
DerekRoberts 6f02d18
Fix db image
DerekRoberts 4e706a0
Dial back DB template, but still use new PVC
DerekRoberts a9c3383
Script revisions, db cloning
DerekRoberts 2933020
Int handling in template
DerekRoberts 46c1908
Use JSON instead of YAML for templace processing
DerekRoberts 2b4fe9b
Save temporary manifest to /tmp
DerekRoberts 3ea6308
Split out rename logic into rename_deployment.sh
DerekRoberts 6e0c04f
Echo matching deployment after renaming
DerekRoberts 1672e79
Fix deployment grep
DerekRoberts 049ea6c
Pare down dbUpgrade.sh
DerekRoberts bd1e759
rename dbUpgrade.sh to db_transfer.sh after changes
DerekRoberts 0ec4855
Stream dump instead of copy
DerekRoberts 0206fbe
Missing shebang
DerekRoberts 6a95c49
Usage
DerekRoberts 6c64a8b
Transfer with streaming only
DerekRoberts ea4c4f6
Verify containers before transfer
DerekRoberts af7742d
Update deployment labels in rename_deployment.sh
DerekRoberts 1565680
Results message
DerekRoberts 265779c
Exclude postgis-managed tables
DerekRoberts cc83ea5
Use DUMP_PARAMETERS envar to make exclusions clear
DerekRoberts 64197f2
Cleanup
DerekRoberts 357b450
Fail if new deployment already exists in rename_deployment.sh
DerekRoberts b8fc064
Use mktemp for safer tempfiles
DerekRoberts 1d05137
Cleanup and comments
DerekRoberts 4b0f54f
Cleanup and comments
DerekRoberts 8b43033
Use old db name for api template
DerekRoberts 7dadd02
Copilot suggestions/improvements
DerekRoberts d674662
Copilot suggestions/improvements
DerekRoberts 112244d
Consistent quoting
DerekRoberts 10b4da2
Safety check: verify source PVC is older than destination PVC
DerekRoberts 34744aa
Replace OLD and NEW with SOURCE and TARGET
DerekRoberts d104dfc
DUMP_PARAMETERS as string not array
DerekRoberts 1f7a7f9
Remove extra template
DerekRoberts 92c41c0
Dial back db template temporarily
DerekRoberts a09de93
Store old template temporarily
DerekRoberts 6f745c4
Store old template temporarily
DerekRoberts 7f4042d
Restore new template
DerekRoberts ba4f9fb
doc(db): added readme about database migration steps
MCatherine1994 5726c9a
doc(db): update readme for db upgrade steps
MCatherine1994 388cfd7
Update db/rename_deployment.sh
DerekRoberts 37e894f
Update db/README.md
DerekRoberts 3fb758c
rename_object.sh script
DerekRoberts fd98e65
Only follow deployments
DerekRoberts 4e0503c
rename_object.sh renamed oc_rename.sh
DerekRoberts 72ddf00
Remove transient script
DerekRoberts 6a3fca2
Temporarily set db image
DerekRoberts f7c6bbb
Remove image override
DerekRoberts File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| # PostgreSQL Database Major Version Upgrade Steps | ||
|
|
||
| This guide outlines the steps to migrate a database to PostgreSQL v17 in an OpenShift environment. It can be reused for upgrading to other versions. | ||
|
|
||
| ## Prerequisites | ||
| - Create a pr branch before upgrading, update the db/openshift.deployment.yml for DB_VERSION to next version (e.g., 18) | ||
| - Check out the pr branch in local and use the branch terminal for execution steps | ||
|
|
||
| ## Notes | ||
| **Whenever we need to do a database migration, run the scripts first before merge the upgradation pr, as it will use the new PVC name for the new database.** | ||
|
|
||
| ## Steps | ||
|
|
||
| ### 1. Set the Target Environment | ||
|
|
||
| Set the target to a PR number (e.g., `819`), `test`, or `prod`: | ||
|
|
||
| ```bash | ||
| export TARGET=819 | ||
| ``` | ||
|
|
||
| ### 2. Log in to OpenShift via CLI and set the appropriate namespace (`dev`, `test`, or `prod`). | ||
|
|
||
| ### 3. Scale Down the API | ||
|
|
||
| ```bash | ||
| oc scale deployment fom-${TARGET}-api --replicas=0 | ||
| ``` | ||
|
|
||
| ### 4. Rename the Old Database | ||
|
|
||
| Navigate to the `db` directory: `cd db`. The script renames the original database deployment config (e.g., `fom-819-db` to `fom-819-db-prev`). | ||
|
|
||
| > Note: In order to rename the deployment config, we must delete and recreate it because we cannot edit it directly. It creates the database using the same PVC. PVC will not get renamed. | ||
|
|
||
| ```bash | ||
| ./rename_deployment.sh fom-${TARGET}-db | ||
| ``` | ||
|
|
||
| ### 5. Deploy the New PostgreSQL Database (e.g., v17) | ||
|
|
||
| Create a new deployment config (e.g., `fom-819-db`) with PostgreSQL v17 and a new PVC (e.g., `fom-819-db-17`) that has the default data: | ||
|
|
||
| ```bash | ||
| oc process -f openshift.deploy.yml -p ZONE=${TARGET} -p TAG=${TARGET} | oc apply -f - | ||
| ``` | ||
|
|
||
| ### 6. Transfer Data from Old to New Database | ||
|
|
||
| ```bash | ||
| ./db_transfer.sh fom-${TARGET}-db-prev fom-${TARGET}-db | ||
| ``` | ||
|
|
||
| ### 7. Manual Verification | ||
|
|
||
| Connect to the new database and verify: | ||
|
|
||
| ```bash | ||
| psql -U postgres -d fom | ||
| ``` | ||
|
|
||
| Run the following checks: | ||
|
|
||
| ```sql | ||
| -- Check project row count | ||
| SELECT COUNT(1) FROM app_fom.project; | ||
|
|
||
| -- Check installed extensions and versions | ||
| SELECT extname AS extension, extversion AS version | ||
| FROM pg_extension | ||
| ORDER BY extname; | ||
| ``` | ||
|
|
||
| In non-prod environments, add a new FOM and verify project counts again. | ||
|
|
||
| ### 8. Scale Up the API | ||
|
|
||
| ```bash | ||
| oc scale deployment fom-${TARGET}-api --replicas=3 | ||
| ``` | ||
|
|
||
| ## Rollback Procedure | ||
|
|
||
| 1. Scale down the API: | ||
|
|
||
| ```bash | ||
| oc scale deployment fom-${TARGET}-api --replicas=0 | ||
| ``` | ||
|
|
||
| 2. Delete or rename the new database deployment config: | ||
|
|
||
| ```bash | ||
| ./rename_deployment.sh fom-${TARGET}-db fom-${TARGET}-db-upgraded | ||
| ``` | ||
|
|
||
| 3. Restore the old database: | ||
|
|
||
| ```bash | ||
| ./rename_deployment.sh fom-${TARGET}-db-prev fom-${TARGET}-db | ||
| ``` | ||
|
|
||
| 4. Verify the database is running. | ||
|
|
||
| 5. Scale up the API: | ||
|
|
||
| ```bash | ||
| oc scale deployment fom-${TARGET}-api --replicas=3 | ||
| ``` | ||
|
|
||
|
|
||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| #!/bin/bash | ||
| # | ||
| # Database Transfer Script for OpenShift | ||
| # Usage: | ||
| # ./db_transfer.sh <source-deployment-name> <target-deployment-name> | ||
| # | ||
| # This script takes a PostgreSQL database dump from the <source-deployment-name> and restores it into the <target-deployment-name>. | ||
| # Requirements: | ||
| # - Both deployments must have running pods managed by the given deployment names. | ||
DerekRoberts marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| # - The database template should use a PersistentVolumeClaim with a different name for the new deployment. | ||
| # - The new deployment should be ready to accept a restore. | ||
| # - The script assumes the container name is the default or the first in the pod spec. | ||
| # | ||
| # Notes: | ||
| # - If the target database is not empty, you may see errors like "schema ... already exists". | ||
| # These are expected if objects already exist and can usually be ignored, but always review | ||
| # the output for unexpected or critical errors. | ||
|
|
||
| # Strict mode: exit on error, unset vars, or failed pipes | ||
| set -euo pipefail | ||
|
|
||
| # Usage | ||
| if [[ $# -lt 2 ]]; then | ||
| grep -v '^#!' "$0" | awk '/^#/ { sub(/^# ?/, ""); print; next } NF==0 { exit }' | ||
| exit 1 | ||
| fi | ||
|
|
||
| SOURCE_DEPLOYMENT="${1}" | ||
| TARGET_DEPLOYMENT="${2}" | ||
| DUMP_PARAMETERS="${DUMP_PARAMETERS:---exclude-schema=tiger --exclude-schema=tiger_data --exclude-schema=topology}" | ||
DerekRoberts marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| # Fail fast if pods aren't found | ||
| if ! oc get po -l deployment="${SOURCE_DEPLOYMENT}" | grep -q .; then | ||
| echo "No pods found for deployment '${SOURCE_DEPLOYMENT}'." | ||
| exit 2 | ||
| fi | ||
| if ! oc get po -l deployment="${TARGET_DEPLOYMENT}" | grep -q .; then | ||
| echo "No pods found for deployment '${TARGET_DEPLOYMENT}'." | ||
| exit 2 | ||
| fi | ||
|
|
||
| # Safety check: compare PVC ages to prevent accidental reverse transfers | ||
| SOURCE_PVC=$(oc get deployment "${SOURCE_DEPLOYMENT}" -o jsonpath='{.spec.template.spec.volumes[?(@.persistentVolumeClaim)].persistentVolumeClaim.claimName}') | ||
| TARGET_PVC=$(oc get deployment "${TARGET_DEPLOYMENT}" -o jsonpath='{.spec.template.spec.volumes[?(@.persistentVolumeClaim)].persistentVolumeClaim.claimName}') | ||
|
|
||
| if [[ -n "${SOURCE_PVC}" && -n "${TARGET_PVC}" ]]; then | ||
| SOURCE_PVC_CREATION_TIME=$(oc get pvc "${SOURCE_PVC}" -o jsonpath='{.metadata.creationTimestamp}') | ||
| TARGET_PVC_CREATION_TIME=$(oc get pvc "${TARGET_PVC}" -o jsonpath='{.metadata.creationTimestamp}') | ||
| SOURCE_PVC_EPOCH=$(date -d "${SOURCE_PVC_CREATION_TIME}" +%s) | ||
| TARGET_PVC_EPOCH=$(date -d "${TARGET_PVC_CREATION_TIME}" +%s) | ||
|
|
||
| if [[ ${SOURCE_PVC_EPOCH} -gt ${TARGET_PVC_EPOCH} ]]; then | ||
| echo "WARNING: Source PVC '${SOURCE_PVC}' ($(date -d "${SOURCE_PVC_CREATION_TIME}" '+%Y-%m-%d %H:%M')) is NEWER than target PVC '${TARGET_PVC}' ($(date -d "${TARGET_PVC_CREATION_TIME}" '+%Y-%m-%d %H:%M'))." | ||
| echo "This may be a reverse transfer that could overwrite newer data with older data." | ||
| echo -n "Are you sure you want to continue? (yes/no): " | ||
| read -r CONFIRM | ||
| if [[ "${CONFIRM}" != "yes" ]]; then | ||
| echo "Transfer cancelled by user." | ||
| exit 2 | ||
| fi | ||
| else | ||
| echo "Safety check passed: Source PVC is older than target PVC." | ||
| fi | ||
| else | ||
| echo "Warning: Could not find PVCs for comparison. Proceeding without age check." | ||
| fi | ||
|
|
||
| # Stream dump directly from old deployment to new deployment | ||
| echo -e "\nDatabase transfer from '${SOURCE_DEPLOYMENT}' to '${TARGET_DEPLOYMENT}' beginning." | ||
| oc exec -i deployment/"${SOURCE_DEPLOYMENT}" -- bash -c "pg_dump -U \${POSTGRES_USER} -d \${POSTGRES_DB} -Fc ${DUMP_PARAMETERS}" \ | ||
DerekRoberts marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| | oc exec -i deployment/"${TARGET_DEPLOYMENT}" -- bash -c "pg_restore -U \${POSTGRES_USER} -d \${POSTGRES_DB} -Fc" | ||
DerekRoberts marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| # Results | ||
| echo -e "\nDatabase transfer from '${SOURCE_DEPLOYMENT}' to '${TARGET_DEPLOYMENT}' complete." | ||
DerekRoberts marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.