Updated Migration Process #2624
Replies: 19 comments 11 replies
-
|
Is there an official pattern for implementing multi-tenancy at the database level? At work, we use TypeORM to create a multi-tenancy repository and entity. This involves extending the base repository and intercepting relational query methods to always add a where clause for the |
Beta Was this translation helpful? Give feedback.
-
|
About strict migrations, will the next update provide some way to rewrite existing migrations so they're strict? Also, not sure if it's within this discussion's scope but could be useful to provide docs on how to migrate existing serial columns to make them identity columns instead in Postgres. I assume some developers will look into this and Drizzle helping out in some way, even if just pointing in the right direction via documentation, could be useful. |
Beta Was this translation helpful? Give feedback.
-
|
How about the |
Beta Was this translation helpful? Give feedback.
-
|
@AndriiSherman |
Beta Was this translation helpful? Give feedback.
-
|
@AndriiSherman |
Beta Was this translation helpful? Give feedback.
-
|
would be nice to include the migration sql file name in the __drizzle_migrations table to see what has run at a glance |
Beta Was this translation helpful? Give feedback.
-
Are these changes live now in the current drizzle-orm and drizzle-kit versions? When I Also I cannot find the |
Beta Was this translation helpful? Give feedback.
-
|
Same here. |
Beta Was this translation helpful? Give feedback.
-
|
We are currently facing a challenge running migrations with multiple devs working in multiple feature branches, all of them integrating their work in the traditional "develop, stage, production" branches. Devs currently generate and run migrations locally, for their changes, and commit the migrations folder to git. This of course generates conflicts while merging the journals and snapshots folders. The integration branches are pulled by github actions to deploy to the corresponding environment, and is currently running the migrations that are stored on the migrations folder committed to git (which had to go a troublesome and manual merge conflict resolution process before landing on the repo). This, of course, is not ideal. I'd like for the CI process to introspect the changes of the environment's db, generate a migration against the schema file, and run it, all without any local persistence or "memory". Would this scenario be possible in the new version? |
Beta Was this translation helpful? Give feedback.
-
I'd also recommend not using It's also a common thing that people using Drizzle on PlanetScale end up with duplicate indexes when using serial, which then ends up getting a schema recommendation. |
Beta Was this translation helpful? Give feedback.
-
|
Any updates when? Now it's a blocker for me |
Beta Was this translation helpful? Give feedback.
-
|
please reconsider naming of the status "rollbacked". it sounds like broken english. a better term would be "rolled_back". |
Beta Was this translation helpful? Give feedback.
-
|
would be interesting to know which of the aforementioned points made it into the 0.34.0 release? |
Beta Was this translation helpful? Give feedback.
-
|
@AndriiSherman What is the status on this? |
Beta Was this translation helpful? Give feedback.
-
|
+1 for Implicit Transactions Removal. Hoping there is an update soon. |
Beta Was this translation helpful? Give feedback.
-
|
It would be great to track when a migration was applied with something like |
Beta Was this translation helpful? Give feedback.
-
Thank you for explaining the logic behind this |
Beta Was this translation helpful? Give feedback.
-
|
I am also failing with the issue
Unrecognized options for command 'migrate': --no-init |
Beta Was this translation helpful? Give feedback.
-
|
For Postgres, would be great if object owners defaulted to pg_database_owner to avoid migration issues across environments. |
Beta Was this translation helpful? Give feedback.


Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Migration process urgrade
After a year of gathering feedback, we have collected enough information and identified cases that were not handled properly or need improvement. In the 0.34.0 drizzle ORM, we plan to implement these changes.
In this post, I'll outline everything we have decided to change, along with the motivations behind these changes. If you have any suggestions for additions to this release regarding the migration process or believe something should be modified, please provide a detailed explanation so we can update this discussion post.
This discussion post will serve as a reference for us while adding test cases, necessary logic, etc.
Overview of Changes to the Migration Process:
Migrations Table Status Column
We will add a
statuscolumn to themigrationstable to handlefailed,applied, orrollbackedstatuses for migrations.New shape of the
__drizzle_migrationstable will be:PostgreSQL
MySQL
SQLite
Strict Migrations by Default
All migrations will be strict by default, meaning there will be no
IF NOT EXISTS,DO, or similar statements. We are considering adding a flag to re-enable these, but ideally, all migrations should be strict.The reason it was done with
IF NOT EXISTSis that initially, drizzle-orm and drizzle-kit were built around our own use cases for our own products, even before going public with it, and this legacy remained until now.Without
IF NOT EXISTSstatements, if you have the same table in the database for some reason and create a table in migration, theIF NOT EXISTScase will just let your migration succeed without indicating that you have the same table. This table could potentially have a different structure and might break your backend code. The migration flow should ensure that it will fail in such cases. If you are applying migrations to the database, you won't encounter an issue from not usingIF NOT EXISTSif everything is done correctly. If there is a shift in this process, you will know it from errors after running DDL statements in migrations.You can always add
IF NOT EXISTSif needed, but we don't recommend it unless it is absolutely necessary.Database Introspection and Initial Migration
When you introspect the database, you will receive an initial migration without comments. Instead of commenting it out, we will add a flag to
journalentity with theinitflag, indicating that this migration was generated by introspect actionCurrently, you see this message during the initial migration after introspecting the database
Everything that was generated was commented out, but we will remove it now. Here are a few flows that can be used with introspect. May have forget something or don't know about a specific flow, so would be happy to update it after your comments
After running
drizzle-kit introspect, you will get aschema.tsfile and amigrationfolder with the initial migration. Along with that, drizzle-kit will add a first entry tojournalwith the statusinitindicating that the migration generated from introspect commandThen, you can proceed with a typical
generate-and-migrateflowWhen you run
migrateon a database that already has all the tables from your schema, you need to run it with thedrizzle-kit migrate --no-initflag, which will skip theinitstep. If you run it without this flag and get an error that such tables already exist,drizzle-kitwill detect it and suggest you add this flag.For such a case, we can add a flag that will instruct
drizzle-kitto just createschema.tswithout creating any migration folders or adding entries to the database.It can be named something like this, but it's not a final name
For example, you have
productionanddevelopmentdatabases with the same set of data. The basic process would be for you to:drizzle-kitwill generateschema.tsand amigrationsfolder. Thejournalentity will have a type of migration:init. The kit will use this in the next steps.migrateon a database that already has all the tables from your schema, you need to run it with thedrizzle-kit migrate --no-initflag, which will skip theinitstep. If you run it without this flag and get an error that such tables already exist,drizzle-kitwill detect it and suggest you add this flag.--no-initflag as well.drizzle-kit migrate, and all the migrations together withinitwill be appliedImplicit Transactions Removal
We will remove implicit transactions for migration files, as there are cases where they are unnecessary or even impossible, such as with
index concurrently. Instead, we will addBEGINandCOMMITstatements inside the generated SQL files, which you can remove if needed. We also won’t addBEGINandCOMMITstatements for operations that are not supported by the database. We are considering adding a flag to exclude transactions by default.Handling Failed Migrations
If a migration fails and no transaction was applied, we will mark this migration as
failedin the database. You will need to manually fix your schema before proceeding.Switch to Identity Columns in Postgres
For the Postgres database, we will migrate from the
serialtype in the__drizzle_migrationstable toidentity columns, as recommended by the Postgres team. This change will also make it fully compatible with Xata and CockroachDB.Proper Lock Mechanism
We will implement a proper lock mechanism to handle cases where several identical
migrate()functions are executed simultaneously.We will implement suggestions from this issue: #874
Summary
If there is anything you would like to add, change, or suggest, feel free to add a comment here. I am open to discussing everything before implementing this in the next release.
Beta Was this translation helpful? Give feedback.
All reactions