gg-mysql-connector 1.0.99 → 1.0.103

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.
package/app_INF.ts CHANGED
@@ -5,6 +5,11 @@ export default interface app_INF { item : {
5
5
  description: string;
6
6
  amount: number;
7
7
  userId: number; }
8
+ item__stock : {
9
+ id: number;
10
+ userId: number;
11
+ itemId: number;
12
+ amount: number; }
8
13
  user : {
9
14
  id: number;
10
15
  name: string; }
@@ -5,7 +5,11 @@
5
5
  price: "number",
6
6
  description: "string",
7
7
  amount: "number",
8
- userId: "number", }, user : {
8
+ userId: "number", }, item__stock : {
9
+ id: "number",
10
+ userId: "number",
11
+ itemId: "number",
12
+ amount: "number", }, user : {
9
13
  id: "number",
10
14
  name: "string", }, item__each_user : {
11
15
  id: "number",
package/dist/app_INF.d.ts CHANGED
@@ -7,6 +7,12 @@ export default interface app_INF {
7
7
  amount: number;
8
8
  userId: number;
9
9
  };
10
+ item__stock: {
11
+ id: number;
12
+ userId: number;
13
+ itemId: number;
14
+ amount: number;
15
+ };
10
16
  user: {
11
17
  id: number;
12
18
  name: string;
@@ -7,6 +7,12 @@ export declare const app_model_const: {
7
7
  readonly amount: "number";
8
8
  readonly userId: "number";
9
9
  };
10
+ readonly item__stock: {
11
+ readonly id: "number";
12
+ readonly userId: "number";
13
+ readonly itemId: "number";
14
+ readonly amount: "number";
15
+ };
10
16
  readonly user: {
11
17
  readonly id: "number";
12
18
  readonly name: "string";
@@ -8,6 +8,11 @@ exports.app_model_const = { item: {
8
8
  description: "string",
9
9
  amount: "number",
10
10
  userId: "number",
11
+ }, item__stock: {
12
+ id: "number",
13
+ userId: "number",
14
+ itemId: "number",
15
+ amount: "number",
11
16
  }, user: {
12
17
  id: "number",
13
18
  name: "string",
@@ -26,6 +26,7 @@ export default class MyDBMigrator {
26
26
  private checkIsColumnDataTypeChange;
27
27
  private alterUniqueKey;
28
28
  private alterIndex;
29
+ private deleteOldIndexVersion;
29
30
  private alterPossibleEnum;
30
31
  migrateView_v2(viewModel: MyViewModel[]): Promise<void>;
31
32
  private checkIsPrimaryKey;
@@ -26,6 +26,8 @@ class MyDBMigrator {
26
26
  await this.alterDataType(row.tableName, col);
27
27
  await this.alterUniqueKey(row.tableName, col.COLUMN_NAME, col.IS_UNIQUE);
28
28
  await this.alterIndex(row.tableName, col.COLUMN_NAME, col.IS_INDEX);
29
+ //!! delete this next version. need to execute only 1 time on production
30
+ await this.deleteOldIndexVersion(row.tableName, col.COLUMN_NAME);
29
31
  await this.alterPossibleEnum(row.tableName, col.COLUMN_NAME, col.POSSIBLE_VALUE, col.IS_NOT_NULL);
30
32
  await this.alterForeignKey(row.tableName, col);
31
33
  }
@@ -170,49 +172,41 @@ class MyDBMigrator {
170
172
  async alterUniqueKey(tableName, columnName, IS_UNIQUE) {
171
173
  console.log("alterUniqueKey()");
172
174
  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();
175
+ const uniqueIndexName = `${columnName}_unique`;
176
+ const indexList = (await this.MyDB.query(`SHOW INDEX FROM ${tableName}`));
177
+ const isFound = indexList.find((iRow) => iRow.Key_name === uniqueIndexName)
178
+ ? true
179
+ : false;
180
+ if (isUnique === true && isFound === false) {
181
+ await this.MyDB.query(`ALTER TABLE ${tableName} ADD CONSTRAINT ${uniqueIndexName} UNIQUE (${columnName});`);
178
182
  }
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
- }
183
+ else if (isUnique === false && isFound === true) {
184
+ //! DROP INDEX ถูกแล้ว อย่าแก้เป็น UNIQUE
185
+ await this.MyDB.query(`ALTER TABLE ${tableName} DROP INDEX ${uniqueIndexName};`);
196
186
  }
197
187
  }
198
188
  async alterIndex(tableName, columnName, IS_INDEX) {
199
189
  console.log("alterIndex()");
200
190
  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
- }
191
+ const indexIndexName = `${columnName}_index`;
192
+ const indexList = (await this.MyDB.query(`SHOW INDEX FROM ${tableName}`));
193
+ const isFound = indexList.find((iRow) => iRow.Key_name === indexIndexName)
194
+ ? true
195
+ : false;
196
+ if (isIndex === true && isFound === false)
197
+ await this.MyDB.query(`ALTER TABLE ${tableName} ADD INDEX ${indexIndexName} (${columnName});`);
198
+ else if (isIndex === false && isFound === true)
199
+ await this.MyDB.query(`DROP INDEX ${indexIndexName} ON ${tableName};`);
200
+ }
201
+ async deleteOldIndexVersion(tableName, columnName) {
202
+ console.log("deleteOldIndexVersion()");
203
+ const indexIndexName = `${columnName}`;
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 (isFound === true)
209
+ await this.MyDB.query(`DROP INDEX ${indexIndexName} ON ${tableName};`);
216
210
  }
217
211
  async alterPossibleEnum(tableName, columnName, possibleValue, isNotNull) {
218
212
  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.103",
4
4
  "description": "",
5
5
  "main": "dist/src/index.js",
6
6
  "scripts": {
@@ -41,6 +41,9 @@ export default class MyDBMigrator {
41
41
  await this.alterDataType(row.tableName, col)
42
42
  await this.alterUniqueKey(row.tableName, col.COLUMN_NAME, col.IS_UNIQUE)
43
43
  await this.alterIndex(row.tableName, col.COLUMN_NAME, col.IS_INDEX)
44
+ //!! delete this next version. need to execute only 1 time on production
45
+ await this.deleteOldIndexVersion(row.tableName, col.COLUMN_NAME)
46
+
44
47
  await this.alterPossibleEnum(
45
48
  row.tableName,
46
49
  col.COLUMN_NAME,
@@ -258,40 +261,22 @@ export default class MyDBMigrator {
258
261
  ) {
259
262
  console.log("alterUniqueKey()")
260
263
  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
- }
264
+ const uniqueIndexName = `${columnName}_unique`
265
+ const indexList = (await this.MyDB.query(
266
+ `SHOW INDEX FROM ${tableName}`
267
+ )) as { Key_name: string }[]
268
+ const isFound = indexList.find((iRow) => iRow.Key_name === uniqueIndexName)
269
+ ? true
270
+ : false
271
+ if (isUnique === true && isFound === false) {
272
+ await this.MyDB.query(
273
+ `ALTER TABLE ${tableName} ADD CONSTRAINT ${uniqueIndexName} UNIQUE (${columnName});`
274
+ )
275
+ } else if (isUnique === false && isFound === true) {
276
+ //! DROP INDEX ถูกแล้ว อย่าแก้เป็น UNIQUE
277
+ await this.MyDB.query(
278
+ `ALTER TABLE ${tableName} DROP INDEX ${uniqueIndexName};`
279
+ )
295
280
  }
296
281
  }
297
282
 
@@ -302,31 +287,35 @@ export default class MyDBMigrator {
302
287
  ) {
303
288
  console.log("alterIndex()")
304
289
  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
- }
290
+
291
+ const indexIndexName = `${columnName}_index`
292
+ const indexList = (await this.MyDB.query(
293
+ `SHOW INDEX FROM ${tableName}`
294
+ )) as { Key_name: string }[]
295
+ const isFound = indexList.find((iRow) => iRow.Key_name === indexIndexName)
296
+ ? true
297
+ : false
298
+ if (isIndex === true && isFound === false)
299
+ await this.MyDB.query(
300
+ `ALTER TABLE ${tableName} ADD INDEX ${indexIndexName} (${columnName});`
301
+ )
302
+ else if (isIndex === false && isFound === true)
303
+ await this.MyDB.query(`DROP INDEX ${indexIndexName} ON ${tableName};`)
304
+ }
305
+
306
+ private async deleteOldIndexVersion(tableName: string, columnName: string) {
307
+ console.log("deleteOldIndexVersion()")
308
+
309
+ const indexIndexName = `${columnName}`
310
+ const indexList = (await this.MyDB.query(
311
+ `SHOW INDEX FROM ${tableName}`
312
+ )) as { Key_name: string }[]
313
+ const isFound = indexList.find((iRow) => iRow.Key_name === indexIndexName)
314
+ ? true
315
+ : false
316
+
317
+ if (isFound === true)
318
+ await this.MyDB.query(`DROP INDEX ${indexIndexName} ON ${tableName};`)
330
319
  }
331
320
 
332
321
  private async alterPossibleEnum(