mirror of
https://github.com/block/buzz.git
synced 2026-08-18 06:50:31 +02:00
+1








14fba21e57
Signed-off-by: tlongwell-block <109685178+tlongwell-block@users.noreply.github.com> Signed-off-by: npub1jh9wn95s0472h86ahapupaf7m6kx4v9sx2n0atj2hltcfer8k06s5n3pyf <95cae996907d7cab9f5dbf43c0f53edeac6ab0b032a6feae4abfd784e467b3f5@sprout-oss.stage.blox.sqprod.co> Signed-off-by: Tyler Longwell <tlongwell@block.xyz> Signed-off-by: npub1t2tgm7d8f995uqvmnm8h88sg3wnpp9a5xysjf6dg3tjmgt3ltulqdp8ehr <5a968df9a7494b4e019b9ecf739e088ba61097b4312124e9a88ae5b42e3f5f3e@sprout-oss.stage.blox.sqprod.co> Signed-off-by: npub17jjz49l9jjmhhk7cac63j8yt9z555n9cw8vk7v5jz4vzw4ppld5qgj57cc <f4a42a97e594b77bdbd8ee35191c8b28a94a4cb871d96f32921558275421fb68@sprout-oss.stage.blox.sqprod.co> Co-authored-by: Eva <011987e296fd5006292d2f930b574be47c7801048d1983c46c425d3c95f0cffd@sprout-oss.stage.blox.sqprod.co> Co-authored-by: Mari <95cae996907d7cab9f5dbf43c0f53edeac6ab0b032a6feae4abfd784e467b3f5@sprout-oss.stage.blox.sqprod.co> Co-authored-by: Sami <f4a42a97e594b77bdbd8ee35191c8b28a94a4cb871d96f32921558275421fb68@sprout-oss.stage.blox.sqprod.co> Co-authored-by: Max <d8473ee32b973aa31a21a65adddcc4b69cc2a8a4dee8121ecd51926e0cddbc02@sprout-oss.stage.blox.sqprod.co> Co-authored-by: Quinn <96f056ad5f2305c8ddf637dc65d048aa4c12d7daeb8867690e34fca46b0ef64c@sprout-oss.stage.blox.sqprod.co> Co-authored-by: Dawn <c6237ef84fa537c78dcee78efd2d4e59f728859c7f194da42ac51ededfa0be05@sprout-oss.stage.blox.sqprod.co> Co-authored-by: Tyler Longwell <tlongwell@block.xyz> Co-authored-by: Sami <sami@sprout-oss.stage.blox.sqprod.co> Co-authored-by: npub1t2tgm7d8f995uqvmnm8h88sg3wnpp9a5xysjf6dg3tjmgt3ltulqdp8ehr <5a968df9a7494b4e019b9ecf739e088ba61097b4312124e9a88ae5b42e3f5f3e@sprout-oss.stage.blox.sqprod.co>
88 lines
3.7 KiB
Markdown
88 lines
3.7 KiB
Markdown
# 1321 cutover: single-community → multi-tenant
|
|
|
|
`1321_backfill_default_community.sql` is a **one-off operator script**, not a
|
|
startup migration. It takes a pre-1321 (single-community) Buzz Postgres to the
|
|
1321 multi-tenant schema, assigning every existing row to one default community
|
|
derived from the deployment host.
|
|
|
|
> It is **not** embedded in the relay binary. The relay's `sqlx::migrate!`
|
|
> embeds only `migrations/0001_initial_schema.sql` — the consolidated 1321
|
|
> schema. Fresh deployments need only that; this script exists solely to carry
|
|
> existing pre-1321 data across the rewrite. (It uses psql client features —
|
|
> `\set`, `\ir`, `:'var'` interpolation, `\gset` — that the embedded sqlx
|
|
> migrator cannot run, which is why it must live here and not under
|
|
> `migrations/`.)
|
|
|
|
## When you need it
|
|
|
|
Only when upgrading a Postgres that already holds **pre-1321 single-community
|
|
data** to 1321. A brand-new deployment does **not** run this — it provisions
|
|
from `migrations/0001_initial_schema.sql` (or `schema/schema.sql`) directly.
|
|
|
|
## Preconditions
|
|
|
|
- The DB is pristine pre-1321: no `communities` table, no `community_id`
|
|
columns, no leftover `legacy` schema. The script's guard **refuses to run**
|
|
otherwise (already-migrated, partially-migrated, or failed-prior-run states
|
|
all raise and abort — see the guard block at the top of the script).
|
|
- **Take a `pg_dump` / PVC snapshot first.** There is no down-path. The
|
|
snapshot **is** the rollback.
|
|
|
|
## Run it
|
|
|
|
The whole script runs in **one transaction** (all-or-nothing). Pass the
|
|
deployment host as `:host`; it is normalized exactly as the relay normalizes a
|
|
community host (`rtrim(lower(host), '.')`).
|
|
|
|
```sh
|
|
psql "$DATABASE_URL" \
|
|
-v host="'your-deployment-host.example.com'" \
|
|
-v ON_ERROR_STOP=1 \
|
|
-f scripts/cutover/1321_backfill_default_community.sql
|
|
```
|
|
|
|
`-f` (not stdin) matters: the script `\ir`-includes
|
|
`../../migrations/0001_initial_schema.sql` **relative to its own location**, so
|
|
it resolves the canonical 1321 schema regardless of your cwd.
|
|
|
|
### What it does
|
|
|
|
1. Renames all pre-1321 tables and enum types aside into a `legacy` schema.
|
|
2. `\ir`-includes the repo's `0001` verbatim → the correct 1321 schema in a
|
|
clean `public` (structurally identical to a fresh 1321 DB **by
|
|
construction** — no hand-transcription).
|
|
3. Creates the one default community from `:host`.
|
|
4. Copies every row forward, stamped with that `community_id` (`search_tsv`
|
|
regenerates as GENERATED; new-in-1321 columns default NULL).
|
|
5. `DROP SCHEMA legacy CASCADE`.
|
|
|
|
## After it commits
|
|
|
|
Boot the 1321 relay with **`BUZZ_AUTO_MIGRATE=false`**. The schema is already
|
|
correct; the relay's idempotent boot-time `ensure_configured_community` /
|
|
allowlist→relay_members backfill finds this community and no-ops.
|
|
|
|
If you want the relay's sqlx migrator to consider `0001` already applied (so a
|
|
later `BUZZ_AUTO_MIGRATE=true` boot doesn't try to re-run it), seed its
|
|
`_sqlx_migrations` row to match a fresh 1321 install:
|
|
|
|
```sql
|
|
-- version 1, description 'initial schema'; checksum must match the embedded 0001.
|
|
-- Easiest: diff _sqlx_migrations against a freshly-migrated 1321 DB and copy the row.
|
|
```
|
|
|
|
This is **not** required for correct operation — a fresh `0001` run against the
|
|
already-correct schema is a no-op only if the migrator's checksum matches, so
|
|
prefer leaving `BUZZ_AUTO_MIGRATE=false` unless you have a reason to seed.
|
|
|
|
## Verify (post-commit, outside the txn)
|
|
|
|
```sql
|
|
SELECT count(*) FROM communities; -- expect 1
|
|
SELECT count(*) FROM events WHERE community_id IS NULL; -- expect 0
|
|
SELECT to_regnamespace('legacy'); -- expect NULL (gone)
|
|
```
|
|
|
|
For a stronger guarantee, take a constraint/trigger/index/column diff of this
|
|
DB against a freshly-migrated 1321 DB — it should be empty.
|