项目作者: thebearingedge

项目描述 :
:elephant: A minimalist SQL migration CLI for PostgreSQL.
高级语言: JavaScript
项目地址: git://github.com/thebearingedge/pg-bump.git
创建时间: 2017-05-07T23:11:14Z
项目社区:https://github.com/thebearingedge/pg-bump

开源协议:MIT License

下载


pg-bump

SQL migration CLI for PostgreSQL.

Github Actions Test Status
codecov
Greenkeeper badge
License: MIT

What is it?

pg-bump provides command line management of PostgreSQL database schema migrations authored in “Plain SQL”. It presumes that a) you are checking schema migrations into source control and b) you are using environment variables for your application configuration.

pg-bump is primarily intended for use as an executable in package.json scripts. By default, applying or reverting migrations is run in a single transaction. However, this can be disabled via command line options.

ESM-only

As of v4, pg-bump is an ECMAScript module. This should not matter much as it is intended to be used as a CLI application and ESM has been supported since Node.js v12. v3 is still CommonJS.

Powered by @porsager/postgres

As of v4, pg-bump connects to PostgreSQL with the postgres package instead of pg.

Installation

To add pg-bump to your dependencies do:

  1. λ npm i pg-bump

It is possible to install pg-bump globally, but I never recommend global installs and I don’t know why some people still suggest it.

  1. λ npm i -g pg-bump

Commands

You can view the CLI documentation using npx pg-bump --help.

  1. Usage: pg-bump [options] [command]
  2. SQL migration CLI for PostgreSQL.
  3. Options:
  4. -v, --version output the version number
  5. -c, --config-path <path> relative path to config file
  6. -r, --require <hook...> require modules for side effects
  7. -f, --files <path> relative path to migrations directory
  8. -e, --env-var <variable> database url environment variable
  9. -j, --journal <table> table used to record migration history
  10. -h, --help display help for command
  11. Commands:
  12. make|create <migration> create a new migration file
  13. status show pending migrations
  14. up [options] apply pending migrations
  15. down [options] revert synced migrations
  16. help [command] display help for command

Creating a Migration

The pg-bump make command generates new .sql migrations in your migrations directory (defaults to ./migrations). The migration is split into two files: up.sql and down.sql.

  1. λ npx pg-bump make --help
  2. # Usage: pg-bump make|create [options] <migration>
  3. #
  4. # create a new migration file
  5. #
  6. # Arguments:
  7. # migration name of new migration
  8. #
  9. # Options:
  10. # -h, --help display help for command

Example

  1. λ npx pg-bump make create_table_users
  2. # [pg-bump] created: <unix-time-in-ms>_create-table-users/{up,down}.sql
  3. λ tree migrations
  4. # migrations/
  5. # └── <unix-time-in-ms>_create-table-users
  6. # ├── down.sql
  7. # └── up.sql

Applying Migrations

The pg-bump up command applies all pending migrations.

  1. λ npx pg-bump up --help
  2. # Usage: pg-bump up [options]
  3. #
  4. # apply pending migrations
  5. #
  6. # Options:
  7. # -l, --lock acquire advisory lock during migration (default: true)
  8. # --no-lock skip advisory lock during migration
  9. # -t, --transaction wrap migrations in a transaction (default: true)
  10. # --no-transaction do not run migrations in a transaction
  11. # -h, --help display help for command

Example

  1. λ npx pg-bump up
  2. # [pg-bump] applied 3 migrations
  3. # 1: 1656785255267_create-table-foos
  4. # 2: 1656785259822_create-table-bars
  5. # 3: 1656785263539_create-table-bazzes

Reverting Migrations

The pg-bump down command reverts migrations. Include --to <version> to only revert migrations to, but not including <version>.

  1. λ npx pg-bump down --help
  2. # Usage: pg-bump down [options]
  3. #
  4. # revert synced migrations
  5. #
  6. # Options:
  7. # --no-lock skip advisory lock during migration
  8. # -l, --lock acquire advisory lock during migration (default: true)
  9. # --no-transaction do not run migrations in a transaction
  10. # -t, --transaction wrap migrations in a transaction (default: true)
  11. # --to <version> revert to schema <version>
  12. # -h, --help display help for command

Example

  1. λ npx pg-bump down
  2. # [pg-bump] reverted 3 migrations
  3. # 3: 1656785263539_create-table-bazzes
  4. # 2: 1656785259822_create-table-bars
  5. # 1: 1656785255267_create-table-foos

Inspecting Migration State

List applied and pending migrations with the pg-bump status command.

  1. λ npx pg-bump status --help
  2. # Usage: pg-bump status [options]
  3. #
  4. # list applied and pending migrations
  5. #
  6. # Options:
  7. # -h, --help display help for command

Example

  1. λ npx pg-bump status
  2. # [pg-bump] found 1 pending migration
  3. # 1: 1656785255267_create-table-foos
  4. # 2: 1656785259822_create-table-bars
  5. # (pending) 1656785263539_create-table-bazzes

Development

Contributions welcome! If you add functionality or options, please include tests.

Environment Variables and Docker

docker-compose can be used to start and stop a local PostgreSQL instance if you don’t have a server running on your machine. If necessary, you can override docker-compose.yml.

Copy the .env Template

  1. cp .env.example .env

Start PostgreSQL Container and pgweb

  1. docker-compose up --build -d

Run Tests

  1. npm test

Generate Coverage Report

  1. npm run cover