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

db-migrator

v2.4.0

Published

The complete and easy to use database migration tool

Downloads

4,632

Readme

DB Migrator

The complete and easy to use database migration tool for Node.js projects. Supports PostgreSQL and MySQL.

Features

  • Auto migration from scratch to up to date
  • Step by step forward migration
  • Step by step backward migration
  • Migrate to a specific version forward or backward
  • Subfolder deep search for migration scripts
  • All or nothing (transactional migration)

Installation

npm install db-migrator --save
# and one of
npm install pg --save
npm install promise-mysql --save

Quick Start

Add following to your package.json:

"scripts": {
  "db-migrate": "db-migrate",
  "db-rollback": "db-rollback",
  "db-create": "db-create",
  "db-status": "db-status"
}

Create .npmrc with a database connection string:

# For PostgreSQL it can look like this
db_migrator_db_url=postgresql://mydatabase@localhost?ssl=false
# For MySQL
db_migrator_db_url=mysql://user:pass@host/db

Create folder for migration files, by default migrations.

mkdir migrations

At this point you should be able to run all following commands.

db-create

Run npm run db-create description of the migration to generate files for UP and DOWN migration and put your SQL to these files. Migration scripts name has to match pattern timestamp-[UP|DOWN](-optional-description).sql.

Example migration scripts

db-migrate

Run npm run db-migrate to migrate your database to the latest version.

db-rollback

Run npm run db-rollback to rollback one last migration.

db-status

Run npm run db-status to see the state of your database.

Example output of db-status

Configuration

There's not too much to set up but it's very common to have several deployment and testing environments for each project. There are several ways how to configure DB Migrator so everyone should find an easy way how to plug it in their stack.

Check out this document to find out more.

Common Pitfalls

  • All migration scripts are executed in the same transaction scope and totally roll back in case of fail so you shouldn't put any transaction statements in your scripts.
  • You should use a database user with sufficient permissions according to your script content.
  • Postgres ENUM type cannot be altered in a transaction so you have to change the columns type to varchar, recreate the enum and set the columns type back to use the enum.

Credits

This is a fork of pg-migrator library with following differences features:

  • Timestamp-based version id
  • All version ids together with time of execution are stored in the database
  • It's possible to get a list of versions migrated in the database
  • Async/Await is used in the codebase so at least Node.js v7.6.0 is required
  • Migration file name can contain a short description of the migration
  • Favors npm scripts (and .npmrc config file) but supports also custom execution scripts
  • MySQL support