knex-mig
v0.1.5
Published
Relational Database migration tool - built on top of knex.js
Downloads
3
Maintainers
Readme
Features
- Atomic operations
- Automatic rollback on error
- Seeding
- Transaction support
- Migration locking
- Multi environment support
- Selective/Partial migration
Installation
npm install -g knex-migUsage
To get started, run mg -h to see the available commands and options.
- To get the directories needed for the migrator to function properly, run
mg setup. This will create some directories in your$USER_HOMEdirectory.
mg setup- 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 newsample.envfile from where you ran the command.
mg env g local-dbOpen up the generated file and fill in the necessary details for your database connection.
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- 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- 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.envfile.
mg make users- Edit the generated file as necessary
- To see a list of available migrations, and their current state run
mg state.
mg ss- 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 usersFor 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
