Code Monkey home page Code Monkey logo

loopback-component-migrate's Introduction

A library to add simple database migration support to loopback projects.

Dependencies

Migrations that have been run will be stored in a table called 'Migrations'. The library will read the loopback datasources.json files based on the NODE_ENV environment variable just like loopback does. The usage is based on the node-db-migrate project.

Installation

Greenkeeper badge

  1. Install in you loopback project:

npm install --save loopback-component-migrate

  1. Create a component-config.json file in your server folder (if you don't already have one)

  2. Enable the component inside component-config.json.

{
  "loopback-component-migrate": {
    "key": "value"
  }
}

Options:

  • log

    [String] : Name of the logging class to use for log messages. (default: 'console')

  • enableRest

    [Boolean] : A boolean indicating wether migrate/rollback REST api methods should be exposed on the Migration model. (default: false)

  • migrationsDir

    [String] : Directory containing migration scripts. (default: server/migrations)

  • dataSource

    [String] : Datasource to connect the Migration and MigrationMap models to. (default: db)

  • acls

    [Array] : ACLs to apply to Migration and MigrationMap models. (default: [])

Running Migrations

Migrations can be run by calling the static migrate method on the Migration model. If you do not specify a callback, a promise will be returned.

Run all pending migrations:

Migrate.migrate('up', function(err) {});

Run all pending migrations upto and including 0002-somechanges:

Migrate.migrate('up', '0002-somechanges', function(err) {});

Rollback all migrations:

Migrate.migrate('down', function(err) {});

Rollback migrations upto and including 0002-somechanges:

Migrate.migrate('down', '0002-somechanges', function(err) {});

Example migrations

module.exports = {
  up: function(app, next) {
    app.models.Users.create({ ... }, next);
  },
  down: function(app, next) {
    app.models.Users.destroyAll({ ... }, next);
  }
};
/* executing raw sql */
module.exports = {
  up: function(app, next) {
    app.dataSources.mysql.connector.query('CREATE TABLE `my_table` ...;', next);
  },
  down: function(app, next) {
   app.dataSources.mysql.connector.query('DROP TABLE `my_table`;', next);
  }
};

loopback-component-migrate's People

Contributors

alexthewilde avatar doublemarked avatar elhoyos avatar greenkeeper[bot] avatar ivesdebruycker avatar listepo avatar lsocrate avatar rahulbile avatar slively avatar slivelygh avatar sundeepgupta avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

loopback-component-migrate's Issues

Migrations based on environment

It would be really nice to be able to run migrations that apply to the current environment. The implementation could be similar to how Loopback currently deals with environment based configs. For example, if you only want to run a migration on stage you could name you file 0001-migration.stage.js or if you only wanted to run it on production, it would be 0001-migration.production.js.

An in-range update of loopback is breaking the build 🚨

Version 3.19.1 of loopback was just published.

Branch Build failing 🚨
Dependency loopback
Current Version 3.19.0
Type devDependency

This version is covered by your current version range and after updating it in your project the build failed.

loopback is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • ❌ ci/circleci Your tests failed on CircleCI Details

Commits

The new version differs by 3 commits.

  • 3e3092c 3.19.1
  • 07e7592 Merge pull request #3883 from strongloop/fix/principal-type-polymorphic-user
  • 2aead13 Fix isOwner() bug in multiple-principal setup

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

An in-range update of ghooks is breaking the build 🚨

Version 2.0.4 of ghooks was just published.

Branch Build failing 🚨
Dependency ghooks
Current Version 2.0.3
Type devDependency

This version is covered by your current version range and after updating it in your project the build failed.

ghooks is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • ❌ ci/circleci: Your tests failed on CircleCI (Details).

Commits

The new version differs by 1 commits.

  • 1e80ce2 fix: node_modules absolute entry point never getting found (#224)

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

Callback not getting called (or promise not being returned) when there are no migrations to run

I'm implementing the migration in a boot script before my actual application code is run like this:

const loopback = require('loopback');
const Migration = loopback.getModel('Migration');

module.exports = app => {
  Migration.migrate('up', error => {
    if (error) {
      console.error('Error in migrations: %s', error.stack);
      return;
    }

    console.log('boot app now');
  });
};

This works when there are actual migrations pending. However, if not, I see "No new migrations to run." outputted, but the "boot app now" log line never gets executed.

If I put cb() here: https://github.com/fullcube/loopback-component-migrate/blob/master/lib/models/migration.js#L152, it works ok, but I'm pretty sure that's not correct, also, this wouldn't return a promise. I'm not sure how to fix this in the right way.

migrationsDir should accept relative path

Currently, migrationsDir expects an absolute path (even though the default value of server/migrations is relative). Path should be relative to the application root.

An in-range update of commander is breaking the build 🚨

Version 2.16.0 of commander was just published.

Branch Build failing 🚨
Dependency commander
Current Version 2.15.1
Type dependency

This version is covered by your current version range and after updating it in your project the build failed.

commander is a direct dependency of this project, and it is very likely causing it to break. If other packages depend on yours, this update is probably also breaking those in turn.

Status Details
  • ❌ ci/circleci Your tests failed on CircleCI Details

Release Notes v2.16.0
  • Remove Makefile and test/run (#821)
  • Make 'npm test' run on Windows (#820)
  • Add badge to display install size (#807)
  • chore: cache node_modules (#814)
  • chore: remove Node.js 4 (EOL), add Node.js 10 (#813)
  • fixed typo in readme (#812)
  • Fix types (#804)
  • Update eslint to resolve vulnerabilities in lodash (#799)
  • updated readme with custom event listeners. (#791)
  • fix tests (#794)
Commits

The new version differs by 17 commits.

  • 4cc348b Merge pull request #822 from abetomo/version_bump_2.16.0
  • 8db14db version bump 2.16.0
  • 1f9354f Remove Makefile and test/run (#821)
  • 3f4f5ca Make 'npm test' run on Windows (#820)
  • 3b8e519 Merge pull request #807 from styfle/patch-1
  • 77ffd4f Merge pull request #814 from DanielRuf/chore/cache-node-modules
  • 6889693 chore: cache node_modules
  • ff2f618 Merge pull request #813 from DanielRuf/chore/remove-nodejs-4-add-nodejs-10
  • d5c1d7d chore: remove Node.js 4 (EOL), add Node.js 10
  • c05ed98 Merge pull request #812 from yausername/fixReadme
  • 55ff22f fixed typo in readme
  • 2415089 Add badge to display install size
  • 89edef0 Fix types (#804)
  • 001d560 Update eslint to resolve vulnerabilities in lodash
  • 988d09b Merge pull request #791 from yausername/master

There are 17 commits in total.

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

Accept dataSource as a parameter

I think you should be able to select the data source that you're migrating as part of the API rather than only selecting a single data source in a config file.

MigrationMap unused?

While working on the pull request I just submitted, I noticed that the MigrationMap model seems unused. Apart from the index.js defining the model and the files that define the model, there is absolutely no reference to it.

Can it be removed? I'd rather not have unused tables in my database... :)

Cheers,

Jouke

An in-range update of bluebird is breaking the build 🚨

Version 3.5.2 of bluebird was just published.

Branch Build failing 🚨
Dependency bluebird
Current Version 3.5.1
Type devDependency

This version is covered by your current version range and after updating it in your project the build failed.

bluebird is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • ❌ ci/circleci: Your tests failed on CircleCI (Details).

Release Notes v3.5.2

Bugfixes:

  • Fix PromiseRejectionEvent to contain .reason and .promise properties. (#1509, #1464)
  • Fix promise chain retaining memory until the entire chain is resolved (#1544, #1529)

id: changelog
title: Changelog

Commits

The new version differs by 22 commits.

  • 50067ec Release v3.5.2
  • f290da0 Fix memory being retained until promise queue is completely empty (#1544)
  • ad6d763 Update benchmarks (#1539)
  • 49da1ac Fix a typo. (#1534)
  • b06106a Fix typo in readme introduced in #1530 (#1531)
  • c1dc5b9 Update README.md (#1530)
  • e35455f chore: clone last 5 commits (#1521)
  • 91ae9ce chore: add Node.js 10 (#1523)
  • 9159472 Added a simple usage example (#1369)
  • 39081ba Update promise.each.md (#1479)
  • 77781fe Fix header (#1513)
  • b8eedc1 Update LICENSE to 2018 (#1490)
  • 4163e82 Added ES6 way to import the bluebird promise (#1461)
  • 3790a92 DOC: add direct link to Promise.delay from API ref (#1465)
  • e8d8525 Update error documentation (#1469)

There are 22 commits in total.

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

How to run migration from cli

How can I run migrations from a command like npm run migrate?

also where to put Migrate.migrate('up', function(err) {});??

Question: How do you implement this via CLI?

I thought I should be implementing this using the CLI so that it could be part of a deploy sequence, something like:

Note: I've aliased loopback-component-migrate binary command to lcm

  1. npm install
  2. lcm up
  3. node server.js

I created a migration via lcm create initial which created the migration file in ./migrations/20160810101725-initial.js. The migration file looks like:

module.exports = {
  up: function(dataSource, next) {
    console.log('running initial up');
    next();
  },
  down: function(dataSource, next) {
    console.log('running initial down');
    next();
  }
};

When I run lcm up, first thing output is "Migrating up to: "20160810101725-initial.js" [TODO]" and then I see output from starting the app up, just like I'd see if I ran node server.js. I don't see the logs I inserted into the migration or anything. The app continues to listen for web requests/socket messages...

I'm using MongoDB as my data store and when I look inside, I don't see a collection for Migrations or anything.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.