mysqlsyncr 1.1.8 → 1.1.12

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/dist/index.js CHANGED
@@ -134,10 +134,7 @@ function _iterableToArrayLimit(r, l) {
134
134
  f = true,
135
135
  o = false;
136
136
  try {
137
- if (i = (t = t.call(r)).next, 0 === l) {
138
- if (Object(t) !== t) return;
139
- f = !1;
140
- } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0);
137
+ if (i = (t = t.call(r)).next, 0 === l) ; else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0);
141
138
  } catch (r) {
142
139
  o = true, n = r;
143
140
  } finally {
@@ -2560,7 +2557,11 @@ var parseCreateTableSQL = function parseCreateTableSQL(createTableSQL) {
2560
2557
  var columnRegex = /`([^`]+)`\s+([^\s,]+)(\s+NOT NULL|\s+NULL)?(\s+DEFAULT\s+([^,\s]+))?(\s+AUTO_INCREMENT)?/;
2561
2558
 
2562
2559
  // Regular expression to match index definitions
2563
- var indexRegex = /(PRIMARY|UNIQUE)?\s*KEY\s*`([^`]+)`\s*\(([^)]+)\)|PRIMARY KEY\s*\(([^)]+)\)/;
2560
+ var indexRegex_Old = /(PRIMARY|UNIQUE)?\s*KEY\s*`([^`]+)`\s*\(([^)]+)\)|PRIMARY KEY\s*\(([^)]+)\)/;
2561
+
2562
+ // Updated to support indexes for text fields
2563
+ // 'KEY `idx_e2alarms_alarmstring` (`alarmString`(255))' -- would cut off the final bracket and wouldnt work.
2564
+ var indexRegex = /(PRIMARY|UNIQUE)?\s*KEY\s*`([^`]+)`\s*\(((?:`[^`]+`(?:\(\d+\))?(?:\s*,\s*)?)+)\)|PRIMARY KEY\s*\(((?:`[^`]+`(?:\(\d+\))?(?:\s*,\s*)?)+)\)/;
2564
2565
 
2565
2566
  // Regular expression to match table options (ENGINE, CHARSET, COLLATE)
2566
2567
  var optionsRegex = /ENGINE=(\w+)\s+DEFAULT\s+CHARSET=(\w+)(\s+COLLATE=(\w+))?/;
@@ -2577,6 +2578,7 @@ var parseCreateTableSQL = function parseCreateTableSQL(createTableSQL) {
2577
2578
  // Check if the line contains a column definition
2578
2579
  var columnMatch = columnRegex.exec(line);
2579
2580
  var indexMatch = indexRegex.exec(line);
2581
+ var indexMatch_Old = indexRegex_Old.exec(line);
2580
2582
  var constraintMatch = parseConstraint(line);
2581
2583
  if (columnMatch && !indexMatch && !constraintMatch) {
2582
2584
  var columnType = parseColumnType(line);
@@ -3599,7 +3601,7 @@ var applyDifferences = /*#__PURE__*/function () {
3599
3601
  _iterator = _createForOfIteratorHelper(differences);
3600
3602
  _context2.prev = 2;
3601
3603
  _loop = /*#__PURE__*/_regeneratorRuntime().mark(function _loop() {
3602
- var diff, createDatabaseSQL, tableName, createSQL, _tableName, dropTableSQL, addFieldSQL, modifyFieldSQL, defaultClause, _modifyFieldSQL, dropFieldSQL, _tableName2, field, currentField, needsBatchProcessing, addColumnSQL, totalUpdated, batchSize, batchCount, updateSQL, result, rowsAffected, verifySQL, _yield$connection$que, _yield$connection$que2, verifyResult, swapColumnsSQL, isTimestamp, _defaultClause, _currentField, hasOnUpdate, _modifyFieldSQL2, 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;
3604
+ var diff, createDatabaseSQL, tableName, createSQL, _tableName, dropTableSQL, addFieldSQL, modifyFieldSQL, defaultClause, _modifyFieldSQL, dropFieldSQL, _tableName2, field, currentField, isTimestamp, _defaultClause, hasOnUpdate, _modifyFieldSQL2, 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;
3603
3605
  return _regeneratorRuntime().wrap(function _loop$(_context) {
3604
3606
  while (1) switch (_context.prev = _context.next) {
3605
3607
  case 0:
@@ -3629,7 +3631,7 @@ var applyDifferences = /*#__PURE__*/function () {
3629
3631
  _context.next = 16;
3630
3632
  return connection.query(createSQL);
3631
3633
  case 16:
3632
- _context.next = 249;
3634
+ _context.next = 212;
3633
3635
  break;
3634
3636
  case 18:
3635
3637
  if (!(diff.type === 'extra_table')) {
@@ -3643,7 +3645,7 @@ var applyDifferences = /*#__PURE__*/function () {
3643
3645
  _context.next = 25;
3644
3646
  return connection.query(dropTableSQL);
3645
3647
  case 25:
3646
- _context.next = 249;
3648
+ _context.next = 212;
3647
3649
  break;
3648
3650
  case 27:
3649
3651
  if (!(diff.type === 'missing_field')) {
@@ -3675,7 +3677,7 @@ var applyDifferences = /*#__PURE__*/function () {
3675
3677
  _context.next = 43;
3676
3678
  return connection.query(_modifyFieldSQL);
3677
3679
  case 43:
3678
- _context.next = 249;
3680
+ _context.next = 212;
3679
3681
  break;
3680
3682
  case 45:
3681
3683
  if (!(diff.type === 'extra_field')) {
@@ -3687,394 +3689,385 @@ var applyDifferences = /*#__PURE__*/function () {
3687
3689
  _context.next = 50;
3688
3690
  return connection.query(dropFieldSQL);
3689
3691
  case 50:
3690
- _context.next = 249;
3692
+ _context.next = 212;
3691
3693
  break;
3692
3694
  case 52:
3693
3695
  if (!(diff.type === 'mismatched_field')) {
3694
- _context.next = 100;
3696
+ _context.next = 63;
3695
3697
  break;
3696
3698
  }
3697
- _tableName2 = diff.tableName, field = diff.field, currentField = diff.currentField; // Helper function to determine if we need batch processing
3698
- needsBatchProcessing = function needsBatchProcessing() {
3699
- var currentType = currentField.Type.toLowerCase();
3700
- var newType = field.Type.toLowerCase();
3701
-
3702
- // Decimal changes
3703
- if (currentType.includes('decimal') && newType.includes('decimal')) {
3704
- return true;
3705
- }
3706
-
3707
- // VARCHAR/CHAR length reductions
3708
- if ((currentType.includes('varchar') || currentType.includes('char')) && (newType.includes('varchar') || newType.includes('char'))) {
3709
- var _currentType$match, _newType$match;
3710
- var currentLength = parseInt(((_currentType$match = currentType.match(/\((\d+)\)/)) === null || _currentType$match === undefined ? undefined : _currentType$match[1]) || '0');
3711
- var newLength = parseInt(((_newType$match = newType.match(/\((\d+)\)/)) === null || _newType$match === undefined ? undefined : _newType$match[1]) || '0');
3712
- return newLength < currentLength;
3713
- }
3714
-
3715
- // TEXT to VARCHAR conversions
3716
- if (currentType.includes('text') && newType.includes('varchar')) {
3717
- return true;
3718
- }
3719
-
3720
- // Numeric type downgrades
3721
- var numericDowngrades = {
3722
- 'bigint': ['int', 'mediumint', 'smallint', 'tinyint'],
3723
- 'int': ['mediumint', 'smallint', 'tinyint'],
3724
- 'mediumint': ['smallint', 'tinyint'],
3725
- 'smallint': ['tinyint'],
3726
- 'double': ['float', 'decimal'],
3727
- 'float': ['decimal']
3728
- };
3729
- for (var _i = 0, _Object$entries = Object.entries(numericDowngrades); _i < _Object$entries.length; _i++) {
3730
- var _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2),
3731
- larger = _Object$entries$_i[0],
3732
- smaller = _Object$entries$_i[1];
3733
- if (currentType.includes(larger) && smaller.some(function (t) {
3734
- return newType.includes(t);
3735
- })) {
3736
- return true;
3737
- }
3738
- }
3739
- return false;
3740
- };
3741
- if (!needsBatchProcessing()) {
3742
- _context.next = 90;
3743
- break;
3744
- }
3745
- // Step 1: Add new column
3746
- addColumnSQL = "ALTER TABLE `".concat(_tableName2, "` ADD COLUMN `").concat(field.Field, "_new` ").concat(field.Type, " DEFAULT NULL;");
3747
- console.log("Adding new column for type conversion: ".concat(addColumnSQL));
3748
- _context.next = 60;
3749
- return connection.query(addColumnSQL);
3750
- case 60:
3751
- // Step 2: Copy data in batches
3752
- totalUpdated = 0;
3753
- batchSize = 10000;
3754
- batchCount = 0;
3755
- case 63:
3756
- updateSQL = "\n UPDATE `".concat(_tableName2, "` \n SET `").concat(field.Field, "_new` = `").concat(field.Field, "`\n WHERE `").concat(field.Field, "_new` IS NULL \n AND `").concat(field.Field, "` IS NOT NULL\n LIMIT ").concat(batchSize, ";\n ");
3757
- _context.next = 67;
3758
- return connection.query(updateSQL);
3759
- case 67:
3760
- result = _context.sent;
3761
- rowsAffected = result.affectedRows;
3762
- if (!(rowsAffected === 0)) {
3763
- _context.next = 71;
3764
- break;
3765
- }
3766
- return _context.abrupt("break", 76);
3767
- case 71:
3768
- totalUpdated += rowsAffected;
3769
- batchCount++;
3770
- console.log("Batch ".concat(batchCount, ": Converted ").concat(rowsAffected, " rows. Total converted: ").concat(totalUpdated));
3771
- _context.next = 63;
3772
- break;
3773
- case 76:
3774
- // Step 3: Verify data (optional)
3775
- verifySQL = "\n SELECT COUNT(*) as mismatch FROM `".concat(_tableName2, "`\n WHERE `").concat(field.Field, "` IS NOT NULL \n AND `").concat(field.Field, "_new` IS NULL;\n ");
3776
- _context.next = 79;
3777
- return connection.query(verifySQL);
3778
- case 79:
3779
- _yield$connection$que = _context.sent;
3780
- _yield$connection$que2 = _slicedToArray(_yield$connection$que, 1);
3781
- verifyResult = _yield$connection$que2[0];
3782
- if (verifyResult.mismatch > 0) {
3783
- console.log("Warning: ".concat(verifyResult.mismatch, " rows may have failed conversion"));
3784
- }
3785
-
3786
- // Step 4: Swap columns
3787
- swapColumnsSQL = "\n ALTER TABLE `".concat(_tableName2, "`\n DROP COLUMN `").concat(field.Field, "`,\n RENAME COLUMN `").concat(field.Field, "_new` TO `").concat(field.Field, "`\n ");
3788
- console.log("Executing: ".concat(swapColumnsSQL));
3789
- _context.next = 87;
3790
- return connection.query(swapColumnsSQL);
3791
- case 87:
3792
- console.log("Completed type conversion for ".concat(field.Field, ". Total rows converted: ").concat(totalUpdated));
3793
- _context.next = 98;
3794
- break;
3795
- case 90:
3699
+ _tableName2 = diff.tableName, field = diff.field, currentField = diff.currentField; // use alter table rather than adding fields
3700
+ // // Helper function to determine if we need batch processing
3701
+ // const needsBatchProcessing = () => {
3702
+ // const currentType = currentField.Type.toLowerCase();
3703
+ // const newType = field.Type.toLowerCase();
3704
+ // // Decimal changes
3705
+ // if (currentType.includes('decimal') && newType.includes('decimal')) {
3706
+ // return true;
3707
+ // }
3708
+ // // VARCHAR/CHAR length reductions
3709
+ // if ((currentType.includes('varchar') || currentType.includes('char')) &&
3710
+ // (newType.includes('varchar') || newType.includes('char'))) {
3711
+ // const currentLength = parseInt(currentType.match(/\((\d+)\)/)?.[1] || '0');
3712
+ // const newLength = parseInt(newType.match(/\((\d+)\)/)?.[1] || '0');
3713
+ // return newLength < currentLength;
3714
+ // }
3715
+ // // TEXT to VARCHAR conversions
3716
+ // if (currentType.includes('text') && newType.includes('varchar')) {
3717
+ // return true;
3718
+ // }
3719
+ // // Numeric type downgrades
3720
+ // const numericDowngrades = {
3721
+ // 'bigint': ['int', 'mediumint', 'smallint', 'tinyint'],
3722
+ // 'int': ['mediumint', 'smallint', 'tinyint'],
3723
+ // 'mediumint': ['smallint', 'tinyint'],
3724
+ // 'smallint': ['tinyint'],
3725
+ // 'double': ['float', 'decimal'],
3726
+ // 'float': ['decimal']
3727
+ // };
3728
+ // for (const [larger, smaller] of Object.entries(numericDowngrades)) {
3729
+ // if (currentType.includes(larger) && smaller.some(t => newType.includes(t))) {
3730
+ // return true;
3731
+ // }
3732
+ // }
3733
+ // return false;
3734
+ // };
3735
+ // if (needsBatchProcessing()) {
3736
+ // // Step 1: Add new column
3737
+ // const addColumnSQL = `ALTER TABLE \`${tableName}\` ADD COLUMN \`${field.Field}_new\` ${field.Type} DEFAULT NULL;`;
3738
+ // console.log(`Adding new column for type conversion: ${addColumnSQL}`);
3739
+ // await connection.query(addColumnSQL);
3740
+ // // Step 2: Copy data in batches
3741
+ // let totalUpdated = 0;
3742
+ // let batchSize = 10000;
3743
+ // let batchCount = 0;
3744
+ // while (true) {
3745
+ // const updateSQL = `
3746
+ // UPDATE \`${tableName}\`
3747
+ // SET \`${field.Field}_new\` = \`${field.Field}\`
3748
+ // WHERE \`${field.Field}_new\` IS NULL
3749
+ // AND \`${field.Field}\` IS NOT NULL
3750
+ // LIMIT ${batchSize};
3751
+ // `;
3752
+ // const result = await connection.query(updateSQL);
3753
+ // const rowsAffected = result.affectedRows;
3754
+ // if (rowsAffected === 0) break;
3755
+ // totalUpdated += rowsAffected;
3756
+ // batchCount++;
3757
+ // console.log(`Batch ${batchCount}: Converted ${rowsAffected} rows. Total converted: ${totalUpdated}`);
3758
+ // }
3759
+ // // Step 3: Verify data (optional)
3760
+ // const verifySQL = `
3761
+ // SELECT COUNT(*) as mismatch FROM \`${tableName}\`
3762
+ // WHERE \`${field.Field}\` IS NOT NULL
3763
+ // AND \`${field.Field}_new\` IS NULL;
3764
+ // `;
3765
+ // const [verifyResult] = await connection.query(verifySQL);
3766
+ // if (verifyResult.mismatch > 0) {
3767
+ // console.log(`Warning: ${verifyResult.mismatch} rows may have failed conversion`);
3768
+ // }
3769
+ // // Step 4: Swap columns
3770
+ // const swapColumnsSQL = `
3771
+ // ALTER TABLE \`${tableName}\`
3772
+ // DROP COLUMN \`${field.Field}\`,
3773
+ // RENAME COLUMN \`${field.Field}_new\` TO \`${field.Field}\`
3774
+ // `;
3775
+ // console.log(`Executing: ${swapColumnsSQL}`);
3776
+ // await connection.query(swapColumnsSQL);
3777
+ // console.log(`Completed type conversion for ${field.Field}. Total rows converted: ${totalUpdated}`);
3778
+ // } else {
3796
3779
  // Original mismatched field handling for simple changes
3797
3780
  isTimestamp = field.Type.toLowerCase().includes('timestamp'); // Handle timestamp specific attributes
3798
3781
  _defaultClause = field.Default === 'CURRENT_TIMESTAMP' ? 'DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP' : field.Default === 'NULL' ? isTimestamp ? 'NULL DEFAULT NULL' : 'DEFAULT NULL' : field.Default !== undefined && field.Default !== null ? "DEFAULT '".concat(field.Default, "'") : isTimestamp ? 'NULL DEFAULT NULL' : 'DEFAULT NULL'; // Remove ON UPDATE CURRENT_TIMESTAMP if we're changing away from CURRENT_TIMESTAMP
3799
- _currentField = diff.currentField;
3800
- hasOnUpdate = (_currentField === null || _currentField === undefined ? undefined : _currentField.Default) === 'CURRENT_TIMESTAMP' && field.Default !== 'CURRENT_TIMESTAMP';
3782
+ hasOnUpdate = (currentField === null || currentField === undefined ? undefined : currentField.Default) === 'CURRENT_TIMESTAMP' && field.Default !== 'CURRENT_TIMESTAMP';
3801
3783
  _modifyFieldSQL2 = "ALTER TABLE `".concat(_tableName2, "` CHANGE `").concat(field.Field, "` `").concat(field.Field, "` ").concat(field.Type, " ").concat(field.NotNull ? 'NOT NULL' : '', " ").concat(_defaultClause).concat(hasOnUpdate ? ' ON UPDATE CURRENT_TIMESTAMP' : '', ";");
3802
3784
  console.log("Executing: ".concat(_modifyFieldSQL2));
3803
- _context.next = 98;
3785
+ _context.next = 61;
3804
3786
  return connection.query(_modifyFieldSQL2);
3805
- case 98:
3806
- _context.next = 249;
3787
+ case 61:
3788
+ _context.next = 212;
3807
3789
  break;
3808
- case 100:
3790
+ case 63:
3809
3791
  if (!(diff.type === 'missing_index')) {
3810
- _context.next = 132;
3792
+ _context.next = 95;
3811
3793
  break;
3812
3794
  }
3813
- _context.next = 103;
3795
+ _context.next = 66;
3814
3796
  return connection.query("SHOW COLUMNS FROM `".concat(diff.tableName, "`"));
3815
- case 103:
3797
+ case 66:
3816
3798
  existingColumns = _context.sent;
3817
3799
  columnNames = existingColumns.map(function (col) {
3818
3800
  return col.Field;
3819
3801
  }); // Check if all columns for the index exist
3820
3802
  missingColumns = Array.isArray(diff.index.ColumnName) ? diff.index.ColumnName.filter(function (col) {
3821
- return !columnNames.includes(col);
3803
+ var _col$split;
3804
+ return !columnNames.includes(col) && !columnNames.includes((_col$split = col.split("(")) === null || _col$split === undefined ? undefined : _col$split[0]);
3822
3805
  }) : [];
3823
3806
  if (!(missingColumns.length > 0)) {
3824
- _context.next = 109;
3807
+ _context.next = 72;
3825
3808
  break;
3826
3809
  }
3827
3810
  logger("Cannot create index ".concat(diff.index.Name, " on ").concat(diff.tableName, ". Missing columns: ").concat(missingColumns.join(', ')));
3828
3811
  return _context.abrupt("return", 0);
3829
- case 109:
3812
+ case 72:
3830
3813
  // Prepare the index columns with proper formatting
3831
3814
  indexColumns = diff.index.ColumnName.map(function (col) {
3832
- return "`".concat(col, "`");
3833
- }); // Format column names
3834
- // Handle PRIMARY index creation
3815
+ if (col.includes("(")) {
3816
+ var match = col.match(/^(?:`([^`]+)`|([A-Za-z_][A-Za-z0-9_\$]*))\((\d+)\)$/);
3817
+ if (match) {
3818
+ var name = match[1] || match[2]; // one of them will be filled
3819
+ var size = match[3];
3820
+ console.log(name, size);
3821
+ return "`".concat(name, "`(").concat(size, ")");
3822
+ }
3823
+ return "`".concat(col.split("(")[0], "`");
3824
+ } else {
3825
+ return "`".concat(col, "`"); // Format column names
3826
+ }
3827
+ }); // Handle PRIMARY index creation
3835
3828
  if (!(diff.index.Name === 'PRIMARY')) {
3836
- _context.next = 121;
3829
+ _context.next = 84;
3837
3830
  break;
3838
3831
  }
3839
3832
  if (!(indexColumns.length > 0)) {
3840
- _context.next = 118;
3833
+ _context.next = 81;
3841
3834
  break;
3842
3835
  }
3843
3836
  primaryKeySQL = "ALTER TABLE `".concat(diff.tableName, "` ADD PRIMARY KEY (").concat(indexColumns.join(', '), ");");
3844
3837
  console.log("Executing: ".concat(primaryKeySQL));
3845
- _context.next = 116;
3838
+ _context.next = 79;
3846
3839
  return connection.query(primaryKeySQL);
3847
- case 116:
3848
- _context.next = 119;
3840
+ case 79:
3841
+ _context.next = 82;
3849
3842
  break;
3850
- case 118:
3843
+ case 81:
3851
3844
  logger("Cannot create PRIMARY index on ".concat(diff.tableName, ". ColumnName is undefined or empty."), diff.index);
3852
- case 119:
3853
- _context.next = 130;
3845
+ case 82:
3846
+ _context.next = 93;
3854
3847
  break;
3855
- case 121:
3848
+ case 84:
3856
3849
  // Create the new index with UNIQUE if applicable
3857
3850
  uniqueSQL = diff.index.Unique ? 'UNIQUE ' : ''; // Check if the index is unique
3858
3851
  if (!(indexColumns.length > 0)) {
3859
- _context.next = 129;
3852
+ _context.next = 92;
3860
3853
  break;
3861
3854
  }
3862
3855
  addIndexSQL = "ALTER TABLE `".concat(diff.tableName, "` ADD ").concat(uniqueSQL, "INDEX `").concat(diff.index.Name, "` (").concat(indexColumns.join(', '), ");");
3863
3856
  console.log("Executing: ".concat(addIndexSQL));
3864
- _context.next = 127;
3857
+ _context.next = 90;
3865
3858
  return connection.query(addIndexSQL);
3866
- case 127:
3867
- _context.next = 130;
3859
+ case 90:
3860
+ _context.next = 93;
3868
3861
  break;
3869
- case 129:
3862
+ case 92:
3870
3863
  logger("Cannot create index ".concat(diff.index.Name, " on ").concat(diff.tableName, ". ColumnName is undefined or empty."), diff.index);
3871
- case 130:
3872
- _context.next = 249;
3864
+ case 93:
3865
+ _context.next = 212;
3873
3866
  break;
3874
- case 132:
3867
+ case 95:
3875
3868
  if (!(diff.type === 'extra_index')) {
3876
- _context.next = 146;
3869
+ _context.next = 109;
3877
3870
  break;
3878
3871
  }
3879
3872
  if (!(diff.index.Name === 'PRIMARY')) {
3880
- _context.next = 140;
3873
+ _context.next = 103;
3881
3874
  break;
3882
3875
  }
3883
3876
  dropPrimaryKeySQL = "ALTER TABLE `".concat(diff.tableName, "` DROP PRIMARY KEY;");
3884
3877
  console.log("Executing: ".concat(dropPrimaryKeySQL));
3885
- _context.next = 138;
3878
+ _context.next = 101;
3886
3879
  return connection.query(dropPrimaryKeySQL);
3887
- case 138:
3888
- _context.next = 144;
3880
+ case 101:
3881
+ _context.next = 107;
3889
3882
  break;
3890
- case 140:
3883
+ case 103:
3891
3884
  dropIndexSQL = "DROP INDEX `".concat(diff.index.Name, "` ON `").concat(diff.tableName, "`;");
3892
3885
  console.log("Executing: ".concat(dropIndexSQL));
3893
- _context.next = 144;
3886
+ _context.next = 107;
3894
3887
  return connection.query(dropIndexSQL);
3895
- case 144:
3896
- _context.next = 249;
3888
+ case 107:
3889
+ _context.next = 212;
3897
3890
  break;
3898
- case 146:
3891
+ case 109:
3899
3892
  if (!(diff.type === 'mismatched_index')) {
3900
- _context.next = 174;
3893
+ _context.next = 137;
3901
3894
  break;
3902
3895
  }
3903
3896
  _tableName3 = diff.tableName, index = diff.index; // Drop the existing index
3904
3897
  if (!(index.Name === 'PRIMARY')) {
3905
- _context.next = 155;
3898
+ _context.next = 118;
3906
3899
  break;
3907
3900
  }
3908
3901
  _dropPrimaryKeySQL = "ALTER TABLE `".concat(_tableName3, "` DROP PRIMARY KEY;");
3909
3902
  console.log("Executing: ".concat(_dropPrimaryKeySQL));
3910
- _context.next = 153;
3903
+ _context.next = 116;
3911
3904
  return connection.query(_dropPrimaryKeySQL);
3912
- case 153:
3913
- _context.next = 159;
3905
+ case 116:
3906
+ _context.next = 122;
3914
3907
  break;
3915
- case 155:
3908
+ case 118:
3916
3909
  _dropIndexSQL = "DROP INDEX `".concat(index.Name, "` ON `").concat(_tableName3, "`;");
3917
3910
  console.log("Executing: ".concat(_dropIndexSQL));
3918
- _context.next = 159;
3911
+ _context.next = 122;
3919
3912
  return connection.query(_dropIndexSQL);
3920
- case 159:
3913
+ case 122:
3921
3914
  console.log('index', index);
3922
3915
 
3923
3916
  // Create the new index
3924
3917
  _uniqueSQL = index.Unique ? 'UNIQUE ' : ''; // Check if the index is unique
3925
3918
  if (!(index.Name === 'PRIMARY')) {
3926
- _context.next = 168;
3919
+ _context.next = 131;
3927
3920
  break;
3928
3921
  }
3929
3922
  addPrimaryKeySQL = "ALTER TABLE `".concat(_tableName3, "` ADD PRIMARY KEY (").concat(index.ColumnName.join(', '), ");");
3930
3923
  console.log("Executing: ".concat(addPrimaryKeySQL));
3931
- _context.next = 166;
3924
+ _context.next = 129;
3932
3925
  return connection.query(addPrimaryKeySQL);
3933
- case 166:
3934
- _context.next = 172;
3926
+ case 129:
3927
+ _context.next = 135;
3935
3928
  break;
3936
- case 168:
3929
+ case 131:
3937
3930
  _addIndexSQL = "ALTER TABLE `".concat(_tableName3, "` ADD ").concat(_uniqueSQL, "INDEX `").concat(index.Name, "` (").concat(index.ColumnName.join(', '), ");"); // Include UNIQUE if applicable
3938
3931
  console.log("Executing: ".concat(_addIndexSQL));
3939
- _context.next = 172;
3932
+ _context.next = 135;
3940
3933
  return connection.query(_addIndexSQL);
3941
- case 172:
3942
- _context.next = 249;
3934
+ case 135:
3935
+ _context.next = 212;
3943
3936
  break;
3944
- case 174:
3937
+ case 137:
3945
3938
  if (!(diff.type === 'missing_procedure' || diff.type === 'mismatched_procedure')) {
3946
- _context.next = 185;
3939
+ _context.next = 148;
3947
3940
  break;
3948
3941
  }
3949
3942
  // Drop the procedure if it exists
3950
3943
  dropCurrentProcSQL = "DROP PROCEDURE IF EXISTS `".concat(diff.Name, "`;");
3951
3944
  console.log("Executing: ".concat(dropCurrentProcSQL));
3952
- _context.next = 179;
3945
+ _context.next = 142;
3953
3946
  return connection.query(dropCurrentProcSQL);
3954
- case 179:
3947
+ case 142:
3955
3948
  // Create the procedure
3956
3949
  createProcSQL = diff.Definition;
3957
3950
  console.log("Executing: ".concat(createProcSQL)); // Log the SQL statement
3958
- _context.next = 183;
3951
+ _context.next = 146;
3959
3952
  return connection.query(createProcSQL);
3960
- case 183:
3961
- _context.next = 249;
3953
+ case 146:
3954
+ _context.next = 212;
3962
3955
  break;
3963
- case 185:
3956
+ case 148:
3964
3957
  if (!(diff.type === 'missing_view')) {
3965
- _context.next = 196;
3958
+ _context.next = 159;
3966
3959
  break;
3967
3960
  }
3968
3961
  dropViewSQL = "DROP VIEW IF EXISTS `".concat(diff.viewName, "`;");
3969
3962
  console.log("Executing: ".concat(dropViewSQL));
3970
- _context.next = 190;
3963
+ _context.next = 153;
3971
3964
  return connection.query(dropViewSQL);
3972
- case 190:
3965
+ case 153:
3973
3966
  createViewSQL = diff.definition; // Use the expected definition to create the view
3974
3967
  console.log("Executing: ".concat(createViewSQL));
3975
- _context.next = 194;
3968
+ _context.next = 157;
3976
3969
  return connection.query(createViewSQL);
3977
- case 194:
3978
- _context.next = 249;
3970
+ case 157:
3971
+ _context.next = 212;
3979
3972
  break;
3980
- case 196:
3973
+ case 159:
3981
3974
  if (!(diff.type === 'mismatched_view')) {
3982
- _context.next = 207;
3975
+ _context.next = 170;
3983
3976
  break;
3984
3977
  }
3985
3978
  _dropViewSQL = "DROP VIEW IF EXISTS `".concat(diff.viewName, "`;");
3986
3979
  console.log("Executing: ".concat(_dropViewSQL));
3987
- _context.next = 201;
3980
+ _context.next = 164;
3988
3981
  return connection.query(_dropViewSQL);
3989
- case 201:
3982
+ case 164:
3990
3983
  _createViewSQL = diff.definition; // Use the expected definition to create the view
3991
3984
  console.log("Executing: ".concat(_createViewSQL));
3992
- _context.next = 205;
3985
+ _context.next = 168;
3993
3986
  return connection.query(_createViewSQL);
3994
- case 205:
3995
- _context.next = 249;
3987
+ case 168:
3988
+ _context.next = 212;
3996
3989
  break;
3997
- case 207:
3990
+ case 170:
3998
3991
  if (!(diff.type === 'extra_view')) {
3999
- _context.next = 214;
3992
+ _context.next = 177;
4000
3993
  break;
4001
3994
  }
4002
3995
  _dropViewSQL2 = "DROP VIEW IF EXISTS `".concat(diff.viewName, "`;");
4003
3996
  console.log("Executing: ".concat(_dropViewSQL2));
4004
- _context.next = 212;
3997
+ _context.next = 175;
4005
3998
  return connection.query(_dropViewSQL2);
4006
- case 212:
4007
- _context.next = 249;
3999
+ case 175:
4000
+ _context.next = 212;
4008
4001
  break;
4009
- case 214:
4002
+ case 177:
4010
4003
  if (!(diff.type === 'missing_trigger')) {
4011
- _context.next = 222;
4004
+ _context.next = 185;
4012
4005
  break;
4013
4006
  }
4014
4007
  createTriggerSQL = diff.trigger.Definition;
4015
4008
  logger('fixing trigger', diff.trigger.Name);
4016
4009
  console.log("Executing: ".concat(createTriggerSQL));
4017
- _context.next = 220;
4010
+ _context.next = 183;
4018
4011
  return connection.query(createTriggerSQL);
4019
- case 220:
4020
- _context.next = 249;
4012
+ case 183:
4013
+ _context.next = 212;
4021
4014
  break;
4022
- case 222:
4015
+ case 185:
4023
4016
  if (!(diff.type === 'mismatched_trigger')) {
4024
- _context.next = 233;
4017
+ _context.next = 196;
4025
4018
  break;
4026
4019
  }
4027
4020
  dropTriggerSQL = "DROP TRIGGER IF EXISTS `".concat(diff.trigger.Name, "`;");
4028
4021
  console.log("Executing: ".concat(dropTriggerSQL));
4029
- _context.next = 227;
4022
+ _context.next = 190;
4030
4023
  return connection.query(dropTriggerSQL);
4031
- case 227:
4024
+ case 190:
4032
4025
  _createTriggerSQL = diff.trigger.Definition;
4033
4026
  console.log("Executing: ".concat(_createTriggerSQL));
4034
- _context.next = 231;
4027
+ _context.next = 194;
4035
4028
  return connection.query(_createTriggerSQL);
4036
- case 231:
4037
- _context.next = 249;
4029
+ case 194:
4030
+ _context.next = 212;
4038
4031
  break;
4039
- case 233:
4032
+ case 196:
4040
4033
  if (!(diff.type === 'extra_trigger')) {
4041
- _context.next = 240;
4034
+ _context.next = 203;
4042
4035
  break;
4043
4036
  }
4044
4037
  _dropTriggerSQL = "DROP TRIGGER IF EXISTS `".concat(diff.trigger.Name, "`;");
4045
4038
  console.log("Executing: ".concat(_dropTriggerSQL));
4046
- _context.next = 238;
4039
+ _context.next = 201;
4047
4040
  return connection.query(_dropTriggerSQL);
4048
- case 238:
4049
- _context.next = 249;
4041
+ case 201:
4042
+ _context.next = 212;
4050
4043
  break;
4051
- case 240:
4044
+ case 203:
4052
4045
  if (!(diff.type === 'missing_database')) {
4053
- _context.next = 248;
4046
+ _context.next = 211;
4054
4047
  break;
4055
4048
  }
4056
4049
  logger('fixing missing database', diff.database);
4057
4050
  _createDatabaseSQL = "CREATE DATABASE `".concat(diff.database, "`;");
4058
4051
  console.log("Executing: ".concat(_createDatabaseSQL));
4059
- _context.next = 246;
4052
+ _context.next = 209;
4060
4053
  return connection.query(_createDatabaseSQL);
4061
- case 246:
4062
- _context.next = 249;
4054
+ case 209:
4055
+ _context.next = 212;
4063
4056
  break;
4064
- case 248:
4057
+ case 211:
4065
4058
  logger("Error applying difference of type:".concat(diff.type), diff);
4066
- case 249:
4067
- _context.next = 254;
4059
+ case 212:
4060
+ _context.next = 217;
4068
4061
  break;
4069
- case 251:
4070
- _context.prev = 251;
4062
+ case 214:
4063
+ _context.prev = 214;
4071
4064
  _context.t0 = _context["catch"](1);
4072
4065
  logger("Error applying difference of type:".concat(diff.type, ". Error: ").concat(_context.t0.message), diff);
4073
- case 254:
4066
+ case 217:
4074
4067
  case "end":
4075
4068
  return _context.stop();
4076
4069
  }
4077
- }, _loop, null, [[1, 251]]);
4070
+ }, _loop, null, [[1, 214]]);
4078
4071
  });
4079
4072
  _iterator.s();
4080
4073
  case 5:
@@ -5469,10 +5462,8 @@ var apply = /*#__PURE__*/function () {
5469
5462
  // Only run this if this is the main module
5470
5463
  var isMain = function () {
5471
5464
  try {
5472
- // console.log('Checking if this is the main module');
5473
- // console.log(require.main === module)
5474
- // console.log(require.main);
5475
- return require.main === module;
5465
+ var modulePath = fileURLToPath$1(import.meta.url);
5466
+ return process.argv[1] === modulePath;
5476
5467
  } catch (e) {
5477
5468
  return false;
5478
5469
  }