Skip to content

Database Migrations

import { Aside } from ‘@astrojs/starlight/components’;

Orimora uses Drizzle ORM with SQL migration files stored in src/lib/server/db/migrations/.

Terminal window
# Development (uses DATABASE_URL from .env)
yarn db:migrate
# Docker / production (runs automatically on container start)
node run-migrations.mjs

Migrations are applied in order and tracked in the drizzle.__drizzle_migrations table.

In Docker deployments (including Coolify), migrations run automatically when the container starts via docker-entrypoint.sh. No manual intervention needed.

Orimora uses a custom migration runner (run-migrations.mjs) instead of drizzle-kit migrate or drizzle-orm/postgres-js/migrator. This was necessary because:

  1. Per-migration transactions: Each migration runs in its own transaction. If migration 15 fails, migrations 1–14 remain applied. The built-in drizzle-orm migrator runs all migrations in a single transaction — one failure rolls back everything.

  2. Visible error output: On failure, the runner prints the migration tag, PostgreSQL error code, detail, and hint. The built-in migrator silently exits with code 1.

  3. Hash-based tracking: The runner uses content hashes (not timestamps) to determine which migrations have been applied. This avoids issues with non-monotonic timestamps in the journal.

After changing a schema file in src/lib/server/db/schema/:

Terminal window
yarn db:generate

This produces a new SQL file in src/lib/server/db/migrations/. Review it before committing.

Some operations (e.g. adding GIN indexes, PostgreSQL extensions) cannot be generated automatically and are written as raw SQL files.

TablePurpose
teamsTenant isolation
usersTeam members
sessionsAuth sessions
collectionsDocument groups
documentsWiki pages
document_revisionsVersion history
commentsInline document comments
notificationsIn-app notification feed
audit_eventsSecurity audit log
tags / document_tagsTagging
starsBookmarked documents
viewsRead history
backlinksCross-document links
webhooksOutgoing webhooks
api_keysAPI authentication
team_llm_configsAI provider configuration