50 lines
2.8 KiB
Markdown
50 lines
2.8 KiB
Markdown
# Subscription plan cleanup before migration 000110
|
|
|
|
This is a separate, one-time data repair, not an automatic migration. The reviewed
|
|
backup contains duplicate LEARN_ENGLISH / non-lifetime / 1 MONTH plans: active
|
|
plan 2 (ETB 300), inactive plan 9 (ETB 1), and inactive plan 14 (ETB 0).
|
|
The repair keeps plan 2 and consolidates plans 9 and 14 into it.
|
|
|
|
The backup has 86 affected payments and 85 affected subscriptions, including three
|
|
subscriptions marked ACTIVE on plan 9. Payment amounts, currency, status,
|
|
subscription dates, and all other reference-row fields are preserved. Screens
|
|
joining to the current plan will show plan 2's name and ETB 300 price. This is a
|
|
product/history change that must be accepted before execution. Old plan IDs in
|
|
external links will no longer resolve. The full original rows and mapping remain
|
|
in the restricted `plan_cleanup_000110` schema.
|
|
|
|
## Execution
|
|
|
|
1. Take a fresh database backup and test this repair on an isolated restore.
|
|
2. Pause application writes and migration startup. Use a database owner session
|
|
against the intended database at migration 109, before applying 110 or 111.
|
|
3. Execute with psql, stopping on any error:
|
|
|
|
```powershell
|
|
psql "$env:DB_URL" -X -v ON_ERROR_STOP=1 -c "SET yimaru.approve_plan_consolidation = 'yes'" -f db/maintenance/consolidate_subscription_plans_before_000110.sql
|
|
```
|
|
|
|
4. Apply migrations 110 and 111 through the normal migration runner. The repair
|
|
already creates the exact unique index, preventing new duplicates in the gap.
|
|
5. Check subscription and payment displays before resuming application traffic.
|
|
|
|
The transaction locks all three affected tables, archives full original rows,
|
|
updates only `plan_id`, removes duplicate plan rows, and validates uniqueness and
|
|
unchanged historical fields before commit. Unknown foreign keys, changed plan
|
|
attributes, in-flight payments, remaining duplicates, or an existing archive/index
|
|
abort the transaction. Do not bypass these checks without reviewing the new data.
|
|
Do not force migration versions if a prior migration failed without inspecting
|
|
the schema and migration state first.
|
|
|
|
## Recovery
|
|
|
|
The original dump remains unchanged. Prefer restoring the fresh backup into an
|
|
isolated database to verify recovery. For an immediate reversal while writes are
|
|
still paused: roll back migrations 111/110 if applied, remove the unique index
|
|
if still present, reinsert archived plans 9 and 14 using explicit column lists,
|
|
then restore only the archived reference rows' original `plan_id` values by ID,
|
|
all in one transaction. Keep the archive until recovery is verified. Do not
|
|
overwrite complete live payment/subscription rows from the archive: their status
|
|
may have changed after cleanup. Recovery after traffic resumes requires a fresh
|
|
comparison of live and archived rows and cannot safely be automated blindly.
|