gg-mysql-connector 1.0.98 → 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,17 +178,17 @@ 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" &&
|
|
181
|
+
if ((data.COLUMN_KEY === "UNI" && !isUnique) ||
|
|
182
|
+
(data.COLUMN_KEY !== "UNI" && isUnique === true)) {
|
|
182
183
|
const uniqueIndexName = `${columnName}_unique`;
|
|
183
184
|
const indexList = (await this.MyDB.query(`SHOW INDEX FROM ${tableName}`));
|
|
184
185
|
const isFound = indexList.find((iRow) => iRow.Key_name === uniqueIndexName)
|
|
185
186
|
? true
|
|
186
187
|
: false;
|
|
187
|
-
if (
|
|
188
|
+
if (isUnique === true && isFound === false) {
|
|
188
189
|
await this.MyDB.query(`ALTER TABLE ${tableName} ADD CONSTRAINT ${uniqueIndexName} UNIQUE (${columnName});`);
|
|
189
190
|
}
|
|
190
|
-
else if (
|
|
191
|
+
else if (isUnique === false && isFound === true) {
|
|
191
192
|
//! DROP INDEX ถูกแล้ว อย่าแก้เป็น UNIQUE
|
|
192
193
|
await this.MyDB.query(`ALTER TABLE ${tableName} DROP INDEX ${uniqueIndexName};`);
|
|
193
194
|
}
|
|
@@ -196,18 +197,19 @@ class MyDBMigrator {
|
|
|
196
197
|
}
|
|
197
198
|
async alterIndex(tableName, columnName, IS_INDEX) {
|
|
198
199
|
console.log("alterIndex()");
|
|
200
|
+
const isIndex = Boolean(IS_INDEX);
|
|
199
201
|
const data = this.onetimeLoadColumnContent.find((row) => row.COLUMN_NAME === columnName && row.TABLE_NAME === tableName);
|
|
200
202
|
if (data) {
|
|
201
|
-
if ((data.COLUMN_KEY === "MUL" && !
|
|
202
|
-
(data.COLUMN_KEY !== "MUL" &&
|
|
203
|
+
if ((data.COLUMN_KEY === "MUL" && !isIndex) ||
|
|
204
|
+
(data.COLUMN_KEY !== "MUL" && isIndex === true)) {
|
|
203
205
|
const indexIndexName = `${columnName}_index`;
|
|
204
206
|
const indexList = (await this.MyDB.query(`SHOW INDEX FROM ${tableName}`));
|
|
205
207
|
const isFound = indexList.find((iRow) => iRow.Key_name === indexIndexName)
|
|
206
208
|
? true
|
|
207
209
|
: false;
|
|
208
|
-
if (
|
|
210
|
+
if (isIndex === true && isFound === false)
|
|
209
211
|
await this.MyDB.query(`ALTER TABLE ${tableName} ADD INDEX ${indexIndexName} (${columnName});`);
|
|
210
|
-
else if (
|
|
212
|
+
else if (isIndex === false && isFound === true)
|
|
211
213
|
await this.MyDB.query(`DROP INDEX ${indexIndexName} ON ${tableName};`);
|
|
212
214
|
}
|
|
213
215
|
}
|
package/package.json
CHANGED
|
@@ -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,8 +269,8 @@ export default class MyDBMigrator {
|
|
|
268
269
|
)
|
|
269
270
|
if (data) {
|
|
270
271
|
if (
|
|
271
|
-
(data.COLUMN_KEY === "UNI" && !
|
|
272
|
-
(data.COLUMN_KEY !== "UNI" &&
|
|
272
|
+
(data.COLUMN_KEY === "UNI" && !isUnique) ||
|
|
273
|
+
(data.COLUMN_KEY !== "UNI" && isUnique === true)
|
|
273
274
|
) {
|
|
274
275
|
const uniqueIndexName = `${columnName}_unique`
|
|
275
276
|
const indexList = (await this.MyDB.query(
|
|
@@ -280,11 +281,11 @@ export default class MyDBMigrator {
|
|
|
280
281
|
)
|
|
281
282
|
? true
|
|
282
283
|
: false
|
|
283
|
-
if (
|
|
284
|
+
if (isUnique === true && isFound === false) {
|
|
284
285
|
await this.MyDB.query(
|
|
285
286
|
`ALTER TABLE ${tableName} ADD CONSTRAINT ${uniqueIndexName} UNIQUE (${columnName});`
|
|
286
287
|
)
|
|
287
|
-
} else if (
|
|
288
|
+
} else if (isUnique === false && isFound === true) {
|
|
288
289
|
//! DROP INDEX ถูกแล้ว อย่าแก้เป็น UNIQUE
|
|
289
290
|
await this.MyDB.query(
|
|
290
291
|
`ALTER TABLE ${tableName} DROP INDEX ${uniqueIndexName};`
|
|
@@ -300,13 +301,14 @@ export default class MyDBMigrator {
|
|
|
300
301
|
IS_INDEX: boolean | undefined
|
|
301
302
|
) {
|
|
302
303
|
console.log("alterIndex()")
|
|
304
|
+
const isIndex = Boolean(IS_INDEX)
|
|
303
305
|
const data = this.onetimeLoadColumnContent.find(
|
|
304
306
|
(row) => row.COLUMN_NAME === columnName && row.TABLE_NAME === tableName
|
|
305
307
|
)
|
|
306
308
|
if (data) {
|
|
307
309
|
if (
|
|
308
|
-
(data.COLUMN_KEY === "MUL" && !
|
|
309
|
-
(data.COLUMN_KEY !== "MUL" &&
|
|
310
|
+
(data.COLUMN_KEY === "MUL" && !isIndex) ||
|
|
311
|
+
(data.COLUMN_KEY !== "MUL" && isIndex === true)
|
|
310
312
|
) {
|
|
311
313
|
const indexIndexName = `${columnName}_index`
|
|
312
314
|
const indexList = (await this.MyDB.query(
|
|
@@ -317,11 +319,11 @@ export default class MyDBMigrator {
|
|
|
317
319
|
)
|
|
318
320
|
? true
|
|
319
321
|
: false
|
|
320
|
-
if (
|
|
322
|
+
if (isIndex === true && isFound === false)
|
|
321
323
|
await this.MyDB.query(
|
|
322
324
|
`ALTER TABLE ${tableName} ADD INDEX ${indexIndexName} (${columnName});`
|
|
323
325
|
)
|
|
324
|
-
else if (
|
|
326
|
+
else if (isIndex === false && isFound === true)
|
|
325
327
|
await this.MyDB.query(`DROP INDEX ${indexIndexName} ON ${tableName};`)
|
|
326
328
|
}
|
|
327
329
|
}
|