The Migration Ran. The Table Was Still Invisible.
A support ticket that said the app had lost data, an AI-written migration marked as applied, and a Postgres default that turns a missing policy into silent emptiness.
The ticket said a customer had lost two weeks of data. A list they had been building since the start of the month showed an empty state. No error, no spinner stuck, no 500 in the logs. A screen where four hundred rows should have been.
I did the usual thing first. Connected as the database owner, counted the rows. All four hundred were there, timestamped, untouched. Then I ran the same query the app runs, with the same role the app uses.
Zero rows.
Nothing was deleted. The data was sitting there the whole time, and one Postgres setting was making it invisible to exactly one role.
What actually happened
The table had been multi-tenant from day one, with a tenantId on every row and a repository layer that always added the filter. Nobody had turned on row-level security, so the filter was a convention rather than an enforcement mechanism.
An agent was asked to add RLS properly. It wrote a migration. The migration enabled row-level security on the table for rows owned by the application role, and the code review passed because the SQL was readable and the reasoning in the summary was sound.
What the review missed is that the app role was not the table owner. The migration created the policy, and the policy was wrong for that role: it admitted rows only where tenantId matched a session variable that this connection never sets. So in every environment where the app connected as anything other than the table owner, the list was empty.
Postgres does not warn about this. Row-level security is a filter, not an error. No rows match the policy, so no rows are returned, and the query succeeds. The only signal is an empty result set, which is indistinguishable from an empty table.
Why development never caught it
The failure needed three things to line up, and two of them were true locally.
Locally, the app connected as the table owner. Table owners bypass row-level security by default, so the policy was never evaluated. The app worked.
Locally, development used a single tenant, so even if the policy had been evaluated, the data would have matched.
Production had a different role and real tenants, which is the entire point of production.
This is the shape of incident I have started to expect from AI-assisted schema work: the SQL is syntactically fine, it runs, it is committed, and the thing that breaks is a semantic assumption about who is asking. Models are good at the shape of a policy and bad at the privileges of the connection that will evaluate it, because the privileges are not in the file they were shown.
The stale signal that cost us the most
Here is the part I would put on a wall.
prisma migrate status reported the database as up to date. The later migration that tried to reconcile the policy was listed as applied. Meanwhile the actual policy on the production table did not match the schema, and nobody could see that from the CLI output.
That is not a bug in the migration tool as much as a boundary of it. Migration tooling tracks history: which migration files were run, in what order. It does not diff the live database against your schema. If a statement was skipped, rolled back by hand, or fixed up out-of-band during the incident, the history still says “applied”, and Prisma’s own issue tracker has an open report for exactly that class of drift.
The practical consequence: a green migrate status is a claim about your migration files, not about your database. Treat it as one.
What I check now, and where
Two commands, run against the deployed database, not against the app.
The first finds tables with row-level security enabled and no policy attached, which is the default-deny state:
select c.relname, c.relrowsecurity as rls_enabled, count(p.polname) as policy_countfrom pg_class cleft join pg_policy p on p.polrelid = c.oidwhere c.relkind = 'r' and c.relnamespace = 'public'::regnamespace and c.relrowsecurity = true -- only tables that opted into RLSgroup by 1, 2having count(p.polname) = 0;Any row that comes back is a table your app cannot read, or can read entirely, depending on which side of the policy it landed on.
The second is more general and belongs in every deploy that touches schema:
select current_user, session_user, pg_has_role(current_user, 'your_table_owner_role', 'member') as is_owner;Run it as the app role, in the environment in question. If the answer is “owner”, your local RLS testing is meaningless, because the owner bypasses policies unless the table is also marked force row level security.
The rule I added to AGENTS.md
One line, and it has prevented a repeat twice:
Any migration that changes access control (row level security, grants, roles) must ship with a query proving the resulting state on the database the app connects to, run as the app’s role.
Note what it does not say. It does not say “ask the agent to write correct policies.” That is not checkable. It asks for an artifact whose absence is obvious in a diff.
I also stopped treating an empty result set as a neutral event. If a table that should have rows returns none, that is a permissions bug until proven otherwise, and it is worth an alert rather than a shrug. The two failure modes here, too few rows and too many, look identical from inside the application, and the second one is how queries that pass review get shipped. The SQL review checklist now has a line for both directions.
None of this is specific to Postgres or to Prisma. It is the general hazard of AI-generated infrastructure work: the code is reviewed as text, while the behaviour depends on a machine state that the text never shows you.