Running complex migrations
I'm a little confused on how to run more complex migrations like backfilling or splitting tables.
Say for a todo app I have a
Currently we're using knex, so in the knex migration file first I'd create the new table, then backfill existing todos into the new table, then drop the columns of the existing table.
In drizzle, it looks like I should first make the changes to the schema, then generate migrations files using drizzle-kit, then manually edit the migration files to add the backfill step.
But what I don't understand is how does drizzle track migrations and make sure that it doesn't run the backfill more than once? It seems to me like when you run migrations through the
This is just one example of a case requiring backfill, but another I can think of is if I need to add a new not nullable foreign key to an existing table.
What is the recommended way of running and keeping track of these migrations?
Say for a todo app I have a
users table with columns id and todo_body. Now I want to split that table into separate users and todos tables to have a many-to-many relation.Currently we're using knex, so in the knex migration file first I'd create the new table, then backfill existing todos into the new table, then drop the columns of the existing table.
In drizzle, it looks like I should first make the changes to the schema, then generate migrations files using drizzle-kit, then manually edit the migration files to add the backfill step.
But what I don't understand is how does drizzle track migrations and make sure that it doesn't run the backfill more than once? It seems to me like when you run migrations through the
migrate() helper, it runs all the migrations every time. This is because drizzle doesn't store the state of which migrations have run in a database table like knex. So every time I run the migration helper, it would try to backfill.This is just one example of a case requiring backfill, but another I can think of is if I need to add a new not nullable foreign key to an existing table.
What is the recommended way of running and keeping track of these migrations?