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

stepwise-migrations

v1.0.13

Published

[![npm version](https://badge.fury.io/js/stepwise-migrations.svg?icon=si%3Anpm&)](https://badge.fury.io/js/stepwise-migrations)

Downloads

489

Readme

Stepwise Migrations

npm version

A tool for managing Raw SQL migrations in a Postgres database. Loosely based on flyway.

Instructions

Up migrations are first sorted in ascending order based on filename. No subdirectories are read below the migration directory.

Name the "up" migration files as .sql and the "down" migration files with the same name but suffixed with .down.sql. e.g. v1_users.sql and v1_users.down.sql. Down migrations are optional.

Usage

Usage: stepwise-migrations [command] [options]

Commands:
  migrate
    Migrate the database to the latest version
  down
    Rollback the database to the previous version
  validate
    Validate the migration files and the migration history table
  audit
    Show the audit history for the migrations in the database
  info
    Show information about the current state of the migrations in the database
  drop
    Drop all tables, schema and migration history table
  get-script
    Get the script for the last applied migration

Options:
  --connection <connection>  The connection string to use to connect to the database
  --schema <schema>          The schema to use for the migrations
  --path <path>              The path to the migrations directory
  --ssl true/false           Whether to use SSL for the connection (default: false)
  --nup                      Number of up migrations to apply (default: all)
  --ndown                    Number of down migrations to apply (default: 1)
  --filename                 The filename to get the script for (default: last applied migration)

Example:
  npx stepwise-migrations migrate \
    --connection=postgresql://postgres:[email protected]:5432/mydatabase \
    --schema=myschema \
    --path=./db/migration/

Examples

Migrate

If all files are in a valid state, runs all the "up" migrations that have not been applied yet.

npx stepwise-migrations migrate \
  --connection=postgresql://postgres:[email protected]:5432/mydb \
  --schema=myschema \
  --path=./db/migration/
Creating schema myschema... done!
Creating migration history table... done!
Applying migration v1_connect_session_table.sql... done!
Applying migration v2_auth.sql... done!
All done! Applied 2 migrations
Migration history:
┌─────────┬────┬────────────────────────────────┬────────────┬──────────────────────────────┐
│ (index) │ id │ name                           │ applied_by │ applied_at                   │
├─────────┼────┼────────────────────────────────┼────────────┼──────────────────────────────┤
│ 0       │ 1  │ 'v1_connect_session_table.sql' │ 'postgres' │ '2024-11-24 05:40:41.211617' │
│ 1       │ 2  │ 'v2_auth.sql'                  │ 'postgres' │ '2024-11-24 05:40:41.214732' │
└─────────┴────┴────────────────────────────────┴────────────┴──────────────────────────────┘
Unapplied migrations:
┌─────────┐
│ (index) │
├─────────┤

Down

Runs a single down migration for the last applied migration. Can run multiple down migrations if the --ndown option is provided.

Command:

npx stepwise-migrations down \
  --connection=postgresql://postgres:[email protected]:5432/mydb \
  --schema=myschema \
  --path=./db/migration/
Applying down migration v2_auth.down.sql... done!
All done! Applied 1 down migration
Migration history:
┌─────────┬────┬────────────────────────────────┬────────────┬──────────────────────────────┐
│ (index) │ id │ name                           │ applied_by │ applied_at                   │
├─────────┼────┼────────────────────────────────┼────────────┼──────────────────────────────┤
│ 0       │ 1  │ 'v1_connect_session_table.sql' │ 'postgres' │ '2024-11-24 05:40:41.211617' │
└─────────┴────┴────────────────────────────────┴────────────┴──────────────────────────────┘
Unapplied migrations:
┌─────────┬───────────────┐
│ (index) │ filename      │
├─────────┼───────────────┤
│ 0       │ 'v2_auth.sql' │
└─────────┴───────────────┘

Validate

Validates the migration files and the migration history table.

npx stepwise-migrations validate \
  --connection=postgresql://postgres:[email protected]:5432/mydb \
  --schema=myschema \
  --path=./db/migration/
Validation passed
Migration history:
┌─────────┬────┬────────────────────────────────┬────────────┬──────────────────────────────┐
│ (index) │ id │ name                           │ applied_by │ applied_at                   │
├─────────┼────┼────────────────────────────────┼────────────┼──────────────────────────────┤
│ 0       │ 1  │ 'v1_connect_session_table.sql' │ 'postgres' │ '2024-11-24 05:40:41.211617' │
└─────────┴────┴────────────────────────────────┴────────────┴──────────────────────────────┘
Unapplied migrations:
┌─────────┬───────────────┐
│ (index) │ filename      │
├─────────┼───────────────┤
│ 0       │ 'v2_auth.sql' │
└─────────┴───────────────┘
Error: migration v1_connect_session_table.sql has been modified, aborting.
 	"expire" timestamp(6) NOT NULL
 )
 WITH (OIDS=FALSE);
-ALTER TABLE "session" ADD CONSTRAINT "session_pkey" PRIMARY KEY ("sid") NOT DEFERRABLE INITIALLY IMMEDIATE;
\ No newline at end of file
+ALTER TABLE "session" ADD CONSTRAINT "session_pkey" PRIMARY KEY ("sid") NOT DEFERRABLE INITIALLY IMMEDIATE;
+
+ALTER TABLE "session" ADD INDEX "session_sid" ON "session" (sid);

Audit

Shows the audit history for the migrations in the database.

npx stepwise-migrations audit \
  --connection=postgresql://postgres:[email protected]:5432/mydb \
  --schema=myschema \
  --path=./db/migration/
Audit history:
┌─────────┬────┬────────┬────────────────────────────────┬────────────┬──────────────────────────────┐
│ (index) │ id │ type   │ name                           │ applied_by │ applied_at                   │
├─────────┼────┼────────┼────────────────────────────────┼────────────┼──────────────────────────────┤
│ 0       │ 1  │ 'up'   │ 'v1_connect_session_table.sql' │ 'postgres' │ '2024-11-24 05:40:41.211617' │
│ 1       │ 2  │ 'up'   │ 'v2_auth.sql'                  │ 'postgres' │ '2024-11-24 05:40:41.214732' │
│ 2       │ 3  │ 'down' │ 'v2_auth.down.sql'             │ 'postgres' │ '2024-11-24 05:41:34.541462' │
└─────────┴────┴────────┴────────────────────────────────┴────────────┴──────────────────────────────┘

Info

Shows the current state of the migrations in the database.

Command:

npx stepwise-migrations info \
  --connection=postgresql://postgres:[email protected]:5432/mydb \
  --schema=myschema \
  --path=./db/migration/
Migration history:
┌─────────┬────┬────────────────────────────────┬────────────┬──────────────────────────────┐
│ (index) │ id │ name                           │ applied_by │ applied_at                   │
├─────────┼────┼────────────────────────────────┼────────────┼──────────────────────────────┤
│ 0       │ 1  │ 'v1_connect_session_table.sql' │ 'postgres' │ '2024-11-24 05:40:41.211617' │
└─────────┴────┴────────────────────────────────┴────────────┴──────────────────────────────┘

Get Script

Gets the script for the last applied migration. Can get the script for a specific migration if the --filename option is provided.

Command:

npx stepwise-migrations get-script --filename v2_auth.sql \
  --connection=postgresql://postgres:[email protected]:5432/mydb \
  --schema=myschema \
  --path=./db/migration/
CREATE TABLE "users" (
	id bigserial primary key,
	email text unique not null,
	first_name text not null,
	last_name text not null,
	created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

Drop

Drops the tables, schema and migration history table.

Command:

npx stepwise-migrations drop \
  --connection=postgresql://postgres:[email protected]:5432/mydb \
  --schema=myschema
Dropping the tables, schema and migration history table... done!