DEV Community

Daniel Pertu
Daniel Pertu

Posted on

Two products share one Postgres, and drizzle skips any migration older than the newest tracked row

CogniPrep Assess runs beside its sister product, CogniPrep, on the same Postgres and the same auth. That is convenient for sign-in and awkward for migrations. Two products, one database, one migrator each, and a rule in drizzle that quietly makes them step on each other.

The product is live here if you want the context for why two products share a database at all.

The rule that bites

Drizzle's migrator records applied migrations in a table, and it decides what to run by comparing each migration's timestamp with the newest row in that table. Anything older than the newest tracked row is skipped.

Put two products' migrations in one tracking table and the failure is silent. Product A ships a migration today; product B has an older migration waiting. B's migration is now older than the newest row, so the migrator skips it and reports success. Nothing is red. A table just does not exist in production.

One tracking table per product

Assess has its own:

export default {
  schema: './lib/db/schema.ts',
  out: './drizzle',
  dialect: 'postgresql',
  schemaFilter: ['assess'],
  migrations: { schema: 'drizzle', table: '__assess_migrations' },
} satisfies Config;
Enter fullscreen mode Exit fullscreen mode

The other product keeps drizzle.__drizzle_migrations. Each migrator only ever sees its own history, so neither can hide the other's.

Why the tracking table is not inside the product's schema

The obvious place for it is the assess schema, next to the tables it describes. That fails. The migrator creates its schema before running migration 0000, and 0000 itself starts with CREATE SCHEMA "assess". The first run would collide with itself. So the tracking table lives in the shared drizzle schema under a name that cannot clash. That reasoning is a comment in the config because I would forget it.

schemaFilter as a seatbelt

schemaFilter: ['assess'] means drizzle-kit never introspects the shared public schema, so generate can never propose to drop or alter a table that belongs to the other product. Every Assess table sits in its own schema, with row level security switched on and no policies. The app connects as the table owner, so the only thing RLS does is close the tables to the auth roles the public API uses.

Resetting while it was safe to

Early on, before any real data existed, I reset the assess schema and started the migrations again from one fresh 0000. A fresh tracking table at that moment was cheap. After the first customer it would not have been. If you are going to change your migration layout, the cheapest moment is always now.

Shared auth without foreign keys

Team members are stored with the shared auth user id as opaque text. There is no foreign key into the auth schema, so deleting a product's data never cascades into auth, and an auth deletion never cascades into ours. The cost is a job that checks for closed accounts. It is the subject of the next post.

For what we do with the data that does live here, the security page and the data processing terms say it in plain words.

Top comments (0)