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

@ -6,7 +6,7 @@
import { drizzle } from 'drizzle-orm/postgres-js';
import postgres from 'postgres';
import {
pgTable,
pgSchema,
uuid,
text,
boolean,
@ -18,7 +18,7 @@ import {
import { relations } from 'drizzle-orm';
const DATABASE_URL =
process.env.DATABASE_URL ?? 'postgresql://manacore:devpassword@localhost:5432/presi';
process.env.DATABASE_URL ?? 'postgresql://manacore:devpassword@localhost:5432/mana_platform';
const connection = postgres(DATABASE_URL, {
max: 5,
@ -27,7 +27,9 @@ const connection = postgres(DATABASE_URL, {
// ─── Schema (read-only for share lookups) ────────────────
export const decks = pgTable('decks', {
export const presiSchema = pgSchema('presi');
export const decks = presiSchema.table('decks', {
id: uuid('id').primaryKey().defaultRandom(),
userId: text('user_id').notNull(),
title: text('title').notNull(),
@ -38,7 +40,7 @@ export const decks = pgTable('decks', {
updatedAt: timestamp('updated_at', { withTimezone: true }).defaultNow().notNull(),
});
export const slides = pgTable(
export const slides = presiSchema.table(
'slides',
{
id: uuid('id').primaryKey().defaultRandom(),
@ -50,7 +52,7 @@ export const slides = pgTable(
(table) => [index('slides_deck_order_idx').on(table.deckId, table.order)]
);
export const themes = pgTable('themes', {
export const themes = presiSchema.table('themes', {
id: uuid('id').primaryKey().defaultRandom(),
name: text('name').notNull(),
colors: jsonb('colors'),
@ -58,7 +60,7 @@ export const themes = pgTable('themes', {
isDefault: boolean('is_default').default(false),
});
export const sharedDecks = pgTable(
export const sharedDecks = presiSchema.table(
'shared_decks',
{
id: uuid('id').primaryKey().defaultRandom(),

View file

@ -8,7 +8,7 @@
import { drizzle } from 'drizzle-orm/postgres-js';
import postgres from 'postgres';
import {
pgTable,
pgSchema,
uuid,
text,
timestamp,
@ -20,16 +20,20 @@ import {
} from 'drizzle-orm/pg-core';
const DATABASE_URL =
process.env.DATABASE_URL ?? 'postgresql://manacore:devpassword@localhost:5432/todo';
process.env.DATABASE_URL ?? 'postgresql://manacore:devpassword@localhost:5432/mana_platform';
const connection = postgres(DATABASE_URL, {
max: 5,
idle_timeout: 20,
});
// ─── Schema ────────────────
export const todoSchema = pgSchema('todo');
// ─── Minimal Schema (only what server needs) ────────────────
export const tasks = pgTable('tasks', {
export const tasks = todoSchema.table('tasks', {
id: uuid('id').primaryKey().defaultRandom(),
userId: text('user_id').notNull(),
projectId: uuid('project_id'),
@ -55,12 +59,12 @@ export const tasks = pgTable('tasks', {
updatedAt: timestamp('updated_at', { withTimezone: true }).defaultNow().notNull(),
});
export const projects = pgTable('projects', {
export const projects = todoSchema.table('projects', {
id: uuid('id').primaryKey().defaultRandom(),
userId: text('user_id').notNull(),
});
export const reminders = pgTable(
export const reminders = todoSchema.table(
'reminders',
{
id: uuid('id').primaryKey().defaultRandom(),

View file

@ -17,7 +17,7 @@ import { GuideService } from './services/guide';
const PORT = parseInt(process.env.PORT || '3026', 10);
const DB_URL =
process.env.DATABASE_URL || 'postgresql://manacore:devpassword@localhost:5432/traces';
process.env.DATABASE_URL || 'postgresql://manacore:devpassword@localhost:5432/mana_platform';
const LLM_URL = process.env.MANA_LLM_URL || 'http://localhost:3025';
const SEARCH_URL = process.env.MANA_SEARCH_URL || 'http://localhost:3021';
const CORS_ORIGINS = (process.env.CORS_ORIGINS || 'http://localhost:5173').split(',');

View file

@ -1,5 +1,5 @@
import {
pgTable,
pgSchema,
uuid,
text,
doublePrecision,
@ -10,6 +10,8 @@ import {
uniqueIndex,
} from 'drizzle-orm/pg-core';
export const tracesSchema = pgSchema('traces');
// ============================================
// Enums
// ============================================
@ -49,7 +51,7 @@ export const guideStatusEnum = pgEnum('guide_status', ['generating', 'ready', 'e
// Tables
// ============================================
export const locations = pgTable(
export const locations = tracesSchema.table(
'locations',
{
id: uuid('id').defaultRandom().primaryKey(),
@ -80,7 +82,7 @@ export const locations = pgTable(
]
);
export const cities = pgTable(
export const cities = tracesSchema.table(
'cities',
{
id: uuid('id').defaultRandom().primaryKey(),
@ -94,7 +96,7 @@ export const cities = pgTable(
(table) => [uniqueIndex('cities_name_country_code_idx').on(table.name, table.countryCode)]
);
export const cityVisits = pgTable(
export const cityVisits = tracesSchema.table(
'city_visits',
{
id: uuid('id').defaultRandom().primaryKey(),
@ -115,7 +117,7 @@ export const cityVisits = pgTable(
]
);
export const places = pgTable(
export const places = tracesSchema.table(
'places',
{
id: uuid('id').defaultRandom().primaryKey(),
@ -139,7 +141,7 @@ export const places = pgTable(
]
);
export const pois = pgTable(
export const pois = tracesSchema.table(
'pois',
{
id: uuid('id').defaultRandom().primaryKey(),
@ -165,7 +167,7 @@ export const pois = pgTable(
]
);
export const guides = pgTable(
export const guides = tracesSchema.table(
'guides',
{
id: uuid('id').defaultRandom().primaryKey(),
@ -190,7 +192,7 @@ export const guides = pgTable(
]
);
export const guidePois = pgTable(
export const guidePois = tracesSchema.table(
'guide_pois',
{
id: uuid('id').defaultRandom().primaryKey(),

View file

@ -22,8 +22,7 @@ let client: ReturnType<typeof postgres> | null = null;
export function getDb(): ReturnType<typeof drizzle<typeof schema>> {
if (!db) {
const connectionString =
process.env.DATABASE_URL ||
'postgresql://uload:uload_dev_password_123@localhost:5432/uload_dev';
process.env.DATABASE_URL || 'postgresql://manacore:devpassword@localhost:5432/mana_platform';
client = postgres(connectionString, {
max: 10,

View file

@ -1,5 +1,5 @@
import {
pgTable,
pgSchema,
uuid,
text,
boolean,
@ -10,10 +10,12 @@ import {
} from 'drizzle-orm/pg-core';
import { relations } from 'drizzle-orm';
export const uloadSchema = pgSchema('uload');
// ============================================
// Users Table
// ============================================
export const users = pgTable(
export const users = uloadSchema.table(
'users',
{
id: uuid('id').primaryKey().defaultRandom(),
@ -48,7 +50,7 @@ export const users = pgTable(
// ============================================
// Accounts Table
// ============================================
export const accounts = pgTable(
export const accounts = uloadSchema.table(
'accounts',
{
id: uuid('id').primaryKey().defaultRandom(),
@ -70,7 +72,7 @@ export const accounts = pgTable(
// ============================================
// Workspaces Table
// ============================================
export const workspaces = pgTable(
export const workspaces = uloadSchema.table(
'workspaces',
{
id: uuid('id').primaryKey().defaultRandom(),
@ -92,7 +94,7 @@ export const workspaces = pgTable(
// ============================================
// Links Table
// ============================================
export const links = pgTable(
export const links = uloadSchema.table(
'links',
{
id: uuid('id').primaryKey().defaultRandom(),
@ -129,7 +131,7 @@ export const links = pgTable(
// ============================================
// Clicks Table
// ============================================
export const clicks = pgTable(
export const clicks = uloadSchema.table(
'clicks',
{
id: uuid('id').primaryKey().defaultRandom(),