Skip to content

Database & Seeding

fulleni edited this page Jan 18, 2026 · 1 revision

🗄️ Database Seeding & Migrations

The API Server includes a robust system for managing database schema evolution and initial data population. This ensures that any developer (or production environment) can spin up a fully functional database from scratch.

🔄 Database Migrations

We do not manually edit the database. Schema changes are code.

  • Service: DatabaseMigrationService (lib/src/services/database_migration_service.dart)
  • Mechanism:
    1. On startup, the service checks the migrations collection.
    2. It compares applied migrations against the list of available migration scripts in lib/src/database/migrations/.
    3. It executes any new up() methods in order.
  • Traceability: Each migration is linked to a specific Pull Request (e.g., Migration20240101_AddUserTier), providing an audit trail.

🌱 Database Seeding

After migrations are applied, the seeding process ensures essential data exists.

  • Service: DatabaseSeedingService (lib/src/services/database_seeding_service.dart)
  • Idempotency: The seeder is designed to be run on every startup. It uses upsert operations (specifically updateOne with $setOnInsert) to ensure it never overwrites existing data or creates duplicates.

What gets seeded?

  1. Indexes: Critical performance indexes (e.g., Text Search on Headlines, TTL on Verification Codes) are ensured via _ensureIndexes().
  2. Static Data:
    • Countries: From packages/core/lib/src/fixtures/countries.dart.
    • Languages: From packages/core/lib/src/fixtures/languages.dart.
  3. Configuration:
    • Remote Config: A default RemoteConfig document is created if none exists.
    • Admin User: If OVERRIDE_ADMIN_EMAIL is set in .env, the system ensures a user with that email exists and has the admin role.

Clone this wiki locally