gg-mysql-connector 1.0.98 → 1.0.102

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,48 +169,32 @@ class MyDBMigrator {
169
169
  }
170
170
  async alterUniqueKey(tableName, columnName, IS_UNIQUE) {
171
171
  console.log("alterUniqueKey()");
172
- if (IS_UNIQUE === true) {
173
- // ต้องโหลดใหม่ เพราะ กรณี ที่เพิ่ม column ใหม่ที่เป็น unique index แล้ว โค้ดก่อนหน้าจะเพิ่ม column เข้าเข้าไม่ก่อน
174
- // onetimeLoadColumnContent จะยังไม่มี ข้อมูล column ใหม่ ทำให้ไม่ยอม add unique key index ให้
175
- this.onetimeLoadColumnContent = [];
176
- await this.loadColumnContentIfEmpty();
172
+ const isUnique = Boolean(IS_UNIQUE);
173
+ const uniqueIndexName = `${columnName}_unique`;
174
+ const indexList = (await this.MyDB.query(`SHOW INDEX FROM ${tableName}`));
175
+ const isFound = indexList.find((iRow) => iRow.Key_name === uniqueIndexName)
176
+ ? true
177
+ : false;
178
+ if (isUnique === true && isFound === false) {
179
+ await this.MyDB.query(`ALTER TABLE ${tableName} ADD CONSTRAINT ${uniqueIndexName} UNIQUE (${columnName});`);
177
180
  }
178
- const data = this.onetimeLoadColumnContent.find((row) => row.COLUMN_NAME === columnName && row.TABLE_NAME === tableName);
179
- if (data) {
180
- if ((data.COLUMN_KEY === "UNI" && !IS_UNIQUE) ||
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});`);
189
- }
190
- else if (IS_UNIQUE === false && isFound === true) {
191
- //! DROP INDEX ถูกแล้ว อย่าแก้เป็น UNIQUE
192
- await this.MyDB.query(`ALTER TABLE ${tableName} DROP INDEX ${uniqueIndexName};`);
193
- }
194
- }
181
+ else if (isUnique === false && isFound === true) {
182
+ //! DROP INDEX ถูกแล้ว อย่าแก้เป็น UNIQUE
183
+ await this.MyDB.query(`ALTER TABLE ${tableName} DROP INDEX ${uniqueIndexName};`);
195
184
  }
196
185
  }
197
186
  async alterIndex(tableName, columnName, IS_INDEX) {
198
187
  console.log("alterIndex()");
199
- const data = this.onetimeLoadColumnContent.find((row) => row.COLUMN_NAME === columnName && row.TABLE_NAME === tableName);
200
- if (data) {
201
- if ((data.COLUMN_KEY === "MUL" && !IS_INDEX) ||
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
- }
213
- }
188
+ const isIndex = Boolean(IS_INDEX);
189
+ const indexIndexName = `${columnName}_index`;
190
+ const indexList = (await this.MyDB.query(`SHOW INDEX FROM ${tableName}`));
191
+ const isFound = indexList.find((iRow) => iRow.Key_name === indexIndexName)
192
+ ? true
193
+ : false;
194
+ if (isIndex === true && isFound === false)
195
+ await this.MyDB.query(`ALTER TABLE ${tableName} ADD INDEX ${indexIndexName} (${columnName});`);
196
+ else if (isIndex === false && isFound === true)
197
+ await this.MyDB.query(`DROP INDEX ${indexIndexName} ON ${tableName};`);
214
198
  }
215
199
  async alterPossibleEnum(tableName, columnName, possibleValue, isNotNull) {
216
200
  console.log("alterPossibleEnum()");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gg-mysql-connector",
3
- "version": "1.0.98",
3
+ "version": "1.0.102",
4
4
  "description": "",
5
5
  "main": "dist/src/index.js",
6
6
  "scripts": {
@@ -257,40 +257,23 @@ export default class MyDBMigrator {
257
257
  IS_UNIQUE: boolean | undefined
258
258
  ) {
259
259
  console.log("alterUniqueKey()")
260
- if (IS_UNIQUE === true) {
261
- // ต้องโหลดใหม่ เพราะ กรณี ที่เพิ่ม column ใหม่ที่เป็น unique index แล้ว โค้ดก่อนหน้าจะเพิ่ม column เข้าเข้าไม่ก่อน
262
- // onetimeLoadColumnContent จะยังไม่มี ข้อมูล column ใหม่ ทำให้ไม่ยอม add unique key index ให้
263
- this.onetimeLoadColumnContent = []
264
- await this.loadColumnContentIfEmpty()
265
- }
266
- const data = this.onetimeLoadColumnContent.find(
267
- (row) => row.COLUMN_NAME === columnName && row.TABLE_NAME === tableName
268
- )
269
- if (data) {
270
- if (
271
- (data.COLUMN_KEY === "UNI" && !IS_UNIQUE) ||
272
- (data.COLUMN_KEY !== "UNI" && 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) {
284
- await this.MyDB.query(
285
- `ALTER TABLE ${tableName} ADD CONSTRAINT ${uniqueIndexName} UNIQUE (${columnName});`
286
- )
287
- } else if (IS_UNIQUE === false && isFound === true) {
288
- //! DROP INDEX ถูกแล้ว อย่าแก้เป็น UNIQUE
289
- await this.MyDB.query(
290
- `ALTER TABLE ${tableName} DROP INDEX ${uniqueIndexName};`
291
- )
292
- }
293
- }
260
+ const isUnique = Boolean(IS_UNIQUE)
261
+ const uniqueIndexName = `${columnName}_unique`
262
+ const indexList = (await this.MyDB.query(
263
+ `SHOW INDEX FROM ${tableName}`
264
+ )) as { Key_name: string }[]
265
+ const isFound = indexList.find((iRow) => iRow.Key_name === uniqueIndexName)
266
+ ? true
267
+ : false
268
+ if (isUnique === true && isFound === false) {
269
+ await this.MyDB.query(
270
+ `ALTER TABLE ${tableName} ADD CONSTRAINT ${uniqueIndexName} UNIQUE (${columnName});`
271
+ )
272
+ } else if (isUnique === false && isFound === true) {
273
+ //! DROP INDEX ถูกแล้ว อย่าแก้เป็น UNIQUE
274
+ await this.MyDB.query(
275
+ `ALTER TABLE ${tableName} DROP INDEX ${uniqueIndexName};`
276
+ )
294
277
  }
295
278
  }
296
279
 
@@ -300,31 +283,21 @@ export default class MyDBMigrator {
300
283
  IS_INDEX: boolean | undefined
301
284
  ) {
302
285
  console.log("alterIndex()")
303
- const data = this.onetimeLoadColumnContent.find(
304
- (row) => row.COLUMN_NAME === columnName && row.TABLE_NAME === tableName
305
- )
306
- if (data) {
307
- if (
308
- (data.COLUMN_KEY === "MUL" && !IS_INDEX) ||
309
- (data.COLUMN_KEY !== "MUL" && 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)
321
- await this.MyDB.query(
322
- `ALTER TABLE ${tableName} ADD INDEX ${indexIndexName} (${columnName});`
323
- )
324
- else if (IS_INDEX === false && isFound === true)
325
- await this.MyDB.query(`DROP INDEX ${indexIndexName} ON ${tableName};`)
326
- }
327
- }
286
+ const isIndex = Boolean(IS_INDEX)
287
+
288
+ const indexIndexName = `${columnName}_index`
289
+ const indexList = (await this.MyDB.query(
290
+ `SHOW INDEX FROM ${tableName}`
291
+ )) as { Key_name: string }[]
292
+ const isFound = indexList.find((iRow) => iRow.Key_name === indexIndexName)
293
+ ? true
294
+ : false
295
+ if (isIndex === true && isFound === false)
296
+ await this.MyDB.query(
297
+ `ALTER TABLE ${tableName} ADD INDEX ${indexIndexName} (${columnName});`
298
+ )
299
+ else if (isIndex === false && isFound === true)
300
+ await this.MyDB.query(`DROP INDEX ${indexIndexName} ON ${tableName};`)
328
301
  }
329
302
 
330
303
  private async alterPossibleEnum(