mysqlsyncr 1.1.0 → 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:
@@ -3610,7 +3610,7 @@ var applyDifferences = /*#__PURE__*/function () {
3610
3610
  break;
3611
3611
  }
3612
3612
  createDatabaseSQL = "CREATE DATABASE `".concat(differences[0].database, "`;");
3613
- logger("Executing: ".concat(createDatabaseSQL));
3613
+ console.log("Executing: ".concat(createDatabaseSQL));
3614
3614
  _context.next = 7;
3615
3615
  return connection.query(createDatabaseSQL);
3616
3616
  case 7:
@@ -3625,11 +3625,11 @@ var applyDifferences = /*#__PURE__*/function () {
3625
3625
  }
3626
3626
  tableName = diff.tableName, createSQL = diff.createSQL;
3627
3627
  logger("Creating missing table ".concat(tableName));
3628
- logger("Executing: ".concat(createSQL));
3628
+ console.log("Executing: ".concat(createSQL));
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')) {
@@ -3639,11 +3639,11 @@ var applyDifferences = /*#__PURE__*/function () {
3639
3639
  _tableName = diff.tableName;
3640
3640
  logger("Dropping extra table ".concat(_tableName));
3641
3641
  dropTableSQL = "DROP TABLE `".concat(_tableName, "`;");
3642
- logger("Executing: ".concat(dropTableSQL));
3642
+ console.log("Executing: ".concat(dropTableSQL));
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')) {
@@ -3652,7 +3652,7 @@ var applyDifferences = /*#__PURE__*/function () {
3652
3652
  }
3653
3653
  // Step 1: Add column with DEFAULT NULL to handle existing records
3654
3654
  addFieldSQL = "ALTER TABLE `".concat(diff.tableName, "` ADD `").concat(diff.field.Field, "` ").concat(diff.field.Type, " DEFAULT NULL;");
3655
- logger("Executing: ".concat(addFieldSQL));
3655
+ console.log("Executing: ".concat(addFieldSQL));
3656
3656
  _context.next = 32;
3657
3657
  return connection.query(addFieldSQL);
3658
3658
  case 32:
@@ -3661,7 +3661,7 @@ var applyDifferences = /*#__PURE__*/function () {
3661
3661
  break;
3662
3662
  }
3663
3663
  modifyFieldSQL = "ALTER TABLE `".concat(diff.tableName, "` MODIFY `").concat(diff.field.Field, "` ").concat(diff.field.Type, " NOT NULL;");
3664
- logger("Executing: ".concat(modifyFieldSQL));
3664
+ console.log("Executing: ".concat(modifyFieldSQL));
3665
3665
  _context.next = 37;
3666
3666
  return connection.query(modifyFieldSQL);
3667
3667
  case 37:
@@ -3671,11 +3671,11 @@ var applyDifferences = /*#__PURE__*/function () {
3671
3671
  }
3672
3672
  defaultClause = diff.field.Default === 'CURRENT_TIMESTAMP' ? 'DEFAULT CURRENT_TIMESTAMP' : diff.field.Default !== undefined && diff.field.Default !== null ? "DEFAULT '".concat(diff.field.Default, "'") : '';
3673
3673
  _modifyFieldSQL = "ALTER TABLE `".concat(diff.tableName, "` MODIFY `").concat(diff.field.Field, "` ").concat(diff.field.Type, " ").concat(defaultClause, ";");
3674
- logger("Executing: ".concat(_modifyFieldSQL));
3674
+ console.log("Executing: ".concat(_modifyFieldSQL));
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')) {
@@ -3683,34 +3683,37 @@ var applyDifferences = /*#__PURE__*/function () {
3683
3683
  break;
3684
3684
  }
3685
3685
  dropFieldSQL = "ALTER TABLE `".concat(diff.tableName, "` DROP COLUMN `").concat(diff.field.Field, "`;");
3686
- logger("Executing: ".concat(dropFieldSQL));
3686
+ console.log("Executing: ".concat(dropFieldSQL));
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, ";");
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' : '', ";");
3700
3703
  logger("Executing: ".concat(_dropFieldSQL));
3701
- _context.next = 59;
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
- logger("Executing: ".concat(primaryKeySQL));
3743
- _context.next = 77;
3744
- return connection.query(primaryKeySQL);
3745
- case 77:
3745
+ console.log("Executing: ".concat(primaryKeySQL));
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
- logger("Executing: ".concat(addIndexSQL));
3762
- _context.next = 88;
3763
- return connection.query(addIndexSQL);
3764
- case 88:
3764
+ console.log("Executing: ".concat(addIndexSQL));
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
- logger("Executing: ".concat(dropPrimaryKeySQL));
3783
- _context.next = 99;
3785
+ console.log("Executing: ".concat(dropPrimaryKeySQL));
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
- logger("Executing: ".concat(dropIndexSQL));
3791
- _context.next = 105;
3793
+ console.log("Executing: ".concat(dropIndexSQL));
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
- logger("Executing: ".concat(_dropPrimaryKeySQL));
3808
- _context.next = 114;
3810
+ console.log("Executing: ".concat(_dropPrimaryKeySQL));
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
- logger("Executing: ".concat(_dropIndexSQL));
3816
- _context.next = 120;
3818
+ console.log("Executing: ".concat(_dropIndexSQL));
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
- logger("Executing: ".concat(addPrimaryKeySQL));
3829
- _context.next = 127;
3831
+ console.log("Executing: ".concat(addPrimaryKeySQL));
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
- logger("Executing: ".concat(_addIndexSQL));
3837
- _context.next = 133;
3839
+ console.log("Executing: ".concat(_addIndexSQL));
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
- logger("Executing: ".concat(dropCurrentProcSQL));
3850
- _context.next = 140;
3852
+ console.log("Executing: ".concat(dropCurrentProcSQL));
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
- logger("Executing: ".concat(createProcSQL)); // Log the SQL statement
3856
- _context.next = 144;
3858
+ console.log("Executing: ".concat(createProcSQL)); // Log the SQL statement
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
- logger("Executing: ".concat(dropViewSQL));
3868
- _context.next = 151;
3870
+ console.log("Executing: ".concat(dropViewSQL));
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
- logger("Executing: ".concat(createViewSQL));
3873
- _context.next = 155;
3875
+ console.log("Executing: ".concat(createViewSQL));
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
- logger("Executing: ".concat(_dropViewSQL));
3885
- _context.next = 162;
3887
+ console.log("Executing: ".concat(_dropViewSQL));
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
- logger("Executing: ".concat(_createViewSQL));
3890
- _context.next = 166;
3892
+ console.log("Executing: ".concat(_createViewSQL));
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
- logger("Executing: ".concat(_dropViewSQL2));
3902
- _context.next = 173;
3904
+ console.log("Executing: ".concat(_dropViewSQL2));
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
- logger("Executing: ".concat(createTriggerSQL));
3915
- _context.next = 181;
3917
+ console.log("Executing: ".concat(createTriggerSQL));
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
- logger("Executing: ".concat(dropTriggerSQL));
3927
- _context.next = 188;
3929
+ console.log("Executing: ".concat(dropTriggerSQL));
3930
+ _context.next = 191;
3928
3931
  return connection.query(dropTriggerSQL);
3929
- case 188:
3932
+ case 191:
3930
3933
  _createTriggerSQL = diff.trigger.Definition;
3931
- logger("Executing: ".concat(_createTriggerSQL));
3932
- _context.next = 192;
3934
+ console.log("Executing: ".concat(_createTriggerSQL));
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
- logger("Executing: ".concat(_dropTriggerSQL));
3944
- _context.next = 199;
3946
+ console.log("Executing: ".concat(_dropTriggerSQL));
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
- logger("Executing: ".concat(_createDatabaseSQL));
3957
- _context.next = 207;
3958
- return connection.query(_createDatabaseSQL);
3959
- case 207:
3959
+ console.log("Executing: ".concat(_createDatabaseSQL));
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:
@@ -3607,7 +3607,7 @@ var applyDifferences = /*#__PURE__*/function () {
3607
3607
  break;
3608
3608
  }
3609
3609
  createDatabaseSQL = "CREATE DATABASE `".concat(differences[0].database, "`;");
3610
- logger("Executing: ".concat(createDatabaseSQL));
3610
+ console.log("Executing: ".concat(createDatabaseSQL));
3611
3611
  _context.next = 7;
3612
3612
  return connection.query(createDatabaseSQL);
3613
3613
  case 7:
@@ -3622,11 +3622,11 @@ var applyDifferences = /*#__PURE__*/function () {
3622
3622
  }
3623
3623
  tableName = diff.tableName, createSQL = diff.createSQL;
3624
3624
  logger("Creating missing table ".concat(tableName));
3625
- logger("Executing: ".concat(createSQL));
3625
+ console.log("Executing: ".concat(createSQL));
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')) {
@@ -3636,11 +3636,11 @@ var applyDifferences = /*#__PURE__*/function () {
3636
3636
  _tableName = diff.tableName;
3637
3637
  logger("Dropping extra table ".concat(_tableName));
3638
3638
  dropTableSQL = "DROP TABLE `".concat(_tableName, "`;");
3639
- logger("Executing: ".concat(dropTableSQL));
3639
+ console.log("Executing: ".concat(dropTableSQL));
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')) {
@@ -3649,7 +3649,7 @@ var applyDifferences = /*#__PURE__*/function () {
3649
3649
  }
3650
3650
  // Step 1: Add column with DEFAULT NULL to handle existing records
3651
3651
  addFieldSQL = "ALTER TABLE `".concat(diff.tableName, "` ADD `").concat(diff.field.Field, "` ").concat(diff.field.Type, " DEFAULT NULL;");
3652
- logger("Executing: ".concat(addFieldSQL));
3652
+ console.log("Executing: ".concat(addFieldSQL));
3653
3653
  _context.next = 32;
3654
3654
  return connection.query(addFieldSQL);
3655
3655
  case 32:
@@ -3658,7 +3658,7 @@ var applyDifferences = /*#__PURE__*/function () {
3658
3658
  break;
3659
3659
  }
3660
3660
  modifyFieldSQL = "ALTER TABLE `".concat(diff.tableName, "` MODIFY `").concat(diff.field.Field, "` ").concat(diff.field.Type, " NOT NULL;");
3661
- logger("Executing: ".concat(modifyFieldSQL));
3661
+ console.log("Executing: ".concat(modifyFieldSQL));
3662
3662
  _context.next = 37;
3663
3663
  return connection.query(modifyFieldSQL);
3664
3664
  case 37:
@@ -3668,11 +3668,11 @@ var applyDifferences = /*#__PURE__*/function () {
3668
3668
  }
3669
3669
  defaultClause = diff.field.Default === 'CURRENT_TIMESTAMP' ? 'DEFAULT CURRENT_TIMESTAMP' : diff.field.Default !== undefined && diff.field.Default !== null ? "DEFAULT '".concat(diff.field.Default, "'") : '';
3670
3670
  _modifyFieldSQL = "ALTER TABLE `".concat(diff.tableName, "` MODIFY `").concat(diff.field.Field, "` ").concat(diff.field.Type, " ").concat(defaultClause, ";");
3671
- logger("Executing: ".concat(_modifyFieldSQL));
3671
+ console.log("Executing: ".concat(_modifyFieldSQL));
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')) {
@@ -3680,34 +3680,37 @@ var applyDifferences = /*#__PURE__*/function () {
3680
3680
  break;
3681
3681
  }
3682
3682
  dropFieldSQL = "ALTER TABLE `".concat(diff.tableName, "` DROP COLUMN `").concat(diff.field.Field, "`;");
3683
- logger("Executing: ".concat(dropFieldSQL));
3683
+ console.log("Executing: ".concat(dropFieldSQL));
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, ";");
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' : '', ";");
3697
3700
  logger("Executing: ".concat(_dropFieldSQL));
3698
- _context.next = 59;
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
- logger("Executing: ".concat(primaryKeySQL));
3740
- _context.next = 77;
3741
- return connection.query(primaryKeySQL);
3742
- case 77:
3742
+ console.log("Executing: ".concat(primaryKeySQL));
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
- logger("Executing: ".concat(addIndexSQL));
3759
- _context.next = 88;
3760
- return connection.query(addIndexSQL);
3761
- case 88:
3761
+ console.log("Executing: ".concat(addIndexSQL));
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
- logger("Executing: ".concat(dropPrimaryKeySQL));
3780
- _context.next = 99;
3782
+ console.log("Executing: ".concat(dropPrimaryKeySQL));
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
- logger("Executing: ".concat(dropIndexSQL));
3788
- _context.next = 105;
3790
+ console.log("Executing: ".concat(dropIndexSQL));
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
- logger("Executing: ".concat(_dropPrimaryKeySQL));
3805
- _context.next = 114;
3807
+ console.log("Executing: ".concat(_dropPrimaryKeySQL));
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
- logger("Executing: ".concat(_dropIndexSQL));
3813
- _context.next = 120;
3815
+ console.log("Executing: ".concat(_dropIndexSQL));
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
- logger("Executing: ".concat(addPrimaryKeySQL));
3826
- _context.next = 127;
3828
+ console.log("Executing: ".concat(addPrimaryKeySQL));
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
- logger("Executing: ".concat(_addIndexSQL));
3834
- _context.next = 133;
3836
+ console.log("Executing: ".concat(_addIndexSQL));
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
- logger("Executing: ".concat(dropCurrentProcSQL));
3847
- _context.next = 140;
3849
+ console.log("Executing: ".concat(dropCurrentProcSQL));
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
- logger("Executing: ".concat(createProcSQL)); // Log the SQL statement
3853
- _context.next = 144;
3855
+ console.log("Executing: ".concat(createProcSQL)); // Log the SQL statement
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
- logger("Executing: ".concat(dropViewSQL));
3865
- _context.next = 151;
3867
+ console.log("Executing: ".concat(dropViewSQL));
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
- logger("Executing: ".concat(createViewSQL));
3870
- _context.next = 155;
3872
+ console.log("Executing: ".concat(createViewSQL));
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
- logger("Executing: ".concat(_dropViewSQL));
3882
- _context.next = 162;
3884
+ console.log("Executing: ".concat(_dropViewSQL));
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
- logger("Executing: ".concat(_createViewSQL));
3887
- _context.next = 166;
3889
+ console.log("Executing: ".concat(_createViewSQL));
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
- logger("Executing: ".concat(_dropViewSQL2));
3899
- _context.next = 173;
3901
+ console.log("Executing: ".concat(_dropViewSQL2));
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
- logger("Executing: ".concat(createTriggerSQL));
3912
- _context.next = 181;
3914
+ console.log("Executing: ".concat(createTriggerSQL));
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
- logger("Executing: ".concat(dropTriggerSQL));
3924
- _context.next = 188;
3926
+ console.log("Executing: ".concat(dropTriggerSQL));
3927
+ _context.next = 191;
3925
3928
  return connection.query(dropTriggerSQL);
3926
- case 188:
3929
+ case 191:
3927
3930
  _createTriggerSQL = diff.trigger.Definition;
3928
- logger("Executing: ".concat(_createTriggerSQL));
3929
- _context.next = 192;
3931
+ console.log("Executing: ".concat(_createTriggerSQL));
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
- logger("Executing: ".concat(_dropTriggerSQL));
3941
- _context.next = 199;
3943
+ console.log("Executing: ".concat(_dropTriggerSQL));
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
- logger("Executing: ".concat(_createDatabaseSQL));
3954
- _context.next = 207;
3955
- return connection.query(_createDatabaseSQL);
3956
- case 207:
3956
+ console.log("Executing: ".concat(_createDatabaseSQL));
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.0",
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
@@ -33,7 +33,7 @@ export const applyDifferences = async (connection, database, differences) => {
33
33
 
34
34
  if(diff.type == 'missing_database'){
35
35
  const createDatabaseSQL = `CREATE DATABASE \`${differences[0].database}\`;`;
36
- logger(`Executing: ${createDatabaseSQL}`);
36
+ console.log(`Executing: ${createDatabaseSQL}`);
37
37
  await connection.query(createDatabaseSQL);
38
38
  continue;
39
39
  }
@@ -43,26 +43,26 @@ export const applyDifferences = async (connection, database, differences) => {
43
43
  if (diff.type === 'missing_table') {
44
44
  const { tableName, createSQL } = diff;
45
45
  logger(`Creating missing table ${tableName}`);
46
- logger(`Executing: ${createSQL}`);
46
+ console.log(`Executing: ${createSQL}`);
47
47
  await connection.query(createSQL);
48
48
 
49
49
  } else if (diff.type === 'extra_table') {
50
50
  const { tableName } = diff;
51
51
  logger(`Dropping extra table ${tableName}`);
52
52
  const dropTableSQL = `DROP TABLE \`${tableName}\`;`;
53
- logger(`Executing: ${dropTableSQL}`);
53
+ console.log(`Executing: ${dropTableSQL}`);
54
54
  await connection.query(dropTableSQL);
55
55
 
56
56
  } else if (diff.type === 'missing_field') {
57
57
  // Step 1: Add column with DEFAULT NULL to handle existing records
58
58
  const addFieldSQL = `ALTER TABLE \`${diff.tableName}\` ADD \`${diff.field.Field}\` ${diff.field.Type} DEFAULT NULL;`;
59
- logger(`Executing: ${addFieldSQL}`);
59
+ console.log(`Executing: ${addFieldSQL}`);
60
60
  await connection.query(addFieldSQL);
61
61
 
62
62
  // Step 2: If NOT NULL is required and no default was specified, modify the column to remove DEFAULT NULL
63
63
  if (diff.field.NotNull && !diff.field.Default) {
64
64
  const modifyFieldSQL = `ALTER TABLE \`${diff.tableName}\` MODIFY \`${diff.field.Field}\` ${diff.field.Type} NOT NULL;`;
65
- logger(`Executing: ${modifyFieldSQL}`);
65
+ console.log(`Executing: ${modifyFieldSQL}`);
66
66
  await connection.query(modifyFieldSQL);
67
67
  }
68
68
  if(diff.field.Default){
@@ -72,26 +72,34 @@ export const applyDifferences = async (connection, database, differences) => {
72
72
  ? `DEFAULT '${diff.field.Default}'`
73
73
  : '';
74
74
  const modifyFieldSQL = `ALTER TABLE \`${diff.tableName}\` MODIFY \`${diff.field.Field}\` ${diff.field.Type} ${defaultClause};`;
75
- logger(`Executing: ${modifyFieldSQL}`);
75
+ console.log(`Executing: ${modifyFieldSQL}`);
76
76
  await connection.query(modifyFieldSQL);
77
77
  }
78
78
 
79
79
  } else if (diff.type === 'extra_field') {
80
80
  const dropFieldSQL = `ALTER TABLE \`${diff.tableName}\` DROP COLUMN \`${diff.field.Field}\`;`;
81
- logger(`Executing: ${dropFieldSQL}`);
81
+ console.log(`Executing: ${dropFieldSQL}`);
82
82
  await connection.query(dropFieldSQL);
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';
96
+
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';
93
101
 
94
- const dropFieldSQL = `ALTER TABLE \`${tableName}\` CHANGE \`${field.Field}\` \`${field.Field}\` ${field.Type} ${field.NotNull ? 'NOT NULL' : ''} ${defaultClause};`;
102
+ const dropFieldSQL = `ALTER TABLE \`${tableName}\` CHANGE \`${field.Field}\` \`${field.Field}\` ${field.Type} ${field.NotNull ? 'NOT NULL' : ''} ${defaultClause}${hasOnUpdate ? ' ON UPDATE CURRENT_TIMESTAMP' : ''};`;
95
103
  logger(`Executing: ${dropFieldSQL}`);
96
104
  await connection.query(dropFieldSQL);
97
105
 
@@ -114,7 +122,7 @@ export const applyDifferences = async (connection, database, differences) => {
114
122
  if (diff.index.Name === 'PRIMARY') {
115
123
  if (indexColumns.length > 0) {
116
124
  const primaryKeySQL = `ALTER TABLE \`${diff.tableName}\` ADD PRIMARY KEY (${indexColumns.join(', ')});`;
117
- logger(`Executing: ${primaryKeySQL}`);
125
+ console.log(`Executing: ${primaryKeySQL}`);
118
126
  await connection.query(primaryKeySQL);
119
127
  } else {
120
128
  logger(`Cannot create PRIMARY index on ${diff.tableName}. ColumnName is undefined or empty.`, diff.index);
@@ -124,7 +132,7 @@ export const applyDifferences = async (connection, database, differences) => {
124
132
  const uniqueSQL = diff.index.Unique ? 'UNIQUE ' : ''; // Check if the index is unique
125
133
  if (indexColumns.length > 0) {
126
134
  const addIndexSQL = `ALTER TABLE \`${diff.tableName}\` ADD ${uniqueSQL}INDEX \`${diff.index.Name}\` (${indexColumns.join(', ')});`;
127
- logger(`Executing: ${addIndexSQL}`);
135
+ console.log(`Executing: ${addIndexSQL}`);
128
136
  await connection.query(addIndexSQL);
129
137
  } else {
130
138
  logger(`Cannot create index ${diff.index.Name} on ${diff.tableName}. ColumnName is undefined or empty.`, diff.index);
@@ -135,11 +143,11 @@ export const applyDifferences = async (connection, database, differences) => {
135
143
  // Handle dropping of extra indexes
136
144
  if (diff.index.Name === 'PRIMARY') {
137
145
  const dropPrimaryKeySQL = `ALTER TABLE \`${diff.tableName}\` DROP PRIMARY KEY;`;
138
- logger(`Executing: ${dropPrimaryKeySQL}`);
146
+ console.log(`Executing: ${dropPrimaryKeySQL}`);
139
147
  await connection.query(dropPrimaryKeySQL);
140
148
  } else {
141
149
  const dropIndexSQL = `DROP INDEX \`${diff.index.Name}\` ON \`${diff.tableName}\`;`;
142
- logger(`Executing: ${dropIndexSQL}`);
150
+ console.log(`Executing: ${dropIndexSQL}`);
143
151
  await connection.query(dropIndexSQL);
144
152
  }
145
153
 
@@ -148,11 +156,11 @@ export const applyDifferences = async (connection, database, differences) => {
148
156
  // Drop the existing index
149
157
  if (index.Name === 'PRIMARY') {
150
158
  const dropPrimaryKeySQL = `ALTER TABLE \`${tableName}\` DROP PRIMARY KEY;`;
151
- logger(`Executing: ${dropPrimaryKeySQL}`);
159
+ console.log(`Executing: ${dropPrimaryKeySQL}`);
152
160
  await connection.query(dropPrimaryKeySQL);
153
161
  } else {
154
162
  const dropIndexSQL = `DROP INDEX \`${index.Name}\` ON \`${tableName}\`;`;
155
- logger(`Executing: ${dropIndexSQL}`);
163
+ console.log(`Executing: ${dropIndexSQL}`);
156
164
  await connection.query(dropIndexSQL);
157
165
  }
158
166
  console.log('index', index)
@@ -161,11 +169,11 @@ export const applyDifferences = async (connection, database, differences) => {
161
169
  const uniqueSQL = index.Unique ? 'UNIQUE ' : ''; // Check if the index is unique
162
170
  if (index.Name === 'PRIMARY') {
163
171
  const addPrimaryKeySQL = `ALTER TABLE \`${tableName}\` ADD PRIMARY KEY (${index.ColumnName.join(', ')});`;
164
- logger(`Executing: ${addPrimaryKeySQL}`);
172
+ console.log(`Executing: ${addPrimaryKeySQL}`);
165
173
  await connection.query(addPrimaryKeySQL);
166
174
  } else {
167
175
  const addIndexSQL = `ALTER TABLE \`${tableName}\` ADD ${uniqueSQL}INDEX \`${index.Name}\` (${index.ColumnName.join(', ')});`; // Include UNIQUE if applicable
168
- logger(`Executing: ${addIndexSQL}`);
176
+ console.log(`Executing: ${addIndexSQL}`);
169
177
  await connection.query(addIndexSQL);
170
178
  }
171
179
 
@@ -173,65 +181,65 @@ export const applyDifferences = async (connection, database, differences) => {
173
181
 
174
182
  // Drop the procedure if it exists
175
183
  const dropCurrentProcSQL = `DROP PROCEDURE IF EXISTS \`${diff.Name}\`;`;
176
- logger(`Executing: ${dropCurrentProcSQL}`);
184
+ console.log(`Executing: ${dropCurrentProcSQL}`);
177
185
  await connection.query(dropCurrentProcSQL);
178
186
 
179
187
  // Create the procedure
180
188
  const createProcSQL = diff.Definition;
181
- logger(`Executing: ${createProcSQL}`); // Log the SQL statement
189
+ console.log(`Executing: ${createProcSQL}`); // Log the SQL statement
182
190
  await connection.query(createProcSQL);
183
191
 
184
192
 
185
193
 
186
194
  } else if (diff.type === 'missing_view') {
187
195
  const dropViewSQL = `DROP VIEW IF EXISTS \`${diff.viewName}\`;`;
188
- logger(`Executing: ${dropViewSQL}`);
196
+ console.log(`Executing: ${dropViewSQL}`);
189
197
  await connection.query(dropViewSQL);
190
198
  const createViewSQL = diff.definition; // Use the expected definition to create the view
191
- logger(`Executing: ${createViewSQL}`);
199
+ console.log(`Executing: ${createViewSQL}`);
192
200
  await connection.query(createViewSQL);
193
201
 
194
202
 
195
203
  } else if (diff.type === 'mismatched_view') {
196
204
  const dropViewSQL = `DROP VIEW IF EXISTS \`${diff.viewName}\`;`;
197
- logger(`Executing: ${dropViewSQL}`);
205
+ console.log(`Executing: ${dropViewSQL}`);
198
206
  await connection.query(dropViewSQL);
199
207
  const createViewSQL = diff.definition; // Use the expected definition to create the view
200
- logger(`Executing: ${createViewSQL}`);
208
+ console.log(`Executing: ${createViewSQL}`);
201
209
  await connection.query(createViewSQL);
202
210
 
203
211
 
204
212
  } else if (diff.type === 'extra_view') {
205
213
  const dropViewSQL = `DROP VIEW IF EXISTS \`${diff.viewName}\`;`;
206
- logger(`Executing: ${dropViewSQL}`);
214
+ console.log(`Executing: ${dropViewSQL}`);
207
215
  await connection.query(dropViewSQL);
208
216
 
209
217
 
210
218
  } else if (diff.type === 'missing_trigger') {
211
219
  const createTriggerSQL = diff.trigger.Definition;
212
220
  logger('fixing trigger', diff.trigger.Name)
213
- logger(`Executing: ${createTriggerSQL}`);
221
+ console.log(`Executing: ${createTriggerSQL}`);
214
222
  await connection.query(createTriggerSQL);
215
223
 
216
224
 
217
225
  } else if (diff.type === 'mismatched_trigger') {
218
226
  const dropTriggerSQL = `DROP TRIGGER IF EXISTS \`${diff.trigger.Name}\`;`;
219
- logger(`Executing: ${dropTriggerSQL}`);
227
+ console.log(`Executing: ${dropTriggerSQL}`);
220
228
  await connection.query(dropTriggerSQL);
221
229
  const createTriggerSQL = diff.trigger.Definition;
222
- logger(`Executing: ${createTriggerSQL}`);
230
+ console.log(`Executing: ${createTriggerSQL}`);
223
231
  await connection.query(createTriggerSQL);
224
232
 
225
233
 
226
234
  } else if (diff.type === 'extra_trigger') {
227
235
  const dropTriggerSQL = `DROP TRIGGER IF EXISTS \`${diff.trigger.Name}\`;`;
228
- logger(`Executing: ${dropTriggerSQL}`);
236
+ console.log(`Executing: ${dropTriggerSQL}`);
229
237
  await connection.query(dropTriggerSQL);
230
238
 
231
239
  }else if(diff.type === 'missing_database'){
232
240
  logger('fixing missing database', diff.database)
233
241
  const createDatabaseSQL = `CREATE DATABASE \`${diff.database}\`;`;
234
- logger(`Executing: ${createDatabaseSQL}`);
242
+ console.log(`Executing: ${createDatabaseSQL}`);
235
243
  await connection.query(createDatabaseSQL);
236
244
  } else {
237
245
  logger(`Error applying difference of type:${diff.type}`, diff)