N
Neon2y ago
wee-brown

Guide on how to apply migrations (Nextjs/ @neondatabase/serverless)

Recently decided to try Neon for my portfolio V2 api. I'm working with Nextjs 14, drizzle and Neon. I'm having difficulties applyinig migrations to Neon locally. Here is my migrate file
import { neonConfig } from "@neondatabase/serverless";
import { migrate } from "drizzle-orm/neon-http/migrator";
import { db } from "./libs/db";

(async () => {
neonConfig.fetchConnectionCache = true;

await migrate(db, { migrationsFolder: "./migrations" });

try {
await migrate(db, { migrationsFolder: "/src/migrations" });
console.log("Done");
} catch (e) {
console.log(e);
}
})();
import { neonConfig } from "@neondatabase/serverless";
import { migrate } from "drizzle-orm/neon-http/migrator";
import { db } from "./libs/db";

(async () => {
neonConfig.fetchConnectionCache = true;

await migrate(db, { migrationsFolder: "./migrations" });

try {
await migrate(db, { migrationsFolder: "/src/migrations" });
console.log("Done");
} catch (e) {
console.log(e);
}
})();
In my package.json i have this script to execute the migrate file in my src dir
"migrations": "npx drizzle-kit generate:pg",
"apply-migrations": "tsx ./src/migrate.ts"
"migrations": "npx drizzle-kit generate:pg",
"apply-migrations": "tsx ./src/migrate.ts"
I'm not sure what the issue is but whenever i run this command, It throws error
Error: Can't find meta/_journal.json file
at readMigrationFiles (/Users/mac/Web/Portfolio/ogdev/node_modules/src/migrator.ts:40:9)
at migrate (/Users/mac/Web/Portfolio/ogdev/node_modules/src/neon-http/migrator.ts:18:21)
at <anonymous> (/Users/mac/Web/Portfolio/ogdev/src/migrate.ts:16:11)
at import_serverless (/Users/mac/Web/Portfolio/ogdev/src/migrate.ts:21:1)
at Object.<anonymous> (/Users/mac/Web/Portfolio/ogdev/src/migrate.ts:21:4)
at Module._compile (node:internal/modules/cjs/loader:1256:14)
at Object.S (/usr/local/lib/node_modules/tsx/dist/cjs/index.cjs:1:1292)
Error: Can't find meta/_journal.json file
at readMigrationFiles (/Users/mac/Web/Portfolio/ogdev/node_modules/src/migrator.ts:40:9)
at migrate (/Users/mac/Web/Portfolio/ogdev/node_modules/src/neon-http/migrator.ts:18:21)
at <anonymous> (/Users/mac/Web/Portfolio/ogdev/src/migrate.ts:16:11)
at import_serverless (/Users/mac/Web/Portfolio/ogdev/src/migrate.ts:21:1)
at Object.<anonymous> (/Users/mac/Web/Portfolio/ogdev/src/migrate.ts:21:4)
at Module._compile (node:internal/modules/cjs/loader:1256:14)
at Object.S (/usr/local/lib/node_modules/tsx/dist/cjs/index.cjs:1:1292)
I'm new to neon and don't know what i'm missing. Woulkd appreciate any resource/guide to achieving this Thanks
2 Replies
frail-apricot
frail-apricot2y ago
@Ogbonna is /src/migrations being interpreted as an absolute path? Drop the leading slash, so it looks like src/migrations and try again. Also, is there a reason you call it twice? Generally when dealing with paths, it's good to resolve them. Maybe something like this could help you?
import { join } from 'node:path'
const migrationsFolder = join(process.cwd(), 'src/migrations')

// This should print an absolute path, that correctly points to your
// migrations. If it doesn't, adjust accordingly. Instead of process.cwd()
// you could try using __dirname (or the module syntax compatible equivalent)
console.log(`resolved migrations folder is: ${migrationsFolder}`)

await migrate(db, { migrationsFolder });
import { join } from 'node:path'
const migrationsFolder = join(process.cwd(), 'src/migrations')

// This should print an absolute path, that correctly points to your
// migrations. If it doesn't, adjust accordingly. Instead of process.cwd()
// you could try using __dirname (or the module syntax compatible equivalent)
console.log(`resolved migrations folder is: ${migrationsFolder}`)

await migrate(db, { migrationsFolder });
national-gold
national-gold2y ago
If you have a defineConfig defined, make sure you're actually defining the target directory there. Example
import { defineConfig } from "drizzle-kit";
import { env } from "@/env";

export default defineConfig({
dialect: "postgresql",
schema: "./src/server/db/schema.ts",
out: "./src/server/db/migrations",
dbCredentials: {
url: env.DATABASE_URL,
},
});
import { defineConfig } from "drizzle-kit";
import { env } from "@/env";

export default defineConfig({
dialect: "postgresql",
schema: "./src/server/db/schema.ts",
out: "./src/server/db/migrations",
dbCredentials: {
url: env.DATABASE_URL,
},
});

Did you find this page helpful?