Skip to content

Run sequelize migration for more than one databases with a single command.

License

Notifications You must be signed in to change notification settings

MJunaidAhmad/multitenant-migration-cli

Repository files navigation

Problem Statement

When running a multi-tenant application i.e. one databse per tenant, it is hard to manage database structure of each tenant database seperately. Altough Sequelize provide us with a great option of migrations & seeders but it does not support the multi-tenant option out the box because every system can have a different approach to implement multi-tenancy.

Solution

This package is a cli base wrapper written on the top of the sequelize cli.

Supported Usecase

Following use case are supported for now:

  1. You need to have a master database where we need a table named as "tenants" with the following columns: username, password, db, host. You need to have the information decsribed above for every tenant database. You can clone this repo and run npm install.

More will be added soon...


migration-cli

  • 🚀 Supports sequelize commands out of the box
  • 🤯 Supports mysql, Postgres, Microsoft SQL Server, Sqlite, mariadb
  • 📟 Sequelize cli output reporting

Install

# Clone this repo:
git clone https://github.com/MJunaidAhmad/multitenant-migration-cli.git

# cd to the clonned repo folder:
cd multitenant-migration-cli-master

# Install npm modules:
npm install

# Run this command in the clonned folder:
node index.js help

sequelize connection setup

You can define your master database connection settings in config/config.json file.

Usage

node index.js <command> [--option]

Commands

run         Run the Sequelize command provided in the option
help        Show help

Options

    --db, --d       To specify the database from where we need to load tenant information. Supported values test,
                    development or production as defined in the config/config.js
--command, --c      To specify the sequelize command to run. For now, supported value are:
		    'db:migrate'
                    'db: migrate: undo'
                    'db: migrate: undo: all'
                    'db: seed: all'
                    'db: seed: undo'
                    'db: seed: undo: all'

Examples

node index.js run --db 'development' --command 'db:migrate'

But in a real world scenario you may want to create a migration first using sequelize-cli e.g. sequelize migration:create --name sometablename

CLI Help

# Display the help data.
    node index.js help

Changelog

Soon to be updated!


Roadmap

  1. Support better error handling.
  2. Run a migration to only specified tenant rather than every tenant.
  3. Advance options for migrations e.g storage, dialect options.
  4. Migration status of every tenant i.e. how many migrations have been run on this tenants.
  5. Support --to (sequelize-cli) option for migrations & seeders.
  6. Support --except (custom) option for running migrations except specified tenants.
  7. Support to load tenants credentials from json file, api or redis rather than database.
  8. Support to check if every database is up to date with migrations.

Feel free to create an issue if you have something in your mind related to this package.


License & Conduct

Sources

  1. Sequelize CLI

Connect

GitHub @mjunaidahmad (follow)

Twitter @junaidahnmad_12 (follow)

LinkedIn @JunaidAhmad (connect) On the LinkedIn profile y'all

Tags

Multi-tenant Multitenant Multi-tenancy Multitenancy Nodejs Multi-tenancy with sequelize & Nodejs Migrations for Multitenant System

Releases

No releases published

Sponsor this project

Packages

No packages published