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

migrate-semver

v0.7.0

Published

Database agnostic migrations based on SemVer

Downloads

409

Readme

Database agnostic migrations based on SemVer using Node.js

migrate-semver is a small library to allow you do to migrations based on SemVer in a database agnostic way using Node.js.

Installation

npm install --save migrate-semver

Usage

migrate-semver just handles the heavy lifting of finding the viable migrations to run based on a SemVer compliant version string passed into it.

The following example runs a migration for version 0.3.0.

const version = '0.3.0';
const migrationsDirectory = path.join(__dirname, 'migrations');
const migrateSemVer = new SemVerMigration({ migrationsDirectory }, plugin());

migrateSemVer.connect({}, err => {
  migrateSemVer.up({ version }, err => {
    assert.equal(migrations[2].version, version);
    done();
  });
});

migrate-semver handles several scenarios for you based on the example above:

  • If your current database has no migrations at all (so it might be empty at all), migrate-semver runs all available migrations until 0.3.0 (including 0.3.0).
  • If your current database has version 0.2.0 applied and 0.3.0 is the next available migration, 0.3.0 is just applied.
  • If your current database has version 0.1.0 applied and 0.2.0 is also available, both 0.2.0 and 0.3.0 wil be applied.

migrate-semver allows you to specify the base directory which contains all your migration folders and files. Just pass the migrationsDirectory option to the SemVerMigration ctor function as shown above.

The file and folder structure has to follow this convention (the index-up.js can be renamed if you implement your own plugin):

canMigrate

If you want to automate your migrations, your code triggering the migrations can check whether an migration for a particular version can be done using the migrateSemVer.canMigrate({ version }) function:

const version = '0.3.0';

migrateSemVer.connect({}, err => {
  migrateSemVer.canMigrate({ version }, (err, canMigrate) => {
    if (canMigrate) {
      migrateSemVer.up({ version }, err => {
        assert.equal(migrations[2].version, version);
        done();
      });
    }
  });
});

If you don't do this sane check and the migration file or directory won't exist, you'll receive an error during when trying to run the migration.

hasMigration

The hasMigration function allows you to check up front, if migrate-semver has applied a particular migration already:

migrateSemVer.hasMigration({ version, direction: 'up' }, (err, hasMigration) => { 
  assert.equal(hasMigration, true);
  done();
});

customOptions

You can pass custom options to the up function as child object of the options parameter. These customOptions will be passed into the plugin and if the plugin handles them appropriately, they'll be passed into every migration (the 'how' depends on the particular plugin).

migrateSemVer.up({ version, customOptions }, err => {
  assert.equal(migrations[2].version, version);
  done();
});

Plugins

As said, migrate-semver is database agnostic, so the concrete implementation for a particular database has to be done by a plugin which has to be passed to the SemVerMigration ctor function as well.

Currently there is one plugin implementation available for MongoDb using mongoose.

For more details on migrate-semver-mongoose visit the GitHub-Repository.

If you want to implement your own migrate-semver plugin, just implement a Node.js module exporting the following functions:

const plugin = function(options, callback) {

  const hasMigrationsTable = function (options, callback) {
    callback(null, hasTable);
  };

  const createMigrationsTable = function (options, callback) {
    callback();
  };

  /**
  * @param {Object} options
  * @param {String} options.version
  * @param {String} options.direction - may be up or down
  */
  const hasMigration = function (options, callback) {
    callback(null, hasMigrationApplied);
  };

  const getLatestAppliedMigration = function (callback) {
    return callback(null, latestMigration); // e.g. '0.2.0'
  };

  /**
  * @param {Object} options
  * @param {String} options.version
  * @param {String} options.direction - may be up or down
  */
  const addMigrationToMigrationsTable = function (options, callback) {
    callback();
  };

  /**
  * @param {Object} options
  * @param {String} options.version
  * @param {String} options.migrationsDirectory
  * @param {Object} [options.customOptions]
  */
  const up = function (options, continueWith) {
    const migrationsDirectory = options.migrationsDirectory;
    const migrationPath = path.join(migrationsDirectory, options.version, 'index-up');
    const migration = require(migrationPath); // eslint-disable-line
    migration.up(options, callback);
  };

return {
    connect,
    hasMigrationsTable,
    createMigrationsTable,
    hasMigration,
    getLatestAppliedMigration,
    addMigrationToMigrationsTable,
    up
  };
}

module.exports = plugin;

After this, just require your plugin and pass it to the SemVerMigration ctor function.

Running the tests

npm test

Want to help?

This project is just getting off the ground and could use some help with cleaning things up and refactoring.

If you want to contribute - we'd love it! Just open an issue to work against so you get full credit for your fork. You can open the issue first so we can discuss and you can work your fork as we go along.

If you see a bug, please be so kind as to show how it's failing, and we'll do our best to get it fixed quickly.

Before sending a PR, please create an issue to introduce your idea and have a reference for your PR.

Also please add tests and make sure to run npm run eslint.

License

MIT License

Copyright (c) 2016 PDMLab

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.