Skip to content

mren/pgmigrate

Repository files navigation

pgmigrate

Migration tool for postgres databases in node.js. pgmigrate helps you to migrate your schema for postgres.

  • use it from cli or via javascript
  • zero dependencies
  • tested code

usage

It is possible to use pgmigrate via cli tools are programmatically. node_modules/.bin contains add-migration and migrate.

add-migration

add-migration <name> will add a migration file to schema/$timestamp-$name.sql. Custom SQl can be written into this file.

migrate via the cli

migrate will execute all sql files in schema which are not yet executed.

Usage:

DATABASE_URL=postgres://user:pass@server/database node_modules/.bin/migrate

Adding --sync will truncate the database before running migrate.

DATABASE_URL=postgres://user:pass@server/database node_modules/.bin/migrate --sync

migrate via code

const pg = require('pg');
const migrate = require('pgmigrate');
const getConnection = cb => pg.connect(process.env.DATABASE_URL, cb);
migrate('path/to/schema', getConnection, {isSync: false})
  .then(console.log)
  .catch(console.error);

technical implementation

pgmigrate adds a schema table to your database called schema_info. In schema_info all filenames of all already executed schema migrations are persisted. As soon as a schema is executed successful, the filename is added to this table.

All sql files in path_to_migrations which are not yet in the database are executed. After successful execution the timestamp of the migration file is added to the schema_info table.

The format of the migrations files should be $iso8601-description.sql

YYYY-MM-DDTHH:MM:SSZ-description.sql

Hooks

Sometimes you want to execute sql logic always before and/or after you run your migrations. In this case you can add pre and post hooks to your schema folder.

For a prehook use a filename with this pattern: 0000-00-00T00:00:00:000Z-$description.sql For a posthook use a filename with this pattern: 9999-99-99T99:99:99:999Z-$description.sql

License MIT

About

migration script for node.js and postgres

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published