Skip to content
Merged
Show file tree
Hide file tree
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 Jun 26, 2025
1c0f1a0
Postgres 17 db with new name and pvc
DerekRoberts Jun 26, 2025
6f02d18
Fix db image
DerekRoberts Jun 26, 2025
4e706a0
Dial back DB template, but still use new PVC
DerekRoberts Jun 30, 2025
a9c3383
Script revisions, db cloning
DerekRoberts Jun 30, 2025
2933020
Int handling in template
DerekRoberts Jun 30, 2025
46c1908
Use JSON instead of YAML for templace processing
DerekRoberts Jun 30, 2025
2b4fe9b
Save temporary manifest to /tmp
DerekRoberts Jun 30, 2025
3ea6308
Split out rename logic into rename_deployment.sh
DerekRoberts Jun 30, 2025
6e0c04f
Echo matching deployment after renaming
DerekRoberts Jun 30, 2025
1672e79
Fix deployment grep
DerekRoberts Jun 30, 2025
049ea6c
Pare down dbUpgrade.sh
DerekRoberts Jun 30, 2025
bd1e759
rename dbUpgrade.sh to db_transfer.sh after changes
DerekRoberts Jun 30, 2025
0ec4855
Stream dump instead of copy
DerekRoberts Jun 30, 2025
0206fbe
Missing shebang
DerekRoberts Jun 30, 2025
6a95c49
Usage
DerekRoberts Jun 30, 2025
6c64a8b
Transfer with streaming only
DerekRoberts Jun 30, 2025
ea4c4f6
Verify containers before transfer
DerekRoberts Jun 30, 2025
af7742d
Update deployment labels in rename_deployment.sh
DerekRoberts Jul 1, 2025
1565680
Results message
DerekRoberts Jul 1, 2025
265779c
Exclude postgis-managed tables
DerekRoberts Jul 1, 2025
cc83ea5
Use DUMP_PARAMETERS envar to make exclusions clear
DerekRoberts Jul 1, 2025
64197f2
Cleanup
DerekRoberts Jul 1, 2025
357b450
Fail if new deployment already exists in rename_deployment.sh
DerekRoberts Jul 1, 2025
b8fc064
Use mktemp for safer tempfiles
DerekRoberts Jul 1, 2025
1d05137
Cleanup and comments
DerekRoberts Jul 1, 2025
4b0f54f
Cleanup and comments
DerekRoberts Jul 1, 2025
8b43033
Use old db name for api template
DerekRoberts Jul 1, 2025
7dadd02
Copilot suggestions/improvements
DerekRoberts Jul 2, 2025
d674662
Copilot suggestions/improvements
DerekRoberts Jul 2, 2025
112244d
Consistent quoting
DerekRoberts Jul 3, 2025
10b4da2
Safety check: verify source PVC is older than destination PVC
DerekRoberts Jul 3, 2025
34744aa
Replace OLD and NEW with SOURCE and TARGET
DerekRoberts Jul 3, 2025
d104dfc
DUMP_PARAMETERS as string not array
DerekRoberts Jul 3, 2025
1f7a7f9
Remove extra template
DerekRoberts Jul 11, 2025
92c41c0
Dial back db template temporarily
DerekRoberts Jul 11, 2025
a09de93
Store old template temporarily
DerekRoberts Jul 11, 2025
6f745c4
Store old template temporarily
DerekRoberts Jul 11, 2025
7f4042d
Restore new template
DerekRoberts Jul 11, 2025
ba4f9fb
doc(db): added readme about database migration steps
MCatherine1994 Jul 15, 2025
5726c9a
doc(db): update readme for db upgrade steps
MCatherine1994 Jul 16, 2025
388cfd7
Update db/rename_deployment.sh
DerekRoberts Jul 29, 2025
37e894f
Update db/README.md
DerekRoberts Jul 29, 2025
3fb758c
rename_object.sh script
DerekRoberts Jul 29, 2025
fd98e65
Only follow deployments
DerekRoberts Jul 29, 2025
4e0503c
rename_object.sh renamed oc_rename.sh
DerekRoberts Jul 29, 2025
72ddf00
Remove transient script
DerekRoberts Jul 29, 2025
6a3fca2
Temporarily set db image
DerekRoberts Jul 31, 2025
f7c6bbb
Remove image override
DerekRoberts Aug 6, 2025
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 db/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM postgis/postgis:13-master
FROM postgis/postgis:17-master

# Enable pgcrypto extension on startup
RUN sed -i '/EXISTS postgis_tiger_geocoder;*/a CREATE EXTENSION IF NOT EXISTS pgcrypto;' \
Expand Down
11 changes: 0 additions & 11 deletions db/Dockerfile-v17

This file was deleted.

112 changes: 112 additions & 0 deletions db/README.md
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
```




74 changes: 74 additions & 0 deletions db/db_transfer.sh
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.
# - 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}"

# 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}" \
| oc exec -i deployment/"${TARGET_DEPLOYMENT}" -- bash -c "pg_restore -U \${POSTGRES_USER} -d \${POSTGRES_DB} -Fc"

# Results
echo -e "\nDatabase transfer from '${SOURCE_DEPLOYMENT}' to '${TARGET_DEPLOYMENT}' complete."
186 changes: 0 additions & 186 deletions db/openshift.deploy.dbupgrade.v17.yml

This file was deleted.

Loading
Loading