gg-mysql-connector 1.0.97 → 1.0.98

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.
@@ -178,14 +178,20 @@ class MyDBMigrator {
178
178
  const data = this.onetimeLoadColumnContent.find((row) => row.COLUMN_NAME === columnName && row.TABLE_NAME === tableName);
179
179
  if (data) {
180
180
  if ((data.COLUMN_KEY === "UNI" && !IS_UNIQUE) ||
181
- (data.COLUMN_KEY !== "UNI" && IS_UNIQUE === true))
182
- if (IS_UNIQUE === true) {
183
- await this.MyDB.query(`ALTER TABLE ${tableName} ADD UNIQUE (${columnName});`);
181
+ (data.COLUMN_KEY !== "UNI" && IS_UNIQUE === true)) {
182
+ const uniqueIndexName = `${columnName}_unique`;
183
+ const indexList = (await this.MyDB.query(`SHOW INDEX FROM ${tableName}`));
184
+ const isFound = indexList.find((iRow) => iRow.Key_name === uniqueIndexName)
185
+ ? true
186
+ : false;
187
+ if (IS_UNIQUE === true && isFound === false) {
188
+ await this.MyDB.query(`ALTER TABLE ${tableName} ADD CONSTRAINT ${uniqueIndexName} UNIQUE (${columnName});`);
184
189
  }
185
- else {
190
+ else if (IS_UNIQUE === false && isFound === true) {
186
191
  //! DROP INDEX ถูกแล้ว อย่าแก้เป็น UNIQUE
187
- await this.MyDB.query(`ALTER TABLE ${tableName} DROP INDEX ${columnName};`);
192
+ await this.MyDB.query(`ALTER TABLE ${tableName} DROP INDEX ${uniqueIndexName};`);
188
193
  }
194
+ }
189
195
  }
190
196
  }
191
197
  async alterIndex(tableName, columnName, IS_INDEX) {
@@ -193,13 +199,17 @@ class MyDBMigrator {
193
199
  const data = this.onetimeLoadColumnContent.find((row) => row.COLUMN_NAME === columnName && row.TABLE_NAME === tableName);
194
200
  if (data) {
195
201
  if ((data.COLUMN_KEY === "MUL" && !IS_INDEX) ||
196
- (data.COLUMN_KEY !== "MUL" && IS_INDEX === true))
197
- if (IS_INDEX === true) {
198
- await this.MyDB.query(`ALTER TABLE ${tableName} ADD INDEX (${columnName});`);
199
- }
200
- else {
201
- await this.MyDB.query(`DROP INDEX ${columnName} ON ${tableName};`);
202
- }
202
+ (data.COLUMN_KEY !== "MUL" && IS_INDEX === true)) {
203
+ const indexIndexName = `${columnName}_index`;
204
+ const indexList = (await this.MyDB.query(`SHOW INDEX FROM ${tableName}`));
205
+ const isFound = indexList.find((iRow) => iRow.Key_name === indexIndexName)
206
+ ? true
207
+ : false;
208
+ if (IS_INDEX === true && isFound === false)
209
+ await this.MyDB.query(`ALTER TABLE ${tableName} ADD INDEX ${indexIndexName} (${columnName});`);
210
+ else if (IS_INDEX === false && isFound === true)
211
+ await this.MyDB.query(`DROP INDEX ${indexIndexName} ON ${tableName};`);
212
+ }
203
213
  }
204
214
  }
205
215
  async alterPossibleEnum(tableName, columnName, possibleValue, isNotNull) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gg-mysql-connector",
3
- "version": "1.0.97",
3
+ "version": "1.0.98",
4
4
  "description": "",
5
5
  "main": "dist/src/index.js",
6
6
  "scripts": {
@@ -19,6 +19,6 @@
19
19
  },
20
20
  "devDependencies": {
21
21
  "@types/node": "^22.7.5",
22
- "typescript": "^5.8.3"
22
+ "typescript": "^5.9.3"
23
23
  }
24
24
  }
@@ -270,17 +270,27 @@ export default class MyDBMigrator {
270
270
  if (
271
271
  (data.COLUMN_KEY === "UNI" && !IS_UNIQUE) ||
272
272
  (data.COLUMN_KEY !== "UNI" && IS_UNIQUE === true)
273
- )
274
- if (IS_UNIQUE === true) {
273
+ ) {
274
+ const uniqueIndexName = `${columnName}_unique`
275
+ const indexList = (await this.MyDB.query(
276
+ `SHOW INDEX FROM ${tableName}`
277
+ )) as { Key_name: string }[]
278
+ const isFound = indexList.find(
279
+ (iRow) => iRow.Key_name === uniqueIndexName
280
+ )
281
+ ? true
282
+ : false
283
+ if (IS_UNIQUE === true && isFound === false) {
275
284
  await this.MyDB.query(
276
- `ALTER TABLE ${tableName} ADD UNIQUE (${columnName});`
285
+ `ALTER TABLE ${tableName} ADD CONSTRAINT ${uniqueIndexName} UNIQUE (${columnName});`
277
286
  )
278
- } else {
287
+ } else if (IS_UNIQUE === false && isFound === true) {
279
288
  //! DROP INDEX ถูกแล้ว อย่าแก้เป็น UNIQUE
280
289
  await this.MyDB.query(
281
- `ALTER TABLE ${tableName} DROP INDEX ${columnName};`
290
+ `ALTER TABLE ${tableName} DROP INDEX ${uniqueIndexName};`
282
291
  )
283
292
  }
293
+ }
284
294
  }
285
295
  }
286
296
 
@@ -297,14 +307,23 @@ export default class MyDBMigrator {
297
307
  if (
298
308
  (data.COLUMN_KEY === "MUL" && !IS_INDEX) ||
299
309
  (data.COLUMN_KEY !== "MUL" && IS_INDEX === true)
300
- )
301
- if (IS_INDEX === true) {
310
+ ) {
311
+ const indexIndexName = `${columnName}_index`
312
+ const indexList = (await this.MyDB.query(
313
+ `SHOW INDEX FROM ${tableName}`
314
+ )) as { Key_name: string }[]
315
+ const isFound = indexList.find(
316
+ (iRow) => iRow.Key_name === indexIndexName
317
+ )
318
+ ? true
319
+ : false
320
+ if (IS_INDEX === true && isFound === false)
302
321
  await this.MyDB.query(
303
- `ALTER TABLE ${tableName} ADD INDEX (${columnName});`
322
+ `ALTER TABLE ${tableName} ADD INDEX ${indexIndexName} (${columnName});`
304
323
  )
305
- } else {
306
- await this.MyDB.query(`DROP INDEX ${columnName} ON ${tableName};`)
307
- }
324
+ else if (IS_INDEX === false && isFound === true)
325
+ await this.MyDB.query(`DROP INDEX ${indexIndexName} ON ${tableName};`)
326
+ }
308
327
  }
309
328
  }
310
329