npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

migratio

v2.0.3

Published

Postgres migrations

Downloads

34

Readme

Automated migrations for Postgres

Build Status

Install

$ npm install --save migratio

Usage

Create directory migrations with migrations and use migratio:

const migratio = require('migratio');

await migratio.current({
	connection: 'postgres://localhost/db',
	verbose: true
});
//   000005-images.sql  (batch:3)
//   000004-files.sql   (batch:3)
//   000003-stats.sql   (batch:3)

await migratio.up({
	connection: 'postgres://localhost/db',
	verbose: true
});
// ↑ 000006-posts.sql   (batch:4)

await migratio.down({
	connection: 'postgres://localhost/db',
	verbose: true
});
// ↓ 000005-images.sql  (batch:3)
// ↓ 000004-files.sql   (batch:3)
// ↓ 000003-stats.sql   (batch:3)

Migrations format

All migrations files should start with revision (digits) followed by - and name of migration. For example 000001-init.js and 000002-users.sql is valid file names.

Migrations will be applied in order of numbers in front of filename.

Migration process is running in transaction with lock on migrations table. If one of migrations failed – all batch will be reverted.

JavaScript format

Migration file with extension .js is treated as module, that exports two functions:

  • up – contains code to apply migration
  • down – contains code to revert migration

These functions must return Promise. If these functions are generators, they will be wrapped in co.

exports.up = async function (db) {
	await db.query(`
		CREATE TABLE test (
			id serial PRIMARY KEY
		)
	`);
};

exports.down = async function (db) {
	await db.query(`
		DROP TABLE IF EXISTS test;
	`);
};

SQL format

Migration file with extension .sql is treated as file with SQL instructions. Instructions to apply migration should be placed after -- +migrate Up and instructions to revert migration should be placed after -- +migrate Down.

-- +migrate Up

CREATE TABLE test (
	id serial PRIMARY KEY
);

-- +migrate Down

DROP TABLE IF EXISTS test;

Configuring defaults

Migratio supports overriding default values with migraio section in package.json:

{
	"migratio": {
		"directory": "migrations",
		"tableName": "migrations"
	}
}

API

up(options)

Applies all migrations from current to latest available.

options

connection

Type: string Default: process.env.DATABASE_URL

Connection string to Postgres database.

db

Type: Database

Database object. Will be used instead of connection.

directory

Type: string Default: ./migrations

Directory with migrations.

revision

Type: Number Default: Infinity

Latest revision to up to.

unsafe

Type: Boolean Default: false

Disables meta-table locking.

verbose

Type: boolean Default: false

Enables output.

tableName

Type: string Default: migratio

Table in which migratio will store metadata.

down(options)

Rollbacks migrations in current batch.

options

connection

Type: string Default: process.env.DATABASE_URL

Connection string to Postgres database.

directory

Type: string Default: ./migrations

Directory with migrations.

unsafe

Type: Boolean Default: false

Disables meta-table locking.

verbose

Type: boolean Default: false

Enables output.

tableName

Type: string Default: migratio

Table in which migratio will store metadata.

current(options)

Shows current batch.

connection

Type: string Default: process.env.DATABASE_URL

Connection string to Postgres database.

unsafe

Type: Boolean Default: false

Disables meta-table locking.

verbose

Type: boolean Default: false

Enables output.

revision

Type: Number Default: Infinity

First revision to show info about.

tableName

Type: string Default: migratio

Table in which migratio will store metadata.

CLI

$ npm install --global migratio
$ migratio --help

  Usage
    migratio [command] [options]

  Options
    -d, --directory    Directory with migrations files [Default: ./migrations]
    -c, --connection   Connection string to Postgres [Default: $DATABASE_URL]
    -r, --revision     Specify revision to up/down to
    -t, --table        Table name for metadata [Default: migratio]
    -s, --schema       Schema name for table with metadata [Default: public]
    --unsafe           Skip transaction and table locking

  Commands

    up             Applies all migrations from current to latest
    down           Rollbacks all migrations in current batch
    current        Shows migrations in current batch

  Examples
    $ migratio

      Current batch:
        000005-images.sql  (batch:3)
        000004-files.sql   (batch:3)
        000003-stats.sql   (batch:3)

    $ migratio down

      ↓ 000005-images.sql  (batch:3)
      ↓ 000004-files.sql   (batch:3)
      ↓ 000003-stats.sql   (batch:3)

    $ migratio up

      ↑ 000003-stats.sql   (batch:3)
      ↑ 000004-files.sql   (batch:3)
      ↑ 000005-images.sql  (batch:3)
      ↑ 000006-posts.sql   (batch:3)

License

MIT © Vsevolod Strukchinsky