{"library":"postgres-migrations","title":"Postgres Migrations","description":"The `postgres-migrations` package provides a robust database migration solution for PostgreSQL, directly inspired by Stack Overflow's deployment methodology. Currently at version 5.3.0, it offers a stable and opinionated approach to managing database schema changes. Key differentiators include its strict sequential SQL file-based ordering, which explicitly avoids timestamp-based naming to ensure consistent execution order across all environments. The library deliberately eschews 'down' migrations, advocating for a 'roll forward' philosophy where issues are addressed by applying new incremental migrations. It enforces database integrity by performing hash checks on previously applied migration files, preventing accidental or unauthorized modifications. The package maintains a hidden `migrations` table to track applied scripts and requires Node.js 10.17.0+ and PostgreSQL 9.4+.","language":"javascript","status":"active","last_verified":"Wed Apr 22","install":{"commands":["npm install postgres-migrations"],"cli":null},"imports":["import { migrate } from 'postgres-migrations'","import { loadMigrationFiles } from 'postgres-migrations'","import { Client } from 'pg'"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { migrate } from 'postgres-migrations';\nimport { Client } from 'pg';\nimport path from 'path';\n\nasync function runMigrations() {\n  const dbConfig = {\n    database: process.env.DB_NAME ?? 'your_database',\n    user: process.env.DB_USER ?? 'postgres',\n    password: process.env.DB_PASSWORD ?? 'password',\n    host: process.env.DB_HOST ?? 'localhost',\n    port: parseInt(process.env.DB_PORT ?? '5432', 10),\n    // ensureDatabaseExists: true // Uncomment if you want the library to create the database if it doesn't exist\n  };\n\n  const client = new Client(dbConfig);\n  await client.connect();\n\n  try {\n    console.log('Starting database migrations...');\n    const migrationsPath = path.resolve(__dirname, 'migrations');\n    await migrate({ client }, migrationsPath);\n    console.log('Database migrations completed successfully.');\n  } catch (error) {\n    console.error('Migration failed:', error);\n    process.exit(1);\n  } finally {\n    await client.end();\n  }\n}\n\n// Example migrations directory structure:\n// my-project/\n// ├── src/\n// │   └── index.ts (or .js)\n// └── migrations/\n//     ├── 1_create_users_table.sql\n//     └── 2_add_posts_table.sql\n\nrunMigrations();","lang":"typescript","description":"Demonstrates how to apply migrations using an existing `pg.Client` instance, ensuring proper connection handling and error reporting. It uses environment variables for database credentials.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}