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/dist/index.cjs +195 -218
- package/dist/index.js +195 -218
- package/package.json +1 -1
- package/src/apply.js +86 -85
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
|
-
|
|
90
|
-
|
|
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
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
94
|
+
// // Decimal changes
|
|
95
|
+
// if (currentType.includes('decimal') && newType.includes('decimal')) {
|
|
96
|
+
// return true;
|
|
97
|
+
// }
|
|
96
98
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
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
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
107
|
+
// // TEXT to VARCHAR conversions
|
|
108
|
+
// if (currentType.includes('text') && newType.includes('varchar')) {
|
|
109
|
+
// return true;
|
|
110
|
+
// }
|
|
109
111
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
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
|
-
|
|
121
|
-
|
|
122
|
-
|
|
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
|
-
|
|
127
|
-
};
|
|
128
|
-
|
|
129
|
-
if (needsBatchProcessing()) {
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
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
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
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
|
-
|
|
153
|
+
// if (rowsAffected === 0) break;
|
|
152
154
|
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
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
|
-
|
|
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}\``);
|