Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

node-pg-migrate

salsita204.1kMIT7.9.0TypeScript support: included

PostgreSQL database migration management tool for node.js

db, database, migrate, migration, migrations, migrator, db-migrate, sql, pg, postgre, postgres, postgresql, cockroach, cockroachdb, extensible, expandable, programatic, programable, api

readme

node-pg-migrate

The core maintainer of this project moved to @Shinigami92 (also core maintainer of FakerJS and core member of Vite).
The project is and remains under the MIT license.

npm version npm downloads Continuous Integration Postgres Test Cockroach Test Licence

Node.js database migration management built exclusively for postgres. (But can also be used for other DBs conforming to SQL standard - e.g. CockroachDB.)
Started by Theo Ephraim, then handed over to Salsita Software and now maintained by @Shinigami92.

Preconditions

  • Node.js 18 or higher
  • PostgreSQL 12.8 or higher (lower versions may work but are not supported officially)

If you don't already have the pg library installed, you will need to add pg as either a direct or dev dependency

npm add pg

Installation

npm add --save-dev node-pg-migrate

Installing this module adds a runnable file into your node_modules/.bin directory. If installed globally (with the -g option), you can run node-pg-migrate and if not, you can run ./node_modules/.bin/node-pg-migrate.js

Quick Example

Add "migrate": "node-pg-migrate" to scripts section of your package.json so you are able to quickly run commands.

Run npm run migrate create my-first-migration. It will create file xxx_my-first-migration.js in migrations folder.
Open it and change contents to:

exports.up = (pgm) => {
  pgm.createTable('users', {
    id: 'id',
    name: { type: 'varchar(1000)', notNull: true },
    createdAt: {
      type: 'timestamp',
      notNull: true,
      default: pgm.func('current_timestamp'),
    },
  });
  pgm.createTable('posts', {
    id: 'id',
    userId: {
      type: 'integer',
      notNull: true,
      references: '"users"',
      onDelete: 'cascade',
    },
    body: { type: 'text', notNull: true },
    createdAt: {
      type: 'timestamp',
      notNull: true,
      default: pgm.func('current_timestamp'),
    },
  });
  pgm.createIndex('posts', 'userId');
};

Save migration file.

Now you should put your DB connection string to DATABASE_URL environment variable and run npm run migrate up. (e.g. DATABASE_URL=postgres://test:test@localhost:5432/test npm run migrate up)

You should now have two tables in your DB :tada:

If you want to change your schema later, you can e.g. add lead paragraph to posts:

Run npm run migrate create posts_lead, edit xxx_posts_lead.js:

exports.up = (pgm) => {
  pgm.addColumns('posts', {
    lead: { type: 'text', notNull: true },
  });
};

Run npm run migrate up and there will be a new column in posts table :tada:

Want to know more? Read docs:

Docs

Full docs are available at https://salsita.github.io/node-pg-migrate

Explanation & Goals

Why only Postgres? - By writing this migration tool specifically for postgres instead of accommodating many databases, we can actually provide a full featured tool that is much simpler to use and maintain. I was tired of using crippled database tools just in case one day we switch our database.

Async / Sync - Everything is async in node, and that's great, but a migration tool should really just be a fancy wrapper that generates SQL. Most other migration tools force you to bring in control flow libraries or wrap everything in callbacks as soon as you want to do more than a single operation in a migration. Plus by building up a stack of operations, we can automatically infer down migrations (sometimes) to save even more time.

Naming / Raw Sql - Many tools force you to use their constants to do things like specify data types. Again, this tool should be a fancy wrapper that generates SQL, so whenever possible, it should just pass through user values directly to the SQL. The hard part is remembering the syntax of the specific operation, not remembering how to type "timestamp"!

Contributing

GitHub repo Good Issues for newbies GitHub Help Wanted issues GitHub Help Wanted PRs GitHub repo Issues

👋 Welcome, new contributors!

Whether you're a seasoned developer or just getting started, your contributions are valuable to us. Don't hesitate to jump in, explore the project, and make an impact.

License

MIT

changelog

Change Log

v7+

Releases from v7 onwards are documented in the GitHub Release Notes

6.2.1

  • gh-863: Fix yargs warning #864

6.2.0

  • gh-754: Allow undefined count #791

6.1.0

Added

  • gh-857: Add unique option to drop index #858
  • gh-813: Stabilize sort for migration files #818

6.0.0

Breaking changes

  • Drop node 10 support #808

Fixed

  • Put migrations table check inside lock #830

5.10.0

Added

  • Make the json5 dependency optional (fallback to JSON.parse) #752
  • export ConnectionConfig, ClientConfig from pg #657

5.9.0

Added

  • Allow expanding dotenv with dotenv-expand #712

5.8.1

Fixed

  • Fix sequence generated #706

5.8.0

Fixed

  • Using string ids for Dollar-string escaping #698

5.7.1

Fixed

  • Handle string params in backward compatible way #696

5.7.0

Added

  • pgm.sql can also accept PgLiteral, number, boolean, null, array #695

5.6.0

Added

  • PgLiteral can be serialized #678

5.5.1

Fixed

  • Do not try to unlock DB if connection failed #677

5.5.0

Changed

  • Allow opclass and sort per column in indexes #671

5.4.0

Changed

  • Update isExternalClient check #664

5.3.0

Added

  • Display additional details when logging errors #660

5.2.0

Added

5.1.1

Fixed

  • Fix creating migration #646

5.1.0

Added

  • Ability to specify own template file #642

5.0.2

Fixed

  • Fix alter column collation #641

5.0.1

Fixed

  • Keep this bind in logger methods #638

5.0.0

Breaking changes

  • remove node 8 support #615
  • Ability to use sort of UTC time in filename #622

    If you used a different format for migrations names than the default one it can potentially break the order of your migrations

  • Migration can be also symlink #630

    If you have symlinks in the migration folder, migration can potentially break

Fixed

  • Fixed position of TEMPORARY clause in create table #629

4.8.0

Aded

  • add DB env var and tsconfig path to config options #613

4.7.0

Aded

  • Export ColumnDefinition #611
  • feat: support for parsing tsconfig.json with comments #606

Fixed

  • pipe return writeable, should use close event #608

4.6.2

Fixed

  • Fixing default options #601

4.6.1

Fixed

  • Accepting Pool Client #596

4.6.0

Added

  • reject-unauthorized CLI option #594

4.5.1

Fixed

  • Do provide default cli option value only if not specified #588
  • Fix locking #586

4.5.0

Added

  • Command line arguments should override config ones #585

4.4.0

Added

  • Support for pg v8 #584

4.3.0

Added

  • Possibility to pass custom logger #580
  • Ability to switch off debug logging #581

4.2.3

Release with updated dependencies

4.2.2

Fixed

  • Escape BEFORE and AFTER in addTypeValue #554

4.2.1

Fixed

  • Fixing typing of createTrigger parameters #548

4.2.0

Added

  • Adding include option for createIndex #537

4.1.0

Added

  • Expression generated columns #532

4.0.0

4.0.0-rc2

Added

  • Allow 'Down' migrations in .sql files #530

4.0.0-rc1

Breaking changes

  • Drop old node support #526

4.0.0-rc

Rewrite in typescript

Breaking changes

  • Removed optional dependencies #509

    If you are using config or dotenv configuration, it is no longer installed as optional dependency. You have to provide this package yourself.

  • Write node-pg-migrate in TypeScript #502 #510 #515 #516 #519 #520 #523

    • Fixed some issues with types which did not correspond to how code behave.
    • functionArgs in trigger options renamed to functionParams because of consistency.
  • Support for TS migrations #521

Added

  • Improve error message in migration.js #506
  • Another way for transpiling TypeScript #522

Fixes

  • Updates to docs - specifying schema for trigger_name #505
  • createIndex doc #524

3.23.3

Fixed

  • Fixing createTrigger TS type #494

3.23.2

Fixed

  • Marking storageParameters of materialized view as optional #490

3.23.1

Fixed

  • Fixing constraint name not optional #486

3.23.0

Added

  • Decamelize (experimental - it may happen some names are not decamelized) #472

3.22.1

Fixed

  • Fix create constraint by string #482

3.22.0

Added

  • Multiple schemas #475
  • Constraints - naming and comments #474
  • Update template files #473
  • Tests for passwords #440, #441, #442

3.21.1

Fixed

  • Drop index when schema and index name is specified #437

3.21.0

Added

  • Implement LiteralUnion for Extension #434

3.20.0

Added

  • Add ifNotExists to addColumns #427

3.19.0

Added

  • Generated option for column #426
  • Testing node version 6,8,10,12, postgres 9,10,11, cockroach 1,2 #423

3.18.1

Fixed

  • Do not use alias function name #414

3.18.0

Added

  • Returning list of run migrations #411

3.17.0

Added

  • Allow user to specify multiple check constraints when creating table #408
  • Allow user to pass db client to migration runner #407

3.16.1

Fixed

  • Fix empty migration files #400

3.16.0

Added

  • Adding LIKE options when creating table #394

3.15.0

Added

  • Infering migration language #391

3.14.2

Fixed

  • Exporting PgType #368

3.14.1

Fixed

  • Pass all params to pg query #361

3.14.0

Changed

  • Add ForeignKey Reference Action #357

3.13.0

Changed

  • Better migration name fix #353
  • Fix migration name substitution #348

3.12.0

Added

  • Shorthand can reference other shorthands #346

3.11.0

Added

  • Ability to mark migrations as run #324

3.10.1

Fixed

  • Fix dropping index in another schema #322

3.10.0

Changed

  • Using default libpq env vars #319

3.9.0

Changed

  • Do not construct connection string #316

Change Log

3.8.1

Changed

  • Removing default value for parallel clause #308

Fixed

  • Fix JSON quotes for script #307

3.8.0

Added

  • Ignoring dotfiles by default #305
  • Encoding special characters in db config #304

3.7.0

Added

  • Renaming of enum values #293

3.6.1

Fixed

  • Workaround for transpilers #299
  • Interface for references #297

Changed

  • Updates to installation and licence #298
  • Update readme #296

3.6.0

Fixed

  • Ability to name references constraint #290
  • Alter column comment #292

Changed

  • Update dependencies to enable Greenkeeper #278
  • Update CI config #281
  • Using async/await #282
  • Using 'function' keyword #283
  • Using code directly for node >= 8 #288
  • Added cockroach v2 to tests #289
  • Using tests without babel #291

3.5.1

Fixed

  • Respect configured logger #277

3.5.0

Fixed

  • Added id column to order by clause of select migrations #272

Added

  • Adding primary key on pgmigrations table #274

3.4.1

Fixed

  • opclass parens typo #270

3.4.0

Changed

  • Constraint name is optional (for backward compatibility) #268
  • Updated node and service versions #269

!!! Minimal supported node version is now 6 !!!

3.3.0

Fixed

  • Promisifying client.connect #265

3.2.1

Fixed

  • Fix type name issue when the type is not in the default schema #264

3.2.0

Added

  • Add support for opclass option in createIndex #259
  • Ability to specify extension schema #260

3.1.2

Fixed

  • Using dollar-quoted strings in comments #255

3.1.1

Fixed

  • Fixing optional options in create and drop statements #250

3.1.0

Added

  • Handling SQL files #246

3.0.0

3.0.0-rc5

Fixed

  • Fix comment not being optional #244
  • Fix behavior when singleTransaction is not set #245

3.0.0-rc4

Changed

  • Implement failsafe locking #239
  • Updated docs about locking #240

3.0.0-rc3

Added

  • Add log option to runner.js #238
  • Structuring docs #237
  • Prettier formatting #236
  • Displaying function name on infer failure #235
  • Materialized views handling #234
  • Handling Views #233
  • Cockroach test #231
  • Prettier #230

3.0.0-rc2

Fixed

  • Fixing setting comments on columns #228

2.26.3

Fixed

  • Fixing setting comments on columns #228

3.0.0-rc

Breaking changes

  • Single transaction as default #205
  • Versioning type shorthands #190 (type shorthands were moved from global config to migrations scripts)
  • Using camel case in API #189
  • Removed pg-migrate script (use node-pg-migrate)

Added

  • Running test migrations on CircleCI #221

2.26.2

Fixed

  • Fix runner for zero migrations #224

2.26.1

Fixed

  • Fixing altering role #222
  • Fixes from 3.0 #223

2.26.0

Added

  • Support for policies #219

2.25.1

Fixed

  • Role inherit fix #218

2.25.0

Fixed

  • (No)Transaction handling #213
  • Parens around INHERITS clause #214

Added

2.24.1

Fixed

  • Fix auto create schema #206

2.24.0

Added

  • Add --single-transaction option #204

2.23.1

Fixed

  • Correct handling of multiline constraints #202

2.23.0

Changed

  • Updating deps, removing vulnerablity status for peer and optional dependencies #199
  • Removing regex #198
  • Adding ability to specify database name with 'database' option #197

2.22.2

Fixed

  • Role encrypted default #196
  • Running queries in order #195

2.22.1

Fixed

  • Passing props #194

2.22.0

Added

  • Auto create configured schemas if they don't exist #192
  • Add ifNotExists option to create extension #188
  • Programmatic API docs #187

2.21.0

Added

  • Table and column comments #183

2.19.0

Added

  • migration-file-language can be set in config file #180
  • Treat number argument to up/down migration as timestamp #179

2.18.1

Fixed

  • Fixing addConstraint method with object expression #176

2.18.0

Added

  • Add no lock option #171
  • Updated docs #174

Changed

  • Remove old version number from index.d.ts #173
  • Remove default match in column reference #172
  • Refactor code to use camel casing #167

2.17.0

Added

  • Added typescript migration template #165
  • Updated type definitions to accept db client config #166

2.16.2

Fixed

  • Deleted duplicate declaration #164

2.16.1

Changed

  • Updated dependencies #158

Fixed

  • Typescript definition fixes #162

2.16.0

Added

  • Uniting drop statements #154
  • Handling domains #155
  • Operator operations #156
  • Sequences operations #157

2.15.0

Fixed

  • Handle rejections in migration actions #148

Added

2.14.0

Added

  • Deferrable column constraints #139
  • Possibility to use function in multi-column index #140

Changed

  • Changed all references from pg-migrate to node-pg-migrate #141

    !!! Breaking change from version 3 !!! (now with warning)

2.13.2

Fixed

  • Cannot use embedded value in config #137
  • add space before USING keyword #138

2.13.1

Fixed

  • addTypeValue's after option is using BEFORE instead of AFTER #133

2.13.0

Added

  • Ability to specify files to ignore in migrations directory #131

2.12.0

Fixed

  • Dollar quoted string constants #127
  • Table unique constraint can be array of arrays #126

Changed

  • If user disables migration, return Error instead of string #125
  • Circle CI integration #124
  • Moved to Salsita organization #122

2.11.1

Fixed

  • Fixed SQL for dropping multiple columns #120

2.11.0

Added

  • Schemas operations #119

2.10.1

Fixed

  • Fixed invalid SQL for table level foreign key #118

2.10.0

Added

  • Ability to specify constraints on table level #114

2.9.0

Added

  • Alter type functions #111
  • redo command #112

2.8.2

Fixed

  • Fix automatic reversal of addColumns #110

2.8.1

Fixed

  • Fixing referencing column #107

Changed

  • Formatting changes, added licence #108

2.8.0

Added

  • Trigger operations #104

2.7.1

Fixed

  • Support object with schema and table name in more places #105

2.7.0

Added

  • Function operations #103

2.6.0

Added

  • Support for pg >=4.3.0 <8.0.0
  • Interpret only files as migrations in migration directory #101

2.5.0

Added

  • USING clause in alter column #99
  • Role operations #100

2.4.0

Changed

  • Do not check file extension of migration file #93

2.3.0

Added

  • JSON config and type shorthands see #91

2.2.1

Fixed

  • Syntax error in node 4

2.2.0

Added

  • Better error logging #86
  • Locking migrations #88
  • Updated docs #89

2.1.1

Fixed

  • Down migration when down method is inferred #84

2.1.0

Added

  • Enable string functions and arrays as default column values #82

2.0.0

Rewritten using es6 (transpiled via babel) and Promises.

Breaking Changes

  • supports only node >= 4
  • check-order flag now defaults to true (to switch it off supply --no-check-order on command line)
  • dotenv package is optionalDependency
  • s option is now alias for schema which sets schema for migrations SQL, if you only need to change schema of migrations table use --migrations-schema

Added

  • config package as optionalDependency
  • Migration can return Promise