I think I don't really understand migrations local sqlite.

It's a local sqlite db (DB_URL=file:dev.db) in a nextjs & trpc setup. I'm trying drizzle for the first time & I did follow the steps mentioned https://github.com/drizzle-team/drizzle-orm/blob/main/drizzle-orm/src/sqlite-core/README.md#-quick-start. I ran npx drizzle-kit generate:sqlite & it succesfully generated a migration. Good Job Drizzle! The problem is how do I push these migrations to my dev.db file? Do I need to push them manually? Am I missing something? I was planning to later use Turso & switch to it but even in the case of turso, I think I would have to manually write each migration in the db shell. Drizzle Kit does not have a push command for sqlite. I'm using drizzle-kit: v0.17.6 drizzle-orm: v0.25.4. Any help is appreciated as I'm tired reading docs & searching for similar issues here.
9 Replies
Dan
Dan2y ago
Currently, the migrations can only be run from the code. The documentation is a bit confusing right now, but you can read about it here: https://github.com/drizzle-team/drizzle-orm/blob/main/drizzle-orm/src/sqlite-core/README.md#-migrations
GitHub
drizzle-orm/README.md at main · drizzle-team/drizzle-orm
TypeScript ORM for SQL. Contribute to drizzle-team/drizzle-orm development by creating an account on GitHub.
isitayush
isitayushOP2y ago
Oh, Okay. I guess until this is taken care of by drizzle-kit push:... command (something similar to what prisma does). It would be impossible to make this work with serverless frameworks (like nextjs) without manually pushing your migrations to db each time you make a change ie. generate new migrations. For now it's a deal breaker & I think I would have to fallback to using prisma but I really wish it's solved soon as would instantly take it over prisma any day. Thank you for the help though Dan!
tacomanator
tacomanator2y ago
It's not hard to just write a small script to execute the code... takes about 2 minutes.
Andrii Sherman
yeah, you can just write a script.ts that is going to run migration from examples above. It would be mostly the same as using db push. You will just get 1 more table in database It's the same as using wrangler with d1 @isitayush but good news are I'm working this month on having db push/db pull available for SQLite, so your experience with Turso + Drizzle will be even better If db push is important and the only thing to go for you - you can follow #kit-releases to get updates about db push/db pull for SQLite available. It a priority for me right now
isitayush
isitayushOP2y ago
It totally slipped from my head that I could include a script such as ensure_migrations.ts with tsc that runs before dev or start alongisde my next app. A native solution in drizzle-kit would still be awesome but this workaround seems to work too. Welp! Thank you Dan & Andrew. I hope you will bump up 'covers ~95% of the common cases' line to '99%' when db:push ships later this month. Goodluck!
Andrii Sherman
Yeah, drizzle-kit will have db push as well as running migrations files from cli command
volks
volks2y ago
How do you run the script.ts? I know its a weird question but I have no nice way of running .ts scripts because of esm, importing other files, and so on. I even wrote a stack overflow question about this exact usecase
Stack Overflow
Typescript Script, ERR_MODULE_NOT_FOUND, running a script with ts-n...
I am pulling my hair out over a very simple usecase. I have a monorepo and inside the monorepo I have a package that has some utilities for my database and the schemas. I want to write a script tha...
Dan
Dan2y ago
npm
tsx
TypeScript Execute (tsx): Node.js enhanced with esbuild to run TypeScript & ESM files. Latest version: 3.12.7, last published: 16 days ago. Start using tsx in your project by running npm i tsx. There are 71 other projects in the npm registry using tsx.
isitayush
isitayushOP2y ago
Hey @bloberenober, Just a small question. How do I set a 'gen_random_uuid()' function as a default value on a custom type? I have the following:
const serial_uuid = customType<
{
data: string;
driverData: string;
notNull: true;
default: true;
}
>({
dataType(config) {
return 'UUID';
},

});
const serial_uuid = customType<
{
data: string;
driverData: string;
notNull: true;
default: true;
}
>({
dataType(config) {
return 'UUID';
},

});
& it generates a following migration, "id" UUID PRIMARY KEY NOT NULL,. I'm looking for it to be something like this. "id" UUID PRIMARY KEY NOT NULL DEFAULT gen_random_uuid() Okay, Figured It out! It's .default(sql``) . : )
Want results from more Discord servers?
Add your server