gg-mysql-connector 1.0.126 → 1.0.131

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.
@@ -9,6 +9,7 @@ export default class MyDBMigrator {
9
9
  COLUMN_DEFAULT: string;
10
10
  COLUMN_KEY: string;
11
11
  COLUMN_TYPE: string;
12
+ CHARACTER_MAXIMUM_LENGTH: number | null;
12
13
  }[];
13
14
  foreignKeyStatementList: string[];
14
15
  constructor(connection: ModelGenerator);
@@ -4,6 +4,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const chalk_1 = __importDefault(require("chalk"));
7
+ function keepOnlyDigits(str) {
8
+ return Number(str.replace(/\D/g, ""));
9
+ }
7
10
  class MyDBMigrator {
8
11
  constructor(connection) {
9
12
  this.MyDB = connection;
@@ -126,7 +129,7 @@ class MyDBMigrator {
126
129
  async alterPrimaryKey(tableName, col) {
127
130
  // console.log("alterPrimaryKey()")
128
131
  if (col.COLUMN_NAME === "id" && col.AUTO_INCREMENT === true) {
129
- const isPrimaryKey = await this.checkIsPrimaryKey(tableName, col.COLUMN_NAME);
132
+ const isPrimaryKey = await this.checkIsPrimaryKey(this.MyDB.dbInfo.database, tableName, col.COLUMN_NAME);
130
133
  // recheck only id
131
134
  if (isPrimaryKey === false && col.COLUMN_NAME === "id")
132
135
  await this.MyDB.query(`ALTER TABLE ${tableName} ADD PRIMARY KEY(id);`);
@@ -151,6 +154,9 @@ class MyDBMigrator {
151
154
  if (this.onetimeLoadColumnContent.length === 0) {
152
155
  this.onetimeLoadColumnContent = (await this.MyDB.query(`SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '${this.MyDB.dbInfo.database}'`, undefined, false));
153
156
  }
157
+ // const temp = this.onetimeLoadColumnContent.forEach((row)=>{
158
+ // if(row.COLUMN_TYPE === '')
159
+ // })
154
160
  }
155
161
  async checkIsColumnDataTypeChange(tableName, columnName, dataType, columnDefault) {
156
162
  await this.loadColumnContentIfEmpty();
@@ -161,16 +167,18 @@ class MyDBMigrator {
161
167
  columnDefault = "NULL";
162
168
  if (temp.length > 0) {
163
169
  if (temp[0].DATA_TYPE !== dataType) {
164
- console.log(chalk_1.default.green `${temp[0].DATA_TYPE} change to ${dataType}`);
165
- return true;
166
- // if (temp[0].DATA_TYPE === "enum") {
167
- // return true
168
- // } else {
169
- // return true
170
- // }
170
+ if (temp[0].DATA_TYPE.search("varchar") >= 0) {
171
+ if (temp[0].CHARACTER_MAXIMUM_LENGTH !==
172
+ keepOnlyDigits(temp[0].DATA_TYPE))
173
+ return true;
174
+ }
175
+ else {
176
+ console.log(chalk_1.default.green `DATA_TYPE -> ${temp[0].DATA_TYPE} change to ${dataType}`);
177
+ return true;
178
+ }
171
179
  }
172
180
  if (temp[0].COLUMN_DEFAULT !== columnDefault) {
173
- console.log(chalk_1.default.green `${temp[0].COLUMN_DEFAULT} change to ${columnDefault}`);
181
+ console.log(chalk_1.default.green `COLUMN_DEFAULT -> ${temp[0].COLUMN_DEFAULT} change to ${columnDefault}`);
174
182
  return true;
175
183
  }
176
184
  return false;
@@ -266,15 +274,17 @@ class MyDBMigrator {
266
274
  loopCount++;
267
275
  }
268
276
  }
269
- async checkIsPrimaryKey(tableName, columnName) {
270
- // let result: any = await this.MyDB.query(`SELECT *
271
- // FROM INFORMATION_SCHEMA.COLUMNS
272
- // WHERE TABLE_SCHEMA='${databaseName}'
273
- // AND TABLE_NAME='${tableName}'
274
- // AND COLUMN_NAME='${columnName}'`)
275
- const result2 = this.onetimeLoadColumnContent.find((row) => row.TABLE_NAME === tableName && columnName === columnName);
276
- if (result2) {
277
- if (result2.COLUMN_KEY === "PRI")
277
+ async checkIsPrimaryKey(databaseName, tableName, columnName) {
278
+ let result = (await this.MyDB.query(`SELECT *
279
+ FROM INFORMATION_SCHEMA.COLUMNS
280
+ WHERE TABLE_SCHEMA='${databaseName}'
281
+ AND TABLE_NAME='${tableName}'
282
+ AND COLUMN_NAME='${columnName}'`));
283
+ // const result2 = this.onetimeLoadColumnContent.find(
284
+ // (row) => row.TABLE_NAME === tableName && columnName === columnName
285
+ // )
286
+ if (result.length) {
287
+ if (result[0].COLUMN_KEY === "PRI")
278
288
  return true;
279
289
  else
280
290
  return false;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gg-mysql-connector",
3
- "version": "1.0.126",
3
+ "version": "1.0.131",
4
4
  "description": "",
5
5
  "main": "dist/src/index.js",
6
6
  "scripts": {
@@ -4,6 +4,9 @@ import ModelGenerator from "../ModelGenerator/ModelGenerator"
4
4
  import { columnType, MyModel, MyViewModel } from "../myModel"
5
5
  import sql from "sql-template-strings"
6
6
  import { table } from "console"
7
+ function keepOnlyDigits(str: string): number {
8
+ return Number(str.replace(/\D/g, ""))
9
+ }
7
10
  export default class MyDBMigrator {
8
11
  MyDB: ModelGenerator
9
12
  onetimeLoadColumnContent: {
@@ -13,6 +16,7 @@ export default class MyDBMigrator {
13
16
  COLUMN_DEFAULT: string
14
17
  COLUMN_KEY: string
15
18
  COLUMN_TYPE: string
19
+ CHARACTER_MAXIMUM_LENGTH: number | null
16
20
  }[]
17
21
  foreignKeyStatementList: string[]
18
22
 
@@ -203,6 +207,7 @@ export default class MyDBMigrator {
203
207
  // console.log("alterPrimaryKey()")
204
208
  if (col.COLUMN_NAME === "id" && col.AUTO_INCREMENT === true) {
205
209
  const isPrimaryKey = await this.checkIsPrimaryKey(
210
+ this.MyDB.dbInfo.database,
206
211
  tableName,
207
212
  col.COLUMN_NAME
208
213
  )
@@ -240,6 +245,9 @@ export default class MyDBMigrator {
240
245
  false
241
246
  )) as any
242
247
  }
248
+ // const temp = this.onetimeLoadColumnContent.forEach((row)=>{
249
+ // if(row.COLUMN_TYPE === '')
250
+ // })
243
251
  }
244
252
 
245
253
  private async checkIsColumnDataTypeChange(
@@ -257,17 +265,22 @@ export default class MyDBMigrator {
257
265
 
258
266
  if (temp.length > 0) {
259
267
  if (temp[0].DATA_TYPE !== dataType) {
260
- console.log(chalk.green`${temp[0].DATA_TYPE} change to ${dataType}`)
261
- return true
262
- // if (temp[0].DATA_TYPE === "enum") {
263
- // return true
264
- // } else {
265
- // return true
266
- // }
268
+ if (temp[0].DATA_TYPE.search("varchar") >= 0) {
269
+ if (
270
+ temp[0].CHARACTER_MAXIMUM_LENGTH !==
271
+ keepOnlyDigits(temp[0].DATA_TYPE)
272
+ )
273
+ return true
274
+ } else {
275
+ console.log(
276
+ chalk.green`DATA_TYPE -> ${temp[0].DATA_TYPE} change to ${dataType}`
277
+ )
278
+ return true
279
+ }
267
280
  }
268
281
  if (temp[0].COLUMN_DEFAULT !== columnDefault) {
269
282
  console.log(
270
- chalk.green`${temp[0].COLUMN_DEFAULT} change to ${columnDefault}`
283
+ chalk.green`COLUMN_DEFAULT -> ${temp[0].COLUMN_DEFAULT} change to ${columnDefault}`
271
284
  )
272
285
  return true
273
286
  }
@@ -401,19 +414,23 @@ export default class MyDBMigrator {
401
414
  }
402
415
  }
403
416
 
404
- private async checkIsPrimaryKey(tableName: string, columnName: string) {
405
- // let result: any = await this.MyDB.query(`SELECT *
406
- // FROM INFORMATION_SCHEMA.COLUMNS
407
- // WHERE TABLE_SCHEMA='${databaseName}'
408
- // AND TABLE_NAME='${tableName}'
409
- // AND COLUMN_NAME='${columnName}'`)
417
+ private async checkIsPrimaryKey(
418
+ databaseName: string,
419
+ tableName: string,
420
+ columnName: string
421
+ ) {
422
+ let result = (await this.MyDB.query(`SELECT *
423
+ FROM INFORMATION_SCHEMA.COLUMNS
424
+ WHERE TABLE_SCHEMA='${databaseName}'
425
+ AND TABLE_NAME='${tableName}'
426
+ AND COLUMN_NAME='${columnName}'`)) as { COLUMN_KEY: string }[]
410
427
 
411
- const result2 = this.onetimeLoadColumnContent.find(
412
- (row) => row.TABLE_NAME === tableName && columnName === columnName
413
- )
428
+ // const result2 = this.onetimeLoadColumnContent.find(
429
+ // (row) => row.TABLE_NAME === tableName && columnName === columnName
430
+ // )
414
431
 
415
- if (result2) {
416
- if (result2.COLUMN_KEY === "PRI") return true
432
+ if (result.length) {
433
+ if (result[0].COLUMN_KEY === "PRI") return true
417
434
  else return false
418
435
  } else return false
419
436
  }