Skip to content

drekuru/knex-mig

Repository files navigation

Knex Mig

CLI tool built on top of knex.js for managing migrations and seeds for PostgreSQL

Features

  • Atomic operations
  • Automatic rollback on error
  • Seeding
  • Transaction support
  • Migration locking
  • Multi environment support
  • Selective/Partial migration

Installation

npm install -g knex-mig

Usage

To get started, run mg -h to see the available commands and options.

  1. To get the directories needed for the migrator to function properly, run mg setup. This will create some directories in your $USER_HOME directory.
mg setup
  1. To setup a connection to a new database, we need to generate a new configuration file. Run mg env generate <filename> to generate a new configuration file. This will generate a new sample.env file from where you ran the command.
mg env g local-db
  1. Open up the generated file and fill in the necessary details for your database connection.

  2. Once you're done editing the necessary details, run mg env add <filename> to add the configuration to the migrator context. This will copy the file into the migrator's configuration directory. Note: Once added, you can always run a command to edit the file directly in the migrator config directory.

# we can use the -d flag to also automatically set the new file as the current configuration/context
mg env a ./local-db.env -d
  1. Now the file is added to the migrator context, however we need to set it as the current context. To do this, run mg env set <filename>.
mg env set local-db.env
  1. To generate a new migration file, run mg make <name>. This will generate a new migration file in the migrations directory that you specified in the .env file.
mg make users
  1. Edit the generated file as necessary
  2. To see a list of available migrations, and their current state run mg state.
mg ss
  1. To run a migration file, do mg up <filename>. This will run the migration file and update the state of the migration in the database.
# we can use the file number instead of the name (in this case it would be `mg up 1` since it's our first migration)
mg up users

For more information on the available commands, run mg -h.

Feature Roadmap:

  • mg seed make <name> - Create a new seed migration (should give user option to create .json or .js file)
  • add support for .sql files
  • mg make <name> - this exists but need to add option to create a .sql file

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published