my-migration-sql
v1.0.3
Published
Node module used for migrating MySQL table
Downloads
11
Readme
my-migration-sql
Table of content
Installation
NodeJS module to automatically load migrations and other queries using MySQL node instance Install NodeJS from the official website by downloading the version based on your O/S Then in your project workspace console run this command
npm install my-migration-sql
You'll install the package
Basic
const { migrations } = require('my-migration-sql');
let Migration = new migrations({
Connection: MySQLConnection,
saveOptions: {
type: 'mysql',
},
path: 'migrations'
});
Migration.up();
This code'll use a MySQL instance to load all the migrations files in the folder called migrations using up method and save the migrated ones in a table in MySQL
Options
Costructor
my-migration-sql
| Option | Type | Explanation |
|---------------------------|----------------------------|--------------------------|
| Connection
| MySQL instance
|Instance of MySQL using createConnection method|
| path
| String
|Path to the migration folder, can be helpful using Node Path |
| saveOptions
| saveOptions instance
or object
|Options used to manage (save or remove) already migrated files|
saveOptions
| Option | Type | Explanation |
|---------------------|--------------------|--------------------|
| type
| String
| Type of saving method of migrated files.Available type:'mysql'
Default: 'mysql'
|
| Connection
| MySQL instance
| Same instance of MySQL from the migrations, used to make queries to the database|
| table_meta
| String
| only mysql
type, assign custom name to the table where saving migrations.Default: 'mysql-meta'
Methods
my-migration-sql
| Method | Return type | Accepted parameter | What does it do? |
|-------------|-------------|---------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| up
| Promise
| migration_name
or [migration_names]
| Execute and load migration_name or migration_names present in the migration path to the database and save the changes. If none migration_name used the method'll load all pending migrations |
| down
| none | migration_name
| Downgrade and unload migration_name or migration_names present in the database. If none migration_name used the method'll unload all migrations (TODO) |
| pending
| none | none | Get the migrations that are still not loaded to the database |
saveOptions
| Method | Return type | Accepted parameter | What does it do? |
|----------------------|-------------|--------------------|-------------------------------------------------------------|
| getAllMigrated
| Promise
| none | Return all migrated files to the database |
| load
| none | migration_name
| Save the migration_name as migrated to the storage option |
| unload
| none | migration_name
| Remove the migration_name from the storage option |
| isValid
| Boolean
| none | Return true if the storageOption is valid to operate |
Migration generate tool
It's also available a tool that can generate a new migration file, by running
node node_modules/my-migration-sql/src/generateMigration.js
you'll create a new migration file called default_name in the folder migrations.
To customize the options of the file you can use add some args to the command
| Args | What does? |
|---------------------|---------------------|
| name
| Customize the name of the migration. Default: 'default_name' |
| path
| Customize the path where save the migration. Default: 'migrations' |
For example:
node node_modules/my-migration-sql/src/generateMigration.js --name=ExampleName --path=./example/test_migration
Create a new migration called ExampleName and save into the folder ./example/test_migration
This is just a demo for this feature because it would be much better have a much cleaner command to run.
IMPORTANT Due to the requirement of node_modules folder if you have a dockerized application you have to run
npm install
anyway to run the use command
REMEMBER For now in the down
and in the up
variable is just allowed use a String
to declare the query
Other information
Issues and Feature
If you have an idea for a new feature or you found a bug please report it in the issues section