mysqlsyncr 1.0.52 → 1.0.56

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.
@@ -0,0 +1,23 @@
1
+ const mysqlSyncr = require('../dist/index.cjs.cjs');
2
+ const path = require('path');
3
+
4
+ const dotenv = require('dotenv');
5
+ dotenv.config({ path: path.resolve(__dirname, '../.env') });
6
+
7
+ console.log(`DB_STRUCTURE_PATH: ${process.env.DB_STRUCTURE_PATH}`);
8
+ console.log(`DB_PASSWORD: ${process.env.DB_PASSWORD}`);
9
+ const config ={
10
+ host: "127.0.0.1",
11
+ user: 'root',
12
+ password: process.env.DB_PASSWORD,
13
+ port: 3306
14
+ };
15
+ const testDbConnection = async () => {
16
+ console.log(mysqlSyncr);
17
+ const diffs = await mysqlSyncr.compare(config, process.env.DB_STRUCTURE_PATH);
18
+ console.log(diffs);
19
+ }
20
+
21
+ testDbConnection();
22
+
23
+
package/dist/apply.js ADDED
@@ -0,0 +1,459 @@
1
+ "use strict";
2
+
3
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.applyDifferences = void 0;
8
+ var _loggers = require("./loggers.js");
9
+ function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return e; }; var t, e = {}, r = Object.prototype, n = r.hasOwnProperty, o = Object.defineProperty || function (t, e, r) { t[e] = r.value; }, i = "function" == typeof Symbol ? Symbol : {}, a = i.iterator || "@@iterator", c = i.asyncIterator || "@@asyncIterator", u = i.toStringTag || "@@toStringTag"; function define(t, e, r) { return Object.defineProperty(t, e, { value: r, enumerable: !0, configurable: !0, writable: !0 }), t[e]; } try { define({}, ""); } catch (t) { define = function define(t, e, r) { return t[e] = r; }; } function wrap(t, e, r, n) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype), c = new Context(n || []); return o(a, "_invoke", { value: makeInvokeMethod(t, r, c) }), a; } function tryCatch(t, e, r) { try { return { type: "normal", arg: t.call(e, r) }; } catch (t) { return { type: "throw", arg: t }; } } e.wrap = wrap; var h = "suspendedStart", l = "suspendedYield", f = "executing", s = "completed", y = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var p = {}; define(p, a, function () { return this; }); var d = Object.getPrototypeOf, v = d && d(d(values([]))); v && v !== r && n.call(v, a) && (p = v); var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p); function defineIteratorMethods(t) { ["next", "throw", "return"].forEach(function (e) { define(t, e, function (t) { return this._invoke(e, t); }); }); } function AsyncIterator(t, e) { function invoke(r, o, i, a) { var c = tryCatch(t[r], t, o); if ("throw" !== c.type) { var u = c.arg, h = u.value; return h && "object" == _typeof(h) && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) { invoke("next", t, i, a); }, function (t) { invoke("throw", t, i, a); }) : e.resolve(h).then(function (t) { u.value = t, i(u); }, function (t) { return invoke("throw", t, i, a); }); } a(c.arg); } var r; o(this, "_invoke", { value: function value(t, n) { function callInvokeWithMethodAndArg() { return new e(function (e, r) { invoke(t, n, e, r); }); } return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(e, r, n) { var o = h; return function (i, a) { if (o === f) throw Error("Generator is already running"); if (o === s) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var c = n.delegate; if (c) { var u = maybeInvokeDelegate(c, n); if (u) { if (u === y) continue; return u; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (o === h) throw o = s, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = f; var p = tryCatch(e, r, n); if ("normal" === p.type) { if (o = n.done ? s : l, p.arg === y) continue; return { value: p.arg, done: n.done }; } "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg); } }; } function maybeInvokeDelegate(e, r) { var n = r.method, o = e.iterator[n]; if (o === t) return r.delegate = null, "throw" === n && e.iterator["return"] && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y; var i = tryCatch(o, e.iterator, r.arg); if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y; var a = i.arg; return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y); } function pushTryEntry(t) { var e = { tryLoc: t[0] }; 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e); } function resetTryEntry(t) { var e = t.completion || {}; e.type = "normal", delete e.arg, t.completion = e; } function Context(t) { this.tryEntries = [{ tryLoc: "root" }], t.forEach(pushTryEntry, this), this.reset(!0); } function values(e) { if (e || "" === e) { var r = e[a]; if (r) return r.call(e); if ("function" == typeof e.next) return e; if (!isNaN(e.length)) { var o = -1, i = function next() { for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next; return next.value = t, next.done = !0, next; }; return i.next = i; } } throw new TypeError(_typeof(e) + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), o(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) { var e = "function" == typeof t && t.constructor; return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name)); }, e.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t; }, e.awrap = function (t) { return { __await: t }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () { return this; }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(wrap(t, r, n, o), i); return e.isGeneratorFunction(r) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () { return this; }), define(g, "toString", function () { return "[object Generator]"; }), e.keys = function (t) { var e = Object(t), r = []; for (var n in e) r.push(n); return r.reverse(), function next() { for (; r.length;) { var t = r.pop(); if (t in e) return next.value = t, next.done = !1, next; } return next.done = !0, next; }; }, e.values = values, Context.prototype = { constructor: Context, reset: function reset(e) { if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t); }, stop: function stop() { this.done = !0; var t = this.tryEntries[0].completion; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function dispatchException(e) { if (this.done) throw e; var r = this; function handle(n, o) { return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o; } for (var o = this.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i.completion; if ("root" === i.tryLoc) return handle("end"); if (i.tryLoc <= this.prev) { var c = n.call(i, "catchLoc"), u = n.call(i, "finallyLoc"); if (c && u) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } else if (c) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); } else { if (!u) throw Error("try statement without catch or finally"); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } } } }, abrupt: function abrupt(t, e) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var o = this.tryEntries[r]; if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) { var i = o; break; } } i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null); var a = i ? i.completion : {}; return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a); }, complete: function complete(t, e) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y; }, finish: function finish(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y; } }, "catch": function _catch(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.tryLoc === t) { var n = r.completion; if ("throw" === n.type) { var o = n.arg; resetTryEntry(r); } return o; } } throw Error("illegal catch attempt"); }, delegateYield: function delegateYield(e, r, n) { return this.delegate = { iterator: values(e), resultName: r, nextLoc: n }, "next" === this.method && (this.arg = t), y; } }, e; }
10
+ function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; }
11
+ function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
12
+ function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
13
+ function asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.value; } catch (n) { return void e(n); } i.done ? t(u) : Promise.resolve(u).then(r, o); }
14
+ function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; }
15
+ var applyDifferences = exports.applyDifferences = /*#__PURE__*/function () {
16
+ var _ref = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(connection, database, differences) {
17
+ var _iterator, _step, _loop, _ret;
18
+ return _regeneratorRuntime().wrap(function _callee$(_context2) {
19
+ while (1) switch (_context2.prev = _context2.next) {
20
+ case 0:
21
+ differences.sort(function (a, b) {
22
+ //Sort to make sure we apply the differences in a logical order.
23
+ var typePriority = {
24
+ 'missing_database': 1,
25
+ 'missing_table': 2,
26
+ 'extra_table': 2,
27
+ 'missing_field': 3,
28
+ 'extra_field': 3,
29
+ 'mismatched_field': 3,
30
+ 'missing_index': 4,
31
+ 'extra_index': 4,
32
+ 'mismatched_index': 4,
33
+ 'missing_procedure': 5,
34
+ 'mismatched_procedure': 5,
35
+ 'missing_view': 6,
36
+ 'mismatched_view': 6,
37
+ 'extra_view': 6,
38
+ 'missing_trigger': 7,
39
+ 'mismatched_trigger': 7,
40
+ 'extra_trigger': 7
41
+ };
42
+ return (typePriority[a.type] || 999) - (typePriority[b.type] || 999);
43
+ });
44
+ _iterator = _createForOfIteratorHelper(differences);
45
+ _context2.prev = 2;
46
+ _loop = /*#__PURE__*/_regeneratorRuntime().mark(function _loop() {
47
+ var diff, createDatabaseSQL, tableName, createSQL, _tableName, dropTableSQL, addFieldSQL, modifyFieldSQL, defaultClause, _modifyFieldSQL, dropFieldSQL, _tableName2, field, _defaultClause, _dropFieldSQL, existingColumns, columnNames, missingColumns, indexColumns, primaryKeySQL, uniqueSQL, addIndexSQL, dropPrimaryKeySQL, dropIndexSQL, _tableName3, index, _dropPrimaryKeySQL, _dropIndexSQL, _uniqueSQL, addPrimaryKeySQL, _addIndexSQL, dropCurrentProcSQL, createProcSQL, dropViewSQL, createViewSQL, _dropViewSQL, _createViewSQL, _dropViewSQL2, createTriggerSQL, dropTriggerSQL, _createTriggerSQL, _dropTriggerSQL, _createDatabaseSQL;
48
+ return _regeneratorRuntime().wrap(function _loop$(_context) {
49
+ while (1) switch (_context.prev = _context.next) {
50
+ case 0:
51
+ diff = _step.value;
52
+ _context.prev = 1;
53
+ if (!(diff.type == 'missing_database')) {
54
+ _context.next = 8;
55
+ break;
56
+ }
57
+ createDatabaseSQL = "CREATE DATABASE `".concat(differences[0].database, "`;");
58
+ (0, _loggers.logger)("Executing: ".concat(createDatabaseSQL));
59
+ _context.next = 7;
60
+ return connection.query(createDatabaseSQL);
61
+ case 7:
62
+ return _context.abrupt("return", 0);
63
+ case 8:
64
+ _context.next = 10;
65
+ return connection.query("USE `".concat(database, "`"));
66
+ case 10:
67
+ if (!(diff.type === 'missing_table')) {
68
+ _context.next = 18;
69
+ break;
70
+ }
71
+ tableName = diff.tableName, createSQL = diff.createSQL;
72
+ (0, _loggers.logger)("Creating missing table ".concat(tableName));
73
+ (0, _loggers.logger)("Executing: ".concat(createSQL));
74
+ _context.next = 16;
75
+ return connection.query(createSQL);
76
+ case 16:
77
+ _context.next = 210;
78
+ break;
79
+ case 18:
80
+ if (!(diff.type === 'extra_table')) {
81
+ _context.next = 27;
82
+ break;
83
+ }
84
+ _tableName = diff.tableName;
85
+ (0, _loggers.logger)("Dropping extra table ".concat(_tableName));
86
+ dropTableSQL = "DROP TABLE `".concat(_tableName, "`;");
87
+ (0, _loggers.logger)("Executing: ".concat(dropTableSQL));
88
+ _context.next = 25;
89
+ return connection.query(dropTableSQL);
90
+ case 25:
91
+ _context.next = 210;
92
+ break;
93
+ case 27:
94
+ if (!(diff.type === 'missing_field')) {
95
+ _context.next = 45;
96
+ break;
97
+ }
98
+ // Step 1: Add column with DEFAULT NULL to handle existing records
99
+ addFieldSQL = "ALTER TABLE `".concat(diff.tableName, "` ADD `").concat(diff.field.Field, "` ").concat(diff.field.Type, " DEFAULT NULL;");
100
+ console.log("Executing: ".concat(addFieldSQL));
101
+ _context.next = 32;
102
+ return connection.query(addFieldSQL);
103
+ case 32:
104
+ if (!(diff.field.NotNull && !diff.field.Default)) {
105
+ _context.next = 37;
106
+ break;
107
+ }
108
+ modifyFieldSQL = "ALTER TABLE `".concat(diff.tableName, "` MODIFY `").concat(diff.field.Field, "` ").concat(diff.field.Type, " NOT NULL;");
109
+ console.log("Executing: ".concat(modifyFieldSQL));
110
+ _context.next = 37;
111
+ return connection.query(modifyFieldSQL);
112
+ case 37:
113
+ if (!diff.field.Default) {
114
+ _context.next = 43;
115
+ break;
116
+ }
117
+ defaultClause = diff.field.Default === 'CURRENT_TIMESTAMP' ? 'DEFAULT CURRENT_TIMESTAMP' : diff.field.Default !== undefined && diff.field.Default !== null ? "DEFAULT '".concat(diff.field.Default, "'") : '';
118
+ _modifyFieldSQL = "ALTER TABLE `".concat(diff.tableName, "` MODIFY `").concat(diff.field.Field, "` ").concat(diff.field.Type, " ").concat(defaultClause, ";");
119
+ console.log("Executing: ".concat(_modifyFieldSQL));
120
+ _context.next = 43;
121
+ return connection.query(_modifyFieldSQL);
122
+ case 43:
123
+ _context.next = 210;
124
+ break;
125
+ case 45:
126
+ if (!(diff.type === 'extra_field')) {
127
+ _context.next = 52;
128
+ break;
129
+ }
130
+ dropFieldSQL = "ALTER TABLE `".concat(diff.tableName, "` DROP COLUMN `").concat(diff.field.Field, "`;");
131
+ (0, _loggers.logger)("Executing: ".concat(dropFieldSQL));
132
+ _context.next = 50;
133
+ return connection.query(dropFieldSQL);
134
+ case 50:
135
+ _context.next = 210;
136
+ break;
137
+ case 52:
138
+ if (!(diff.type === 'mismatched_field')) {
139
+ _context.next = 61;
140
+ break;
141
+ }
142
+ _tableName2 = diff.tableName, field = diff.field;
143
+ _defaultClause = field.Default === 'CURRENT_TIMESTAMP' ? 'DEFAULT CURRENT_TIMESTAMP' : field.Default !== undefined && field.Default !== null ? "DEFAULT '".concat(field.Default, "'") : '';
144
+ _dropFieldSQL = "ALTER TABLE `".concat(_tableName2, "` CHANGE `").concat(field.Field, "` `").concat(field.Field, "` ").concat(field.Type, " ").concat(field.NotNull ? 'NOT NULL' : '', " ").concat(_defaultClause, ";");
145
+ (0, _loggers.logger)("Executing: ".concat(_dropFieldSQL));
146
+ _context.next = 59;
147
+ return connection.query(_dropFieldSQL);
148
+ case 59:
149
+ _context.next = 210;
150
+ break;
151
+ case 61:
152
+ if (!(diff.type === 'missing_index')) {
153
+ _context.next = 93;
154
+ break;
155
+ }
156
+ _context.next = 64;
157
+ return connection.query("SHOW COLUMNS FROM `".concat(diff.tableName, "`"));
158
+ case 64:
159
+ existingColumns = _context.sent;
160
+ columnNames = existingColumns.map(function (col) {
161
+ return col.Field;
162
+ }); // Check if all columns for the index exist
163
+ missingColumns = Array.isArray(diff.index.ColumnName) ? diff.index.ColumnName.filter(function (col) {
164
+ return !columnNames.includes(col);
165
+ }) : [];
166
+ if (!(missingColumns.length > 0)) {
167
+ _context.next = 70;
168
+ break;
169
+ }
170
+ (0, _loggers.logger)("Cannot create index ".concat(diff.index.Name, " on ").concat(diff.tableName, ". Missing columns: ").concat(missingColumns.join(', ')));
171
+ return _context.abrupt("return", 0);
172
+ case 70:
173
+ // Prepare the index columns with proper formatting
174
+ indexColumns = diff.index.ColumnName.map(function (col) {
175
+ return "`".concat(col, "`");
176
+ }); // Format column names
177
+ // Handle PRIMARY index creation
178
+ if (!(diff.index.Name === 'PRIMARY')) {
179
+ _context.next = 82;
180
+ break;
181
+ }
182
+ if (!(indexColumns.length > 0)) {
183
+ _context.next = 79;
184
+ break;
185
+ }
186
+ primaryKeySQL = "ALTER TABLE `".concat(diff.tableName, "` ADD PRIMARY KEY (").concat(indexColumns.join(', '), ");");
187
+ (0, _loggers.logger)("Executing: ".concat(primaryKeySQL));
188
+ _context.next = 77;
189
+ return connection.query(primaryKeySQL);
190
+ case 77:
191
+ _context.next = 80;
192
+ break;
193
+ case 79:
194
+ (0, _loggers.logger)("Cannot create PRIMARY index on ".concat(diff.tableName, ". ColumnName is undefined or empty."), diff.index);
195
+ case 80:
196
+ _context.next = 91;
197
+ break;
198
+ case 82:
199
+ // Create the new index with UNIQUE if applicable
200
+ uniqueSQL = diff.index.Unique ? 'UNIQUE ' : ''; // Check if the index is unique
201
+ if (!(indexColumns.length > 0)) {
202
+ _context.next = 90;
203
+ break;
204
+ }
205
+ addIndexSQL = "ALTER TABLE `".concat(diff.tableName, "` ADD ").concat(uniqueSQL, "INDEX `").concat(diff.index.Name, "` (").concat(indexColumns.join(', '), ");");
206
+ (0, _loggers.logger)("Executing: ".concat(addIndexSQL));
207
+ _context.next = 88;
208
+ return connection.query(addIndexSQL);
209
+ case 88:
210
+ _context.next = 91;
211
+ break;
212
+ case 90:
213
+ (0, _loggers.logger)("Cannot create index ".concat(diff.index.Name, " on ").concat(diff.tableName, ". ColumnName is undefined or empty."), diff.index);
214
+ case 91:
215
+ _context.next = 210;
216
+ break;
217
+ case 93:
218
+ if (!(diff.type === 'extra_index')) {
219
+ _context.next = 107;
220
+ break;
221
+ }
222
+ if (!(diff.index.Name === 'PRIMARY')) {
223
+ _context.next = 101;
224
+ break;
225
+ }
226
+ dropPrimaryKeySQL = "ALTER TABLE `".concat(diff.tableName, "` DROP PRIMARY KEY;");
227
+ (0, _loggers.logger)("Executing: ".concat(dropPrimaryKeySQL));
228
+ _context.next = 99;
229
+ return connection.query(dropPrimaryKeySQL);
230
+ case 99:
231
+ _context.next = 105;
232
+ break;
233
+ case 101:
234
+ dropIndexSQL = "DROP INDEX `".concat(diff.index.Name, "` ON `").concat(diff.tableName, "`;");
235
+ (0, _loggers.logger)("Executing: ".concat(dropIndexSQL));
236
+ _context.next = 105;
237
+ return connection.query(dropIndexSQL);
238
+ case 105:
239
+ _context.next = 210;
240
+ break;
241
+ case 107:
242
+ if (!(diff.type === 'mismatched_index')) {
243
+ _context.next = 135;
244
+ break;
245
+ }
246
+ _tableName3 = diff.tableName, index = diff.index; // Drop the existing index
247
+ if (!(index.Name === 'PRIMARY')) {
248
+ _context.next = 116;
249
+ break;
250
+ }
251
+ _dropPrimaryKeySQL = "ALTER TABLE `".concat(_tableName3, "` DROP PRIMARY KEY;");
252
+ (0, _loggers.logger)("Executing: ".concat(_dropPrimaryKeySQL));
253
+ _context.next = 114;
254
+ return connection.query(_dropPrimaryKeySQL);
255
+ case 114:
256
+ _context.next = 120;
257
+ break;
258
+ case 116:
259
+ _dropIndexSQL = "DROP INDEX `".concat(index.Name, "` ON `").concat(_tableName3, "`;");
260
+ (0, _loggers.logger)("Executing: ".concat(_dropIndexSQL));
261
+ _context.next = 120;
262
+ return connection.query(_dropIndexSQL);
263
+ case 120:
264
+ console.log('index', index);
265
+
266
+ // Create the new index
267
+ _uniqueSQL = index.Unique ? 'UNIQUE ' : ''; // Check if the index is unique
268
+ if (!(index.Name === 'PRIMARY')) {
269
+ _context.next = 129;
270
+ break;
271
+ }
272
+ addPrimaryKeySQL = "ALTER TABLE `".concat(_tableName3, "` ADD PRIMARY KEY (").concat(index.ColumnName.join(', '), ");");
273
+ (0, _loggers.logger)("Executing: ".concat(addPrimaryKeySQL));
274
+ _context.next = 127;
275
+ return connection.query(addPrimaryKeySQL);
276
+ case 127:
277
+ _context.next = 133;
278
+ break;
279
+ case 129:
280
+ _addIndexSQL = "ALTER TABLE `".concat(_tableName3, "` ADD ").concat(_uniqueSQL, "INDEX `").concat(index.Name, "` (").concat(index.ColumnName.join(', '), ");"); // Include UNIQUE if applicable
281
+ (0, _loggers.logger)("Executing: ".concat(_addIndexSQL));
282
+ _context.next = 133;
283
+ return connection.query(_addIndexSQL);
284
+ case 133:
285
+ _context.next = 210;
286
+ break;
287
+ case 135:
288
+ if (!(diff.type === 'missing_procedure' || diff.type === 'mismatched_procedure')) {
289
+ _context.next = 146;
290
+ break;
291
+ }
292
+ // Drop the procedure if it exists
293
+ dropCurrentProcSQL = "DROP PROCEDURE IF EXISTS `".concat(diff.Name, "`;");
294
+ (0, _loggers.logger)("Executing: ".concat(dropCurrentProcSQL));
295
+ _context.next = 140;
296
+ return connection.query(dropCurrentProcSQL);
297
+ case 140:
298
+ // Create the procedure
299
+ createProcSQL = diff.Definition;
300
+ (0, _loggers.logger)("Executing: ".concat(createProcSQL)); // Log the SQL statement
301
+ _context.next = 144;
302
+ return connection.query(createProcSQL);
303
+ case 144:
304
+ _context.next = 210;
305
+ break;
306
+ case 146:
307
+ if (!(diff.type === 'missing_view')) {
308
+ _context.next = 157;
309
+ break;
310
+ }
311
+ dropViewSQL = "DROP VIEW IF EXISTS `".concat(diff.viewName, "`;");
312
+ (0, _loggers.logger)("Executing: ".concat(dropViewSQL));
313
+ _context.next = 151;
314
+ return connection.query(dropViewSQL);
315
+ case 151:
316
+ createViewSQL = diff.definition; // Use the expected definition to create the view
317
+ (0, _loggers.logger)("Executing: ".concat(createViewSQL));
318
+ _context.next = 155;
319
+ return connection.query(createViewSQL);
320
+ case 155:
321
+ _context.next = 210;
322
+ break;
323
+ case 157:
324
+ if (!(diff.type === 'mismatched_view')) {
325
+ _context.next = 168;
326
+ break;
327
+ }
328
+ _dropViewSQL = "DROP VIEW IF EXISTS `".concat(diff.viewName, "`;");
329
+ (0, _loggers.logger)("Executing: ".concat(_dropViewSQL));
330
+ _context.next = 162;
331
+ return connection.query(_dropViewSQL);
332
+ case 162:
333
+ _createViewSQL = diff.definition; // Use the expected definition to create the view
334
+ (0, _loggers.logger)("Executing: ".concat(_createViewSQL));
335
+ _context.next = 166;
336
+ return connection.query(_createViewSQL);
337
+ case 166:
338
+ _context.next = 210;
339
+ break;
340
+ case 168:
341
+ if (!(diff.type === 'extra_view')) {
342
+ _context.next = 175;
343
+ break;
344
+ }
345
+ _dropViewSQL2 = "DROP VIEW IF EXISTS `".concat(diff.viewName, "`;");
346
+ (0, _loggers.logger)("Executing: ".concat(_dropViewSQL2));
347
+ _context.next = 173;
348
+ return connection.query(_dropViewSQL2);
349
+ case 173:
350
+ _context.next = 210;
351
+ break;
352
+ case 175:
353
+ if (!(diff.type === 'missing_trigger')) {
354
+ _context.next = 183;
355
+ break;
356
+ }
357
+ createTriggerSQL = diff.trigger.Definition;
358
+ console.log('fixing trigger', diff.trigger.Name);
359
+ console.log("Executing: ".concat(createTriggerSQL));
360
+ _context.next = 181;
361
+ return connection.query(createTriggerSQL);
362
+ case 181:
363
+ _context.next = 210;
364
+ break;
365
+ case 183:
366
+ if (!(diff.type === 'mismatched_trigger')) {
367
+ _context.next = 194;
368
+ break;
369
+ }
370
+ dropTriggerSQL = "DROP TRIGGER IF EXISTS `".concat(diff.trigger.Name, "`;");
371
+ (0, _loggers.logger)("Executing: ".concat(dropTriggerSQL));
372
+ _context.next = 188;
373
+ return connection.query(dropTriggerSQL);
374
+ case 188:
375
+ _createTriggerSQL = diff.trigger.Definition;
376
+ console.log("Executing: ".concat(_createTriggerSQL));
377
+ _context.next = 192;
378
+ return connection.query(_createTriggerSQL);
379
+ case 192:
380
+ _context.next = 210;
381
+ break;
382
+ case 194:
383
+ if (!(diff.type === 'extra_trigger')) {
384
+ _context.next = 201;
385
+ break;
386
+ }
387
+ _dropTriggerSQL = "DROP TRIGGER IF EXISTS `".concat(diff.trigger.Name, "`;");
388
+ (0, _loggers.logger)("Executing: ".concat(_dropTriggerSQL));
389
+ _context.next = 199;
390
+ return connection.query(_dropTriggerSQL);
391
+ case 199:
392
+ _context.next = 210;
393
+ break;
394
+ case 201:
395
+ if (!(diff.type === 'missing_database')) {
396
+ _context.next = 209;
397
+ break;
398
+ }
399
+ console.log('fixing missing database', diff.database);
400
+ _createDatabaseSQL = "CREATE DATABASE `".concat(diff.database, "`;");
401
+ (0, _loggers.logger)("Executing: ".concat(_createDatabaseSQL));
402
+ _context.next = 207;
403
+ return connection.query(_createDatabaseSQL);
404
+ case 207:
405
+ _context.next = 210;
406
+ break;
407
+ case 209:
408
+ (0, _loggers.logger)("Error applying difference of type:".concat(diff.type), diff);
409
+ case 210:
410
+ _context.next = 215;
411
+ break;
412
+ case 212:
413
+ _context.prev = 212;
414
+ _context.t0 = _context["catch"](1);
415
+ (0, _loggers.logger)("Error applying difference of type:".concat(diff.type, ". Error: ").concat(_context.t0.message), diff);
416
+ case 215:
417
+ case "end":
418
+ return _context.stop();
419
+ }
420
+ }, _loop, null, [[1, 212]]);
421
+ });
422
+ _iterator.s();
423
+ case 5:
424
+ if ((_step = _iterator.n()).done) {
425
+ _context2.next = 12;
426
+ break;
427
+ }
428
+ return _context2.delegateYield(_loop(), "t0", 7);
429
+ case 7:
430
+ _ret = _context2.t0;
431
+ if (!(_ret === 0)) {
432
+ _context2.next = 10;
433
+ break;
434
+ }
435
+ return _context2.abrupt("continue", 10);
436
+ case 10:
437
+ _context2.next = 5;
438
+ break;
439
+ case 12:
440
+ _context2.next = 17;
441
+ break;
442
+ case 14:
443
+ _context2.prev = 14;
444
+ _context2.t1 = _context2["catch"](2);
445
+ _iterator.e(_context2.t1);
446
+ case 17:
447
+ _context2.prev = 17;
448
+ _iterator.f();
449
+ return _context2.finish(17);
450
+ case 20:
451
+ case "end":
452
+ return _context2.stop();
453
+ }
454
+ }, _callee, null, [[2, 14, 17, 20]]);
455
+ }));
456
+ return function applyDifferences(_x, _x2, _x3) {
457
+ return _ref.apply(this, arguments);
458
+ };
459
+ }();
package/dist/cli.js ADDED
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.cli = void 0;
7
+ var _yargs = _interopRequireDefault(require("yargs"));
8
+ var _helpers = require("yargs/helpers");
9
+ var _loggers = require("./loggers.js");
10
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
11
+ var cli = exports.cli = function cli() {
12
+ var argv = (0, _yargs["default"])((0, _helpers.hideBin)(process.argv)).command('dump', 'Dump the database structure to files').command('compare', 'Compare the database structure with JSON dumps (dry run)').command('update', 'Update the database structure based on JSON dumps').command('ui', 'Open the UI').option('uiPort', {
13
+ alias: 'U',
14
+ describe: 'UI port',
15
+ "default": 3600
16
+ }).option('openUI', {
17
+ alias: 'O',
18
+ describe: 'Open UI',
19
+ "default": true
20
+ }).option('host', {
21
+ alias: 'h',
22
+ describe: 'Database host',
23
+ "default": 'localhost'
24
+ }).option('port', {
25
+ alias: 'P',
26
+ describe: 'Database port',
27
+ "default": 3306
28
+ }).option('user', {
29
+ alias: 'u',
30
+ describe: 'Database user',
31
+ "default": 'root'
32
+ }).option('password', {
33
+ alias: 'p',
34
+ describe: 'Database password',
35
+ "default": ''
36
+ }).option('database', {
37
+ alias: 'd',
38
+ describe: 'Target database',
39
+ "default": null
40
+ }).option('output', {
41
+ alias: 'o',
42
+ describe: 'Output directory for dumping. default is one level up from the current directory in a new folder called db-dump',
43
+ "default": '../db-dump'
44
+ }).option('verbose', {
45
+ alias: 'v',
46
+ describe: 'Enable verbose logging',
47
+ type: 'boolean',
48
+ "default": false
49
+ })
50
+ // .demandCommand(1)
51
+ .help().argv;
52
+ var command = argv._[0];
53
+ var config = {
54
+ host: argv.host,
55
+ port: argv.port,
56
+ user: argv.user,
57
+ password: argv.password
58
+ };
59
+ (0, _loggers.setVerbose)(argv.verbose);
60
+ return {
61
+ command: command,
62
+ config: config,
63
+ argv: argv
64
+ };
65
+ };