Infrastructure: How to isolate staging databases from production when enviro...

Documentation for Sessions

📝 Infrastructure: How to isolate staging databases from production when enviro...

Date: 2026-03-30
Time: 14:33:51 UTC
Session: gspace1-283.4
Agent: backend
Confidence: 95%
Status: ✅ Documented


📋 Decision Context

Question:

How to isolate staging databases from production when environments may share MongoDB clusters?

Task: S283: Staging environment database isolation


🎯 Decision Made

Chosen:

DB_ENV environment variable that appends a suffix to ALL database names (static and dynamic). DB_ENV=staging produces 'gograb-api-staging', 'ai-agents-staging', 'space_*-staging', etc. Production leaves DB_ENV unset — empty suffix — zero change to existing behavior.

Options Considered:

  1. Separate connection strings per environment only
  2. Database name prefix instead of suffix
  3. Completely separate clusters with no code changes
  4. DB_ENV suffix on all database names (chosen)

💭 Reasoning

ROOT CAUSE: No environment isolation existed. Local dev and Railway production shared the same MongoDB Atlas clusters and database names. A bad migration or seed script could corrupt production data. ALTERNATIVES: (1) Separate connection strings only — no defense if misconfigured. (2) Database name prefixes — more invasive, breaks existing tooling. (3) Completely separate clusters with no code changes — doesn't prevent accidental cross-writes if connection string is wrong. CHOSEN BECAUSE: (1) Zero production impact — unset DB_ENV = empty suffix = identical behavior. (2) Defense in depth — even with wrong connection string, database names differ. (3) Single control point — one env var controls all static names (database-names.config.ts) AND dynamic space names (SpaceDatabaseResolverService, space-database.utils.ts). (4) Future-proof — DB_ENV=test, DB_ENV=preview-42 work without code changes. WOULD BE WRONG IF: MongoDB enforces max database name length and the suffix pushes names over the limit (mitigated by buildBoundedSpaceDbName). EARLY WARNING: Monitor Atlas for databases without the expected suffix in staging environment.


📁 Files Involved


🔗 Related


Auto-generated by MCP Smart Consolidation