drizzle-team / drizzle-orm

Headless TypeScript ORM with a head. Runs on Node, Bun and Deno. Lives on the Edge and yes, it's a JavaScript ORM too 😅
https://orm.drizzle.team
Apache License 2.0
24.54k stars 643 forks source link

[FEATURE]: Wrap (sqlite?) migrations in transaction #1559

Closed luc-tielen closed 11 months ago

luc-tielen commented 11 months ago

Describe what you want

Hi,

I've been trying out drizzle together with turso and really liking it so far! But one thing I did seem to miss: there's no transaction blocks in the SQL generated by drizzlekit generate:sqlite? This could be nice in case a migration fails halfway through the transaction?

For example, this is some SQL it generated for a "address" table:

CREATE TABLE `addresses` (
    `id` integer PRIMARY KEY NOT NULL,
    `street_name` text NOT NULL,
    `street_number` text NOT NULL,
    `zip_code` text NOT NULL,
    `city` text NOT NULL
);

Could the generator wrap each of the generated .sql files in a transaction block? Also, I only checked with sqlite, but maybe all SQL engines are affected?

Angelelz commented 11 months ago

The transactions are in fact run inside a transaction. For Sqlite the relevant code would is here. Are you having issues? This is if you run the in the migrate function. If you run them any other way, you'll need to create the transaction yourself. I haven't come across an ORM that writes transaction statements in the generated sql migration itself.

luc-tielen commented 11 months ago

Oh, you're right, I'm stupid. When prototyping something I used a quick shell script to apply the SQL, but that is indeed outside of drizzle. Oops!