How to apply migrations on postgres in a serverless enviroment?

Hello, I want to apply postgres migrations to my vercel DB. I see that the push command does not work for postgres and am therefore attempting to use the migrate function like so:
// ./src/db/index.ts
import { sql } from "@vercel/postgres";
import { drizzle } from "drizzle-orm/vercel-postgres";
import { migrate } from "drizzle-orm/vercel-postgres/migrator";
import * as schema from "./schema";

export const db = drizzle(sql, { schema });

await migrate(db, { migrationsFolder: "../../drizzle" });
// ./src/db/index.ts
import { sql } from "@vercel/postgres";
import { drizzle } from "drizzle-orm/vercel-postgres";
import { migrate } from "drizzle-orm/vercel-postgres/migrator";
import * as schema from "./schema";

export const db = drizzle(sql, { schema });

await migrate(db, { migrationsFolder: "../../drizzle" });
However this does not seem like the best way to do this as that would make it so migrations are applied only one I am attempting to make a call instead of at something like build time as far as I can tell. Additionally, it is additional imports that will have to be loaded in the serverless and/or edge environment. Is there a better way to do this?
NinjaBunny
NinjaBunny322d ago
you have to migrate the schema to the database
NinjaBunny
NinjaBunny322d ago
or if you're using pg you would swap out the conection connection
import { drizzle } from "drizzle-orm/node-postgres";
import { migrate } from "drizzle-orm/node-postgres/migrator";
import * as dotenv from "dotenv";
import { Client } from "pg";

dotenv.config({
path: `.env.${process.env.NODE_ENV}`,
});

const main = async () => {
console.log("running migration...");
const config = {
host: process.env.PG_HOST,
database: process.env.PG_DATABASE,
user: process.env.PG_USER,
password: process.env.PG_PASSWORD,
port: process.env.PG_PORT,
};

const connectionString = `postgres://${config.user}:${config.password}@${
config.host
}${process.env.NODE_ENV === "docker" ? `:${config.port}` : ""}/${
config.database
}`;

const client = new Client({
connectionString,
ssl:
process.env.NODE_ENV === "production"
? true
: process.env.NODE_ENV === "development"
? true
: false,
});

await client.connect();

const db = drizzle(client);

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

main()
.catch((error) => {
console.error(error);
})
.then(() => {
console.log("migration complete");
})
.finally(() => {
process.exit(1);
});
import { drizzle } from "drizzle-orm/node-postgres";
import { migrate } from "drizzle-orm/node-postgres/migrator";
import * as dotenv from "dotenv";
import { Client } from "pg";

dotenv.config({
path: `.env.${process.env.NODE_ENV}`,
});

const main = async () => {
console.log("running migration...");
const config = {
host: process.env.PG_HOST,
database: process.env.PG_DATABASE,
user: process.env.PG_USER,
password: process.env.PG_PASSWORD,
port: process.env.PG_PORT,
};

const connectionString = `postgres://${config.user}:${config.password}@${
config.host
}${process.env.NODE_ENV === "docker" ? `:${config.port}` : ""}/${
config.database
}`;

const client = new Client({
connectionString,
ssl:
process.env.NODE_ENV === "production"
? true
: process.env.NODE_ENV === "development"
? true
: false,
});

await client.connect();

const db = drizzle(client);

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

main()
.catch((error) => {
console.error(error);
})
.then(() => {
console.log("migration complete");
})
.finally(() => {
process.exit(1);
});
Want results from more Discord servers?
Add your server
More Posts
Error when trying to generate a migration schemaanyone any ideas to what the problem is? It was working fine yesterday and now it throws an error whModelling self relationsI have a table `categories` with a parent fields: ``` { id: text("id").notNull().primaryKey(), .Issue running migrations to DBI am having an issue running migrations to Neon. I checked that everything is exporting const variabIt is possible to have prepared statements inside transactions?Is there a way to insert prepared queries inside a transaction ?type config findMany or findFirstHi, I've one question i don't find the way to type correctly findMany() Like: export async functionAny idea on how to pass a pool from postgres-pool to drizzle?Passing a single client works, but for better connection management it would help to be able to passHow to get another linked tables count?I am new to Drizzle ORM and I wanted to get another tables data in my api. This is my api ```ts impI created a next auth adapter for postgres-jsThe open PR was not working for me so I created a new adapter. I added it to a starter project so pe.prepare() in Next.js App RouterHello! I'm trying to use PostgresQL prepared statement in the Next.js App Router and I have been getDeleting records w/ sqlite-core.Hello all, hoping to find some clarity/direction. So I'm trying to delete a record using; ```ts expCan't figure out how to design relational queryI have three tables - categories - products - media I want to retrieve products that belong to a padrizzle-zod@0.4.4 with drizzle-orm@0.26.5added drizzle-zod to my project and I am getting this error when starting the dev server - error ./