Skip to content

Unify the SQLite/Postgres Runtime Dialect Switching into a Single Schema Facade #389

@mcowger

Description

@mcowger

Problem: Every database service (usage-storage.ts, quota-enforcer.ts, cooldown manager, etc.) imports both sqliteSchema and postgresSchema, then
branches at runtime:

  const schema = getCurrentDialect() === 'postgres' ? postgresSchema : sqliteSchema;

This branching appears dozens of times across the codebase. Additionally, the Drizzle schemas are fully duplicated under drizzle/schema/postgres/ and
drizzle/schema/sqlite/ (e.g., providers.ts, api-keys.ts, request-usage.ts). Every schema change (new column, new table, new enum) requires editing two
files and verifying they stay in sync.

Impact: High maintenance burden, high risk of schema drift, and noisy, error-prone runtime conditionals in every DB query path. The manual
boolean-to-integer conversions in usage-storage.ts also suggest the Drizzle column types are not abstracted correctly across dialects.

Recommendation: Create a dialect-aware schema factory that exports a unified schema object:

  // db/schema/index.ts
  export const schema = getCurrentDialect() === 'postgres' ? postgresSchema : sqliteSchema;

All service code should import this single export and never branch on dialect again. For Drizzle’s dialect-specific column definitions (e.g., timestamp
vs integer(mode: 'timestamp_ms')), use helper factories that accept the dialect and return the correct column builder. This removes ~80% of the runtime
dialect branching and halves the surface area for schema bugs.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions