Files
rose-ash/_config/split-databases.sh
giles 95bd32bd71
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 6m2s
Decouple cross-domain DB queries for per-app database split
Move Ghost membership sync from blog to account service so blog no
longer queries account tables (users, ghost_labels, etc.). Account
runs membership sync at startup and exposes HTTP action/data endpoints
for webhook-triggered syncs and user lookups.

Key changes:
- account/services/ghost_membership.py: all membership sync functions
- account/bp/actions + data: ghost-sync-member, user-by-email, newsletters
- blog ghost_sync.py: stripped to content-only (posts, authors, tags)
- blog webhook member: delegates to account via call_action()
- try_publish: opens federation session when DBs differ
- oauth.py callback: uses get_account_session() for OAuthCode
- page_configs moved from db_events to db_blog in split script

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-25 11:32:14 +00:00

154 lines
3.9 KiB
Bash
Executable File

#!/usr/bin/env bash
#
# split-databases.sh — Migrate from single appdb to per-domain databases.
#
# Prerequisites:
# - All apps stopped (5-min maintenance window)
# - init-databases.sql already run (CREATE DATABASE db_*)
# - Run from a host that can reach the Postgres container
#
# Usage:
# PGHOST=db PGUSER=postgres PGPASSWORD=change-me bash split-databases.sh
#
set -euo pipefail
SOURCE_DB="${SOURCE_DB:-appdb}"
# ── Table → database mapping ───────────────────────────────────────────────
declare -A DB_TABLES
DB_TABLES[db_account]="
users
magic_links
oauth_codes
oauth_grants
ghost_labels
user_labels
ghost_newsletters
user_newsletters
ghost_tiers
ghost_subscriptions
kv
"
DB_TABLES[db_blog]="
authors
tags
posts
post_authors
post_tags
post_likes
menu_items
menu_nodes
container_relations
page_configs
"
DB_TABLES[db_market]="
products
product_images
product_sections
product_labels
product_stickers
product_attributes
product_nutrition
product_allergens
product_likes
product_logs
market_places
nav_tops
nav_subs
listings
listing_items
link_errors
link_externals
subcategory_redirects
"
DB_TABLES[db_cart]="
cart_items
orders
order_items
"
DB_TABLES[db_events]="
calendars
calendar_slots
calendar_entries
calendar_entry_posts
ticket_types
tickets
"
DB_TABLES[db_federation]="
ap_anchors
ap_actor_profiles
ap_activities
ap_followers
ap_inbox_items
ap_remote_actors
ap_following
ap_remote_posts
ap_local_posts
ap_interactions
ap_notifications
ap_delivery_log
ipfs_pins
"
# ── Migrate each domain ────────────────────────────────────────────────────
for target_db in db_account db_blog db_market db_cart db_events db_federation; do
tables="${DB_TABLES[$target_db]}"
table_list=""
for t in $tables; do
table_list="$table_list --table=$t"
done
echo "=== Migrating $target_db ==="
echo " Tables: $(echo $tables | tr '\n' ' ')"
# Dump schema + data for these tables from the source DB
pg_dump "$SOURCE_DB" $table_list --no-owner --no-privileges \
| psql -q "$target_db"
echo " Done."
done
# ── Stamp Alembic head in each domain DB ──────────────────────────────────
echo ""
echo "=== Stamping Alembic head in each DB ==="
for target_db in db_account db_blog db_market db_cart db_events db_federation; do
# Create alembic_version table and stamp current head
psql -q "$target_db" <<'SQL'
CREATE TABLE IF NOT EXISTS alembic_version (
version_num VARCHAR(32) NOT NULL,
CONSTRAINT alembic_version_pkc PRIMARY KEY (version_num)
);
DELETE FROM alembic_version;
INSERT INTO alembic_version (version_num) VALUES ('w3u1q9r0s1');
SQL
echo " $target_db stamped at w3u1q9r0s1"
done
echo ""
echo "=== Migration complete ==="
echo ""
echo "Next steps:"
echo " 1. Update docker-compose.yml — set per-app DATABASE_URL to the new DBs"
echo " 2. Remove schema_sql config (no longer needed)"
echo " 3. Redeploy all services"
echo ""
echo "Per-app DATABASE_URL values:"
echo " blog: postgresql+asyncpg://postgres:change-me@db:5432/db_blog"
echo " market: postgresql+asyncpg://postgres:change-me@db:5432/db_market"
echo " cart: postgresql+asyncpg://postgres:change-me@db:5432/db_cart"
echo " events: postgresql+asyncpg://postgres:change-me@db:5432/db_events"
echo " federation: postgresql+asyncpg://postgres:change-me@db:5432/db_federation"
echo " account: postgresql+asyncpg://postgres:change-me@db:5432/db_account"
echo ""
echo " DATABASE_URL_ACCOUNT: postgresql+asyncpg://postgres:change-me@db:5432/db_account"
echo " DATABASE_URL_FEDERATION: postgresql+asyncpg://postgres:change-me@db:5432/db_federation"