Giter VIP home page Giter VIP logo

pg-bump's Introduction

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.

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

Installation

To add pg-bump to your dependencies do:

λ 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.

λ npm i -g pg-bump

Commands

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

Usage: pg-bump [options] [command]

SQL migration CLI for PostgreSQL.

Options:
  -v, --version             output the version number
  -c, --config-path <path>  relative path to config file
  -r, --require <hook...>   require modules for side effects
  -f, --files <path>        relative path to migrations directory
  -e, --env-var <variable>  database url environment variable
  -j, --journal <table>     table used to record migration history
  -h, --help                display help for command

Commands:
  make|create <migration>   create a new migration file
  status                    show pending migrations
  up [options]              apply pending migrations
  down [options]            revert synced migrations
  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.

λ npx pg-bump make --help
# Usage: pg-bump make|create [options] <migration>
#
# create a new migration file
#
# Arguments:
#   migration   name of new migration
#
# Options:
#   -h, --help  display help for command

Example

λ npx pg-bump make create_table_users
# [pg-bump]  created: <unix-time-in-ms>_create-table-users/{up,down}.sql

λ tree migrations
# migrations/
# └── <unix-time-in-ms>_create-table-users
#     ├── down.sql
#     └── up.sql

Applying Migrations

The pg-bump up command applies all pending migrations.

λ npx pg-bump up --help
# Usage: pg-bump up [options]
#
# apply pending migrations
#
# Options:
#   -l, --lock         acquire advisory lock during migration (default: true)
#   --no-lock          skip advisory lock during migration
#   -t, --transaction  wrap migrations in a transaction (default: true)
#   --no-transaction   do not run migrations in a transaction
#   -h, --help         display help for command

Example

λ npx pg-bump up
# [pg-bump] applied 3 migrations
#        1: 1656785255267_create-table-foos
#        2: 1656785259822_create-table-bars
#        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>.

λ npx pg-bump down --help
# Usage: pg-bump down [options]
#
# revert synced migrations
#
# Options:
#   --no-lock          skip advisory lock during migration
#   -l, --lock         acquire advisory lock during migration (default: true)
#   --no-transaction   do not run migrations in a transaction
#   -t, --transaction  wrap migrations in a transaction (default: true)
#   --to <version>     revert to schema <version>
#   -h, --help         display help for command

Example

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

Inspecting Migration State

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

λ npx pg-bump status --help
# Usage: pg-bump status [options]
#
# list applied and pending migrations
#
# Options:
#   -h, --help  display help for command

Example

λ npx pg-bump status
# [pg-bump] found 1 pending migration
#        1: 1656785255267_create-table-foos
#        2: 1656785259822_create-table-bars
# (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

cp .env.example .env

Start PostgreSQL Container and pgweb

docker-compose up --build -d

Run Tests

npm test

Generate Coverage Report

npm run cover

pg-bump's People

Contributors

greenkeeper[bot] avatar thebearingedge avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

pg-bump's Issues

Really like this library

I've been spending all day looking for a migration solution that makes sense and this is the first one that really seemed to fit the bill.

I like that it is just raw SQL. However in discussing this with the team the one piece we are kind of unsure of is combining the up/down migrations in one file. In our minds this makes that file far less useful as it can't really be treated as a proper migration without parsing it first. I realize in the context of the tool this probably doesn't matter, but I was wondering have you thought about keeping the up/down migrations as separate files and what was the reason for keeping them together?

rewrite

  • support pure esm?
  • advisory locks
  • typescript
  • porsager/postgres
  • commander cli
  • minimum node version 14
  • separate up and down scripts
  • revert to version
  • fix tls support
    • to fully support this, the simplest approach would be to use a javascript config file? While we're at it, may as well support a client factory function?
  • redo tests
  • update readme

Version 3

Development

  • Use docker-compose for pg-bump development database(s).
  • Move ES Lint config into package.json.
  • Use lint-staged instead of a full lint during pre-commit.
  • Move nyc config into package.json.

Maintenance

  • Upgrade all dependencies.
  • Release v2.1.

Enhancements

  • Add support for pg-bump config in package.json.
  • Remove hard dependency on dotenv in favor of a --require hook.
  • Record migration numbers in schema journal.
  • Simplify directory structure to skip "build" step.
  • Refactor to async/await.
  • Refactor to named parameters.
  • Refactor implementation to decouple logging, file system operations, and database operations.
  • Bump Node.js version support to 8.3 up.
  • Allow multiple db configs per project.
  • Refactor scripts.
  • Update docs and description for v3.

Missing dotenv dependancy.

What I expected.

To run pg-bump without errors.

What I experienced instead.

I received an error when running pg-bump.

module.js:472
    throw err;
    ^

Error: Cannot find module 'dotenv/config'
    at Function.Module._resolveFilename (module.js:470:15)
    at Function.Module._load (module.js:418:25)
    at Module.require (module.js:498:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/usr/local/lib/node_modules/pg-bump/build/cli.js:2:1)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)

Add documentation for annotations in migration file.

Motivation

The pg-bump create command creates a file with comments inside of it. It would be nice to know what they mean. It would also be good to know if they serve a function or if they can be deleted.

An example of a migration file.

-- up

---

-- down

User can generate an SQL script.

✅ Tasks

  • Test-drive a function for copying a new empty script by name.
  • Test-drive execution of CLI.
  • Define a yargs command for executing create function.
  • Enable option to override default destination.
~/project-directory λ pg-bump create foo.sql
[pg-bump] Creating migration file...
created: migrations/1494462120852_foo.sql

Version 10 of node.js has been released

Version 10 of Node.js (code name Dubnium) has been released! 🎊

To see what happens to your code in Node.js 10, Greenkeeper has created a branch with the following changes:

  • Added the new Node.js version to your .travis.yml
  • The new Node.js version is in-range for the engines in 1 of your package.json files, so that was left alone

If you’re interested in upgrading this repo to Node.js 10, you can open a PR with these changes. Please note that this issue is just intended as a friendly reminder and the PR as a possible starting point for getting your code running on Node.js 10.

More information on this issue

Greenkeeper has checked the engines key in any package.json file, the .nvmrc file, and the .travis.yml file, if present.

  • engines was only updated if it defined a single version, not a range.
  • .nvmrc was updated to Node.js 10
  • .travis.yml was only changed if there was a root-level node_js that didn’t already include Node.js 10, such as node or lts/*. In this case, the new version was appended to the list. We didn’t touch job or matrix configurations because these tend to be quite specific and complex, and it’s difficult to infer what the intentions were.

For many simpler .travis.yml configurations, this PR should suffice as-is, but depending on what you’re doing it may require additional work or may not be applicable at all. We’re also aware that you may have good reasons to not update to Node.js 10, which is why this was sent as an issue and not a pull request. Feel free to delete it without comment, I’m a humble robot and won’t feel rejected 🤖


FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

Refactor.

  • Review public interface for consistency.
  • Review tests for simplification and consitency.
  • Modularize bootstrapping function.
  • Add hard dependency on pg.

An in-range update of fs-extra is breaking the build 🚨

Version 4.0.3 of fs-extra was just published.

Branch Build failing 🚨
Dependency fs-extra
Current Version 4.0.2
Type dependency

This version is covered by your current version range and after updating it in your project the build failed.

fs-extra is a direct dependency of this project, and it is very likely causing it to break. If other packages depend on yours, this update is probably also breaking those in turn.

Status Details
  • continuous-integration/travis-ci/push The Travis CI build failed Details

Commits

The new version differs by 10 commits.

  • bd3376b 4.0.3
  • f934357 Merge pull request #520 from arcanis/patch-1
  • d21da93 Fixes fs-extra on linux
  • a6e8cd6 Add issue template (#514)
  • 2599b67 Merge pull request #501 from jprichardson/rimraf-backports
  • ddb5cb4 Use octal notation for chmod values in rimraf.js
  • cc6d425 Merge pull request #500 from revelt/patch-1
  • d416fa7 typo fixed
  • 41a33c2 Merge pull request #497 from nico29/docs/copy-improvements
  • 4f01664 docs: update copy to show differences with cp

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.