mysql-migration 1.1.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -5
- package/index.js +20 -1
- package/package.json +4 -4
- package/src/commands/back.js +1 -1
- package/src/commands/batch.js +52 -0
- package/src/commands/run.js +1 -1
- package/src/utils/functions.js +11 -1
package/README.md
CHANGED
|
@@ -41,7 +41,7 @@ The configuration file `mysql-migration.config.json` should export an object wit
|
|
|
41
41
|
### Create a new migration
|
|
42
42
|
|
|
43
43
|
To create a new migration, run the following command:\
|
|
44
|
-
 `npx mysql-migration migration-name database-name`
|
|
44
|
+
 `npx mysql-migration create migration-name database-name`
|
|
45
45
|
|
|
46
46
|
This will create a new migration file in the `migrations` directory with a timestamp and the name `migration-name`.
|
|
47
47
|
|
|
@@ -59,8 +59,4 @@ To roll back the last migration, run the following command:\
|
|
|
59
59
|
|
|
60
60
|
This will roll back migrations to the given batch number.
|
|
61
61
|
|
|
62
|
-
<br>
|
|
63
|
-
|
|
64
62
|
> **Copyright (c) 2023-present [𝐒𝐡𝐞𝐫𝐊𝐚𝐧](https://github.com/SherKan-n). All Rights Reserved.**
|
|
65
|
-
|
|
66
|
-
<br>
|
package/index.js
CHANGED
|
@@ -1,6 +1,20 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
const program = require('commander');
|
|
3
3
|
//---------------------------------------
|
|
4
|
+
program
|
|
5
|
+
.command('help')
|
|
6
|
+
.description('Show all available commands')
|
|
7
|
+
.action(() => {
|
|
8
|
+
console.log(`\nUsage: cli-tool <command> [options]\n`);
|
|
9
|
+
console.log(`Available commands:\n`);
|
|
10
|
+
console.log(` init Initialize migration`);
|
|
11
|
+
console.log(` run [dbName] Run migration`);
|
|
12
|
+
console.log(` rollback <dbName> <batch> Rollback migration`);
|
|
13
|
+
console.log(` create <name> <dbName> Create a new migration`);
|
|
14
|
+
console.log(` batch <dbName> Get the batched migrations`);
|
|
15
|
+
console.log(` help Show this help message\n`);
|
|
16
|
+
});
|
|
17
|
+
//---------------------------------------
|
|
4
18
|
program
|
|
5
19
|
.command('init')
|
|
6
20
|
.description('Initialize migration')
|
|
@@ -13,7 +27,7 @@ program
|
|
|
13
27
|
//---------------------------------------
|
|
14
28
|
program
|
|
15
29
|
.command('rollback <dbName> <batch>')
|
|
16
|
-
.description('
|
|
30
|
+
.description('Rollback migration')
|
|
17
31
|
.action((dbName, batch) => require('./src/commands/back')(dbName, batch));
|
|
18
32
|
//---------------------------------------
|
|
19
33
|
program
|
|
@@ -21,4 +35,9 @@ program
|
|
|
21
35
|
.description('Create a new migration')
|
|
22
36
|
.action((migrationName, dbName) => require('./src/commands/create')(migrationName, dbName));
|
|
23
37
|
//---------------------------------------
|
|
38
|
+
program
|
|
39
|
+
.command('batch <dbName>')
|
|
40
|
+
.description('Get the batched migrations')
|
|
41
|
+
.action(() => require('./src/commands/batch'));
|
|
42
|
+
//---------------------------------------
|
|
24
43
|
program.parse(process.argv);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mysql-migration",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "Migration for mysql database",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"bin": {
|
|
@@ -15,10 +15,10 @@
|
|
|
15
15
|
"mysql"
|
|
16
16
|
],
|
|
17
17
|
"dependencies": {
|
|
18
|
-
"
|
|
19
|
-
"moment": "^2.24.0",
|
|
18
|
+
"commander": "^13.1.0",
|
|
20
19
|
"fs": "0.0.1-security",
|
|
21
|
-
"
|
|
20
|
+
"moment": "^2.30.1",
|
|
21
|
+
"mysql2": "^3.13.0"
|
|
22
22
|
},
|
|
23
23
|
"author": "SherKan",
|
|
24
24
|
"license": "SEE LICENSE IN LICENSE.md"
|
package/src/commands/back.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
//==============================================================================
|
|
3
3
|
const fs = require('fs');
|
|
4
|
-
const mysql = require('
|
|
4
|
+
const mysql = require('mysql2');
|
|
5
5
|
//---------------------------------------
|
|
6
6
|
const currentPath = process.cwd();
|
|
7
7
|
const config = require(`${currentPath}/migrations/mysql-migration.config.json`);
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
//==============================================================================
|
|
3
|
+
const mysql = require('mysql2');
|
|
4
|
+
//---------------------------------------
|
|
5
|
+
const currentPath = process.cwd();
|
|
6
|
+
const config = require(`${currentPath}/migrations/mysql-migration.config.json`);
|
|
7
|
+
const { checkTableMigrations, createTableMigrations, getAllBatches, getCurrentBatch } = require("../utils/functions");
|
|
8
|
+
//==============================================================================
|
|
9
|
+
async function show_batched_migrations(dbName) {
|
|
10
|
+
const connection = {};
|
|
11
|
+
const databases = config.databases;
|
|
12
|
+
//---------------------------------------
|
|
13
|
+
if (dbName) {
|
|
14
|
+
if (!databases[dbName]) {
|
|
15
|
+
console.error('\x1b[31m%s\x1b[0m', `Error: Invalid database name "${dbName}".`);
|
|
16
|
+
process.exit(1);
|
|
17
|
+
}
|
|
18
|
+
//---------------------------------------
|
|
19
|
+
connection[dbName] = mysql.createConnection(databases[dbName]);
|
|
20
|
+
connection[dbName].connect((err) => {
|
|
21
|
+
if (err) {
|
|
22
|
+
console.error('\x1b[31m%s\x1b[0m', `Error: Unable to connect to database "${dbName}".\n${err}`);
|
|
23
|
+
process.exit(1);
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
//---------------------------------------
|
|
27
|
+
const tableMigrations = checkTableMigrations(connection[dbName]);
|
|
28
|
+
if (!tableMigrations) createTableMigrations(connection[dbName]);
|
|
29
|
+
//---------------------------------------
|
|
30
|
+
const currentBatch = await getCurrentBatch(connection[dbName]);
|
|
31
|
+
//---------------------------------------
|
|
32
|
+
getAllBatches(connection[dbName]).then((migrations) => {
|
|
33
|
+
if (migrations.length > 0) {
|
|
34
|
+
console.log('\x1b[32m%s\x1b[0m', `Batched migrations for database "${dbName}":`);
|
|
35
|
+
migrations.forEach((migration) => {
|
|
36
|
+
const migrationName = migration.migration.split('_').slice(4).join('_');
|
|
37
|
+
console.log(`[Batch ${migration.batch}] - ${migrationName}`);
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
else console.log('\x1b[32m%s\x1b[0m', `No batched migrations for database "${dbName}".`);
|
|
41
|
+
connection[dbName].end();
|
|
42
|
+
}).catch((err) => {
|
|
43
|
+
console.error('\x1b[31m%s\x1b[0m', `Error: ${err}`);
|
|
44
|
+
connection[dbName].end();
|
|
45
|
+
}).finally(() => {
|
|
46
|
+
console.log('\x1b[36m%s\x1b[0m', `Current batch for database "${dbName}": ${currentBatch}`);
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
else console.error('\x1b[31m%s\x1b[0m', `Error: Database name is empty !`);
|
|
50
|
+
}
|
|
51
|
+
//==============================================================================
|
|
52
|
+
module.exports = show_batched_migrations;
|
package/src/commands/run.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
//==============================================================================
|
|
3
3
|
const fs = require('fs');
|
|
4
|
-
const mysql = require('
|
|
4
|
+
const mysql = require('mysql2');
|
|
5
5
|
//---------------------------------------
|
|
6
6
|
const currentPath = process.cwd();
|
|
7
7
|
const config = require(`${currentPath}/migrations/mysql-migration.config.json`);
|
package/src/utils/functions.js
CHANGED
|
@@ -64,6 +64,16 @@ function deleteMigration(connection, migration, batch) {
|
|
|
64
64
|
});
|
|
65
65
|
}
|
|
66
66
|
//---------------------------------------
|
|
67
|
+
function getAllBatches(connection) {
|
|
68
|
+
return new Promise((resolve) => {
|
|
69
|
+
connection.query("SELECT `batch`, `migration` FROM `migrations`;", (error, results) => {
|
|
70
|
+
if (error) throw error;
|
|
71
|
+
resolve(results?.length > 0 ? results : []);
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
//---------------------------------------
|
|
67
76
|
module.exports = {
|
|
68
|
-
checkTableMigrations, createTableMigrations, getAllMigrations, getCurrentBatch, insertMigration, deleteMigration
|
|
77
|
+
checkTableMigrations, createTableMigrations, getAllMigrations, getCurrentBatch, insertMigration, deleteMigration,
|
|
78
|
+
getAllBatches
|
|
69
79
|
}
|