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