Best practice on running migrations

I have a serverless framework setup and during my testing I've been using the db push but now that I have something setup to actually test my migrations I'd like to run:
await migrate(db, { migrationsFolder: "drizzle" });
await migrate(db, { migrationsFolder: "drizzle" });
whats the best path to actually running this from an npm script or from a ci for example. Or even locally without using push. Basically, I've generated my sql migrations but I now need to run them.
12 Replies
zendev
zendev16mo ago
I’ve been wondering the same thing, keen to hear what ppl say
pandareaper
pandareaper16mo ago
We currently have a lambda function dedicated to running migrations, using await migrate(...). At least at the time, drizzle wasn't safe for running migrations across multiple machines concurrently AFAIK. We run this manually at the moment, but soon plan on making it a custom cloudformation resource so that it runs as part of our CDK deployment (I'm sure the same can be done in sls). Or a more trivial approach, we will just invoke it as part of our deployment pipeline
rphlmr ⚡
rphlmr ⚡16mo ago
I'm doing that on ci/manually.
No description
rphlmr ⚡
rphlmr ⚡16mo ago
/* eslint-disable no-console */
import { drizzle } from "drizzle-orm/postgres-js";
import { migrate } from "drizzle-orm/postgres-js/migrator";
import postgres from "postgres";

async function runMigrate() {
if (!process.env.DATABASE_URL) {
throw new Error("DATABASE_URL is not set");
}

const migrationsClient = postgres(process.env.DATABASE_URL, {
max: 1,
});

const db = drizzle(migrationsClient, { logger: true });

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

const start = Date.now();

await migrate(db, { migrationsFolder: `${__dirname}/migrations` });

const end = Date.now();

console.log(`✅ Migration end & took ${end - start}ms`);

process.exit(0);
}

runMigrate().catch((err) => {
console.error("❌ Migration failed");
console.error(err);
process.exit(1);
});
/* eslint-disable no-console */
import { drizzle } from "drizzle-orm/postgres-js";
import { migrate } from "drizzle-orm/postgres-js/migrator";
import postgres from "postgres";

async function runMigrate() {
if (!process.env.DATABASE_URL) {
throw new Error("DATABASE_URL is not set");
}

const migrationsClient = postgres(process.env.DATABASE_URL, {
max: 1,
});

const db = drizzle(migrationsClient, { logger: true });

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

const start = Date.now();

await migrate(db, { migrationsFolder: `${__dirname}/migrations` });

const end = Date.now();

console.log(`✅ Migration end & took ${end - start}ms`);

process.exit(0);
}

runMigrate().catch((err) => {
console.error("❌ Migration failed");
console.error(err);
process.exit(1);
});
I remember it is planned to be added in drizzle-kit Or some of us will add that when it is open sourced
zendev
zendev16mo ago
Looks great! Probably going to use this
imoby
imobyOP16mo ago
thank you! I was having issues with running it locally especially because it needed to be transpiled first. I"ll try this out but would love to still have a local alternative
rphlmr ⚡
rphlmr ⚡16mo ago
yea you need to run it with ts-node (like in my screenshot) or tsx or tsm 🫣
imoby
imobyOP16mo ago
@Raphaël M (@rphlmr) ⚡ this might be a silly question but how did you get around this specific error:
SyntaxError: Cannot use import statement outside a module
at internalCompileFunction (node:internal/vm:73:18)
at wrapSafe (node:internal/modules/cjs/loader:1176:20)
at Module._compile (node:internal/modules/cjs/loader:1218:27)
at Module.m._compile
SyntaxError: Cannot use import statement outside a module
at internalCompileFunction (node:internal/vm:73:18)
at wrapSafe (node:internal/modules/cjs/loader:1176:20)
at Module._compile (node:internal/modules/cjs/loader:1218:27)
at Module.m._compile
rphlmr ⚡
rphlmr ⚡16mo ago
How do you get this error?
imoby
imobyOP16mo ago
I think I got it figured out, it was due to my tsconfig thank you
ulic
ulic16mo ago
and what did you change in your tsconfig?
imoby
imobyOP16mo ago
I had to change it to be commonjs because it was complaining about using imports
Want results from more Discord servers?
Add your server