How to automatically run migration on NextJS start

The docs is not clear on how to do it. It still uses npx tsx src/index.ts which is a CLI command instead of runtime.
No description
3 Replies
Eco 🌤
Eco 🌤OP•4w ago
No description
Eco 🌤
Eco 🌤OP•4w ago
No description
Eco 🌤
Eco 🌤OP•4w ago
instructions is not clear. I was able to make it work using instrumentation.ts instrumentation.ts
export async function register() {
if (process.env.NEXT_RUNTIME === "nodejs") {
await import("./sentry.server.config");
await import("./server/db/migrate");
await import("./server/db/seed");
}

if (process.env.NEXT_RUNTIME === "edge") {
await import("./sentry.edge.config");
}
}
export async function register() {
if (process.env.NEXT_RUNTIME === "nodejs") {
await import("./sentry.server.config");
await import("./server/db/migrate");
await import("./server/db/seed");
}

if (process.env.NEXT_RUNTIME === "edge") {
await import("./sentry.edge.config");
}
}
migrate.ts
import path from "path";
import { migrate } from "drizzle-orm/mysql2/migrator";
import { db } from "./";

async function run() {
console.log("Running migrations...");
await migrate(db, {
migrationsFolder: path.join(process.cwd(), "server/db/migrations"),
});
console.log("Migrations done!");
}

run().catch((error) => {
console.error(error, "MIGRATION_FAILED");
process.exit(1);
});
import path from "path";
import { migrate } from "drizzle-orm/mysql2/migrator";
import { db } from "./";

async function run() {
console.log("Running migrations...");
await migrate(db, {
migrationsFolder: path.join(process.cwd(), "server/db/migrations"),
});
console.log("Migrations done!");
}

run().catch((error) => {
console.error(error, "MIGRATION_FAILED");
process.exit(1);
});
Want results from more Discord servers?
Add your server