Your supabase migrations worked until the day supabase db push --remote stopped with a sync error, a missing migration, or SQL that wants to create a table production already has.
The app still runs. The database is not empty. The frightening part is that three versions of the truth now exist: the migration files in git, the migration history table in Supabase, and the schema production actually uses.
That split usually appears in AI-built apps after weeks of fast changes. Lovable writes reviewed migration files. Then someone fixes one column in the Table Editor. Another prompt or another AI tool runs SQL directly. A teammate edits a policy in the SQL editor. Each change is small. Together they make the repository stop describing production.
supabase migrations have three states, not two
Most people compare only “files” and “database”. Supabase compares three separate things:
- Migration files in
supabase/migrations/— what git knows. - The migration history table — normally
supabase_migrations.schema_migrations, what Supabase believes was applied. - The real schema — tables, columns, policies, functions and triggers that exist in production right now.
db push does not look at your production schema and reconcile it like magic. It compares local migration files with the remote migration history, then applies missing files in order. If reality was changed by hand, the history can say “not applied” even though the table already exists. If the file was edited after being applied, the file and the history no longer mean the same thing.
The fix is not “run the command again”. First discover which of the three states is wrong.
Why Lovable apps drift even though Lovable writes migrations
Lovable’s own Supabase integration is better than the rumor. When your Lovable project is connected to your own Supabase project, Lovable shows the SQL in chat, asks for approval, executes approved schema changes, saves the migration under supabase/migrations/, and regenerates the app’s TypeScript types.
The problem is everything beside that flow:
- A quick column added in the Supabase Table Editor never becomes a file.
- An RLS policy pasted into the SQL editor is invisible to the repository.
- A function or trigger installed by another AI tool may run, but nobody can rebuild it.
- A migration copied manually into production makes the schema correct while the history table stays wrong.
- Two people or agents push from different machines and one machine does not know the other migration yet.
Supabase’s rule is direct: once you use migrations, remote schema changes go through migration files. Dashboard edits bypass the history and produce db push sync errors.
First: classify the failure before repairing anything
Open the terminal in the repository that contains the supabase folder. Sign in and link the project:
supabase login
supabase link --project-ref YOUR_PROJECT_REF
supabase migration list --remote
Then match the symptom to the state problem:
- Migration missing remotely, schema already exists. The change was applied by hand or by another tool. Do not let push run the same
create tableagain. - Migration marked applied, schema does not exist. The history table is wrong or someone repaired the wrong migration. Push will skip a change production needs.
- db diff or pull shows unexpected SQL. Production contains hand edits that are not represented in files. This is drift, not a CLI bug.
- Migration works locally, fails remotely. Local was rebuilt from files, but remote contains extra objects, dependencies or permissions the files never captured.
- Everything passes, but a new staging project lacks policies or triggers. The schema is in files, but security objects were never captured. Treat it as drift too.
The safe recovery click path
Before repair, pull or push, confirm what you can restore if the evidence is wrong. The supabase backup checklist separates database recovery from storage objects, custom roles and settings — the pieces migration repair does not protect.
- Freeze production edits. Tell every person and AI tool to stop changing the live schema for the duration of the repair. One more hand edit can invalidate the evidence you are collecting.
- List migration state. Run
supabase migration list --remoteand save the output. Mark which files are applied locally, applied remotely, missing remotely, or recorded remotely but unknown locally. - Capture production reality. If remote contains changes that never became files, run
supabase db pull --remote. Read the generated migration before committing it. Look for tables withoutenable row level security, policies you do not recognize, triggers, functions and storage rules. - Commit the baseline. Add the pulled migration files to git with a message like
baseline: capture production schema. This is not cosmetic. It re-establishes the repository as the source of truth. - Repair only the tracking table when needed. If the schema change already exists but the history row is missing, use
supabase migration repair --status applied <migration-timestamp> --remote. If a migration is recorded as applied but never really ran, usesupabase migration repair --status reverted <migration-timestamp> --remote. - Prove the files can rebuild a database. On a local or disposable staging database, run the normal local stack and
supabase db reset. A reset is destructive for that disposable database, which is exactly why it proves the migrations recreate the schema from empty. - Deploy through one controlled path. After the baseline is committed and the history agrees, use
supabase db push --remotefrom one machine or, better, from CI. Do not let two agents push concurrently.
The order is evidence → capture → repair → rebuild → push.
What migration repair does — and what it never does
supabase migration repair sounds like a database repair tool. It is not. It edits only the bookkeeping row that says whether a migration was applied.
If a column already exists because someone added it manually, repair --status applied is correct: you are telling Supabase “do not run this file again; reality already contains it”. If the history claims a migration ran but the object is absent, repair --status reverted is correct: you are telling Supabase “this file still needs to run”.
Repair never fixes SQL or policies. Inspect the real schema; a table missing its RLS policy is exposed, not recovered. Use the supabase rls checklist before closing the incident.
How to keep the AI fast without losing control
Don’t stop using Lovable. Give schema changes one door.
Let Lovable propose SQL and save migration files. Review destructive statements before approval: drop table, drop column, truncate, unscoped delete from, disabled RLS, broad using (true) policies, or grants to anon that the screen does not need. Then stop editing production beside that flow.
For manual work, use the dashboard only on local or staging. Capture the result into a file, test it, commit it, and deploy it through the same path as Lovable-generated migrations. If the AI already deleted production data, schema repair is only half the job; replit database covers the data-loss side.
Production migration checklist
- One repository contains the current
supabase/migrations/folder. migration list --remoteagrees with the files after the baseline.- No one edits the production schema directly in Table Editor or SQL editor.
- Every new table has explicit RLS status and reviewed policies.
- Triggers, functions and storage rules are captured in versioned migrations, not only in the dashboard.
- A disposable database can be rebuilt from the files before production is touched.
- One machine or CI job performs remote pushes; AI agents do not race each other.
- Destructive changes use a reviewed expand-and-contract plan instead of one blind drop.
- After recovery, new feature work goes through the same migration door every time.
The lovable deploy checklist covers the surrounding production settings. The schema itself must be file-driven, not a memory in a browser tab.
If the failure is not the backup itself but the way the app connects after restore or redeploy, use the supabase connection string runtime audit before rotating secrets randomly.
FAQ
Does Lovable create Supabase migration files?
Yes. When a Lovable project is connected to your own Supabase project, Lovable shows the SQL for approval, executes approved schema changes, saves migration files under supabase/migrations and regenerates the TypeScript types. The drift comes from changes made beside that flow, for example in the Supabase Table Editor or SQL editor.
What does supabase migration repair actually do?
It only corrects the migration tracking table. It does not apply SQL and it does not undo SQL. Use repair –status applied when the schema change already exists but the history row is missing, and repair –status reverted when a migration is recorded but was never really applied.
Should I use supabase db reset in production?
No. db reset is for a local or disposable staging database because it rebuilds the database from migrations and destroys the current local state. Production recovery starts with migration list, db pull, a reviewed baseline migration and then controlled db push operations.