Backend Go: - Remplacement complet des anciennes migrations par la base V1 alignée sur ORIGIN. - Durcissement global du parsing JSON (BindAndValidateJSON + RespondWithAppError). - Sécurisation de config.go, CORS, statuts de santé et monitoring. - Implémentation des transactions P0 (RBAC, duplication de playlists, social toggles). - Ajout d’un job worker structuré (emails, analytics, thumbnails) + tests associés. - Nouvelle doc backend : AUDIT_CONFIG, BACKEND_CONFIG, AUTH_PASSWORD_RESET, JOB_WORKER_*. Chat server (Rust): - Refonte du pipeline JWT + sécurité, audit et rate limiting avancé. - Implémentation complète du cycle de message (read receipts, delivered, edit/delete, typing). - Nettoyage des panics, gestion d’erreurs robuste, logs structurés. - Migrations chat alignées sur le schéma UUID et nouvelles features. Stream server (Rust): - Refonte du moteur de streaming (encoding pipeline + HLS) et des modules core. - Transactions P0 pour les jobs et segments, garanties d’atomicité. - Documentation détaillée de la pipeline (AUDIT_STREAM_*, DESIGN_STREAM_PIPELINE, TRANSACTIONS_P0_IMPLEMENTATION). Documentation & audits: - TRIAGE.md et AUDIT_STABILITY.md à jour avec l’état réel des 3 services. - Cartographie complète des migrations et des transactions (DB_MIGRATIONS_*, DB_TRANSACTION_PLAN, AUDIT_DB_TRANSACTIONS, TRANSACTION_TESTS_PHASE3). - Scripts de reset et de cleanup pour la lab DB et la V1. Ce commit fige l’ensemble du travail de stabilisation P0 (UUID, backend, chat et stream) avant les phases suivantes (Coherence Guardian, WS hardening, etc.).
3.7 KiB
3.7 KiB
🏗️ DB Migrations Strategy V1
Date: 04/12/2025
Scope: veza-backend-api
Goal: Canonical, UUID-first, production-ready PostgreSQL schema.
1. Philosophy
We are moving from an "Iterative/Repair" mindset (fixing types, patching IDs) to a "Declarative/Final" mindset. The V1 migrations represent the database as it should be created for a fresh deployment.
Core Rules ("The Standard")
- Identity: All Primary Keys are
UUID(gen_random_uuid()). NoSERIALorBIGINTPKs. - Time:
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMPupdated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP(with Trigger)deleted_at TIMESTAMPTZ(Nullable, for Soft Delete)
- Integrity:
- All Foreign Keys must be
UUID. - All Foreign Keys must have explicit
ON DELETEclauses (mostlyCASCADEfor child entities). - All Foreign Keys must be Indexed.
- All Foreign Keys must be
- Text: Use
TEXTorVARCHAR(N)appropriately. IDs/Tokens are usuallyVARCHAR.
2. Migration File Structure
We use a grouped numbering system to organize domains.
migrations/
-
001_extensions_and_types.sql- Enable
pgcrypto(legacy support),uuid-ossp. - Define global ENUMs (e.g.,
user_role,playlist_permissionif DB-enforced).
- Enable
-
010_auth_and_users.sqlusers,federated_identities.refresh_tokens,password_reset_tokens,email_verification_tokens.user_sessions.
-
020_rbac_and_profiles.sqlroles,permissions,user_roles,role_permissions.user_profiles(if distinct from users),user_settings.
-
040_streaming_core.sqltracks,track_versions.playlists,playlist_tracks.playlist_collaborators,playlist_follows.
-
041_streaming_analytics.sqltrack_plays,track_likes,track_shares,track_comments.track_history.
-
042_media_processing.sqlhls_streams.hls_transcode_queue.bitrate_adaptation_logs.
-
050_legacy_chat.sqlrooms(Legacy support).- Note: Modern chat is in
chatschema, managed by Rust service.
-
900_triggers_and_functions.sqlupdate_updated_at_column()function.- Apply triggers to all tables with
updated_at.
3. Idempotence & Forward-Only
- Production: Migrations are applied forward. We do not support
DOWNmigrations for V1 in the strict sense (rollback is usually "restore backup"). - Development: We support a
reset_db.shscript that drops the schema and reapplies all V1 migrations.
4. Indexes Strategy
- Primary Keys: Implicit B-Tree.
- Foreign Keys: MUST be indexed explicitly (Postgres does not do this automatically).
- Naming:
idx_<table>_<column>
- Naming:
- Search Fields:
email,username,sluggetUNIQUEindexes. - Sorting:
created_at DESCindexes for activity feeds.
5. Example Migration Snippet
-- === USERS ===
CREATE TABLE public.users (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
email VARCHAR(255) NOT NULL,
username VARCHAR(30) NOT NULL,
-- Timestamps
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
deleted_at TIMESTAMPTZ
);
CREATE UNIQUE INDEX idx_users_email ON public.users(email) WHERE deleted_at IS NULL;
CREATE UNIQUE INDEX idx_users_username ON public.users(username) WHERE deleted_at IS NULL;
-- Trigger
CREATE TRIGGER update_users_updated_at
BEFORE UPDATE ON public.users
FOR EACH ROW
EXECUTE FUNCTION update_updated_at_column();