chore: add scripts to scrub and import partial db dumps - #2110
Conversation
|
wow. Will try out these locally |
There was a problem hiding this comment.
Pull request overview
This PR adds two developer scripts to help (1) scrub sensitive fields from partial Postgres COPY-TEXT dumps under db/dump, and (2) import those dumps into a local Postgres instance, including optional Flyway-version targeting/auto-detection.
Changes:
- Added
scripts/scrub-db-dump.shto spin up a scratch Postgres, import selected dump tables, redact sensitive columns, and write the redacted data back to the dump directory. - Added
scripts/import-db-dump.shto load the dump into a target Postgres in a single transaction, including schema-version auto-detection and sequence fixups.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
| scripts/scrub-db-dump.sh | New scrubber script: detects dump-era schema, imports key tables into scratch DB, redacts sensitive fields, and replaces dump CSVs. |
| scripts/import-db-dump.sh | New importer script: detects dump-era column layout via scratch DB, migrates/validates target state, deletes + reloads dump tables transactionally, and resets sequences. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| UPDATE personal_access_token | ||
| SET value = encode(sha256(('redacted-pat-' || id)::bytea), 'hex'); |
| UPDATE user_data | ||
| SET email = 'user' || id || '@example.invalid', | ||
| full_name = 'Redacted User ' || id, | ||
| avatar_url = NULL, | ||
| eclipse_token = NULL, | ||
| eclipse_person_id = NULL; |
| # rather than from custom bookkeeping. | ||
| mapfile -t VERSIONS < <( | ||
| find "${MIGRATIONS_DIR}" -maxdepth 1 -name 'V*__*' -printf '%f\n' \ | ||
| | sed -E 's/^V([0-9_]+)__.*/\1/; s/_/./' \ |
|
|
||
| mapfile -t VERSIONS < <( | ||
| find "${MIGRATIONS_DIR}" -maxdepth 1 -name 'V*__*' -printf '%f\n' \ | ||
| | sed -E 's/^V([0-9_]+)__.*/\1/; s/_/./' \ |
| for t in "${TABLES[@]}"; do | ||
| IFS=',' read -r -a dump_cols <<< "${COLUMN_LISTS[${t}]}" | ||
| gap_cols=$(target_psql -t -c " | ||
| select column_name from information_schema.columns | ||
| where table_schema='public' and table_name='${t}' | ||
| and is_nullable='NO' and column_default is null | ||
| and column_name not in ($(printf "'%s'," "${dump_cols[@]}" | sed 's/,$//')); | ||
| " | tr -d ' ' | grep -v '^$' || true) | ||
| while IFS= read -r col; do | ||
| [ -z "${col}" ] && continue | ||
| key="${t}.${col}" | ||
| GAP_COLUMNS["${key}"]=1 | ||
| if [ -z "${KNOWN_BACKFILLS[${key}]:-}" ]; then | ||
| UNKNOWN_GAPS+=("${key}") | ||
| else | ||
| echo " ${key}: no value in the dump, not nullable, no default - will backfill (known)" | ||
| fi | ||
| done <<< "${gap_cols}" | ||
| done |
|
I did use the scripts in the following way:
After that run the local server with a later migration version, which will then automatically migrate to that version. That also allows to verify that the migration e.g. on a production DB will work as expected. |
This PR adds 2 scripts to easily import partial db dumps:
scripts/scrub-db-dump.shscripts/import-db-dump.shthe first script scrubs all sensitive and PII information from a dump located in
db/dump.the second script imports the dump in the some location to a locally running postgresql instance
both scripts support a
-tparameter to indicate the target db migration version.