Run migration with Drizzle for Neon

Sorry if this is a n00b question, but I'm having some troubles running a migration of a Neon database locally with the env.js. I'm trying to run this with the command pnpm run db:migrate in the terminal This is the script in package.json: "db:migrate": "tsx ./src/server/db/global/migrate.ts" This gives me an error about the environment variables, anyone have an idea what I could do to fix this? : I only need the DATABASE_URL in this migration and this is defined so I'm not sure why I'm getting an error on this.
> tsx ./src/server/db/global/migrate.ts

❌ Invalid environment variables: {
DATABASE_URL: [ 'Required' ],
NEXT_PUBLIC_POSTHOG_HOST: [ 'Required' ],
NEXT_PUBLIC_POSTHOG_KEY: [ 'Required' ]
}
file:///home/woet/prioflux/finisync/node_modules/.pnpm/@[email protected][email protected][email protected]/node_modules/@t3-oss/env-core/dist/index.js:29
throw new Error("Invalid environment variables");
^

Error: Invalid environment variables
at onValidationError (file:///home/woet/prioflux/finisync/node_modules/.pnpm/@[email protected][email protected][email protected]/node_modules/@t3-oss/env-core/dist/index.js:29:15)
at createEnv (file:///home/woet/prioflux/finisync/node_modules/.pnpm/@[email protected][email protected][email protected]/node_modules/@t3-oss/env-core/dist/index.js:35:16)
at createEnv (file:///home/woet/prioflux/finisync/node_modules/.pnpm/@[email protected][email protected][email protected]/node_modules/@t3-oss/env-nextjs/dist/index.js:12:12)
at file:///home/woet/prioflux/finisync/src/env.js:4:20
at ModuleJob.run (node:internal/modules/esm/module_job:192:25)
at async CustomizedModuleLoader.import (node:internal/modules/esm/loader:228:24)
at async loadESM (node:internal/process/esm_loader:40:7)
at async handleMainPromise (node:internal/modules/run_main:66:12)

Node.js v20.5.1
 ELIFECYCLE  Command failed with exit code 1.
> tsx ./src/server/db/global/migrate.ts

❌ Invalid environment variables: {
DATABASE_URL: [ 'Required' ],
NEXT_PUBLIC_POSTHOG_HOST: [ 'Required' ],
NEXT_PUBLIC_POSTHOG_KEY: [ 'Required' ]
}
file:///home/woet/prioflux/finisync/node_modules/.pnpm/@[email protected][email protected][email protected]/node_modules/@t3-oss/env-core/dist/index.js:29
throw new Error("Invalid environment variables");
^

Error: Invalid environment variables
at onValidationError (file:///home/woet/prioflux/finisync/node_modules/.pnpm/@[email protected][email protected][email protected]/node_modules/@t3-oss/env-core/dist/index.js:29:15)
at createEnv (file:///home/woet/prioflux/finisync/node_modules/.pnpm/@[email protected][email protected][email protected]/node_modules/@t3-oss/env-core/dist/index.js:35:16)
at createEnv (file:///home/woet/prioflux/finisync/node_modules/.pnpm/@[email protected][email protected][email protected]/node_modules/@t3-oss/env-nextjs/dist/index.js:12:12)
at file:///home/woet/prioflux/finisync/src/env.js:4:20
at ModuleJob.run (node:internal/modules/esm/module_job:192:25)
at async CustomizedModuleLoader.import (node:internal/modules/esm/loader:228:24)
at async loadESM (node:internal/process/esm_loader:40:7)
at async handleMainPromise (node:internal/modules/run_main:66:12)

Node.js v20.5.1
 ELIFECYCLE  Command failed with exit code 1.
7 Replies
Woet
WoetOP8mo ago
This is my migration file:
import { env } from "~/env";
import { drizzle } from "drizzle-orm/neon-http";
import { migrate } from "drizzle-orm/neon-http/migrator";
import { neon } from "@neondatabase/serverless";

const sql: ReturnType<typeof neon> | undefined = neon(env.DATABASE_URL);

const db = drizzle(sql);

const main = async () => {
try {
console.log("DATABASE URL");
console.log(env.DATABASE_URL);
await migrate(db, {
migrationsFolder: "src/db/global/migrations",
});

console.log("Migration complete!");
} catch (error) {
console.error("Migration failed", error);
}
};

await main();
import { env } from "~/env";
import { drizzle } from "drizzle-orm/neon-http";
import { migrate } from "drizzle-orm/neon-http/migrator";
import { neon } from "@neondatabase/serverless";

const sql: ReturnType<typeof neon> | undefined = neon(env.DATABASE_URL);

const db = drizzle(sql);

const main = async () => {
try {
console.log("DATABASE URL");
console.log(env.DATABASE_URL);
await migrate(db, {
migrationsFolder: "src/db/global/migrations",
});

console.log("Migration complete!");
} catch (error) {
console.error("Migration failed", error);
}
};

await main();
Astra
Astra8mo ago
The error you're giving is that you don't have 3 ENV variables defined, not necessarily the migration file
Woet
WoetOP8mo ago
Hmm all these variables are defined in my .env file though But for some reason they're not picked up when running the scripts They get picked up at runtime, but just not in this script
barry
barry8mo ago
@Woet pnpm/npm/yarn add dotenv Inside of env js config file at the top
+ import 'dotenv/config'
+ import 'dotenv/config'
Woet
WoetOP8mo ago
thanks @barry ! that idd did the trick first time building some with the t3-stack, was expecting it to already take care of the need to import "dotenv/config";
barry
barry8mo ago
nextjs handles it for you, but as you're outside of next when running the script on your own there's nothing to handle environment variables from .env
Woet
WoetOP8mo ago
makes sense, thanks!
Want results from more Discord servers?
Add your server