Node.Js Mysql Migration tool
A simple Mysql migration tool than you can integrate in your project to perform database migration.
How to use:
- type
npm i -g migratify
(for CLI) - type
npm i migratify
(inside your project directory for utility classes) - That's it
Features
Go to your project directory and type Create database:
migratify create-db
This will ask you for your database name, password, user, host etc. Type the answers accordingly. And it will create a
database for you. And inside your project directory, it will create a folder called "migrations". This is where your
migration files will be stored.
Go to your project directory and type Create table:
migratify create-table tablename
.
This will create a migration file in the migrations
directory. Replace the tablename
argument with the name of the table you want to create. Open the newly created file in your code editor. It will look
something like this:
const { Table } = require('migratify/Migration.class')
let newTable =new Table("tablename")
module.exports = async () => {
newTable.create()
}
Here is a list of methods that you can use to add properties in your table:
Method | Description |
---|---|
newTable.setId(idName) |
Adds an ID property of that table. The parameter is a string. The name of the ID column |
newTable.addColumn(columnName,dataType) |
Adds a column named "columnName" with type "dataType". The data types are MySQL data type. |
newTable.addColumn(columnName,dataType).setNullable(true) |
Makes the column NOT NULL |
newTable.addColumn(columnName,dataType).setDefaultValue(defaultValue) |
Sets the default value to a certain value. |
newTable.addColumn(columnName,dataType).setUnique(true) |
Sets the default value to a certain value. |
newTable.addForeignKey(columnName, refTable, refColumn)
|
Sets a foreign key to column name that refers to refColumn column from the
refTable table
|
Go to your project directory and type Update table:
migratify update-table tablename
.
This will create a migration file in the migrations
directory. Replace the tablename
argument with the name of the table you want to create. Open the newly created file in your code editor. It will look
something like this:
const { Table } = require('../templates/Migration.class')
let newTable = new Table("tablename")
module.exports = async () => {
newTable.update()
}
Here is a list of methods that you can use to update your table:
Method | Description |
---|---|
newTable.renameColumn(oldName, newName) |
Renames the column from oldName to newName |
newTable.removeProperty(columnName) |
Removes a column named columnName |
newTable.addColumn(columnName,dataType) |
Described earlier |
newTable.addForeignKey(columnName, refTable, refColumn)
|
Sets a foreign key to column name that refers to refColumn column from the
refTable table
|
renameColumn(oldName, newName) |
Renames a column to a new name |
To commit the changes to the database, run the following command: Type Migrate:
migratify migrate
This will run all the migration files and update the database schema.
Dump schema
To dump the schema of the database, run the command migratify dump-data
.
Dump Data
To dump the schema of the database along with its data, run the command migratify dump-data
.
To create migration files from an existing database, type the command on the terminal Load from existing database:
migratify load-db
.
This will ask the user for database name, password etc.
To undo the effect of the latest commit file, type the command on the terminal Rollback:
migratify rollback
.
Not only that it will roll back, it will also delete the migration file.
To see the list of commands, Type Help:
migratify help
Please add the Note:
logs.txt
and config.txt
to your project's .gitignore
file.