gg-mysql-connector 1.0.97 → 1.0.99
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.
|
@@ -169,7 +169,8 @@ class MyDBMigrator {
|
|
|
169
169
|
}
|
|
170
170
|
async alterUniqueKey(tableName, columnName, IS_UNIQUE) {
|
|
171
171
|
console.log("alterUniqueKey()");
|
|
172
|
-
|
|
172
|
+
const isUnique = Boolean(IS_UNIQUE);
|
|
173
|
+
if (isUnique === true) {
|
|
173
174
|
// ต้องโหลดใหม่ เพราะ กรณี ที่เพิ่ม column ใหม่ที่เป็น unique index แล้ว โค้ดก่อนหน้าจะเพิ่ม column เข้าเข้าไม่ก่อน
|
|
174
175
|
// onetimeLoadColumnContent จะยังไม่มี ข้อมูล column ใหม่ ทำให้ไม่ยอม add unique key index ให้
|
|
175
176
|
this.onetimeLoadColumnContent = [];
|
|
@@ -177,29 +178,40 @@ class MyDBMigrator {
|
|
|
177
178
|
}
|
|
178
179
|
const data = this.onetimeLoadColumnContent.find((row) => row.COLUMN_NAME === columnName && row.TABLE_NAME === tableName);
|
|
179
180
|
if (data) {
|
|
180
|
-
if ((data.COLUMN_KEY === "UNI" && !
|
|
181
|
-
(data.COLUMN_KEY !== "UNI" &&
|
|
182
|
-
|
|
183
|
-
|
|
181
|
+
if ((data.COLUMN_KEY === "UNI" && !isUnique) ||
|
|
182
|
+
(data.COLUMN_KEY !== "UNI" && isUnique === true)) {
|
|
183
|
+
const uniqueIndexName = `${columnName}_unique`;
|
|
184
|
+
const indexList = (await this.MyDB.query(`SHOW INDEX FROM ${tableName}`));
|
|
185
|
+
const isFound = indexList.find((iRow) => iRow.Key_name === uniqueIndexName)
|
|
186
|
+
? true
|
|
187
|
+
: false;
|
|
188
|
+
if (isUnique === true && isFound === false) {
|
|
189
|
+
await this.MyDB.query(`ALTER TABLE ${tableName} ADD CONSTRAINT ${uniqueIndexName} UNIQUE (${columnName});`);
|
|
184
190
|
}
|
|
185
|
-
else {
|
|
191
|
+
else if (isUnique === false && isFound === true) {
|
|
186
192
|
//! DROP INDEX ถูกแล้ว อย่าแก้เป็น UNIQUE
|
|
187
|
-
await this.MyDB.query(`ALTER TABLE ${tableName} DROP INDEX ${
|
|
193
|
+
await this.MyDB.query(`ALTER TABLE ${tableName} DROP INDEX ${uniqueIndexName};`);
|
|
188
194
|
}
|
|
195
|
+
}
|
|
189
196
|
}
|
|
190
197
|
}
|
|
191
198
|
async alterIndex(tableName, columnName, IS_INDEX) {
|
|
192
199
|
console.log("alterIndex()");
|
|
200
|
+
const isIndex = Boolean(IS_INDEX);
|
|
193
201
|
const data = this.onetimeLoadColumnContent.find((row) => row.COLUMN_NAME === columnName && row.TABLE_NAME === tableName);
|
|
194
202
|
if (data) {
|
|
195
|
-
if ((data.COLUMN_KEY === "MUL" && !
|
|
196
|
-
(data.COLUMN_KEY !== "MUL" &&
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
+
if ((data.COLUMN_KEY === "MUL" && !isIndex) ||
|
|
204
|
+
(data.COLUMN_KEY !== "MUL" && isIndex === true)) {
|
|
205
|
+
const indexIndexName = `${columnName}_index`;
|
|
206
|
+
const indexList = (await this.MyDB.query(`SHOW INDEX FROM ${tableName}`));
|
|
207
|
+
const isFound = indexList.find((iRow) => iRow.Key_name === indexIndexName)
|
|
208
|
+
? true
|
|
209
|
+
: false;
|
|
210
|
+
if (isIndex === true && isFound === false)
|
|
211
|
+
await this.MyDB.query(`ALTER TABLE ${tableName} ADD INDEX ${indexIndexName} (${columnName});`);
|
|
212
|
+
else if (isIndex === false && isFound === true)
|
|
213
|
+
await this.MyDB.query(`DROP INDEX ${indexIndexName} ON ${tableName};`);
|
|
214
|
+
}
|
|
203
215
|
}
|
|
204
216
|
}
|
|
205
217
|
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.
|
|
3
|
+
"version": "1.0.99",
|
|
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.
|
|
22
|
+
"typescript": "^5.9.3"
|
|
23
23
|
}
|
|
24
24
|
}
|
|
@@ -257,7 +257,8 @@ export default class MyDBMigrator {
|
|
|
257
257
|
IS_UNIQUE: boolean | undefined
|
|
258
258
|
) {
|
|
259
259
|
console.log("alterUniqueKey()")
|
|
260
|
-
|
|
260
|
+
const isUnique = Boolean(IS_UNIQUE)
|
|
261
|
+
if (isUnique === true) {
|
|
261
262
|
// ต้องโหลดใหม่ เพราะ กรณี ที่เพิ่ม column ใหม่ที่เป็น unique index แล้ว โค้ดก่อนหน้าจะเพิ่ม column เข้าเข้าไม่ก่อน
|
|
262
263
|
// onetimeLoadColumnContent จะยังไม่มี ข้อมูล column ใหม่ ทำให้ไม่ยอม add unique key index ให้
|
|
263
264
|
this.onetimeLoadColumnContent = []
|
|
@@ -268,19 +269,29 @@ export default class MyDBMigrator {
|
|
|
268
269
|
)
|
|
269
270
|
if (data) {
|
|
270
271
|
if (
|
|
271
|
-
(data.COLUMN_KEY === "UNI" && !
|
|
272
|
-
(data.COLUMN_KEY !== "UNI" &&
|
|
273
|
-
)
|
|
274
|
-
|
|
272
|
+
(data.COLUMN_KEY === "UNI" && !isUnique) ||
|
|
273
|
+
(data.COLUMN_KEY !== "UNI" && isUnique === true)
|
|
274
|
+
) {
|
|
275
|
+
const uniqueIndexName = `${columnName}_unique`
|
|
276
|
+
const indexList = (await this.MyDB.query(
|
|
277
|
+
`SHOW INDEX FROM ${tableName}`
|
|
278
|
+
)) as { Key_name: string }[]
|
|
279
|
+
const isFound = indexList.find(
|
|
280
|
+
(iRow) => iRow.Key_name === uniqueIndexName
|
|
281
|
+
)
|
|
282
|
+
? true
|
|
283
|
+
: false
|
|
284
|
+
if (isUnique === true && isFound === false) {
|
|
275
285
|
await this.MyDB.query(
|
|
276
|
-
`ALTER TABLE ${tableName} ADD UNIQUE (${columnName});`
|
|
286
|
+
`ALTER TABLE ${tableName} ADD CONSTRAINT ${uniqueIndexName} UNIQUE (${columnName});`
|
|
277
287
|
)
|
|
278
|
-
} else {
|
|
288
|
+
} else if (isUnique === false && isFound === true) {
|
|
279
289
|
//! DROP INDEX ถูกแล้ว อย่าแก้เป็น UNIQUE
|
|
280
290
|
await this.MyDB.query(
|
|
281
|
-
`ALTER TABLE ${tableName} DROP INDEX ${
|
|
291
|
+
`ALTER TABLE ${tableName} DROP INDEX ${uniqueIndexName};`
|
|
282
292
|
)
|
|
283
293
|
}
|
|
294
|
+
}
|
|
284
295
|
}
|
|
285
296
|
}
|
|
286
297
|
|
|
@@ -290,21 +301,31 @@ export default class MyDBMigrator {
|
|
|
290
301
|
IS_INDEX: boolean | undefined
|
|
291
302
|
) {
|
|
292
303
|
console.log("alterIndex()")
|
|
304
|
+
const isIndex = Boolean(IS_INDEX)
|
|
293
305
|
const data = this.onetimeLoadColumnContent.find(
|
|
294
306
|
(row) => row.COLUMN_NAME === columnName && row.TABLE_NAME === tableName
|
|
295
307
|
)
|
|
296
308
|
if (data) {
|
|
297
309
|
if (
|
|
298
|
-
(data.COLUMN_KEY === "MUL" && !
|
|
299
|
-
(data.COLUMN_KEY !== "MUL" &&
|
|
300
|
-
)
|
|
301
|
-
|
|
310
|
+
(data.COLUMN_KEY === "MUL" && !isIndex) ||
|
|
311
|
+
(data.COLUMN_KEY !== "MUL" && isIndex === true)
|
|
312
|
+
) {
|
|
313
|
+
const indexIndexName = `${columnName}_index`
|
|
314
|
+
const indexList = (await this.MyDB.query(
|
|
315
|
+
`SHOW INDEX FROM ${tableName}`
|
|
316
|
+
)) as { Key_name: string }[]
|
|
317
|
+
const isFound = indexList.find(
|
|
318
|
+
(iRow) => iRow.Key_name === indexIndexName
|
|
319
|
+
)
|
|
320
|
+
? true
|
|
321
|
+
: false
|
|
322
|
+
if (isIndex === true && isFound === false)
|
|
302
323
|
await this.MyDB.query(
|
|
303
|
-
`ALTER TABLE ${tableName} ADD INDEX (${columnName});`
|
|
324
|
+
`ALTER TABLE ${tableName} ADD INDEX ${indexIndexName} (${columnName});`
|
|
304
325
|
)
|
|
305
|
-
|
|
306
|
-
await this.MyDB.query(`DROP INDEX ${
|
|
307
|
-
|
|
326
|
+
else if (isIndex === false && isFound === true)
|
|
327
|
+
await this.MyDB.query(`DROP INDEX ${indexIndexName} ON ${tableName};`)
|
|
328
|
+
}
|
|
308
329
|
}
|
|
309
330
|
}
|
|
310
331
|
|