-
Notifications
You must be signed in to change notification settings - Fork 2
Fix database migration with Drizzle Kit #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,13 +1,20 @@ | ||||||||||||||
| /** | ||||||||||||||
| * Database Migration Runner | ||||||||||||||
| * Runs pending migrations against the database | ||||||||||||||
| * Note: Bun auto-loads .env files, no dotenv needed | ||||||||||||||
| */ | ||||||||||||||
|
|
||||||||||||||
| import "dotenv/config"; | ||||||||||||||
| import { drizzle } from "drizzle-orm/postgres-js"; | ||||||||||||||
| import { logger } from "../utils/logger"; | ||||||||||||||
| import { migrate } from "drizzle-orm/postgres-js/migrator"; | ||||||||||||||
| import postgres from "postgres"; | ||||||||||||||
| import { fileURLToPath } from "node:url"; | ||||||||||||||
| import { dirname, join } from "node:path"; | ||||||||||||||
| import { logger } from "../utils/logger"; | ||||||||||||||
|
|
||||||||||||||
| // Get absolute path to migrations folder | ||||||||||||||
| const __filename = fileURLToPath(import.meta.url); | ||||||||||||||
| const __dirname = dirname(__filename); | ||||||||||||||
| const migrationsFolder = join(__dirname, "migrations"); | ||||||||||||||
|
|
||||||||||||||
| // Validate environment | ||||||||||||||
| if (!process.env.DATABASE_URL) { | ||||||||||||||
|
|
@@ -22,6 +29,15 @@ if (!process.env.DATABASE_URL) { | |||||||||||||
| process.exit(1); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // Log migration info for debugging | ||||||||||||||
| logger.info( | ||||||||||||||
| { | ||||||||||||||
| migrationsFolder, | ||||||||||||||
| databaseUrl: process.env.DATABASE_URL?.replace(/:[^:@]+@/, ":****@"), // Hide password | ||||||||||||||
| }, | ||||||||||||||
|
Comment on lines
+35
to
+45
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: Remove redundant optional chaining operator
Suggested change
|
||||||||||||||
| "[Migrations] Starting migration process", | ||||||||||||||
| ); | ||||||||||||||
|
|
||||||||||||||
| // Migration connection with NOTICE suppression | ||||||||||||||
| const migrationClient = postgres(process.env.DATABASE_URL, { | ||||||||||||||
| max: 1, | ||||||||||||||
|
|
@@ -34,7 +50,7 @@ async function main() { | |||||||||||||
| logger.info({}, "[Migrations] Running migrations..."); | ||||||||||||||
|
|
||||||||||||||
| try { | ||||||||||||||
| await migrate(db, { migrationsFolder: "./server/db/migrations" }); | ||||||||||||||
| await migrate(db, { migrationsFolder }); | ||||||||||||||
| logger.info({}, "[Migrations] ✓ Migrations completed successfully"); | ||||||||||||||
| } catch (error: any) { | ||||||||||||||
| // Check if it's a "relation already exists" error (PostgreSQL code 42P07) | ||||||||||||||
|
|
@@ -49,7 +65,15 @@ async function main() { | |||||||||||||
| logger.warn({}, "[Migrations] ⚠️ Some tables already exist - skipping"); | ||||||||||||||
| logger.info({}, "[Migrations] ✓ Database schema is up to date"); | ||||||||||||||
| } else { | ||||||||||||||
| logger.error({ err: error }, "[Migrations] ✗ Migration failed:"); | ||||||||||||||
| logger.error( | ||||||||||||||
| { | ||||||||||||||
| err: error, | ||||||||||||||
| code: errorCode, | ||||||||||||||
| message: errorMessage, | ||||||||||||||
| migrationsFolder, | ||||||||||||||
| }, | ||||||||||||||
| "[Migrations] ✗ Migration failed", | ||||||||||||||
| ); | ||||||||||||||
| process.exit(1); | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Resource Management: Potential Unclosed Database ConnectionIf an error occurs before try {
await migrate(db, { migrationsFolder });
logger.info({}, "[Migrations] ✓ Migrations completed successfully");
} catch (error: any) {
// ... error handling ...
} finally {
await migrationClient.end();
}This guarantees that the connection is closed even if an error is thrown. |
||||||||||||||
|
|
||||||||||||||
This file was deleted.
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.