Skip to content

Commit

Permalink
🔨 chore (finance): Updated migration script to use dotenv-cli
Browse files Browse the repository at this point in the history
  • Loading branch information
aryanprince committed Feb 12, 2024
1 parent ca7bab8 commit c9371e1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
4 changes: 2 additions & 2 deletions apps/finance/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
"db:direct-push": "drizzle-kit push:pg",
"db:generate": "drizzle-kit generate:pg",
"db:migrate": "tsx --env-file .env ./src/server/db/migrate.ts",
"db:migrate-dotenv": "dotenv tsx ./src/server/db/migrate.ts",
"db:push": "pnpm run db:generate && pnpm run db:migrate",
"db:seed": "tsx --env-file .env ./src/server/db/seed.ts",
"db:reset": "pnpm run db:migrate && pnpm run db:seed",
"postinstall": "pnpm run db:reset"
"db:reset": "pnpm run db:migrate && pnpm run db:seed"
},
"dependencies": {
"@auth/drizzle-adapter": "^0.3.6",
Expand Down
22 changes: 16 additions & 6 deletions apps/finance/src/server/db/migrate.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
import { config } from "dotenv";
import { drizzle } from "drizzle-orm/postgres-js";
import { migrate } from "drizzle-orm/postgres-js/migrator";
import postgres from "postgres";

import { env } from "~/env";

// eslint-disable-next-line @typescript-eslint/no-empty-function
const sql = postgres(env.DATABASE_URL, { max: 1, onnotice: () => {} });
const db = drizzle(sql);
config();

await migrate(db, { migrationsFolder: "./src/server/db/migrations" });
const migrateDb = async () => {
if (!env.DATABASE_URL) {
throw new Error("DATABASE_URL environment variable is not set");
}

await sql.end();
// eslint-disable-next-line @typescript-eslint/no-empty-function
const sql = postgres(env.DATABASE_URL, { max: 1, onnotice: () => {} });
const db = drizzle(sql);

console.log("💦 Database migrated successfully");
await migrate(db, { migrationsFolder: "./src/server/db/migrations" });
await sql.end();

console.info("💦 Database migrated successfully");
};

void migrateDb();

0 comments on commit c9371e1

Please sign in to comment.