mysql2-migration
v2.2.1
Published
A tool to support migration using the mysql 2 package in node.js
Downloads
15
Readme
node-mysql2-migrations
A tool to support migration using the mysql 2 package in node.js
Prerequisites
A node project with mysql2 used for database.
Install
It can be installed using npm.
npm i mysql2-migration
Setup
- Create a file in config directory where you define your database config. (./config/database.js)
database.js:
module.exports = {
host: 'localhost', // optional
port: '3306', // optional
user: 'user',
database: 'database',
password: 'password'
}
- In the root of your project, create a file with the package connection. (Should be on one level with node_modules)
migration.js:
require('mysql2-migrations')
Create a migration
Run
node migration.js create create_users_table
node migration.js create update_users_table
node migration.js create drop_users_table
node migration.js create add_age_column_to_users_table
node migration.js create add_age_column_to_users_table string
node migration.js create change_age_column_users_table
node migration.js create change_age_column_users_table integer
node migration.js create drop_age_column_from_users_table
node migration.js create drop_age_column_from_users_table tnteger
After that, you can find your migration in ./database/migrations.
Executing Migrations
There are few ways to run migrations.
- Run
node migration.js up
. Runs all the pendingup
migrations. - Run
node migration.js up 1
. Runs only 1 pending migrationup
. - Run
node migration.js up 2022_22_07_00675_create_users_table.js
. Runs a migration with name 2022_22_07_00675_create_users_table.js. - Run
node migration.js down
. Runs all the migrationsdown
from the last upped. - Run
node migration.js down 1
. Runs only 1down
migrations from the last upped. - Run
node migration.js refresh
. Deletes all tables and runs all the pendingup
migrations.
If you forgot something
node migration.js help
Documentation
See documentation.