/schema-migrations

Meteor package for mongodb migrations with simple schema

Primary LanguageJavaScriptMIT LicenseMIT

Circle CI

bookmd:schema-migrations

@DEPRECATION NOTICE
We decided to deprecate the package due to major architectural changes and the Meteor version update that require fundamental changes in the package.
Last stable version: 0.1.5, Meteor version: 1.2.0.1

This package ables to perform migrations with mongodb and simple schema.
It compares 2 schemas and creates a migration file that contains auto migration actions wherever it is possible.
The auto migrations can be edited, you don't have to use it, it just can make your life easier when creating migrations with a lot of collections.
The package contains scripts for an easy use also on production environment, that can give proper control on your migrations.

Installation

  1. Create a scripts folder in your meteor app:

    mkdir scripts
    
  2. The package contains scripts for easier usage. So Create a migrations-setup-settings.json file for the package installation definitions:

    {
     "scriptsDir": "relative/path/to/scripts/directory/from/meteor/project/dir"
    }
    

    Create the file in your Meteor project home folder: $PROJECT_HOME/migrations-setup-settings.json

  3. Install the package as mentioned above.

    meteor add bookmd:schema-migrations
    
  4. Run your Meteor app, you'll probably get a message like that: "Cannot copy schema-migrations package scripts yet. dir still not exists.".
    Thats ok, because the package needs to copy its scripts to your project, and the package is not loaded yet by Meteor

  5. Run your Meteor app again, now the package will copy the scripts to the directory specified in the migrations-setup-settings.json file.

Usage

Scripts Usage

!! Meteor must be up while running the scripts !!
From your Meteor app directory, run:

  1. To create migration (example)

    scripts/create-migration.sh --name my_migration --version 1 --oldVersion 0 --path /path/to/migrations/parent/dir
  2. To run the migrations

    scripts/run-migrations.sh --config migrations-config.json --environment dev --op up --targetDir /path/to/migrations/parent/dir
  3. Creating the first migration:
    The package compares between your old schema and the new one. But what to do on he first time?
    You can run the creation script with an init parameter, and it will create your schema file to compare with the next migrations:

    scripts/create-migration.sh --init --name my_migration --version 0 --path /path/to/migrations/parent/dir
  4. Checking migrations ran:
    Another script for you to use is check-migrations.sh. It will show you the migration files that already ran on your database.

    scripts/check-migrations --config migrations-config.json --environment dev

    You can run it with a --last flag (or -l) to get the last migration file ran

Meteor call usage

You can create a migration file using a Meteor call:

Meteor.call(MigrationsMethodName.MIGRATIONS_CREATE, 'migration-name', migration-version, migration-old-version, '/absolute/path/to/migrations/parent/dir');

If you wish just to create a schema file, you can call this method:

Meteor.call(MigrationsMethodName.MIGRATIONS_CREATE_SCHEMA_ONLY, 'migration-name', migration-version, '/absolute/path/to/migrations/parent/dir');

Configuration

You'll need a configuration file with details of the database connection.
Several environments can be set also. Example:

{
  "dev" : {
    "host": "localhost",
    "db": "meteor",
    "port": "3001",
    "username": "user name if needed",
    "password": "password if needed",
    "authOptions": "mongo auth options if needed"
  }
}

You can use an example configuration file located in:

.meteor/local/build/programs/server/assets/packages/bookmd_schema-migrations/migrations-config.json

You can create several environments and call a different --environment when running run-migrations.sh parameter as described above.

Stop copying scripts

The package will copy the scripts to the folder specified in the migrations-setup-settings.json file every Meteor build, unless the files exists. If you wish to stop it, and NOT copy the package scripts anymore, you can set an environment variable for skipping the package plug in:

    export SKIP_MIGRATIONS_COPY=1