Skip to content

[Backend] Database Migration from SQLite to PostgreSQL for Production #280

Description

@KuchiMercy

Problem

The backend currently uses SQLite (file-based database at ./data/vaultix.db) which is unsuitable for production:

  • No concurrent write support
  • No replication or backup strategy
  • Limited scalability for high-traffic scenarios
  • TypeORM migrations work differently between SQLite and PostgreSQL (e.g., ALTER TABLE limitations)

This was previously opened as #204 but not fully implemented.

Current State

  • data-source.ts uses SQLite with DATABASE_PATH env var
  • 3 existing migrations written against SQLite dialect
  • No PostgreSQL-specific migration or connection config
  • No connection pooling configured

Acceptance Criteria

  • Add PostgreSQL connection support alongside SQLite:
    • If DATABASE_URL is set, use PostgreSQL
    • If only DATABASE_PATH is set, fall back to SQLite (for local dev)
    • Configure connection pooling with pg driver (min: 2, max: 10 connections)
  • Update data-source.ts to support both drivers:
    • PostgreSQL: type: 'postgres', url from DATABASE_URL
    • SQLite: type: 'better-sqlite3', database from DATABASE_PATH
    • Add SSL configuration for production PostgreSQL
  • Review and update all 3 existing migrations for PostgreSQL compatibility:
    • Fix any SQLite-specific syntax (e.g., AUTOINCREMENT vs SERIAL)
    • Ensure UUID type works correctly in PostgreSQL
    • Verify decimal precision handling for monetary amounts
  • Add a PostgreSQL-specific Docker Compose config for local development:
    • docker-compose.yml with PostgreSQL 15
    • Pre-configured with vaultix_db, user, password
  • Add database health check endpoint: GET /health/database
    • Returns connection status, database type, migration status
  • Update .env.example with PostgreSQL configuration options
  • Document the PostgreSQL setup in the project README

Technical Notes

  • Use pg and pg-pool packages for PostgreSQL
  • Connection string format: postgresql://user:password@host:5432/vaultix_db
  • Set synchronize: false in production (use migrations only)
  • Consider using @nestjs/typeorm for better NestJS integration

Points: 150 (Medium)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions