mysqlsyncr 1.1.6 → 1.1.10

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/src/apply.js CHANGED
@@ -83,100 +83,102 @@ export const applyDifferences = async (connection, database, differences) => {
83
83
 
84
84
  } else if (diff.type === 'mismatched_field') {
85
85
  const { tableName, field, currentField } = diff;
86
+
87
+ // use alter table rather than adding fields
86
88
 
87
- // Helper function to determine if we need batch processing
88
- const needsBatchProcessing = () => {
89
- const currentType = currentField.Type.toLowerCase();
90
- const newType = field.Type.toLowerCase();
89
+ // // Helper function to determine if we need batch processing
90
+ // const needsBatchProcessing = () => {
91
+ // const currentType = currentField.Type.toLowerCase();
92
+ // const newType = field.Type.toLowerCase();
91
93
 
92
- // Decimal changes
93
- if (currentType.includes('decimal') && newType.includes('decimal')) {
94
- return true;
95
- }
94
+ // // Decimal changes
95
+ // if (currentType.includes('decimal') && newType.includes('decimal')) {
96
+ // return true;
97
+ // }
96
98
 
97
- // VARCHAR/CHAR length reductions
98
- if ((currentType.includes('varchar') || currentType.includes('char')) &&
99
- (newType.includes('varchar') || newType.includes('char'))) {
100
- const currentLength = parseInt(currentType.match(/\((\d+)\)/)?.[1] || '0');
101
- const newLength = parseInt(newType.match(/\((\d+)\)/)?.[1] || '0');
102
- return newLength < currentLength;
103
- }
99
+ // // VARCHAR/CHAR length reductions
100
+ // if ((currentType.includes('varchar') || currentType.includes('char')) &&
101
+ // (newType.includes('varchar') || newType.includes('char'))) {
102
+ // const currentLength = parseInt(currentType.match(/\((\d+)\)/)?.[1] || '0');
103
+ // const newLength = parseInt(newType.match(/\((\d+)\)/)?.[1] || '0');
104
+ // return newLength < currentLength;
105
+ // }
104
106
 
105
- // TEXT to VARCHAR conversions
106
- if (currentType.includes('text') && newType.includes('varchar')) {
107
- return true;
108
- }
107
+ // // TEXT to VARCHAR conversions
108
+ // if (currentType.includes('text') && newType.includes('varchar')) {
109
+ // return true;
110
+ // }
109
111
 
110
- // Numeric type downgrades
111
- const numericDowngrades = {
112
- 'bigint': ['int', 'mediumint', 'smallint', 'tinyint'],
113
- 'int': ['mediumint', 'smallint', 'tinyint'],
114
- 'mediumint': ['smallint', 'tinyint'],
115
- 'smallint': ['tinyint'],
116
- 'double': ['float', 'decimal'],
117
- 'float': ['decimal']
118
- };
112
+ // // Numeric type downgrades
113
+ // const numericDowngrades = {
114
+ // 'bigint': ['int', 'mediumint', 'smallint', 'tinyint'],
115
+ // 'int': ['mediumint', 'smallint', 'tinyint'],
116
+ // 'mediumint': ['smallint', 'tinyint'],
117
+ // 'smallint': ['tinyint'],
118
+ // 'double': ['float', 'decimal'],
119
+ // 'float': ['decimal']
120
+ // };
119
121
 
120
- for (const [larger, smaller] of Object.entries(numericDowngrades)) {
121
- if (currentType.includes(larger) && smaller.some(t => newType.includes(t))) {
122
- return true;
123
- }
124
- }
122
+ // for (const [larger, smaller] of Object.entries(numericDowngrades)) {
123
+ // if (currentType.includes(larger) && smaller.some(t => newType.includes(t))) {
124
+ // return true;
125
+ // }
126
+ // }
125
127
 
126
- return false;
127
- };
128
-
129
- if (needsBatchProcessing()) {
130
- // Step 1: Add new column
131
- const addColumnSQL = `ALTER TABLE \`${tableName}\` ADD COLUMN \`${field.Field}_new\` ${field.Type} DEFAULT NULL;`;
132
- logger(`Adding new column for type conversion: ${addColumnSQL}`);
133
- await connection.query(addColumnSQL);
134
-
135
- // Step 2: Copy data in batches
136
- let totalUpdated = 0;
137
- let batchSize = 10000;
138
- let batchCount = 0;
128
+ // return false;
129
+ // };
130
+
131
+ // if (needsBatchProcessing()) {
132
+ // // Step 1: Add new column
133
+ // const addColumnSQL = `ALTER TABLE \`${tableName}\` ADD COLUMN \`${field.Field}_new\` ${field.Type} DEFAULT NULL;`;
134
+ // console.log(`Adding new column for type conversion: ${addColumnSQL}`);
135
+ // await connection.query(addColumnSQL);
136
+
137
+ // // Step 2: Copy data in batches
138
+ // let totalUpdated = 0;
139
+ // let batchSize = 10000;
140
+ // let batchCount = 0;
139
141
 
140
- while (true) {
141
- const updateSQL = `
142
- UPDATE \`${tableName}\`
143
- SET \`${field.Field}_new\` = \`${field.Field}\`
144
- WHERE \`${field.Field}_new\` IS NULL
145
- AND \`${field.Field}\` IS NOT NULL
146
- LIMIT ${batchSize};
147
- `;
148
- const result = await connection.query(updateSQL);
149
- const rowsAffected = result.affectedRows;
142
+ // while (true) {
143
+ // const updateSQL = `
144
+ // UPDATE \`${tableName}\`
145
+ // SET \`${field.Field}_new\` = \`${field.Field}\`
146
+ // WHERE \`${field.Field}_new\` IS NULL
147
+ // AND \`${field.Field}\` IS NOT NULL
148
+ // LIMIT ${batchSize};
149
+ // `;
150
+ // const result = await connection.query(updateSQL);
151
+ // const rowsAffected = result.affectedRows;
150
152
 
151
- if (rowsAffected === 0) break;
153
+ // if (rowsAffected === 0) break;
152
154
 
153
- totalUpdated += rowsAffected;
154
- batchCount++;
155
- logger(`Batch ${batchCount}: Converted ${rowsAffected} rows. Total converted: ${totalUpdated}`);
156
- }
157
-
158
- // Step 3: Verify data (optional)
159
- const verifySQL = `
160
- SELECT COUNT(*) as mismatch FROM \`${tableName}\`
161
- WHERE \`${field.Field}\` IS NOT NULL
162
- AND \`${field.Field}_new\` IS NULL;
163
- `;
164
- const [verifyResult] = await connection.query(verifySQL);
165
- if (verifyResult.mismatch > 0) {
166
- logger(`Warning: ${verifyResult.mismatch} rows may have failed conversion`);
167
- }
168
-
169
- // Step 4: Swap columns
170
- const swapColumnsSQL = `
171
- ALTER TABLE \`${tableName}\`
172
- DROP COLUMN \`${field.Field}\`,
173
- RENAME COLUMN \`${field.Field}_new\` TO \`${field.Field}\`
174
- `;
175
- logger(`Swapping columns: ${swapColumnsSQL}`);
176
- await connection.query(swapColumnsSQL);
155
+ // totalUpdated += rowsAffected;
156
+ // batchCount++;
157
+ // console.log(`Batch ${batchCount}: Converted ${rowsAffected} rows. Total converted: ${totalUpdated}`);
158
+ // }
159
+
160
+ // // Step 3: Verify data (optional)
161
+ // const verifySQL = `
162
+ // SELECT COUNT(*) as mismatch FROM \`${tableName}\`
163
+ // WHERE \`${field.Field}\` IS NOT NULL
164
+ // AND \`${field.Field}_new\` IS NULL;
165
+ // `;
166
+ // const [verifyResult] = await connection.query(verifySQL);
167
+ // if (verifyResult.mismatch > 0) {
168
+ // console.log(`Warning: ${verifyResult.mismatch} rows may have failed conversion`);
169
+ // }
170
+
171
+ // // Step 4: Swap columns
172
+ // const swapColumnsSQL = `
173
+ // ALTER TABLE \`${tableName}\`
174
+ // DROP COLUMN \`${field.Field}\`,
175
+ // RENAME COLUMN \`${field.Field}_new\` TO \`${field.Field}\`
176
+ // `;
177
+ // console.log(`Executing: ${swapColumnsSQL}`);
178
+ // await connection.query(swapColumnsSQL);
177
179
 
178
- logger(`Completed type conversion for ${field.Field}. Total rows converted: ${totalUpdated}`);
179
- } else {
180
+ // console.log(`Completed type conversion for ${field.Field}. Total rows converted: ${totalUpdated}`);
181
+ // } else {
180
182
  // Original mismatched field handling for simple changes
181
183
  const isTimestamp = field.Type.toLowerCase().includes('timestamp');
182
184
 
@@ -190,14 +192,13 @@ export const applyDifferences = async (connection, database, differences) => {
190
192
  : isTimestamp ? 'NULL DEFAULT NULL' : 'DEFAULT NULL';
191
193
 
192
194
  // Remove ON UPDATE CURRENT_TIMESTAMP if we're changing away from CURRENT_TIMESTAMP
193
- const currentField = diff.currentField;
194
195
  const hasOnUpdate = currentField?.Default === 'CURRENT_TIMESTAMP' &&
195
196
  field.Default !== 'CURRENT_TIMESTAMP';
196
197
 
197
198
  const modifyFieldSQL = `ALTER TABLE \`${tableName}\` CHANGE \`${field.Field}\` \`${field.Field}\` ${field.Type} ${field.NotNull ? 'NOT NULL' : ''} ${defaultClause}${hasOnUpdate ? ' ON UPDATE CURRENT_TIMESTAMP' : ''};`;
198
199
  console.log(`Executing: ${modifyFieldSQL}`);
199
200
  await connection.query(modifyFieldSQL);
200
- }
201
+ // }
201
202
 
202
203
  } else if (diff.type === 'missing_index') {
203
204
  const existingColumns = await connection.query(`SHOW COLUMNS FROM \`${diff.tableName}\``);