Skip to content

Migrations

Generating, hand-writing and running migrations — and why there is no push.

On this page

Schema changes are SQL migration files in ikazebot-server/src/db/migrations, generated by drizzle-kit from the TypeScript schema or hand-written for things it does not know about, such as row-level security policies, functions and triggers.

Commands

bash
pnpm db:generate --name=add_widgets          # from schema changes; needs no database
pnpm db:generate --custom --name=widgets_rls # an empty file for hand-written SQL
pnpm db:migrate                              # apply pending migrations, as the superuser
pnpm db:studio                               # browse the database

Which database user runs them

Migrations need the schema owner, the Dokploy superuser, because ikazebot_app has no DDL privilege by design. Its URL lives in a separate file:

bash
cp .env.migrate.example .env.migrate   # MIGRATION_DATABASE_URL=postgresql://postgres:…

It is kept out of .env on purpose: Nest loads all of .env into the API's environment, so the superuser URL there would be one environment dump away from anyone who compromised the API. You can also pass it inline for one command:

bash
MIGRATION_DATABASE_URL='postgresql://postgres:…@host:5432/ikazebotdb' pnpm db:migrate

The workflow

  1. 1Change src/db/schema
  2. 2pnpm db:generateread the SQL in full
  3. 3Hand-write RLS / triggers--custom
  4. 4pnpm db:migratedevelopment database
  5. 5Commit both filessame pull request
  • Name migrations after the feature or change; existing ones read like 0035_actions.sql and 0036_actions_rls.sql.
  • A new tenant table always comes as two migrations, the table and its RLS, in the same pull request. See Adding a tenant table.
  • Never edit a migration that has already been applied anywhere. Write a new one.

There is no db:push, and there never will be

drizzle-kit push changes a live database without producing a reviewable file. A schema diff would also silently drop the RLS policies, because they exist only in hand-written SQL. There is no push script in the repository, in any environment.

In production

Railpack deploys have no one-shot step, so migrations run from your machine against production, before the code that needs them is deployed:

  1. Check out the commit about to be deployed.
  2. MIGRATION_DATABASE_URL='…production…' pnpm db:migrate over an SSH tunnel or an allowlisted IP.
  3. Deploy.

Keep migrations additive, so the code still running keeps working against the new schema until its redeploy finishes. Migrations only move forward. Rolling back across one needs old code that tolerates the new schema, or a database restore. See Deploying.

Get started free — 50 credits a month