Automatically apply migrations on serverless deploy

I've got a basic Todo app using Turso DB and Nuxt: https://github.com/Amar-Gill/nuxt-todo I'm wondering if there is a way to automatically apply migrations when I deploy to Vercel? The build command is npm run build. Right now, I open a shell into my Turso DB and paste the migration scripts. I might have to wait for a migration runner in drizzle-kit to ship: https://orm.drizzle.team/kit-docs/faq#how-do-i-apply-migrations-generated-by-drizzle-kit
GitHub
GitHub - Amar-Gill/nuxt-todo
Contribute to Amar-Gill/nuxt-todo development by creating an account on GitHub.
3 Replies
AGill
AGill11mo ago
part of the challenge of using the migrate function is that the _journal.json file is not preserved in a nuxt build output
AGill
AGill11mo ago
here's an example using nitro plugin system: https://github.com/Atinux/nuxt-todos-edge/blob/main/server/plugins/migrations.ts but again only for dev -- on prod the _journal.json file is not available
GitHub
nuxt-todos-edge/server/plugins/migrations.ts at main · Atinux/nuxt-...
A todos application with user authentication, SSR and SQLite, working on the edge. - Atinux/nuxt-todos-edge
AGill
AGill11mo ago
can also use nuxt build time hooks:
import { createDb } from "./server/utils/drizzle/create-db";
import { migrate } from "drizzle-orm/libsql/migrator";

// https://nuxt.com/docs/api/configuration/nuxt-config
export default defineNuxtConfig({
devtools: { enabled: true },
runtimeConfig: {
tursoDbUrl: "file:./server/sqlite.db",
tursoDbToken: "",
},
hooks: {
ready(nuxt) {
const runtimeConfig = nuxt.options.runtimeConfig;

const db = createDb(runtimeConfig);

console.log("Migrating database...");
migrate(db, { migrationsFolder: "./drizzle" })
.then(() => console.log("Database migrated."))
.catch(console.error);
},
},
});
import { createDb } from "./server/utils/drizzle/create-db";
import { migrate } from "drizzle-orm/libsql/migrator";

// https://nuxt.com/docs/api/configuration/nuxt-config
export default defineNuxtConfig({
devtools: { enabled: true },
runtimeConfig: {
tursoDbUrl: "file:./server/sqlite.db",
tursoDbToken: "",
},
hooks: {
ready(nuxt) {
const runtimeConfig = nuxt.options.runtimeConfig;

const db = createDb(runtimeConfig);

console.log("Migrating database...");
migrate(db, { migrationsFolder: "./drizzle" })
.then(() => console.log("Database migrated."))
.catch(console.error);
},
},
});