-
-
Notifications
You must be signed in to change notification settings - Fork 721
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BUG]: migrate
isn't protected against simultaneous execution
#874
Comments
migrate
isn't safeguarded of being executed multiple times at the same timemigrate
isn't protected against simultaneous execution
In what case do you see this as a problem? |
@dankochetov I deploy my Node app using Kubernetes, and I have multiple Node instances (pods) in production, and each one of them runs migrations pre-deployment. |
@dankochetov Fixed the concurrency issue with an advisory locking. Would be great if it will be built-in. const advisoryLock = async (
{
key1,
key2,
timeoutInMs = 10_000,
}: { key1: number; key2: number; timeoutInMs?: number },
callback: () => Promise<unknown>,
) => {
const client = await pool.connect();
await client.query(`SELECT pg_advisory_lock($1, $2)`, [key1, key2]);
try {
await Promise.race([
callback(),
new Promise((_, reject) => {
setTimeout(() => {
reject(new Error('Timeout'));
}, timeoutInMs);
}),
]);
} finally {
await client.query(`SELECT pg_advisory_unlock($1, $2)`, [key1, key2]);
client.release();
}
};
const MIGRATE_ADVISORY_LOCKING_KEY1 = 123_456;
const MIGRATE_ADVISORY_LOCKING_KEY2 = 456_789;
export const migrate = async () => {
await advisoryLock(
{
key1: MIGRATE_ADVISORY_LOCKING_KEY1,
key2: MIGRATE_ADVISORY_LOCKING_KEY2,
},
async () => {
await drizzleMigrate(database, {
migrationsFolder: './drizzle',
});
},
);
}; // Now only one migration runs at a time
await Promise.all([migrate(), migrate(), migrate()]); |
It seems like a table lock on It definitely could become problematic if certain migrations are run multiple times on the same database though. |
Indeed, it would be great if it supported the built-in advisory lock. |
Here we are in 2024 and another hyped JS ORM is delivered without a proper migration locking mechanism (cue in laughter from the Laravel/Rails/Django/… communities). I get it: Y'all are using some hyped up database SaaS provider which probably takes care of migrations for you automagically. But believe it or not: There are folks still running their own PostgreSQL clusters and deploying Docker containers themselves. |
DISCLAIMER: I say this as myself, I'm not related to the development team in any way. @soulchild I don't think that your comment adds anything of value to this thread. Sure, it would be great if we had professional-grade tools, high quality software tools are excessively scarce. But let's remember that:
I say this as an open source contributor: some of you are shamelessly disrespectful. It is one thing to point at a problem and debate its importance/priority, and another to mock the projects or its developers. |
Sorry to have come across rude. I didn't mean to attack the maintainers of Drizzle. I'm just perplexed by the sheer number of developers who seemingly over night started bashing Prisma and other established ORMs on their social media channels while simultaneously praising Drizzle as a superior solution, just because it performs better on the edge or something. Issues like this one tell a completely different story, IMHO. There's still lots of stuff missing or broken which other ORMs have figured out a long time ago. I really want to like Drizzle, but in its current state it's just not for me and I'm having a hard time finding advantages over Prisma, which is far from perfect either and has its own set of weird problems/architectural decisions (like basic JOINs still being an experimental feature). So again, apologies to anyone I have offended with my comment. I know that open-source is a lot of blood, sweat and tears and I absolutely appreciate the hard work the Drizzle team is doing! ❤️ |
Thanks, everyone! We are finally going through all the bugs and trying to address all of them. This one is important, and thanks a lot for the link explaining how it should be handled. I'm taking this one on and will try to ship it as fast as I can! |
@AndriiSherman Thanks a lot for taking this on! One minor suggestion which would make the migrations feature even more useful IMHO: The We're running migrations before starting the server and currently there's no way to see whether any migrations were run at all. |
What version of
drizzle-orm
are you using?0.27.0
What version of
drizzle-kit
are you using?0.19.3
Describe the Bug
There's no protection against the
migrate
function being executed simultaneously multiple times.Executing
await Promise.all([migrate(), migrate(), migrate()])
will lead to the migrations running three times and potentially causing problems.Expected behavior
The
migrate
function should be designed to execute only once at any given time, probably by using a locking mechanism.For reference, Prisma uses Advisory Locking.
Environment & setup
Postgres 15.3
The text was updated successfully, but these errors were encountered: