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

knex-migrator

v5.2.1

Published

Database migrations with knex.

Downloads

16,972

Readme

knex-migrator

A database migration tool for knex.js, which supports MySQL and SQlite3.

Features

  • [x] JS API
  • [x] CLI Tool
  • [x] Differentiation between database initialization and migration (Support for a database schema, like we use in Ghost)
  • [x] Support for database creation
  • [x] Hooks
  • [x] Rollback to latest version
  • [x] Auto-Rollback on error
  • [x] Database health check
  • [x] Supports transactions
  • [x] Full atomic, support for separate DML/DDL scripts (no autocommit)
  • [x] Migration lock
  • [x] Full debug & pretty log support
  • [x] Custom migration folder structure
  • [x] Stable (Used in Ghost for many years in thousands of blogs in production mode)

Install

npm install knex-migrator --save

or

yarn add knex-migrator

Add me to your globals:

  • npm install --global knex-migrator

Usage

Pre-word

  • Replicas are unsupported, because Knex.js doesn't support them.
  • Sqlite does not support read locks by default. Read here why.
  • Comparison with other available migration tools.
  • Don't mix DDL/DML statements in a migration script. In MySQL DDL statements use implicit commits.
  • It's highly recommended to write both the up and the down function to ensure a full rollback.
  • If your process dies while migrations are running, knex-migrator won't be able to release the migration lock. To release to lock you can run knex-migrator rollback. But it's recommended to check your database first to see in which state it is. You can check the tables migrations and migrations_lock. The rollback will rollback any migrations which were executed based on your current version.

Configure knex-migrator

The tool requires a config file in your project root. Please add a file named MigratorConfig.js. Knex-migrator will load the config file.

module.exports = {
    database: {
        client:         String          (Required) ['mysql', 'mysql2', 'sqlite3']
        connection: {
            host:       String,         (Required) [e.g. '127.0.0.1']
            user:       String,         (Required)
            password:   String,         (Required)
            charset:    String,         (Optional) [Default: 'utf8mb4']
            database:   String          (Required)
        }
    },
    migrationPath:      String,         (Required) [e.g. '/var/www/project/migrations']
    currentVersion:     String,         (Required) [e.g. '2.0']
    subfolder:          String          (Optional) [Default: 'versions']
}

Please take a look at this real example.

Folder Structure

project/
    migrations/
        hooks/
            init/
                index.js
                before.js
                shutdown.js
            migrate/
                index.js
                after.js
                shutdown.js
        init/
            1-add-tables.js
        versions/
            1.0/
                1-add-events-table.js
                2-normalise-settings.js
            2.0/
                1-add-timestamps-columns.js
            2.1/
                1-remove-empty-strings.js
                2-add-webhooks-table.js
                3-add-permissions.js

Please take a look at this real example.

Hooks

Knex-migrator offers a couple of hooks, which makes it possible to hook into the migration process. You can create a hook per type: 'init' or 'migrate'. The folder name must be hooks and is not configurable. Please create an index.js file to export your functions, see example.

|hook|description| |---|---| |before|is called before anything happens| |beforeEach| is called before each migration script| |after|is called after everything happened| |afterEach|is called after each migration script| |shutdown|is called before the migrator shuts down|

Migration Files

Config

You can configure each migration script.

module.exports.config = {
  transaction: Boolean
}

Examples


module.exports.up = function(options) {
  const connection = options.connection;

  ...

  return Promise.resolve();
};

module.exports.down = function(options) {
  const connection = options.connection;

  ...

  return Promise.resolve();
}

module.exports.config = {
  transaction: true
};

module.exports.up = function(options) {
  const connection = options.transacting;

  ...

  return Promise.resolve();
};

module.exports.down = function(options) {
  const connection = options.transacting;

  ...

  return Promise.resolve();
}

CLI

Commands

knex-migrator help

$ knex-migrator help
Usage: knex-migrator [options] [command]

Options:
  -v, --version       output the version number
  -h, --help          output usage information

Commands:
  init|i [config]     init db
  migrate|m [config]  migrate db
  reset|r             reset db
  health|h            health of db
  rollback|ro         rollbacks your db
  help [cmd]          display help for [cmd]

knex-migrator health

  • Returns the database health/state
  • Based on your current version and your migration scripts

knex-migrator init

  • Initializes your database based on your init scripts
  • Creates the database if it was not created yet
Options
# Skips a specific migration script
--skip

# Runs only a specific migration script
--only

# Path to MigratorConfig.js
--mgpath

knex-migrator migrate

  • Migrates your database to latest version
  • Automatic rollback if an error occurs
Options
# The version you would like to migrate to
--v

# Combo Feature to check whether the database was already initialized
--init

# Force the execution no matter which current version you are on
--force

# Path to MigratorConfig.js
--mgpath

knex-migrator rollback

  • Rolls back your database
  • By default, you can only rollback if the database is locked
Options
# Ignores the migration lock
--force

# Version you would like to rollback to
--v

knex-migrator reset

  • Resets your database
  • Removes the database
Options
# Ignores the migration lock
--force

Advanced

DEBUG=knex-migrator:* knex-migrator migrate

JS API

Instantiation

const KnexMigrator = require('knex-migrator');

# Option 1: Pass path to MigratorConfig.js
const knexMigrator = new KnexMigrator({
    knexMigratorFilePath: process.cwd()
});

# Option 2: Pass object with config
const knexMigrator = new KnexMigrator({
    knexMigratorConfig: { ... }
});

Commands

# Health
knexMigrator.isDatabaseOK

# Initialise database
knexMigrator.init

# Migrate database
knexMigrator.migrate

# Rollback database
knexMigrator.rollback

# Reset database
knexMigrator.reset

Examples

knexMigrator.isDatabaseOK()
  .then(function() {
     // database is OK
     // initialization & migrations are not missing
  })
  .catch(function(err) {
      if (err.code === 'DB_NOT_INITIALISED') {
          return knexMigrator.init();
      }

      if (err.code === 'DB_NEEDS_MIGRATION') {
        return knexMigrator.migrate();
      }
  });

Test

  • yarn lint run just eslint
  • yarn test run eslint && then tests
  • NODE_ENV=testing-mysql yarn test to test with MySQL

Publish

  • yarn ship

Copyright & License

Copyright (c) 2013-2023 Ghost Foundation - Released under the MIT license.