@mlagie/sql-connector 1.3.0 → 1.3.2
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/package.json +1 -1
- package/src/models/Model.js +83 -17
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mlagie/sql-connector",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.2",
|
|
4
4
|
"description": "Le module sql-connector permet de gérer les connexions à une base de données MySQL, de définir des schémas de tables, et d'interagir avec les données de manière simple et efficace.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
package/src/models/Model.js
CHANGED
|
@@ -245,39 +245,105 @@ class Model {
|
|
|
245
245
|
`SHOW INDEX FROM \`${model.name}\` WHERE Column_name = ?`, [fieldName]
|
|
246
246
|
);
|
|
247
247
|
|
|
248
|
-
// 2. Vérifie les propriétés principales
|
|
248
|
+
// 2. Vérifie les propriétés principales avec plus de précision
|
|
249
249
|
let needModify = false;
|
|
250
250
|
|
|
251
|
-
//
|
|
252
|
-
const
|
|
253
|
-
|
|
254
|
-
|
|
251
|
+
// --- Vérification du TYPE ---
|
|
252
|
+
const fieldType = getFieldType(field);
|
|
253
|
+
let expectedTypeDef;
|
|
254
|
+
|
|
255
|
+
if (Array.isArray(field.enum) && field.enum.length > 0) {
|
|
256
|
+
// Pour les ENUM, on compare la définition complète
|
|
257
|
+
const enumValues = field.enum.map(v => `'${v.replace(/'/g, "''")}'`).join(", ");
|
|
258
|
+
expectedTypeDef = `enum(${enumValues})`;
|
|
259
|
+
} else {
|
|
260
|
+
// Pour les autres types
|
|
261
|
+
if (!sqlTypeMap[fieldType]) throw new Error(`Field ${fieldName} has unsupported type ${fieldType}.`);
|
|
262
|
+
|
|
263
|
+
expectedTypeDef = sqlTypeMap[fieldType];
|
|
264
|
+
if (sqlTypeMap[fieldType] === "VARCHAR" || sqlTypeMap[fieldType] === "INT") {
|
|
265
|
+
const length = field.length > 0 ? field.length : 255;
|
|
266
|
+
expectedTypeDef += `(${length})`;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
// Normalisation pour la comparaison (minuscules, suppression des espaces)
|
|
271
|
+
const normalizeType = (typeStr) => {
|
|
272
|
+
return typeStr.toLowerCase().replace(/\s+/g, '').replace(/`/g, '');
|
|
273
|
+
};
|
|
274
|
+
|
|
275
|
+
const currentTypeNormalized = normalizeType(currentCol.Type);
|
|
276
|
+
const expectedTypeNormalized = normalizeType(expectedTypeDef);
|
|
277
|
+
|
|
278
|
+
if (currentTypeNormalized !== expectedTypeNormalized) {
|
|
279
|
+
logs(`Type mismatch for ${fieldName}: DB has '${currentCol.Type}', expected '${expectedTypeDef}'`);
|
|
255
280
|
needModify = true;
|
|
281
|
+
}
|
|
256
282
|
|
|
257
|
-
//
|
|
283
|
+
// --- Vérification NULL/NOT NULL ---
|
|
258
284
|
const shouldBeNotNull = field.required === true || field.primary_key === true;
|
|
259
|
-
|
|
285
|
+
const isNullableInDB = currentCol.Null === "YES";
|
|
286
|
+
|
|
287
|
+
if (shouldBeNotNull && isNullableInDB) {
|
|
288
|
+
logs(`Nullability mismatch for ${fieldName}: DB allows NULL, expected NOT NULL`);
|
|
289
|
+
needModify = true;
|
|
290
|
+
}
|
|
291
|
+
if (!shouldBeNotNull && !isNullableInDB) {
|
|
292
|
+
logs(`Nullability mismatch for ${fieldName}: DB is NOT NULL, expected NULL allowed`);
|
|
260
293
|
needModify = true;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
// --- Vérification DEFAULT VALUE ---
|
|
297
|
+
const expectedDefault = field.default !== undefined ? field.default : null;
|
|
298
|
+
const currentDefault = currentCol.Default;
|
|
261
299
|
|
|
262
|
-
//
|
|
263
|
-
if (
|
|
300
|
+
// Gestion spéciale pour les valeurs par défaut
|
|
301
|
+
if (expectedDefault !== null && currentDefault === null) {
|
|
302
|
+
logs(`Default value mismatch for ${fieldName}: DB has NULL, expected '${expectedDefault}'`);
|
|
303
|
+
needModify = true;
|
|
304
|
+
} else if (expectedDefault === null && currentDefault !== null) {
|
|
305
|
+
logs(`Default value mismatch for ${fieldName}: DB has '${currentDefault}', expected NULL`);
|
|
264
306
|
needModify = true;
|
|
307
|
+
} else if (expectedDefault !== null && currentDefault !== null) {
|
|
308
|
+
// Comparaison stringifiée pour éviter les problèmes de type
|
|
309
|
+
if (String(expectedDefault) !== String(currentDefault)) {
|
|
310
|
+
logs(`Default value mismatch for ${fieldName}: DB has '${currentDefault}', expected '${expectedDefault}'`);
|
|
311
|
+
needModify = true;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
265
314
|
|
|
266
|
-
//
|
|
315
|
+
// --- Vérification UNIQUE constraint ---
|
|
267
316
|
const isUniqueInDB = indexes.some(idx => idx.Non_unique === 0 && idx.Key_name !== 'PRIMARY');
|
|
268
|
-
if (!!field.unique !== isUniqueInDB)
|
|
317
|
+
if (!!field.unique !== isUniqueInDB) {
|
|
318
|
+
logs(`Unique constraint mismatch for ${fieldName}: DB has unique=${isUniqueInDB}, expected=${!!field.unique}`);
|
|
269
319
|
needModify = true;
|
|
320
|
+
}
|
|
270
321
|
|
|
271
|
-
//
|
|
322
|
+
// --- Vérification PRIMARY KEY ---
|
|
272
323
|
const isPrimaryInDB = indexes.some(idx => idx.Key_name === 'PRIMARY');
|
|
273
|
-
if (!!field.primary_key !== isPrimaryInDB)
|
|
324
|
+
if (!!field.primary_key !== isPrimaryInDB) {
|
|
325
|
+
logs(`Primary key mismatch for ${fieldName}: DB has PK=${isPrimaryInDB}, expected=${!!field.primary_key}`);
|
|
274
326
|
needModify = true;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
// --- Vérification AUTO_INCREMENT ---
|
|
330
|
+
const isAutoIncrementInDB = currentCol.Extra.toLowerCase().includes('auto_increment');
|
|
331
|
+
if (!!field.auto_increment !== isAutoIncrementInDB) {
|
|
332
|
+
logs(`Auto increment mismatch for ${fieldName}: DB has AI=${isAutoIncrementInDB}, expected=${!!field.auto_increment}`);
|
|
333
|
+
needModify = true;
|
|
334
|
+
}
|
|
275
335
|
|
|
276
336
|
// 3. Si différence, on modifie
|
|
277
337
|
if (needModify) {
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
338
|
+
try {
|
|
339
|
+
const newColDef = getColumnDefinition(fieldName, field);
|
|
340
|
+
const alterSQL = `ALTER TABLE \`${model.name}\` MODIFY COLUMN ${newColDef}`;
|
|
341
|
+
logs(`Modifying column ${fieldName}: ${alterSQL}`);
|
|
342
|
+
await conn.promise().query(alterSQL);
|
|
343
|
+
logs(`Column ${fieldName} modified successfully`);
|
|
344
|
+
} catch (err) {
|
|
345
|
+
error(`Error modifying column ${fieldName}: ${err}`);
|
|
346
|
+
}
|
|
281
347
|
}
|
|
282
348
|
}
|
|
283
349
|
// --- Warn si oldName est présent dans le schéma ---
|
|
@@ -307,7 +373,7 @@ class Model {
|
|
|
307
373
|
let colDef = getColumnDefinition(fieldName, field);
|
|
308
374
|
|
|
309
375
|
// Ajoute la colonne
|
|
310
|
-
const alterSQL = `ALTER TABLE \`${model.name}\` ADD COLUMN
|
|
376
|
+
const alterSQL = `ALTER TABLE \`${model.name}\` ADD COLUMN ${colDef}`;
|
|
311
377
|
await conn.promise().query(alterSQL);
|
|
312
378
|
logs(`Colonne ${fieldName} ajoutée à ${model.name}`);
|
|
313
379
|
existingCols.push(fieldName);
|