mysql-migration 1.1.1 → 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/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')
@@ -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.1.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
- "mysql": "^2.18.1",
19
- "moment": "^2.24.0",
18
+ "commander": "^13.1.0",
20
19
  "fs": "0.0.1-security",
21
- "commander": "^2.20.0"
20
+ "moment": "^2.30.1",
21
+ "mysql2": "^3.13.0"
22
22
  },
23
23
  "author": "SherKan",
24
24
  "license": "SEE LICENSE IN LICENSE.md"
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
  //==============================================================================
3
3
  const fs = require('fs');
4
- const mysql = require('mysql');
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;
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
  //==============================================================================
3
3
  const fs = require('fs');
4
- const mysql = require('mysql');
4
+ const mysql = require('mysql2');
5
5
  //---------------------------------------
6
6
  const currentPath = process.cwd();
7
7
  const config = require(`${currentPath}/migrations/mysql-migration.config.json`);
@@ -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
  }