{"library":"postgres-schema-migrations","title":"PostgreSQL Schema Migrations with Schema Support","description":"Postgres-schema-migrations is a JavaScript/TypeScript library for managing database schema changes in PostgreSQL. It is a fork of `postgres-migrations` that specifically adds support for schema namespaces, allowing separate migrations to be tracked per schema, which is beneficial for multi-tenant applications or reusing database code. Currently at version 7.0.2, this library is actively maintained. It mandates SQL files for migration definitions, ordered numerically, and deliberately omits 'down' migrations, advocating for 'rolling forward' with new migrations to reverse changes. A key differentiator is its emphasis on atomic transactions for each migration and hash-based checks to ensure migration immutability, preventing accidental changes to already-applied migrations. It supports Node.js 10.17.0+ and PostgreSQL 9.4+.","language":"javascript","status":"active","last_verified":"Wed Apr 22","install":{"commands":["npm install postgres-schema-migrations"],"cli":null},"imports":["import { migrate } from 'postgres-schema-migrations'","import { loadMigrationFiles } from 'postgres-schema-migrations'","import { Client } from 'pg'"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { migrate } from \"postgres-schema-migrations\";\nimport { Client } from 'pg';\nimport path from 'path';\n\nasync function runMigrations() {\n  const migrationFilesPath = path.resolve(__dirname, 'migrations');\n\n  // Using a direct pg client allows more control over connection lifecycle\n  const dbConfig = {\n    database: process.env.DB_DATABASE ?? 'database-name',\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  };\n\n  const client = new Client(dbConfig);\n  await client.connect();\n\n  try {\n    console.log('Running migrations for public schema...');\n    await migrate({ client }, migrationFilesPath); // Defaults to 'public' schema\n    console.log('Public schema migrations complete.');\n\n    // Example of running migrations for a specific schema\n    console.log('Running migrations for custom_schema...');\n    const schemaMigrationsPath = path.resolve(__dirname, 'schema_migrations');\n    await migrate({ client }, schemaMigrationsPath, { schema: 'custom_schema' });\n    console.log('Custom schema migrations complete.');\n\n  } catch (error) {\n    console.error('Migration failed:', error);\n    process.exit(1);\n  } finally {\n    await client.end();\n  }\n}\n\n// Dummy migration files setup for example to be runnable\n// In a real project, these would be actual .sql files.\n// Make sure these directories exist for the quickstart to fully run.\n// Example: migrations/1_create_users_table.sql\n// CREATE TABLE users (id SERIAL PRIMARY KEY, name VARCHAR(255) NOT NULL);\n// Example: schema_migrations/1_create_products_table.sql\n// CREATE SCHEMA IF NOT EXISTS custom_schema;\n// CREATE TABLE custom_schema.products (id SERIAL PRIMARY KEY, name VARCHAR(255) NOT NULL);\n\nrunMigrations();","lang":"typescript","description":"This quickstart demonstrates how to apply database migrations using a `pg` client, showing both default (public) and namespaced schema migrations. It highlights connection management and error handling.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}