refactor(db): consolidate ~20+ databases into 2 (mana_platform + mana_sync)

Mirrors the frontend unification (single IndexedDB) on the backend.
All services now use pgSchema() for isolation within one shared database,
enabling cross-schema JOINs, simplified ops, and zero DB setup for new apps.

- Migrate 7 services from pgTable() to pgSchema(): mana-user (usr),
  mana-media (media), todo, traces, presi, uload, cards
- Update all DATABASE_URLs in .env.development, docker-compose, configs
- Rewrite init-db scripts for 2 databases + 12 schemas
- Rewrite setup-databases.sh for consolidated architecture
- Update shared-drizzle-config default to mana_platform
- Update CLAUDE.md with new database architecture docs

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Till JS 2026-04-02 14:31:28 +02:00
parent b1a5c95f1d
commit 3ea28b9065
44 changed files with 311 additions and 346 deletions

View file

@ -4,8 +4,10 @@
# Usage: ./scripts/setup-databases.sh [service]
# Examples:
# ./scripts/setup-databases.sh # Setup all
# ./scripts/setup-databases.sh chat # Setup only chat
# ./scripts/setup-databases.sh auth # Setup only auth
# ./scripts/setup-databases.sh auth # Setup only auth schema
#
# Architecture: 2 databases (mana_platform + mana_sync)
# All service schemas live in mana_platform as separate PostgreSQL schemas.
set -e
@ -21,7 +23,7 @@ YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color
echo -e "${GREEN}🗄️ Database Setup Script${NC}"
echo -e "${GREEN}Database Setup Script${NC}"
echo "======================================"
# Function to create database if it doesn't exist
@ -40,12 +42,19 @@ create_db_if_not_exists() {
fi
}
# Function to create schema within a database
create_schema_if_not_exists() {
local db_name=$1
local schema_name=$2
PGPASSWORD=$DB_PASSWORD psql -h $DB_HOST -p $DB_PORT -U $DB_USER -d $db_name -c \
"CREATE SCHEMA IF NOT EXISTS $schema_name;" > /dev/null 2>&1
}
# Function to push schema for a service
push_schema() {
local filter=$1
local name=$2
echo -e "${YELLOW}Pushing schema for ${name}...${NC}"
# Use --force to auto-approve in development (skips interactive prompts)
if pnpm --filter "$filter" db:push --force 2>/dev/null; then
echo -e " ${GREEN}✓ Schema pushed${NC}"
else
@ -53,41 +62,20 @@ push_schema() {
fi
}
# All databases that should exist
ALL_DATABASES=(
"manacore"
"mana_auth"
"chat"
"zitare"
"contacts"
"calendar"
"clock"
# All schemas in mana_platform
PLATFORM_SCHEMAS=(
"auth"
"credits"
"gifts"
"subscriptions"
"feedback"
"usr"
"media"
"todo"
"cards"
"storage"
"presi"
"moodlit"
"inventory"
"planta"
"nutriphi"
"photos"
"projectdoc"
"zitare_bot"
"todo_bot"
"nutriphi_bot"
"questions"
"skilltree"
"mukke"
"traces"
"context"
"citycorners"
"presi"
"uload"
# Hono service databases (extracted from former mana-core-auth)
"mana_credits"
"mana_user"
"mana_subscriptions"
"mana_analytics"
"mana_sync"
"cards"
)
# Check if specific service requested
@ -98,112 +86,41 @@ setup_service() {
case $service in
auth|mana-auth)
create_db_if_not_exists "mana_auth"
push_schema "@mana/auth" "mana-auth"
;;
chat)
create_db_if_not_exists "chat"
push_schema "@chat/server" "chat"
credits|mana-credits)
push_schema "@mana/credits" "mana-credits"
;;
zitare)
create_db_if_not_exists "zitare"
# Schema managed by mana-sync (backend removed)
user|mana-user)
push_schema "@mana/user" "mana-user"
;;
contacts)
create_db_if_not_exists "contacts"
# Schema managed by mana-sync (local-first)
subscriptions|mana-subscriptions)
push_schema "@mana/subscriptions" "mana-subscriptions"
;;
calendar)
create_db_if_not_exists "calendar"
# Schema managed by mana-sync (local-first)
analytics|mana-analytics)
push_schema "@mana/analytics" "mana-analytics"
;;
clock)
create_db_if_not_exists "clock"
# Schema managed by mana-sync (backend removed)
media|mana-media)
push_schema "@mana/media" "mana-media"
;;
todo)
create_db_if_not_exists "todo"
push_schema "@todo/server" "todo"
;;
cards)
create_db_if_not_exists "cards"
# Schema managed by mana-sync (local-first)
;;
moodlit)
create_db_if_not_exists "moodlit"
push_schema "@moodlit/server" "moodlit"
;;
picture)
create_db_if_not_exists "picture"
# Schema managed by mana-sync (local-first)
;;
photos)
create_db_if_not_exists "photos"
# Schema managed by mana-sync (backend removed)
;;
planta)
create_db_if_not_exists "planta"
push_schema "@planta/server" "planta"
;;
nutriphi)
create_db_if_not_exists "nutriphi"
# Schema managed by mana-sync (local-first)
;;
presi)
create_db_if_not_exists "presi"
# Schema managed by mana-sync (NestJS backend removed, Hono server for shares)
;;
storage)
create_db_if_not_exists "storage"
# Schema managed by mana-sync (local-first)
;;
projectdoc)
create_db_if_not_exists "projectdoc"
push_schema "@manacore/telegram-project-doc-bot" "projectdoc"
;;
zitare_bot|zitare-bot)
create_db_if_not_exists "zitare_bot"
push_schema "@manacore/telegram-zitare-bot" "zitare-bot"
;;
todo_bot|todo-bot)
create_db_if_not_exists "todo_bot"
push_schema "@manacore/telegram-todo-bot" "todo-bot"
;;
nutriphi_bot|nutriphi-bot)
create_db_if_not_exists "nutriphi_bot"
push_schema "@manacore/telegram-nutriphi-bot" "nutriphi-bot"
;;
questions)
create_db_if_not_exists "questions"
# Schema managed by mana-sync (local-first)
;;
skilltree)
create_db_if_not_exists "skilltree"
# Schema managed by mana-sync (backend removed)
;;
mukke)
create_db_if_not_exists "mukke"
push_schema "@mukke/server" "mukke"
;;
traces)
create_db_if_not_exists "traces"
push_schema "@traces/server" "traces"
;;
context)
create_db_if_not_exists "context"
push_schema "@context/server" "context"
;;
citycorners)
create_db_if_not_exists "citycorners"
# Schema managed by mana-sync (backend removed)
presi)
push_schema "@presi/server" "presi"
;;
uload)
create_db_if_not_exists "uload"
# Schema managed by mana-sync (local-first app)
push_schema "@manacore/uload-database" "uload"
;;
cards)
push_schema "@manacore/cards-database" "cards"
;;
*)
echo -e "${RED}Unknown service: $service${NC}"
echo "Available services: auth, chat, zitare, contacts, calendar, clock, todo, cards, moodlit, picture, photos, planta, nutriphi, presi, storage, projectdoc, zitare_bot, todo_bot, nutriphi_bot, questions, skilltree, mukke, traces, context, citycorners, uload"
echo "Available services: auth, credits, user, subscriptions, analytics, media, todo, traces, presi, uload, cards"
exit 1
;;
esac
@ -219,15 +136,21 @@ fi
# Setup all databases
echo -e "\n${GREEN}Step 1: Creating databases${NC}"
echo "--------------------------------------"
for db in "${ALL_DATABASES[@]}"; do
create_db_if_not_exists "$db"
create_db_if_not_exists "mana_platform"
create_db_if_not_exists "mana_sync"
echo -e "\n${GREEN}Step 2: Creating schemas in mana_platform${NC}"
echo "--------------------------------------"
for schema in "${PLATFORM_SCHEMAS[@]}"; do
echo -e " ${YELLOW}Creating schema: ${schema}${NC}"
create_schema_if_not_exists "mana_platform" "$schema"
echo -e " ${GREEN}${schema}${NC}"
done
echo -e "\n${GREEN}Step 2: Pushing schemas${NC}"
echo -e "\n${GREEN}Step 3: Pushing schemas${NC}"
echo "--------------------------------------"
# Push schemas for all known services
for service in auth chat zitare contacts calendar clock todo cards picture photos moodlit planta nutriphi presi storage questions skilltree mukke traces context citycorners; do
for service in auth credits user subscriptions analytics media todo traces presi uload cards; do
setup_service "$service" 2>/dev/null || true
done