mysqlsyncr 1.1.2 → 1.1.4

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
@@ -3599,7 +3599,7 @@ var applyDifferences = /*#__PURE__*/function () {
3599
3599
  _iterator = _createForOfIteratorHelper(differences);
3600
3600
  _context2.prev = 2;
3601
3601
  _loop = /*#__PURE__*/_regeneratorRuntime().mark(function _loop() {
3602
- 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;
3602
+ var diff, createDatabaseSQL, tableName, createSQL, _tableName, dropTableSQL, addFieldSQL, modifyFieldSQL, defaultClause, _modifyFieldSQL, dropFieldSQL, _tableName2, field, isTimestamp, _defaultClause, currentField, hasOnUpdate, _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;
3603
3603
  return _regeneratorRuntime().wrap(function _loop$(_context) {
3604
3604
  while (1) switch (_context.prev = _context.next) {
3605
3605
  case 0:
@@ -3629,7 +3629,7 @@ var applyDifferences = /*#__PURE__*/function () {
3629
3629
  _context.next = 16;
3630
3630
  return connection.query(createSQL);
3631
3631
  case 16:
3632
- _context.next = 210;
3632
+ _context.next = 213;
3633
3633
  break;
3634
3634
  case 18:
3635
3635
  if (!(diff.type === 'extra_table')) {
@@ -3643,7 +3643,7 @@ var applyDifferences = /*#__PURE__*/function () {
3643
3643
  _context.next = 25;
3644
3644
  return connection.query(dropTableSQL);
3645
3645
  case 25:
3646
- _context.next = 210;
3646
+ _context.next = 213;
3647
3647
  break;
3648
3648
  case 27:
3649
3649
  if (!(diff.type === 'missing_field')) {
@@ -3675,7 +3675,7 @@ var applyDifferences = /*#__PURE__*/function () {
3675
3675
  _context.next = 43;
3676
3676
  return connection.query(_modifyFieldSQL);
3677
3677
  case 43:
3678
- _context.next = 210;
3678
+ _context.next = 213;
3679
3679
  break;
3680
3680
  case 45:
3681
3681
  if (!(diff.type === 'extra_field')) {
@@ -3687,30 +3687,33 @@ var applyDifferences = /*#__PURE__*/function () {
3687
3687
  _context.next = 50;
3688
3688
  return connection.query(dropFieldSQL);
3689
3689
  case 50:
3690
- _context.next = 210;
3690
+ _context.next = 213;
3691
3691
  break;
3692
3692
  case 52:
3693
3693
  if (!(diff.type === 'mismatched_field')) {
3694
- _context.next = 61;
3694
+ _context.next = 64;
3695
3695
  break;
3696
3696
  }
3697
3697
  _tableName2 = diff.tableName, field = diff.field;
3698
- _defaultClause = field.Default === 'CURRENT_TIMESTAMP' ? 'DEFAULT CURRENT_TIMESTAMP' : field.Default === 'NULL' ? 'DEFAULT NULL' : field.Default !== undefined && field.Default !== null ? "DEFAULT '".concat(field.Default, "'") : 'DEFAULT NULL';
3699
- _dropFieldSQL = "ALTER TABLE `".concat(_tableName2, "` CHANGE `").concat(field.Field, "` `").concat(field.Field, "` ").concat(field.Type, " ").concat(field.NotNull ? 'NOT NULL' : '', " ").concat(_defaultClause, ";");
3700
- console.log("Executing: ".concat(_dropFieldSQL));
3701
- _context.next = 59;
3698
+ isTimestamp = field.Type.toLowerCase().includes('timestamp'); // Handle timestamp specific attributes
3699
+ _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
3700
+ currentField = diff.currentField;
3701
+ hasOnUpdate = (currentField === null || currentField === undefined ? undefined : currentField.Default) === 'CURRENT_TIMESTAMP' && field.Default !== 'CURRENT_TIMESTAMP';
3702
+ _dropFieldSQL = "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' : '', ";");
3703
+ logger("Executing: ".concat(_dropFieldSQL));
3704
+ _context.next = 62;
3702
3705
  return connection.query(_dropFieldSQL);
3703
- case 59:
3704
- _context.next = 210;
3706
+ case 62:
3707
+ _context.next = 213;
3705
3708
  break;
3706
- case 61:
3709
+ case 64:
3707
3710
  if (!(diff.type === 'missing_index')) {
3708
- _context.next = 93;
3711
+ _context.next = 96;
3709
3712
  break;
3710
3713
  }
3711
- _context.next = 64;
3714
+ _context.next = 67;
3712
3715
  return connection.query("SHOW COLUMNS FROM `".concat(diff.tableName, "`"));
3713
- case 64:
3716
+ case 67:
3714
3717
  existingColumns = _context.sent;
3715
3718
  columnNames = existingColumns.map(function (col) {
3716
3719
  return col.Field;
@@ -3719,260 +3722,260 @@ var applyDifferences = /*#__PURE__*/function () {
3719
3722
  return !columnNames.includes(col);
3720
3723
  }) : [];
3721
3724
  if (!(missingColumns.length > 0)) {
3722
- _context.next = 70;
3725
+ _context.next = 73;
3723
3726
  break;
3724
3727
  }
3725
3728
  logger("Cannot create index ".concat(diff.index.Name, " on ").concat(diff.tableName, ". Missing columns: ").concat(missingColumns.join(', ')));
3726
3729
  return _context.abrupt("return", 0);
3727
- case 70:
3730
+ case 73:
3728
3731
  // Prepare the index columns with proper formatting
3729
3732
  indexColumns = diff.index.ColumnName.map(function (col) {
3730
3733
  return "`".concat(col, "`");
3731
3734
  }); // Format column names
3732
3735
  // Handle PRIMARY index creation
3733
3736
  if (!(diff.index.Name === 'PRIMARY')) {
3734
- _context.next = 82;
3737
+ _context.next = 85;
3735
3738
  break;
3736
3739
  }
3737
3740
  if (!(indexColumns.length > 0)) {
3738
- _context.next = 79;
3741
+ _context.next = 82;
3739
3742
  break;
3740
3743
  }
3741
3744
  primaryKeySQL = "ALTER TABLE `".concat(diff.tableName, "` ADD PRIMARY KEY (").concat(indexColumns.join(', '), ");");
3742
3745
  console.log("Executing: ".concat(primaryKeySQL));
3743
- _context.next = 77;
3744
- return connection.query(primaryKeySQL);
3745
- case 77:
3746
3746
  _context.next = 80;
3747
- break;
3748
- case 79:
3749
- logger("Cannot create PRIMARY index on ".concat(diff.tableName, ". ColumnName is undefined or empty."), diff.index);
3747
+ return connection.query(primaryKeySQL);
3750
3748
  case 80:
3751
- _context.next = 91;
3749
+ _context.next = 83;
3752
3750
  break;
3753
3751
  case 82:
3752
+ logger("Cannot create PRIMARY index on ".concat(diff.tableName, ". ColumnName is undefined or empty."), diff.index);
3753
+ case 83:
3754
+ _context.next = 94;
3755
+ break;
3756
+ case 85:
3754
3757
  // Create the new index with UNIQUE if applicable
3755
3758
  uniqueSQL = diff.index.Unique ? 'UNIQUE ' : ''; // Check if the index is unique
3756
3759
  if (!(indexColumns.length > 0)) {
3757
- _context.next = 90;
3760
+ _context.next = 93;
3758
3761
  break;
3759
3762
  }
3760
3763
  addIndexSQL = "ALTER TABLE `".concat(diff.tableName, "` ADD ").concat(uniqueSQL, "INDEX `").concat(diff.index.Name, "` (").concat(indexColumns.join(', '), ");");
3761
3764
  console.log("Executing: ".concat(addIndexSQL));
3762
- _context.next = 88;
3763
- return connection.query(addIndexSQL);
3764
- case 88:
3765
3765
  _context.next = 91;
3766
- break;
3767
- case 90:
3768
- logger("Cannot create index ".concat(diff.index.Name, " on ").concat(diff.tableName, ". ColumnName is undefined or empty."), diff.index);
3766
+ return connection.query(addIndexSQL);
3769
3767
  case 91:
3770
- _context.next = 210;
3768
+ _context.next = 94;
3771
3769
  break;
3772
3770
  case 93:
3771
+ logger("Cannot create index ".concat(diff.index.Name, " on ").concat(diff.tableName, ". ColumnName is undefined or empty."), diff.index);
3772
+ case 94:
3773
+ _context.next = 213;
3774
+ break;
3775
+ case 96:
3773
3776
  if (!(diff.type === 'extra_index')) {
3774
- _context.next = 107;
3777
+ _context.next = 110;
3775
3778
  break;
3776
3779
  }
3777
3780
  if (!(diff.index.Name === 'PRIMARY')) {
3778
- _context.next = 101;
3781
+ _context.next = 104;
3779
3782
  break;
3780
3783
  }
3781
3784
  dropPrimaryKeySQL = "ALTER TABLE `".concat(diff.tableName, "` DROP PRIMARY KEY;");
3782
3785
  console.log("Executing: ".concat(dropPrimaryKeySQL));
3783
- _context.next = 99;
3786
+ _context.next = 102;
3784
3787
  return connection.query(dropPrimaryKeySQL);
3785
- case 99:
3786
- _context.next = 105;
3788
+ case 102:
3789
+ _context.next = 108;
3787
3790
  break;
3788
- case 101:
3791
+ case 104:
3789
3792
  dropIndexSQL = "DROP INDEX `".concat(diff.index.Name, "` ON `").concat(diff.tableName, "`;");
3790
3793
  console.log("Executing: ".concat(dropIndexSQL));
3791
- _context.next = 105;
3794
+ _context.next = 108;
3792
3795
  return connection.query(dropIndexSQL);
3793
- case 105:
3794
- _context.next = 210;
3796
+ case 108:
3797
+ _context.next = 213;
3795
3798
  break;
3796
- case 107:
3799
+ case 110:
3797
3800
  if (!(diff.type === 'mismatched_index')) {
3798
- _context.next = 135;
3801
+ _context.next = 138;
3799
3802
  break;
3800
3803
  }
3801
3804
  _tableName3 = diff.tableName, index = diff.index; // Drop the existing index
3802
3805
  if (!(index.Name === 'PRIMARY')) {
3803
- _context.next = 116;
3806
+ _context.next = 119;
3804
3807
  break;
3805
3808
  }
3806
3809
  _dropPrimaryKeySQL = "ALTER TABLE `".concat(_tableName3, "` DROP PRIMARY KEY;");
3807
3810
  console.log("Executing: ".concat(_dropPrimaryKeySQL));
3808
- _context.next = 114;
3811
+ _context.next = 117;
3809
3812
  return connection.query(_dropPrimaryKeySQL);
3810
- case 114:
3811
- _context.next = 120;
3813
+ case 117:
3814
+ _context.next = 123;
3812
3815
  break;
3813
- case 116:
3816
+ case 119:
3814
3817
  _dropIndexSQL = "DROP INDEX `".concat(index.Name, "` ON `").concat(_tableName3, "`;");
3815
3818
  console.log("Executing: ".concat(_dropIndexSQL));
3816
- _context.next = 120;
3819
+ _context.next = 123;
3817
3820
  return connection.query(_dropIndexSQL);
3818
- case 120:
3821
+ case 123:
3819
3822
  console.log('index', index);
3820
3823
 
3821
3824
  // Create the new index
3822
3825
  _uniqueSQL = index.Unique ? 'UNIQUE ' : ''; // Check if the index is unique
3823
3826
  if (!(index.Name === 'PRIMARY')) {
3824
- _context.next = 129;
3827
+ _context.next = 132;
3825
3828
  break;
3826
3829
  }
3827
3830
  addPrimaryKeySQL = "ALTER TABLE `".concat(_tableName3, "` ADD PRIMARY KEY (").concat(index.ColumnName.join(', '), ");");
3828
3831
  console.log("Executing: ".concat(addPrimaryKeySQL));
3829
- _context.next = 127;
3832
+ _context.next = 130;
3830
3833
  return connection.query(addPrimaryKeySQL);
3831
- case 127:
3832
- _context.next = 133;
3834
+ case 130:
3835
+ _context.next = 136;
3833
3836
  break;
3834
- case 129:
3837
+ case 132:
3835
3838
  _addIndexSQL = "ALTER TABLE `".concat(_tableName3, "` ADD ").concat(_uniqueSQL, "INDEX `").concat(index.Name, "` (").concat(index.ColumnName.join(', '), ");"); // Include UNIQUE if applicable
3836
3839
  console.log("Executing: ".concat(_addIndexSQL));
3837
- _context.next = 133;
3840
+ _context.next = 136;
3838
3841
  return connection.query(_addIndexSQL);
3839
- case 133:
3840
- _context.next = 210;
3842
+ case 136:
3843
+ _context.next = 213;
3841
3844
  break;
3842
- case 135:
3845
+ case 138:
3843
3846
  if (!(diff.type === 'missing_procedure' || diff.type === 'mismatched_procedure')) {
3844
- _context.next = 146;
3847
+ _context.next = 149;
3845
3848
  break;
3846
3849
  }
3847
3850
  // Drop the procedure if it exists
3848
3851
  dropCurrentProcSQL = "DROP PROCEDURE IF EXISTS `".concat(diff.Name, "`;");
3849
3852
  console.log("Executing: ".concat(dropCurrentProcSQL));
3850
- _context.next = 140;
3853
+ _context.next = 143;
3851
3854
  return connection.query(dropCurrentProcSQL);
3852
- case 140:
3855
+ case 143:
3853
3856
  // Create the procedure
3854
3857
  createProcSQL = diff.Definition;
3855
3858
  console.log("Executing: ".concat(createProcSQL)); // Log the SQL statement
3856
- _context.next = 144;
3859
+ _context.next = 147;
3857
3860
  return connection.query(createProcSQL);
3858
- case 144:
3859
- _context.next = 210;
3861
+ case 147:
3862
+ _context.next = 213;
3860
3863
  break;
3861
- case 146:
3864
+ case 149:
3862
3865
  if (!(diff.type === 'missing_view')) {
3863
- _context.next = 157;
3866
+ _context.next = 160;
3864
3867
  break;
3865
3868
  }
3866
3869
  dropViewSQL = "DROP VIEW IF EXISTS `".concat(diff.viewName, "`;");
3867
3870
  console.log("Executing: ".concat(dropViewSQL));
3868
- _context.next = 151;
3871
+ _context.next = 154;
3869
3872
  return connection.query(dropViewSQL);
3870
- case 151:
3873
+ case 154:
3871
3874
  createViewSQL = diff.definition; // Use the expected definition to create the view
3872
3875
  console.log("Executing: ".concat(createViewSQL));
3873
- _context.next = 155;
3876
+ _context.next = 158;
3874
3877
  return connection.query(createViewSQL);
3875
- case 155:
3876
- _context.next = 210;
3878
+ case 158:
3879
+ _context.next = 213;
3877
3880
  break;
3878
- case 157:
3881
+ case 160:
3879
3882
  if (!(diff.type === 'mismatched_view')) {
3880
- _context.next = 168;
3883
+ _context.next = 171;
3881
3884
  break;
3882
3885
  }
3883
3886
  _dropViewSQL = "DROP VIEW IF EXISTS `".concat(diff.viewName, "`;");
3884
3887
  console.log("Executing: ".concat(_dropViewSQL));
3885
- _context.next = 162;
3888
+ _context.next = 165;
3886
3889
  return connection.query(_dropViewSQL);
3887
- case 162:
3890
+ case 165:
3888
3891
  _createViewSQL = diff.definition; // Use the expected definition to create the view
3889
3892
  console.log("Executing: ".concat(_createViewSQL));
3890
- _context.next = 166;
3893
+ _context.next = 169;
3891
3894
  return connection.query(_createViewSQL);
3892
- case 166:
3893
- _context.next = 210;
3895
+ case 169:
3896
+ _context.next = 213;
3894
3897
  break;
3895
- case 168:
3898
+ case 171:
3896
3899
  if (!(diff.type === 'extra_view')) {
3897
- _context.next = 175;
3900
+ _context.next = 178;
3898
3901
  break;
3899
3902
  }
3900
3903
  _dropViewSQL2 = "DROP VIEW IF EXISTS `".concat(diff.viewName, "`;");
3901
3904
  console.log("Executing: ".concat(_dropViewSQL2));
3902
- _context.next = 173;
3905
+ _context.next = 176;
3903
3906
  return connection.query(_dropViewSQL2);
3904
- case 173:
3905
- _context.next = 210;
3907
+ case 176:
3908
+ _context.next = 213;
3906
3909
  break;
3907
- case 175:
3910
+ case 178:
3908
3911
  if (!(diff.type === 'missing_trigger')) {
3909
- _context.next = 183;
3912
+ _context.next = 186;
3910
3913
  break;
3911
3914
  }
3912
3915
  createTriggerSQL = diff.trigger.Definition;
3913
3916
  logger('fixing trigger', diff.trigger.Name);
3914
3917
  console.log("Executing: ".concat(createTriggerSQL));
3915
- _context.next = 181;
3918
+ _context.next = 184;
3916
3919
  return connection.query(createTriggerSQL);
3917
- case 181:
3918
- _context.next = 210;
3920
+ case 184:
3921
+ _context.next = 213;
3919
3922
  break;
3920
- case 183:
3923
+ case 186:
3921
3924
  if (!(diff.type === 'mismatched_trigger')) {
3922
- _context.next = 194;
3925
+ _context.next = 197;
3923
3926
  break;
3924
3927
  }
3925
3928
  dropTriggerSQL = "DROP TRIGGER IF EXISTS `".concat(diff.trigger.Name, "`;");
3926
3929
  console.log("Executing: ".concat(dropTriggerSQL));
3927
- _context.next = 188;
3930
+ _context.next = 191;
3928
3931
  return connection.query(dropTriggerSQL);
3929
- case 188:
3932
+ case 191:
3930
3933
  _createTriggerSQL = diff.trigger.Definition;
3931
3934
  console.log("Executing: ".concat(_createTriggerSQL));
3932
- _context.next = 192;
3935
+ _context.next = 195;
3933
3936
  return connection.query(_createTriggerSQL);
3934
- case 192:
3935
- _context.next = 210;
3937
+ case 195:
3938
+ _context.next = 213;
3936
3939
  break;
3937
- case 194:
3940
+ case 197:
3938
3941
  if (!(diff.type === 'extra_trigger')) {
3939
- _context.next = 201;
3942
+ _context.next = 204;
3940
3943
  break;
3941
3944
  }
3942
3945
  _dropTriggerSQL = "DROP TRIGGER IF EXISTS `".concat(diff.trigger.Name, "`;");
3943
3946
  console.log("Executing: ".concat(_dropTriggerSQL));
3944
- _context.next = 199;
3947
+ _context.next = 202;
3945
3948
  return connection.query(_dropTriggerSQL);
3946
- case 199:
3947
- _context.next = 210;
3949
+ case 202:
3950
+ _context.next = 213;
3948
3951
  break;
3949
- case 201:
3952
+ case 204:
3950
3953
  if (!(diff.type === 'missing_database')) {
3951
- _context.next = 209;
3954
+ _context.next = 212;
3952
3955
  break;
3953
3956
  }
3954
3957
  logger('fixing missing database', diff.database);
3955
3958
  _createDatabaseSQL = "CREATE DATABASE `".concat(diff.database, "`;");
3956
3959
  console.log("Executing: ".concat(_createDatabaseSQL));
3957
- _context.next = 207;
3958
- return connection.query(_createDatabaseSQL);
3959
- case 207:
3960
3960
  _context.next = 210;
3961
- break;
3962
- case 209:
3963
- logger("Error applying difference of type:".concat(diff.type), diff);
3961
+ return connection.query(_createDatabaseSQL);
3964
3962
  case 210:
3965
- _context.next = 215;
3963
+ _context.next = 213;
3966
3964
  break;
3967
3965
  case 212:
3968
- _context.prev = 212;
3966
+ logger("Error applying difference of type:".concat(diff.type), diff);
3967
+ case 213:
3968
+ _context.next = 218;
3969
+ break;
3970
+ case 215:
3971
+ _context.prev = 215;
3969
3972
  _context.t0 = _context["catch"](1);
3970
3973
  logger("Error applying difference of type:".concat(diff.type, ". Error: ").concat(_context.t0.message), diff);
3971
- case 215:
3974
+ case 218:
3972
3975
  case "end":
3973
3976
  return _context.stop();
3974
3977
  }
3975
- }, _loop, null, [[1, 212]]);
3978
+ }, _loop, null, [[1, 215]]);
3976
3979
  });
3977
3980
  _iterator.s();
3978
3981
  case 5:
package/dist/index.js CHANGED
@@ -3596,7 +3596,7 @@ var applyDifferences = /*#__PURE__*/function () {
3596
3596
  _iterator = _createForOfIteratorHelper(differences);
3597
3597
  _context2.prev = 2;
3598
3598
  _loop = /*#__PURE__*/_regeneratorRuntime().mark(function _loop() {
3599
- 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;
3599
+ var diff, createDatabaseSQL, tableName, createSQL, _tableName, dropTableSQL, addFieldSQL, modifyFieldSQL, defaultClause, _modifyFieldSQL, dropFieldSQL, _tableName2, field, isTimestamp, _defaultClause, currentField, hasOnUpdate, _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;
3600
3600
  return _regeneratorRuntime().wrap(function _loop$(_context) {
3601
3601
  while (1) switch (_context.prev = _context.next) {
3602
3602
  case 0:
@@ -3626,7 +3626,7 @@ var applyDifferences = /*#__PURE__*/function () {
3626
3626
  _context.next = 16;
3627
3627
  return connection.query(createSQL);
3628
3628
  case 16:
3629
- _context.next = 210;
3629
+ _context.next = 213;
3630
3630
  break;
3631
3631
  case 18:
3632
3632
  if (!(diff.type === 'extra_table')) {
@@ -3640,7 +3640,7 @@ var applyDifferences = /*#__PURE__*/function () {
3640
3640
  _context.next = 25;
3641
3641
  return connection.query(dropTableSQL);
3642
3642
  case 25:
3643
- _context.next = 210;
3643
+ _context.next = 213;
3644
3644
  break;
3645
3645
  case 27:
3646
3646
  if (!(diff.type === 'missing_field')) {
@@ -3672,7 +3672,7 @@ var applyDifferences = /*#__PURE__*/function () {
3672
3672
  _context.next = 43;
3673
3673
  return connection.query(_modifyFieldSQL);
3674
3674
  case 43:
3675
- _context.next = 210;
3675
+ _context.next = 213;
3676
3676
  break;
3677
3677
  case 45:
3678
3678
  if (!(diff.type === 'extra_field')) {
@@ -3684,30 +3684,33 @@ var applyDifferences = /*#__PURE__*/function () {
3684
3684
  _context.next = 50;
3685
3685
  return connection.query(dropFieldSQL);
3686
3686
  case 50:
3687
- _context.next = 210;
3687
+ _context.next = 213;
3688
3688
  break;
3689
3689
  case 52:
3690
3690
  if (!(diff.type === 'mismatched_field')) {
3691
- _context.next = 61;
3691
+ _context.next = 64;
3692
3692
  break;
3693
3693
  }
3694
3694
  _tableName2 = diff.tableName, field = diff.field;
3695
- _defaultClause = field.Default === 'CURRENT_TIMESTAMP' ? 'DEFAULT CURRENT_TIMESTAMP' : field.Default === 'NULL' ? 'DEFAULT NULL' : field.Default !== undefined && field.Default !== null ? "DEFAULT '".concat(field.Default, "'") : 'DEFAULT NULL';
3696
- _dropFieldSQL = "ALTER TABLE `".concat(_tableName2, "` CHANGE `").concat(field.Field, "` `").concat(field.Field, "` ").concat(field.Type, " ").concat(field.NotNull ? 'NOT NULL' : '', " ").concat(_defaultClause, ";");
3697
- console.log("Executing: ".concat(_dropFieldSQL));
3698
- _context.next = 59;
3695
+ isTimestamp = field.Type.toLowerCase().includes('timestamp'); // Handle timestamp specific attributes
3696
+ _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
3697
+ currentField = diff.currentField;
3698
+ hasOnUpdate = (currentField === null || currentField === undefined ? undefined : currentField.Default) === 'CURRENT_TIMESTAMP' && field.Default !== 'CURRENT_TIMESTAMP';
3699
+ _dropFieldSQL = "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' : '', ";");
3700
+ logger("Executing: ".concat(_dropFieldSQL));
3701
+ _context.next = 62;
3699
3702
  return connection.query(_dropFieldSQL);
3700
- case 59:
3701
- _context.next = 210;
3703
+ case 62:
3704
+ _context.next = 213;
3702
3705
  break;
3703
- case 61:
3706
+ case 64:
3704
3707
  if (!(diff.type === 'missing_index')) {
3705
- _context.next = 93;
3708
+ _context.next = 96;
3706
3709
  break;
3707
3710
  }
3708
- _context.next = 64;
3711
+ _context.next = 67;
3709
3712
  return connection.query("SHOW COLUMNS FROM `".concat(diff.tableName, "`"));
3710
- case 64:
3713
+ case 67:
3711
3714
  existingColumns = _context.sent;
3712
3715
  columnNames = existingColumns.map(function (col) {
3713
3716
  return col.Field;
@@ -3716,260 +3719,260 @@ var applyDifferences = /*#__PURE__*/function () {
3716
3719
  return !columnNames.includes(col);
3717
3720
  }) : [];
3718
3721
  if (!(missingColumns.length > 0)) {
3719
- _context.next = 70;
3722
+ _context.next = 73;
3720
3723
  break;
3721
3724
  }
3722
3725
  logger("Cannot create index ".concat(diff.index.Name, " on ").concat(diff.tableName, ". Missing columns: ").concat(missingColumns.join(', ')));
3723
3726
  return _context.abrupt("return", 0);
3724
- case 70:
3727
+ case 73:
3725
3728
  // Prepare the index columns with proper formatting
3726
3729
  indexColumns = diff.index.ColumnName.map(function (col) {
3727
3730
  return "`".concat(col, "`");
3728
3731
  }); // Format column names
3729
3732
  // Handle PRIMARY index creation
3730
3733
  if (!(diff.index.Name === 'PRIMARY')) {
3731
- _context.next = 82;
3734
+ _context.next = 85;
3732
3735
  break;
3733
3736
  }
3734
3737
  if (!(indexColumns.length > 0)) {
3735
- _context.next = 79;
3738
+ _context.next = 82;
3736
3739
  break;
3737
3740
  }
3738
3741
  primaryKeySQL = "ALTER TABLE `".concat(diff.tableName, "` ADD PRIMARY KEY (").concat(indexColumns.join(', '), ");");
3739
3742
  console.log("Executing: ".concat(primaryKeySQL));
3740
- _context.next = 77;
3741
- return connection.query(primaryKeySQL);
3742
- case 77:
3743
3743
  _context.next = 80;
3744
- break;
3745
- case 79:
3746
- logger("Cannot create PRIMARY index on ".concat(diff.tableName, ". ColumnName is undefined or empty."), diff.index);
3744
+ return connection.query(primaryKeySQL);
3747
3745
  case 80:
3748
- _context.next = 91;
3746
+ _context.next = 83;
3749
3747
  break;
3750
3748
  case 82:
3749
+ logger("Cannot create PRIMARY index on ".concat(diff.tableName, ". ColumnName is undefined or empty."), diff.index);
3750
+ case 83:
3751
+ _context.next = 94;
3752
+ break;
3753
+ case 85:
3751
3754
  // Create the new index with UNIQUE if applicable
3752
3755
  uniqueSQL = diff.index.Unique ? 'UNIQUE ' : ''; // Check if the index is unique
3753
3756
  if (!(indexColumns.length > 0)) {
3754
- _context.next = 90;
3757
+ _context.next = 93;
3755
3758
  break;
3756
3759
  }
3757
3760
  addIndexSQL = "ALTER TABLE `".concat(diff.tableName, "` ADD ").concat(uniqueSQL, "INDEX `").concat(diff.index.Name, "` (").concat(indexColumns.join(', '), ");");
3758
3761
  console.log("Executing: ".concat(addIndexSQL));
3759
- _context.next = 88;
3760
- return connection.query(addIndexSQL);
3761
- case 88:
3762
3762
  _context.next = 91;
3763
- break;
3764
- case 90:
3765
- logger("Cannot create index ".concat(diff.index.Name, " on ").concat(diff.tableName, ". ColumnName is undefined or empty."), diff.index);
3763
+ return connection.query(addIndexSQL);
3766
3764
  case 91:
3767
- _context.next = 210;
3765
+ _context.next = 94;
3768
3766
  break;
3769
3767
  case 93:
3768
+ logger("Cannot create index ".concat(diff.index.Name, " on ").concat(diff.tableName, ". ColumnName is undefined or empty."), diff.index);
3769
+ case 94:
3770
+ _context.next = 213;
3771
+ break;
3772
+ case 96:
3770
3773
  if (!(diff.type === 'extra_index')) {
3771
- _context.next = 107;
3774
+ _context.next = 110;
3772
3775
  break;
3773
3776
  }
3774
3777
  if (!(diff.index.Name === 'PRIMARY')) {
3775
- _context.next = 101;
3778
+ _context.next = 104;
3776
3779
  break;
3777
3780
  }
3778
3781
  dropPrimaryKeySQL = "ALTER TABLE `".concat(diff.tableName, "` DROP PRIMARY KEY;");
3779
3782
  console.log("Executing: ".concat(dropPrimaryKeySQL));
3780
- _context.next = 99;
3783
+ _context.next = 102;
3781
3784
  return connection.query(dropPrimaryKeySQL);
3782
- case 99:
3783
- _context.next = 105;
3785
+ case 102:
3786
+ _context.next = 108;
3784
3787
  break;
3785
- case 101:
3788
+ case 104:
3786
3789
  dropIndexSQL = "DROP INDEX `".concat(diff.index.Name, "` ON `").concat(diff.tableName, "`;");
3787
3790
  console.log("Executing: ".concat(dropIndexSQL));
3788
- _context.next = 105;
3791
+ _context.next = 108;
3789
3792
  return connection.query(dropIndexSQL);
3790
- case 105:
3791
- _context.next = 210;
3793
+ case 108:
3794
+ _context.next = 213;
3792
3795
  break;
3793
- case 107:
3796
+ case 110:
3794
3797
  if (!(diff.type === 'mismatched_index')) {
3795
- _context.next = 135;
3798
+ _context.next = 138;
3796
3799
  break;
3797
3800
  }
3798
3801
  _tableName3 = diff.tableName, index = diff.index; // Drop the existing index
3799
3802
  if (!(index.Name === 'PRIMARY')) {
3800
- _context.next = 116;
3803
+ _context.next = 119;
3801
3804
  break;
3802
3805
  }
3803
3806
  _dropPrimaryKeySQL = "ALTER TABLE `".concat(_tableName3, "` DROP PRIMARY KEY;");
3804
3807
  console.log("Executing: ".concat(_dropPrimaryKeySQL));
3805
- _context.next = 114;
3808
+ _context.next = 117;
3806
3809
  return connection.query(_dropPrimaryKeySQL);
3807
- case 114:
3808
- _context.next = 120;
3810
+ case 117:
3811
+ _context.next = 123;
3809
3812
  break;
3810
- case 116:
3813
+ case 119:
3811
3814
  _dropIndexSQL = "DROP INDEX `".concat(index.Name, "` ON `").concat(_tableName3, "`;");
3812
3815
  console.log("Executing: ".concat(_dropIndexSQL));
3813
- _context.next = 120;
3816
+ _context.next = 123;
3814
3817
  return connection.query(_dropIndexSQL);
3815
- case 120:
3818
+ case 123:
3816
3819
  console.log('index', index);
3817
3820
 
3818
3821
  // Create the new index
3819
3822
  _uniqueSQL = index.Unique ? 'UNIQUE ' : ''; // Check if the index is unique
3820
3823
  if (!(index.Name === 'PRIMARY')) {
3821
- _context.next = 129;
3824
+ _context.next = 132;
3822
3825
  break;
3823
3826
  }
3824
3827
  addPrimaryKeySQL = "ALTER TABLE `".concat(_tableName3, "` ADD PRIMARY KEY (").concat(index.ColumnName.join(', '), ");");
3825
3828
  console.log("Executing: ".concat(addPrimaryKeySQL));
3826
- _context.next = 127;
3829
+ _context.next = 130;
3827
3830
  return connection.query(addPrimaryKeySQL);
3828
- case 127:
3829
- _context.next = 133;
3831
+ case 130:
3832
+ _context.next = 136;
3830
3833
  break;
3831
- case 129:
3834
+ case 132:
3832
3835
  _addIndexSQL = "ALTER TABLE `".concat(_tableName3, "` ADD ").concat(_uniqueSQL, "INDEX `").concat(index.Name, "` (").concat(index.ColumnName.join(', '), ");"); // Include UNIQUE if applicable
3833
3836
  console.log("Executing: ".concat(_addIndexSQL));
3834
- _context.next = 133;
3837
+ _context.next = 136;
3835
3838
  return connection.query(_addIndexSQL);
3836
- case 133:
3837
- _context.next = 210;
3839
+ case 136:
3840
+ _context.next = 213;
3838
3841
  break;
3839
- case 135:
3842
+ case 138:
3840
3843
  if (!(diff.type === 'missing_procedure' || diff.type === 'mismatched_procedure')) {
3841
- _context.next = 146;
3844
+ _context.next = 149;
3842
3845
  break;
3843
3846
  }
3844
3847
  // Drop the procedure if it exists
3845
3848
  dropCurrentProcSQL = "DROP PROCEDURE IF EXISTS `".concat(diff.Name, "`;");
3846
3849
  console.log("Executing: ".concat(dropCurrentProcSQL));
3847
- _context.next = 140;
3850
+ _context.next = 143;
3848
3851
  return connection.query(dropCurrentProcSQL);
3849
- case 140:
3852
+ case 143:
3850
3853
  // Create the procedure
3851
3854
  createProcSQL = diff.Definition;
3852
3855
  console.log("Executing: ".concat(createProcSQL)); // Log the SQL statement
3853
- _context.next = 144;
3856
+ _context.next = 147;
3854
3857
  return connection.query(createProcSQL);
3855
- case 144:
3856
- _context.next = 210;
3858
+ case 147:
3859
+ _context.next = 213;
3857
3860
  break;
3858
- case 146:
3861
+ case 149:
3859
3862
  if (!(diff.type === 'missing_view')) {
3860
- _context.next = 157;
3863
+ _context.next = 160;
3861
3864
  break;
3862
3865
  }
3863
3866
  dropViewSQL = "DROP VIEW IF EXISTS `".concat(diff.viewName, "`;");
3864
3867
  console.log("Executing: ".concat(dropViewSQL));
3865
- _context.next = 151;
3868
+ _context.next = 154;
3866
3869
  return connection.query(dropViewSQL);
3867
- case 151:
3870
+ case 154:
3868
3871
  createViewSQL = diff.definition; // Use the expected definition to create the view
3869
3872
  console.log("Executing: ".concat(createViewSQL));
3870
- _context.next = 155;
3873
+ _context.next = 158;
3871
3874
  return connection.query(createViewSQL);
3872
- case 155:
3873
- _context.next = 210;
3875
+ case 158:
3876
+ _context.next = 213;
3874
3877
  break;
3875
- case 157:
3878
+ case 160:
3876
3879
  if (!(diff.type === 'mismatched_view')) {
3877
- _context.next = 168;
3880
+ _context.next = 171;
3878
3881
  break;
3879
3882
  }
3880
3883
  _dropViewSQL = "DROP VIEW IF EXISTS `".concat(diff.viewName, "`;");
3881
3884
  console.log("Executing: ".concat(_dropViewSQL));
3882
- _context.next = 162;
3885
+ _context.next = 165;
3883
3886
  return connection.query(_dropViewSQL);
3884
- case 162:
3887
+ case 165:
3885
3888
  _createViewSQL = diff.definition; // Use the expected definition to create the view
3886
3889
  console.log("Executing: ".concat(_createViewSQL));
3887
- _context.next = 166;
3890
+ _context.next = 169;
3888
3891
  return connection.query(_createViewSQL);
3889
- case 166:
3890
- _context.next = 210;
3892
+ case 169:
3893
+ _context.next = 213;
3891
3894
  break;
3892
- case 168:
3895
+ case 171:
3893
3896
  if (!(diff.type === 'extra_view')) {
3894
- _context.next = 175;
3897
+ _context.next = 178;
3895
3898
  break;
3896
3899
  }
3897
3900
  _dropViewSQL2 = "DROP VIEW IF EXISTS `".concat(diff.viewName, "`;");
3898
3901
  console.log("Executing: ".concat(_dropViewSQL2));
3899
- _context.next = 173;
3902
+ _context.next = 176;
3900
3903
  return connection.query(_dropViewSQL2);
3901
- case 173:
3902
- _context.next = 210;
3904
+ case 176:
3905
+ _context.next = 213;
3903
3906
  break;
3904
- case 175:
3907
+ case 178:
3905
3908
  if (!(diff.type === 'missing_trigger')) {
3906
- _context.next = 183;
3909
+ _context.next = 186;
3907
3910
  break;
3908
3911
  }
3909
3912
  createTriggerSQL = diff.trigger.Definition;
3910
3913
  logger('fixing trigger', diff.trigger.Name);
3911
3914
  console.log("Executing: ".concat(createTriggerSQL));
3912
- _context.next = 181;
3915
+ _context.next = 184;
3913
3916
  return connection.query(createTriggerSQL);
3914
- case 181:
3915
- _context.next = 210;
3917
+ case 184:
3918
+ _context.next = 213;
3916
3919
  break;
3917
- case 183:
3920
+ case 186:
3918
3921
  if (!(diff.type === 'mismatched_trigger')) {
3919
- _context.next = 194;
3922
+ _context.next = 197;
3920
3923
  break;
3921
3924
  }
3922
3925
  dropTriggerSQL = "DROP TRIGGER IF EXISTS `".concat(diff.trigger.Name, "`;");
3923
3926
  console.log("Executing: ".concat(dropTriggerSQL));
3924
- _context.next = 188;
3927
+ _context.next = 191;
3925
3928
  return connection.query(dropTriggerSQL);
3926
- case 188:
3929
+ case 191:
3927
3930
  _createTriggerSQL = diff.trigger.Definition;
3928
3931
  console.log("Executing: ".concat(_createTriggerSQL));
3929
- _context.next = 192;
3932
+ _context.next = 195;
3930
3933
  return connection.query(_createTriggerSQL);
3931
- case 192:
3932
- _context.next = 210;
3934
+ case 195:
3935
+ _context.next = 213;
3933
3936
  break;
3934
- case 194:
3937
+ case 197:
3935
3938
  if (!(diff.type === 'extra_trigger')) {
3936
- _context.next = 201;
3939
+ _context.next = 204;
3937
3940
  break;
3938
3941
  }
3939
3942
  _dropTriggerSQL = "DROP TRIGGER IF EXISTS `".concat(diff.trigger.Name, "`;");
3940
3943
  console.log("Executing: ".concat(_dropTriggerSQL));
3941
- _context.next = 199;
3944
+ _context.next = 202;
3942
3945
  return connection.query(_dropTriggerSQL);
3943
- case 199:
3944
- _context.next = 210;
3946
+ case 202:
3947
+ _context.next = 213;
3945
3948
  break;
3946
- case 201:
3949
+ case 204:
3947
3950
  if (!(diff.type === 'missing_database')) {
3948
- _context.next = 209;
3951
+ _context.next = 212;
3949
3952
  break;
3950
3953
  }
3951
3954
  logger('fixing missing database', diff.database);
3952
3955
  _createDatabaseSQL = "CREATE DATABASE `".concat(diff.database, "`;");
3953
3956
  console.log("Executing: ".concat(_createDatabaseSQL));
3954
- _context.next = 207;
3955
- return connection.query(_createDatabaseSQL);
3956
- case 207:
3957
3957
  _context.next = 210;
3958
- break;
3959
- case 209:
3960
- logger("Error applying difference of type:".concat(diff.type), diff);
3958
+ return connection.query(_createDatabaseSQL);
3961
3959
  case 210:
3962
- _context.next = 215;
3960
+ _context.next = 213;
3963
3961
  break;
3964
3962
  case 212:
3965
- _context.prev = 212;
3963
+ logger("Error applying difference of type:".concat(diff.type), diff);
3964
+ case 213:
3965
+ _context.next = 218;
3966
+ break;
3967
+ case 215:
3968
+ _context.prev = 215;
3966
3969
  _context.t0 = _context["catch"](1);
3967
3970
  logger("Error applying difference of type:".concat(diff.type, ". Error: ").concat(_context.t0.message), diff);
3968
- case 215:
3971
+ case 218:
3969
3972
  case "end":
3970
3973
  return _context.stop();
3971
3974
  }
3972
- }, _loop, null, [[1, 212]]);
3975
+ }, _loop, null, [[1, 215]]);
3973
3976
  });
3974
3977
  _iterator.s();
3975
3978
  case 5:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mysqlsyncr",
3
- "version": "1.1.2",
3
+ "version": "1.1.4",
4
4
  "description": "",
5
5
  "main": "dist/index.cjs.js",
6
6
  "module": "dist/index.esm.js",
package/src/apply.js CHANGED
@@ -83,16 +83,24 @@ export const applyDifferences = async (connection, database, differences) => {
83
83
 
84
84
  } else if (diff.type === 'mismatched_field') {
85
85
  const { tableName, field } = diff;
86
+ const isTimestamp = field.Type.toLowerCase().includes('timestamp');
87
+
88
+ // Handle timestamp specific attributes
86
89
  const defaultClause = field.Default === 'CURRENT_TIMESTAMP'
87
- ? 'DEFAULT CURRENT_TIMESTAMP'
90
+ ? 'DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'
88
91
  : field.Default === 'NULL'
89
- ? 'DEFAULT NULL'
92
+ ? isTimestamp ? 'NULL DEFAULT NULL' : 'DEFAULT NULL'
90
93
  : field.Default !== undefined && field.Default !== null
91
94
  ? `DEFAULT '${field.Default}'`
92
- : 'DEFAULT NULL';
95
+ : isTimestamp ? 'NULL DEFAULT NULL' : 'DEFAULT NULL';
93
96
 
94
- const dropFieldSQL = `ALTER TABLE \`${tableName}\` CHANGE \`${field.Field}\` \`${field.Field}\` ${field.Type} ${field.NotNull ? 'NOT NULL' : ''} ${defaultClause};`;
95
- console.log(`Executing: ${dropFieldSQL}`);
97
+ // Remove ON UPDATE CURRENT_TIMESTAMP if we're changing away from CURRENT_TIMESTAMP
98
+ const currentField = diff.currentField;
99
+ const hasOnUpdate = currentField?.Default === 'CURRENT_TIMESTAMP' &&
100
+ field.Default !== 'CURRENT_TIMESTAMP';
101
+
102
+ const dropFieldSQL = `ALTER TABLE \`${tableName}\` CHANGE \`${field.Field}\` \`${field.Field}\` ${field.Type} ${field.NotNull ? 'NOT NULL' : ''} ${defaultClause}${hasOnUpdate ? ' ON UPDATE CURRENT_TIMESTAMP' : ''};`;
103
+ logger(`Executing: ${dropFieldSQL}`);
96
104
  await connection.query(dropFieldSQL);
97
105
 
98
106
  } else if (diff.type === 'missing_index') {