gg-mysql-connector 1.0.91 → 1.0.93

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.
@@ -25,7 +25,7 @@ class MyDBMigrator {
25
25
  await this.alterDataType(row.tableName, col);
26
26
  await this.alterUniqueKey(row.tableName, col.COLUMN_NAME, col.IS_UNIQUE);
27
27
  await this.alterIndex(row.tableName, col.COLUMN_NAME, col.IS_INDEX);
28
- await this.alterPossibleEnum(row.tableName, col.COLUMN_NAME, col.POSSIBLE_VALUE);
28
+ await this.alterPossibleEnum(row.tableName, col.COLUMN_NAME, col.POSSIBLE_VALUE, col.IS_NOT_NULL);
29
29
  await this.alterForeignKey(row.tableName, col);
30
30
  }
31
31
  await this.alterUniqueColumnCombination(row.tableName, row.compoisteUniqueColumn);
@@ -88,14 +88,15 @@ class MyDBMigrator {
88
88
  const columnDefault = col.COLUMN_DEFAULT
89
89
  ? `DEFAULT ${col.COLUMN_DEFAULT}`
90
90
  : "";
91
+ const notNull = col.IS_NOT_NULL ? "NOT NULL" : "";
91
92
  const isColumnChange = await this.checkIsColumnDataTypeChange(tableName, col.COLUMN_NAME, col.DATA_TYPE, col.COLUMN_DEFAULT);
92
93
  if (isColumnChange) {
93
94
  const isColumnExist = await this.columnInTableExist(tableName, col.COLUMN_NAME);
94
95
  if (!isColumnExist) {
95
- await this.MyDB.query(`ALTER TABLE ${tableName} ADD COLUMN ${col.COLUMN_NAME} ${col.DATA_TYPE} ${AutoIncrement} ${columnDefault}`);
96
+ await this.MyDB.query(`ALTER TABLE ${tableName} ADD COLUMN ${col.COLUMN_NAME} ${col.DATA_TYPE} ${AutoIncrement} ${notNull} ${columnDefault}`);
96
97
  }
97
98
  else {
98
- await this.MyDB.query(`ALTER TABLE ${tableName} MODIFY COLUMN ${col.COLUMN_NAME} ${col.DATA_TYPE} ${AutoIncrement} ${columnDefault}`);
99
+ await this.MyDB.query(`ALTER TABLE ${tableName} MODIFY COLUMN ${col.COLUMN_NAME} ${col.DATA_TYPE} ${AutoIncrement} ${notNull} ${columnDefault}`);
99
100
  }
100
101
  }
101
102
  }
@@ -146,6 +147,12 @@ class MyDBMigrator {
146
147
  }
147
148
  }
148
149
  async alterUniqueKey(tableName, columnName, IS_UNIQUE) {
150
+ if (IS_UNIQUE === true) {
151
+ // ต้องโหลดใหม่ เพราะ กรณี ที่เพิ่ม column ใหม่ที่เป็น unique index แล้ว โค้ดก่อนหน้าจะเพิ่ม column เข้าเข้าไม่ก่อน
152
+ // onetimeLoadColumnContent จะยังไม่มี ข้อมูล column ใหม่ ทำให้ไม่ยอม add unique key index ให้
153
+ this.onetimeLoadColumnContent = [];
154
+ await this.loadColumnContentIfEmpty();
155
+ }
149
156
  const data = this.onetimeLoadColumnContent.find((row) => row.COLUMN_NAME === columnName && row.TABLE_NAME === tableName);
150
157
  if (data) {
151
158
  if ((data.COLUMN_KEY === "UNI" && !IS_UNIQUE) ||
@@ -172,16 +179,17 @@ class MyDBMigrator {
172
179
  }
173
180
  }
174
181
  }
175
- async alterPossibleEnum(tableName, columnName, possibleValue) {
182
+ async alterPossibleEnum(tableName, columnName, possibleValue, isNotNull) {
176
183
  if (!possibleValue)
177
184
  return null;
178
185
  const data = this.onetimeLoadColumnContent.find((row) => row.COLUMN_NAME === columnName && row.TABLE_NAME === tableName);
186
+ const notNull = isNotNull ? "NOT NULL" : "";
179
187
  if (data) {
180
188
  let possibleValueString = possibleValue
181
189
  .map((row) => `"${row}"`)
182
190
  .join(", ");
183
191
  try {
184
- await this.MyDB.query(`ALTER TABLE ${tableName} MODIFY COLUMN ${columnName} ENUM(${possibleValueString}) ;`);
192
+ await this.MyDB.query(`ALTER TABLE ${tableName} MODIFY COLUMN ${columnName} ENUM(${possibleValueString}) ${notNull} ;`);
185
193
  }
186
194
  catch (e) {
187
195
  const data = await this.MyDB.query(`SELECT DISTINCT(${columnName}) as columnValue FROM ${tableName}`);
@@ -14,6 +14,7 @@ export interface columnContent {
14
14
  }[];
15
15
  IS_UNIQUE?: boolean;
16
16
  IS_INDEX?: boolean;
17
+ IS_NOT_NULL?: boolean;
17
18
  }
18
19
  export interface compoisteUniqueColumn {
19
20
  columnName: string[];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gg-mysql-connector",
3
- "version": "1.0.91",
3
+ "version": "1.0.93",
4
4
  "description": "",
5
5
  "main": "dist/src/index.js",
6
6
  "scripts": {
@@ -41,7 +41,8 @@ export default class MyDBMigrator {
41
41
  await this.alterPossibleEnum(
42
42
  row.tableName,
43
43
  col.COLUMN_NAME,
44
- col.POSSIBLE_VALUE
44
+ col.POSSIBLE_VALUE,
45
+ col.IS_NOT_NULL
45
46
  )
46
47
  await this.alterForeignKey(row.tableName, col)
47
48
  }
@@ -143,6 +144,7 @@ export default class MyDBMigrator {
143
144
  const columnDefault = col.COLUMN_DEFAULT
144
145
  ? `DEFAULT ${col.COLUMN_DEFAULT}`
145
146
  : ""
147
+ const notNull = col.IS_NOT_NULL ? "NOT NULL" : ""
146
148
  const isColumnChange = await this.checkIsColumnDataTypeChange(
147
149
  tableName,
148
150
  col.COLUMN_NAME,
@@ -156,11 +158,11 @@ export default class MyDBMigrator {
156
158
  )
157
159
  if (!isColumnExist) {
158
160
  await this.MyDB.query(
159
- `ALTER TABLE ${tableName} ADD COLUMN ${col.COLUMN_NAME} ${col.DATA_TYPE} ${AutoIncrement} ${columnDefault}`
161
+ `ALTER TABLE ${tableName} ADD COLUMN ${col.COLUMN_NAME} ${col.DATA_TYPE} ${AutoIncrement} ${notNull} ${columnDefault}`
160
162
  )
161
163
  } else {
162
164
  await this.MyDB.query(
163
- `ALTER TABLE ${tableName} MODIFY COLUMN ${col.COLUMN_NAME} ${col.DATA_TYPE} ${AutoIncrement} ${columnDefault}`
165
+ `ALTER TABLE ${tableName} MODIFY COLUMN ${col.COLUMN_NAME} ${col.DATA_TYPE} ${AutoIncrement} ${notNull} ${columnDefault}`
164
166
  )
165
167
  }
166
168
  }
@@ -241,6 +243,12 @@ export default class MyDBMigrator {
241
243
  columnName: string,
242
244
  IS_UNIQUE: boolean | undefined
243
245
  ) {
246
+ if (IS_UNIQUE === true) {
247
+ // ต้องโหลดใหม่ เพราะ กรณี ที่เพิ่ม column ใหม่ที่เป็น unique index แล้ว โค้ดก่อนหน้าจะเพิ่ม column เข้าเข้าไม่ก่อน
248
+ // onetimeLoadColumnContent จะยังไม่มี ข้อมูล column ใหม่ ทำให้ไม่ยอม add unique key index ให้
249
+ this.onetimeLoadColumnContent = []
250
+ await this.loadColumnContentIfEmpty()
251
+ }
244
252
  const data = this.onetimeLoadColumnContent.find(
245
253
  (row) => row.COLUMN_NAME === columnName && row.TABLE_NAME === tableName
246
254
  )
@@ -288,12 +296,14 @@ export default class MyDBMigrator {
288
296
  private async alterPossibleEnum(
289
297
  tableName: string,
290
298
  columnName: string,
291
- possibleValue?: string[]
299
+ possibleValue?: string[],
300
+ isNotNull?: boolean
292
301
  ) {
293
302
  if (!possibleValue) return null
294
303
  const data = this.onetimeLoadColumnContent.find(
295
304
  (row) => row.COLUMN_NAME === columnName && row.TABLE_NAME === tableName
296
305
  )
306
+ const notNull = isNotNull ? "NOT NULL" : ""
297
307
  if (data) {
298
308
  let possibleValueString = possibleValue
299
309
  .map((row) => `"${row}"`)
@@ -301,7 +311,7 @@ export default class MyDBMigrator {
301
311
 
302
312
  try {
303
313
  await this.MyDB.query(
304
- `ALTER TABLE ${tableName} MODIFY COLUMN ${columnName} ENUM(${possibleValueString}) ;`
314
+ `ALTER TABLE ${tableName} MODIFY COLUMN ${columnName} ENUM(${possibleValueString}) ${notNull} ;`
305
315
  )
306
316
  } catch (e) {
307
317
  const data: { columnValue: any }[] = await this.MyDB.query(
package/src/myModel.ts CHANGED
@@ -31,6 +31,7 @@ export interface columnContent {
31
31
  }[]
32
32
  IS_UNIQUE?: boolean
33
33
  IS_INDEX?: boolean
34
+ IS_NOT_NULL?: boolean
34
35
  }
35
36
 
36
37
  export interface compoisteUniqueColumn {