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