|
| 1 | +# Drizzle Tracks Migrations In A Log Table |
| 2 | + |
| 3 | +When I generate (`npx drizzle-kit generate`) and apply (`npx drizzle-kit |
| 4 | +migrate`) schema migrations against my database with Drizzle, there are SQL |
| 5 | +files that get created and run. |
| 6 | + |
| 7 | +How does Drizzle know which SQL files have been run and which haven't? |
| 8 | + |
| 9 | +Like many SQL schema migration tools, it uses a table in the database to record |
| 10 | +this metadata. Drizzle defaults to calling this table `__drizzle_migrations` |
| 11 | +and puts it in the `drizzle` schema (which is like a database namespace). |
| 12 | + |
| 13 | +Let's take a look at this table for a project with two migrations: |
| 14 | + |
| 15 | +```sql |
| 16 | +postgres> \d drizzle.__drizzle_migrations |
| 17 | + Table "drizzle.__drizzle_migrations" |
| 18 | + Column | Type | Collation | Nullable | Default |
| 19 | +------------+---------+-----------+----------+---------------------------------------------------------- |
| 20 | + id | integer | | not null | nextval('drizzle.__drizzle_migrations_id_seq'::regclass) |
| 21 | + hash | text | | not null | |
| 22 | + created_at | bigint | | | |
| 23 | +Indexes: |
| 24 | + "__drizzle_migrations_pkey" PRIMARY KEY, btree (id) |
| 25 | + |
| 26 | +postgres> select * from drizzle.__drizzle_migrations; |
| 27 | + id | hash | created_at |
| 28 | +----+------------------------------------------------------------------+--------------- |
| 29 | + 1 | 8961353bf66f9b3fe1a715f6ea9d9ef2bc65697bb8a5c2569df939a61e72a318 | 1730219291288 |
| 30 | + 2 | b75e61451e2ce37d831608b1bc9231bf3af09e0ab54bf169be117de9d4ff6805 | 1730224013018 |
| 31 | +(2 rows) |
| 32 | +``` |
| 33 | + |
| 34 | +Notice that Drizzle stores each migration record as [a SHA256 hash of the |
| 35 | +migration |
| 36 | +file](https://github.com/drizzle-team/drizzle-orm/blob/526996bd2ea20d5b1a0d65e743b47e23329d441c/drizzle-orm/src/migrator.ts#L52) |
| 37 | +and a timestamp of when the migration was run. |
| 38 | + |
| 39 | +[source](https://orm.drizzle.team/docs/drizzle-kit-migrate#applied-migrations-log-in-the-database) |
0 commit comments