NeonDB: Migration Taking Long

Hi, I have been using dirrzle with neon DB, and it has been a great experience. However, once I changed my schema a lil bit and migrate again, it was taking extremely long time and query is never executed. db/index.ts
import { neon, neonConfig } from "@neondatabase/serverless";
import { drizzle } from "drizzle-orm/neon-http";
import * as schema from "@/db/schema";
import "dotenv/config";

// create database connection

neonConfig.fetchConnectionCache = true;

const connection = neon(process.env.DATABASE_URL!);

export const db = drizzle(connection, { schema: schema });
import { neon, neonConfig } from "@neondatabase/serverless";
import { drizzle } from "drizzle-orm/neon-http";
import * as schema from "@/db/schema";
import "dotenv/config";

// create database connection

neonConfig.fetchConnectionCache = true;

const connection = neon(process.env.DATABASE_URL!);

export const db = drizzle(connection, { schema: schema });
db/migrate.ts
import { migrate } from "drizzle-orm/neon-http/migrator";
import { neon, neonConfig } from "@neondatabase/serverless";
import { drizzle } from "drizzle-orm/neon-http";

import "dotenv/config";

// inspired by Raphael Moreau @rphlmr for Postgres, extended for Planetscale
const runMigrate = async () => {
if (!process.env.DATABASE_URL) {
throw new Error("DATABASE_URL is not defined");
}

neonConfig.fetchConnectionCache = true;

const connection = neon(process.env.DATABASE_URL!);

const db = drizzle(connection);

console.log("⏳ Running migrations...");

const start = Date.now();

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

const end = Date.now();

console.log(`✅ Migrations completed in ${end - start}ms`);

process.exit(0);
};

runMigrate().catch((err) => {
console.error("❌ Migration failed");
console.error(err);
process.exit(1);
});
import { migrate } from "drizzle-orm/neon-http/migrator";
import { neon, neonConfig } from "@neondatabase/serverless";
import { drizzle } from "drizzle-orm/neon-http";

import "dotenv/config";

// inspired by Raphael Moreau @rphlmr for Postgres, extended for Planetscale
const runMigrate = async () => {
if (!process.env.DATABASE_URL) {
throw new Error("DATABASE_URL is not defined");
}

neonConfig.fetchConnectionCache = true;

const connection = neon(process.env.DATABASE_URL!);

const db = drizzle(connection);

console.log("⏳ Running migrations...");

const start = Date.now();

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

const end = Date.now();

console.log(`✅ Migrations completed in ${end - start}ms`);

process.exit(0);
};

runMigrate().catch((err) => {
console.error("❌ Migration failed");
console.error(err);
process.exit(1);
});
package.json
"scripts": {
"start": "tsx --watch src/index.ts",
"test": "tsx src/test.ts",
"db:migrate": "drizzle-kit generate:pg",
"db:studio": "drizzle-kit studio",
"db:push": "tsx db/migrate.ts",
"db:drop": "drizzle-kit drop --config=drizzle.config.ts"
}
"scripts": {
"start": "tsx --watch src/index.ts",
"test": "tsx src/test.ts",
"db:migrate": "drizzle-kit generate:pg",
"db:studio": "drizzle-kit studio",
"db:push": "tsx db/migrate.ts",
"db:drop": "drizzle-kit drop --config=drizzle.config.ts"
}
4 Replies
tomeverson
tomeversonOP16mo ago
after like a 10 min
❌ Migration failed
NeonDbError: Error connecting to database: fetch failed
at <anonymous> (/Users/tom/Desktop/server/node_modules/@neondatabase/serverless/index.js:1523:86)
at migrate (/Users/tom/Desktop/server/node_modules/src/neon-http/migrator.ts:26:2)
at runMigrate (/Users/tom/Desktop/server/db/migrate.ts:23:3) {
code: null,
sourceError: TypeError: fetch failed
at Object.fetch (node:internal/deps/undici/undici:11576:11)
at <anonymous> (/Users/tom/Desktop/server/node_modules/@neondatabase/serverless/index.js:1521:61)
at migrate (/Users/tom/Desktop/server/node_modules/src/neon-http/migrator.ts:26:2)
at runMigrate (/Users/tom/Desktop/server/db/migrate.ts:23:3) {
cause: HeadersTimeoutError: Headers Timeout Error
at Timeout.onParserTimeout [as callback] (node:internal/deps/undici/undici:9748:32)
at Timeout.onTimeout [as _onTimeout] (node:internal/deps/undici/undici:8047:17)
at listOnTimeout (node:internal/timers:569:17)
at process.processTimers (node:internal/timers:512:7) {
code: 'UND_ERR_HEADERS_TIMEOUT'
}
}
}
❌ Migration failed
NeonDbError: Error connecting to database: fetch failed
at <anonymous> (/Users/tom/Desktop/server/node_modules/@neondatabase/serverless/index.js:1523:86)
at migrate (/Users/tom/Desktop/server/node_modules/src/neon-http/migrator.ts:26:2)
at runMigrate (/Users/tom/Desktop/server/db/migrate.ts:23:3) {
code: null,
sourceError: TypeError: fetch failed
at Object.fetch (node:internal/deps/undici/undici:11576:11)
at <anonymous> (/Users/tom/Desktop/server/node_modules/@neondatabase/serverless/index.js:1521:61)
at migrate (/Users/tom/Desktop/server/node_modules/src/neon-http/migrator.ts:26:2)
at runMigrate (/Users/tom/Desktop/server/db/migrate.ts:23:3) {
cause: HeadersTimeoutError: Headers Timeout Error
at Timeout.onParserTimeout [as callback] (node:internal/deps/undici/undici:9748:32)
at Timeout.onTimeout [as _onTimeout] (node:internal/deps/undici/undici:8047:17)
at listOnTimeout (node:internal/timers:569:17)
at process.processTimers (node:internal/timers:512:7) {
code: 'UND_ERR_HEADERS_TIMEOUT'
}
}
}
Andrii Sherman
Andrii Sherman16mo ago
It means something with database connection and not Drizzle itself
spigelli
spigelli16mo ago
@tomeverson Did you ever figure out what was going on with neon?
tomeverson
tomeversonOP16mo ago
idk why I reset the connection string and it works for now
Want results from more Discord servers?
Add your server