gg-mysql-connector 1.0.123 → 1.0.129

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.
@@ -137,7 +137,9 @@ class ModelGenerator {
137
137
  if (foundTable) {
138
138
  const foundColumn = foundTable["columns"].find((row) => row.COLUMN_NAME === columnName);
139
139
  // find in possible value in table model first
140
- if (foundColumn && foundColumn.POSSIBLE_VALUE) {
140
+ if (foundColumn &&
141
+ foundColumn.DATA_TYPE === "enum" &&
142
+ foundColumn.POSSIBLE_VALUE) {
141
143
  return foundColumn.POSSIBLE_VALUE;
142
144
  }
143
145
  else {
@@ -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);
@@ -26,7 +27,6 @@ export default class MyDBMigrator {
26
27
  private checkIsColumnDataTypeChange;
27
28
  private alterUniqueKey;
28
29
  private alterIndex;
29
- private deleteOldIndexVersion;
30
30
  private alterPossibleEnum;
31
31
  migrateView_v2(viewModel: MyViewModel[]): Promise<void>;
32
32
  private checkIsPrimaryKey;
@@ -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;
@@ -13,12 +16,11 @@ class MyDBMigrator {
13
16
  async migrateTable(model) {
14
17
  let data = model.filter((row) => row.tableName !== "sessions" && row.tableName.search("prisma") < 0);
15
18
  let index = 1;
19
+ this.onetimeLoadColumnContent = [];
20
+ await this.loadColumnContentIfEmpty();
16
21
  for (let row of data) {
17
22
  console.log(chalk_1.default.bgBlue(`${index}/${data.length} ${row.tableName}`));
18
- console.log(row);
19
23
  await this.createTableIfNotExist(row.tableName);
20
- this.onetimeLoadColumnContent = [];
21
- await this.loadColumnContentIfEmpty();
22
24
  index++;
23
25
  const indexList = (await this.MyDB.query(`SHOW INDEX FROM ${row.tableName}`));
24
26
  for (let col of row.columns) {
@@ -27,7 +29,8 @@ class MyDBMigrator {
27
29
  await this.alterDataType(row.tableName, col);
28
30
  await this.alterUniqueKey(row.tableName, col.COLUMN_NAME, col.IS_UNIQUE, indexList);
29
31
  await this.alterIndex(row.tableName, col.COLUMN_NAME, col.IS_INDEX, indexList);
30
- await this.alterPossibleEnum(row.tableName, col.COLUMN_NAME, col.POSSIBLE_VALUE, col.IS_NOT_NULL);
32
+ if (col.DATA_TYPE === "enum")
33
+ await this.alterPossibleEnum(row.tableName, col.COLUMN_NAME, col.POSSIBLE_VALUE, col.IS_NOT_NULL);
31
34
  await this.alterForeignKey(row.tableName, col);
32
35
  }
33
36
  await this.alterColumnCombination(row.tableName, row.compoisteColumn);
@@ -68,7 +71,7 @@ class MyDBMigrator {
68
71
  return data.length ? true : false;
69
72
  }
70
73
  async alterForeignKey(tableName, col) {
71
- console.log("alterForeignKey()");
74
+ // console.log("alterForeignKey()")
72
75
  if (col.FOREIGN_KEY) {
73
76
  for (const row of col.FOREIGN_KEY) {
74
77
  const CONSTRAINT = `fk_${tableName}_${col.COLUMN_NAME}_${row.tableName}_${row.columnName}`.slice(0, 64);
@@ -104,7 +107,9 @@ class MyDBMigrator {
104
107
  }
105
108
  }
106
109
  async alterDataType(tableName, col) {
107
- console.log("alterDataType()");
110
+ if (col.DATA_TYPE === "enum")
111
+ return null;
112
+ // console.log("alterDataType()")
108
113
  const AutoIncrement = col.AUTO_INCREMENT ? "AUTO_INCREMENT" : "";
109
114
  const columnDefault = col.COLUMN_DEFAULT
110
115
  ? `DEFAULT ${col.COLUMN_DEFAULT}`
@@ -122,15 +127,20 @@ class MyDBMigrator {
122
127
  }
123
128
  }
124
129
  async alterPrimaryKey(tableName, col) {
125
- console.log("alterPrimaryKey()");
130
+ // console.log("alterPrimaryKey()")
126
131
  if (col.COLUMN_NAME === "id" && col.AUTO_INCREMENT === true) {
127
- const isPrimaryKey = await this.checkIsPrimaryKey(this.MyDB.dbInfo.database, tableName, col.COLUMN_NAME);
128
- if (isPrimaryKey === false)
132
+ const isPrimaryKey = await this.checkIsPrimaryKey(tableName, col.COLUMN_NAME);
133
+ // recheck only id
134
+ if (isPrimaryKey === false && col.COLUMN_NAME === "id")
129
135
  await this.MyDB.query(`ALTER TABLE ${tableName} ADD PRIMARY KEY(id);`);
130
136
  }
131
137
  }
132
138
  async createTableIfNotExist(tableName) {
133
- await this.MyDB.query(`CREATE TABLE IF NOT EXISTS ${tableName} (id INT(11) AUTO_INCREMENT PRIMARY KEY)`);
139
+ const foundTable = this.onetimeLoadColumnContent.find((row) => row.TABLE_NAME === tableName);
140
+ if (!foundTable) {
141
+ await this.MyDB.query(`CREATE TABLE IF NOT EXISTS ${tableName} (id INT(11) AUTO_INCREMENT PRIMARY KEY)`);
142
+ this.loadColumnContentIfEmpty();
143
+ }
134
144
  }
135
145
  async columnInTableExist(tableName, columnName) {
136
146
  await this.loadColumnContentIfEmpty();
@@ -144,6 +154,9 @@ class MyDBMigrator {
144
154
  if (this.onetimeLoadColumnContent.length === 0) {
145
155
  this.onetimeLoadColumnContent = (await this.MyDB.query(`SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '${this.MyDB.dbInfo.database}'`, undefined, false));
146
156
  }
157
+ // const temp = this.onetimeLoadColumnContent.forEach((row)=>{
158
+ // if(row.COLUMN_TYPE === '')
159
+ // })
147
160
  }
148
161
  async checkIsColumnDataTypeChange(tableName, columnName, dataType, columnDefault) {
149
162
  await this.loadColumnContentIfEmpty();
@@ -154,11 +167,18 @@ class MyDBMigrator {
154
167
  columnDefault = "NULL";
155
168
  if (temp.length > 0) {
156
169
  if (temp[0].DATA_TYPE !== dataType) {
157
- console.log(chalk_1.default.green `${temp[0].DATA_TYPE} change to ${dataType}`);
158
- return true;
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
+ }
159
179
  }
160
180
  if (temp[0].COLUMN_DEFAULT !== columnDefault) {
161
- 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}`);
162
182
  return true;
163
183
  }
164
184
  return false;
@@ -184,7 +204,7 @@ class MyDBMigrator {
184
204
  }
185
205
  }
186
206
  async alterIndex(tableName, columnName, IS_INDEX, indexList) {
187
- console.log("alterIndex()");
207
+ // console.log("alterIndex()")
188
208
  const isIndex = Boolean(IS_INDEX);
189
209
  const indexIndexName = `${columnName}_index`;
190
210
  const isFound = indexList.find((iRow) => iRow.Key_name === indexIndexName)
@@ -195,25 +215,18 @@ class MyDBMigrator {
195
215
  else if (isIndex === false && isFound === true)
196
216
  await this.MyDB.query(`DROP INDEX ${indexIndexName} ON ${tableName};`);
197
217
  }
198
- async deleteOldIndexVersion(tableName, columnName, indexList) {
199
- console.log("deleteOldIndexVersion()");
200
- const indexIndexName = `${columnName}`;
201
- const isFound = indexList.find((iRow) => iRow.Key_name === indexIndexName)
202
- ? true
203
- : false;
204
- if (isFound === true)
205
- await this.MyDB.query(`DROP INDEX ${indexIndexName} ON ${tableName};`);
206
- }
207
218
  async alterPossibleEnum(tableName, columnName, possibleValue, isNotNull) {
208
- console.log("alterPossibleEnum()");
219
+ // console.log("alterPossibleEnum()")
209
220
  if (!possibleValue)
210
221
  return null;
211
222
  const data = this.onetimeLoadColumnContent.find((row) => row.COLUMN_NAME === columnName && row.TABLE_NAME === tableName);
223
+ const possibleValueString = possibleValue.map((row) => `'${row}'`).join(",");
224
+ const enumType = `enum(${possibleValueString})`;
225
+ // console.log("enum ->", data?.COLUMN_TYPE)
226
+ // console.log("enumType ->", enumType)
212
227
  const notNull = isNotNull ? "NOT NULL" : "";
213
- if (data) {
214
- let possibleValueString = possibleValue
215
- .map((row) => `'${row}'`)
216
- .join(", ");
228
+ if (data && enumType !== data.COLUMN_TYPE) {
229
+ // console.log(chalk.red("alter enum"))
217
230
  try {
218
231
  await this.MyDB.query(`ALTER TABLE ${tableName} MODIFY COLUMN ${columnName} ENUM(${possibleValueString}) ${notNull} ;`);
219
232
  }
@@ -261,15 +274,15 @@ class MyDBMigrator {
261
274
  loopCount++;
262
275
  }
263
276
  }
264
- async checkIsPrimaryKey(databaseName, tableName, columnName) {
265
- let result = await this.MyDB.query(`SELECT *
266
- FROM INFORMATION_SCHEMA.COLUMNS
267
- WHERE TABLE_SCHEMA='${databaseName}'
268
- AND TABLE_NAME='${tableName}'
269
- AND COLUMN_NAME='${columnName}'`);
270
- console.log("result", result);
271
- if (result.length) {
272
- if (result[0].COLUMN_KEY === "PRI")
277
+ async checkIsPrimaryKey(tableName, columnName) {
278
+ // let result: any = 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((row) => row.TABLE_NAME === tableName && columnName === columnName);
284
+ if (result2) {
285
+ if (result2.COLUMN_KEY === "PRI")
273
286
  return true;
274
287
  else
275
288
  return false;
@@ -1,11 +1,19 @@
1
1
  import { SQLStatement } from "sql-template-strings";
2
- export type columnType = "int" | "tinyint" | "float" | "double" | "text" | "varchar(8)" | "varchar(16)" | "varchar(32)" | "varchar(64)" | "varchar(128)" | "varchar(256)" | "varchar(512)" | "longtext" | "date" | "time" | "datetime";
3
- export interface columnContent {
2
+ export type columnType = "int" | "tinyint" | "float" | "double" | "text" | "varchar(8)" | "varchar(16)" | "varchar(32)" | "varchar(64)" | "varchar(128)" | "varchar(256)" | "varchar(512)" | "longtext" | "date" | "time" | "datetime" | "enum";
3
+ type ColumnTypeMap = {
4
+ [K in Exclude<columnType, "enum">]: BaseColumn & {
5
+ DATA_TYPE: K;
6
+ };
7
+ } & {
8
+ enum: BaseColumn & {
9
+ DATA_TYPE: "enum";
10
+ POSSIBLE_VALUE: string[];
11
+ };
12
+ };
13
+ type BaseColumn = {
4
14
  COLUMN_NAME: string;
5
- DATA_TYPE: columnType;
6
15
  COLUMN_DEFAULT?: string | number | null;
7
16
  AUTO_INCREMENT?: boolean;
8
- POSSIBLE_VALUE?: string[];
9
17
  FOREIGN_KEY?: {
10
18
  tableName: string;
11
19
  columnName: string;
@@ -15,7 +23,9 @@ export interface columnContent {
15
23
  IS_UNIQUE?: boolean;
16
24
  IS_INDEX?: boolean;
17
25
  IS_NOT_NULL?: boolean;
18
- }
26
+ };
27
+ export type ColumnContent = ColumnTypeMap[keyof ColumnTypeMap];
28
+ export type columnContent = ColumnContent;
19
29
  export interface compoisteColumn {
20
30
  type: "UNIQUE" | "INDEX";
21
31
  columnName: string[];
@@ -31,3 +41,4 @@ export interface MyViewModel {
31
41
  viewName: string;
32
42
  sqlStatement: string | SQLStatement;
33
43
  }
44
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gg-mysql-connector",
3
- "version": "1.0.123",
3
+ "version": "1.0.129",
4
4
  "description": "",
5
5
  "main": "dist/src/index.js",
6
6
  "scripts": {
@@ -187,7 +187,11 @@ export default class ModelGenerator {
187
187
  (row) => row.COLUMN_NAME === columnName
188
188
  )
189
189
  // find in possible value in table model first
190
- if (foundColumn && foundColumn.POSSIBLE_VALUE) {
190
+ if (
191
+ foundColumn &&
192
+ foundColumn.DATA_TYPE === "enum" &&
193
+ foundColumn.POSSIBLE_VALUE
194
+ ) {
191
195
  return foundColumn.POSSIBLE_VALUE
192
196
  } else {
193
197
  return false
@@ -3,6 +3,10 @@ import { Unarray } from "../GGMySQLConnector/GGMySQLConnector"
3
3
  import ModelGenerator from "../ModelGenerator/ModelGenerator"
4
4
  import { columnType, MyModel, MyViewModel } from "../myModel"
5
5
  import sql from "sql-template-strings"
6
+ import { table } from "console"
7
+ function keepOnlyDigits(str: string): number {
8
+ return Number(str.replace(/\D/g, ""))
9
+ }
6
10
  export default class MyDBMigrator {
7
11
  MyDB: ModelGenerator
8
12
  onetimeLoadColumnContent: {
@@ -12,6 +16,7 @@ export default class MyDBMigrator {
12
16
  COLUMN_DEFAULT: string
13
17
  COLUMN_KEY: string
14
18
  COLUMN_TYPE: string
19
+ CHARACTER_MAXIMUM_LENGTH: number | null
15
20
  }[]
16
21
  foreignKeyStatementList: string[]
17
22
 
@@ -27,12 +32,12 @@ export default class MyDBMigrator {
27
32
  row.tableName !== "sessions" && row.tableName.search("prisma") < 0
28
33
  )
29
34
  let index = 1
35
+ this.onetimeLoadColumnContent = []
36
+ await this.loadColumnContentIfEmpty()
30
37
  for (let row of data) {
31
38
  console.log(chalk.bgBlue(`${index}/${data.length} ${row.tableName}`))
32
- console.log(row)
33
39
  await this.createTableIfNotExist(row.tableName)
34
- this.onetimeLoadColumnContent = []
35
- await this.loadColumnContentIfEmpty()
40
+
36
41
  index++
37
42
 
38
43
  const indexList = (await this.MyDB.query(
@@ -55,13 +60,13 @@ export default class MyDBMigrator {
55
60
  col.IS_INDEX,
56
61
  indexList
57
62
  )
58
-
59
- await this.alterPossibleEnum(
60
- row.tableName,
61
- col.COLUMN_NAME,
62
- col.POSSIBLE_VALUE,
63
- col.IS_NOT_NULL
64
- )
63
+ if (col.DATA_TYPE === "enum")
64
+ await this.alterPossibleEnum(
65
+ row.tableName,
66
+ col.COLUMN_NAME,
67
+ col.POSSIBLE_VALUE,
68
+ col.IS_NOT_NULL
69
+ )
65
70
  await this.alterForeignKey(row.tableName, col)
66
71
  }
67
72
  await this.alterColumnCombination(row.tableName, row.compoisteColumn)
@@ -113,7 +118,7 @@ export default class MyDBMigrator {
113
118
  tableName: string,
114
119
  col: Unarray<MyModel["columns"]>
115
120
  ) {
116
- console.log("alterForeignKey()")
121
+ // console.log("alterForeignKey()")
117
122
  if (col.FOREIGN_KEY) {
118
123
  for (const row of col.FOREIGN_KEY) {
119
124
  const CONSTRAINT =
@@ -121,7 +126,6 @@ export default class MyDBMigrator {
121
126
  0,
122
127
  64
123
128
  )
124
-
125
129
  const constraintExist = await this.isConstraintNameExist(CONSTRAINT)
126
130
  if (constraintExist) {
127
131
  await this.MyDB.query(
@@ -166,7 +170,8 @@ export default class MyDBMigrator {
166
170
  tableName: string,
167
171
  col: Unarray<MyModel["columns"]>
168
172
  ) {
169
- console.log("alterDataType()")
173
+ if (col.DATA_TYPE === "enum") return null
174
+ // console.log("alterDataType()")
170
175
  const AutoIncrement = col.AUTO_INCREMENT ? "AUTO_INCREMENT" : ""
171
176
  const columnDefault = col.COLUMN_DEFAULT
172
177
  ? `DEFAULT ${col.COLUMN_DEFAULT}`
@@ -199,22 +204,28 @@ export default class MyDBMigrator {
199
204
  tableName: string,
200
205
  col: Unarray<MyModel["columns"]>
201
206
  ) {
202
- console.log("alterPrimaryKey()")
207
+ // console.log("alterPrimaryKey()")
203
208
  if (col.COLUMN_NAME === "id" && col.AUTO_INCREMENT === true) {
204
209
  const isPrimaryKey = await this.checkIsPrimaryKey(
205
- this.MyDB.dbInfo.database,
206
210
  tableName,
207
211
  col.COLUMN_NAME
208
212
  )
209
- if (isPrimaryKey === false)
213
+ // recheck only id
214
+ if (isPrimaryKey === false && col.COLUMN_NAME === "id")
210
215
  await this.MyDB.query(`ALTER TABLE ${tableName} ADD PRIMARY KEY(id);`)
211
216
  }
212
217
  }
213
218
 
214
219
  private async createTableIfNotExist(tableName: string) {
215
- await this.MyDB.query(
216
- `CREATE TABLE IF NOT EXISTS ${tableName} (id INT(11) AUTO_INCREMENT PRIMARY KEY)`
220
+ const foundTable = this.onetimeLoadColumnContent.find(
221
+ (row) => row.TABLE_NAME === tableName
217
222
  )
223
+ if (!foundTable) {
224
+ await this.MyDB.query(
225
+ `CREATE TABLE IF NOT EXISTS ${tableName} (id INT(11) AUTO_INCREMENT PRIMARY KEY)`
226
+ )
227
+ this.loadColumnContentIfEmpty()
228
+ }
218
229
  }
219
230
 
220
231
  private async columnInTableExist(tableName: string, columnName: string) {
@@ -233,30 +244,42 @@ export default class MyDBMigrator {
233
244
  false
234
245
  )) as any
235
246
  }
247
+ // const temp = this.onetimeLoadColumnContent.forEach((row)=>{
248
+ // if(row.COLUMN_TYPE === '')
249
+ // })
236
250
  }
237
251
 
238
252
  private async checkIsColumnDataTypeChange(
239
253
  tableName: string,
240
254
  columnName: string,
241
255
  dataType: columnType,
242
- columnDefault: string | null | undefined | number
256
+ columnDefault: string | number | null | undefined
243
257
  ) {
244
258
  await this.loadColumnContentIfEmpty()
245
259
  let temp = this.onetimeLoadColumnContent.filter(
246
- (row: any) =>
247
- row.TABLE_NAME === tableName && row.COLUMN_NAME === columnName
260
+ (row) => row.TABLE_NAME === tableName && row.COLUMN_NAME === columnName
248
261
  )
249
262
  if (columnDefault === undefined) columnDefault = "NULL"
250
263
  if (columnDefault === null) columnDefault = "NULL"
251
264
 
252
265
  if (temp.length > 0) {
253
266
  if (temp[0].DATA_TYPE !== dataType) {
254
- console.log(chalk.green`${temp[0].DATA_TYPE} change to ${dataType}`)
255
- return true
267
+ if (temp[0].DATA_TYPE.search("varchar") >= 0) {
268
+ if (
269
+ temp[0].CHARACTER_MAXIMUM_LENGTH !==
270
+ keepOnlyDigits(temp[0].DATA_TYPE)
271
+ )
272
+ return true
273
+ } else {
274
+ console.log(
275
+ chalk.green`DATA_TYPE -> ${temp[0].DATA_TYPE} change to ${dataType}`
276
+ )
277
+ return true
278
+ }
256
279
  }
257
280
  if (temp[0].COLUMN_DEFAULT !== columnDefault) {
258
281
  console.log(
259
- chalk.green`${temp[0].COLUMN_DEFAULT} change to ${columnDefault}`
282
+ chalk.green`COLUMN_DEFAULT -> ${temp[0].COLUMN_DEFAULT} change to ${columnDefault}`
260
283
  )
261
284
  return true
262
285
  }
@@ -296,7 +319,7 @@ export default class MyDBMigrator {
296
319
  IS_INDEX: boolean | undefined,
297
320
  indexList: { Key_name: string }[]
298
321
  ) {
299
- console.log("alterIndex()")
322
+ // console.log("alterIndex()")
300
323
  const isIndex = Boolean(IS_INDEX)
301
324
  const indexIndexName = `${columnName}_index`
302
325
  const isFound = indexList.find((iRow) => iRow.Key_name === indexIndexName)
@@ -310,38 +333,24 @@ export default class MyDBMigrator {
310
333
  await this.MyDB.query(`DROP INDEX ${indexIndexName} ON ${tableName};`)
311
334
  }
312
335
 
313
- private async deleteOldIndexVersion(
314
- tableName: string,
315
- columnName: string,
316
- indexList: { Key_name: string }[]
317
- ) {
318
- console.log("deleteOldIndexVersion()")
319
- const indexIndexName = `${columnName}`
320
- const isFound = indexList.find((iRow) => iRow.Key_name === indexIndexName)
321
- ? true
322
- : false
323
-
324
- if (isFound === true)
325
- await this.MyDB.query(`DROP INDEX ${indexIndexName} ON ${tableName};`)
326
- }
327
-
328
336
  private async alterPossibleEnum(
329
337
  tableName: string,
330
338
  columnName: string,
331
339
  possibleValue?: string[],
332
340
  isNotNull?: boolean
333
341
  ) {
334
- console.log("alterPossibleEnum()")
342
+ // console.log("alterPossibleEnum()")
335
343
  if (!possibleValue) return null
336
344
  const data = this.onetimeLoadColumnContent.find(
337
345
  (row) => row.COLUMN_NAME === columnName && row.TABLE_NAME === tableName
338
346
  )
347
+ const possibleValueString = possibleValue.map((row) => `'${row}'`).join(",")
348
+ const enumType = `enum(${possibleValueString})`
349
+ // console.log("enum ->", data?.COLUMN_TYPE)
350
+ // console.log("enumType ->", enumType)
339
351
  const notNull = isNotNull ? "NOT NULL" : ""
340
- if (data) {
341
- let possibleValueString = possibleValue
342
- .map((row) => `'${row}'`)
343
- .join(", ")
344
-
352
+ if (data && enumType !== data.COLUMN_TYPE) {
353
+ // console.log(chalk.red("alter enum"))
345
354
  try {
346
355
  await this.MyDB.query(
347
356
  `ALTER TABLE ${tableName} MODIFY COLUMN ${columnName} ENUM(${possibleValueString}) ${notNull} ;`
@@ -351,11 +360,9 @@ export default class MyDBMigrator {
351
360
  `SELECT DISTINCT(${columnName}) as columnValue FROM ${tableName}`
352
361
  )
353
362
  const valueInColumn = data.map((dRow) => `['${dRow.columnValue}']`)
354
-
355
363
  const problemValue = valueInColumn.filter(
356
364
  (row) => !possibleValue.includes(row)
357
365
  )
358
-
359
366
  console.log(
360
367
  chalk.bgRed.white(
361
368
  `error table(${tableName}) - column (${columnName}) has problem value is ${problemValue.join(
@@ -406,19 +413,19 @@ export default class MyDBMigrator {
406
413
  }
407
414
  }
408
415
 
409
- private async checkIsPrimaryKey(
410
- databaseName: string,
411
- tableName: string,
412
- columnName: string
413
- ) {
414
- let result: any = await this.MyDB.query(`SELECT *
415
- FROM INFORMATION_SCHEMA.COLUMNS
416
- WHERE TABLE_SCHEMA='${databaseName}'
417
- AND TABLE_NAME='${tableName}'
418
- AND COLUMN_NAME='${columnName}'`)
419
- console.log("result", result)
420
- if (result.length) {
421
- if (result[0].COLUMN_KEY === "PRI") return true
416
+ private async checkIsPrimaryKey(tableName: string, columnName: string) {
417
+ // let result: any = await this.MyDB.query(`SELECT *
418
+ // FROM INFORMATION_SCHEMA.COLUMNS
419
+ // WHERE TABLE_SCHEMA='${databaseName}'
420
+ // AND TABLE_NAME='${tableName}'
421
+ // AND COLUMN_NAME='${columnName}'`)
422
+
423
+ const result2 = this.onetimeLoadColumnContent.find(
424
+ (row) => row.TABLE_NAME === tableName && columnName === columnName
425
+ )
426
+
427
+ if (result2) {
428
+ if (result2.COLUMN_KEY === "PRI") return true
422
429
  else return false
423
430
  } else return false
424
431
  }
package/src/myModel.ts CHANGED
@@ -17,12 +17,33 @@ export type columnType =
17
17
  | "date"
18
18
  | "time"
19
19
  | "datetime"
20
- export interface columnContent {
20
+ | "enum"
21
+ // export interface columnContent {
22
+ // COLUMN_NAME: string
23
+ // DATA_TYPE: columnType
24
+ // COLUMN_DEFAULT?: string | number | null
25
+ // AUTO_INCREMENT?: boolean
26
+ // POSSIBLE_VALUE?: string[]
27
+ // FOREIGN_KEY?: {
28
+ // tableName: string
29
+ // columnName: string
30
+ // ON_UPDATE: "CASCADE" | "SET NULL" | "RESTRICT"
31
+ // ON_DELETE: "CASCADE" | "SET NULL" | "RESTRICT"
32
+ // }[]
33
+ // IS_UNIQUE?: boolean
34
+ // IS_INDEX?: boolean
35
+ // IS_NOT_NULL?: boolean
36
+ // }
37
+ type ColumnTypeMap = {
38
+ [K in Exclude<columnType, "enum">]: BaseColumn & { DATA_TYPE: K }
39
+ } & {
40
+ enum: BaseColumn & { DATA_TYPE: "enum"; POSSIBLE_VALUE: string[] }
41
+ }
42
+
43
+ type BaseColumn = {
21
44
  COLUMN_NAME: string
22
- DATA_TYPE: columnType
23
45
  COLUMN_DEFAULT?: string | number | null
24
46
  AUTO_INCREMENT?: boolean
25
- POSSIBLE_VALUE?: string[]
26
47
  FOREIGN_KEY?: {
27
48
  tableName: string
28
49
  columnName: string
@@ -33,7 +54,25 @@ export interface columnContent {
33
54
  IS_INDEX?: boolean
34
55
  IS_NOT_NULL?: boolean
35
56
  }
57
+ // export type columnContent = {
58
+ // COLUMN_NAME: string
59
+ // DATA_TYPE: columnType
60
+ // COLUMN_DEFAULT?: string | number | null
61
+ // AUTO_INCREMENT?: boolean
62
+ // POSSIBLE_VALUE?: string[]
63
+ // FOREIGN_KEY?: {
64
+ // tableName: string
65
+ // columnName: string
66
+ // ON_UPDATE: "CASCADE" | "SET NULL" | "RESTRICT"
67
+ // ON_DELETE: "CASCADE" | "SET NULL" | "RESTRICT"
68
+ // }[]
69
+ // IS_UNIQUE?: boolean
70
+ // IS_INDEX?: boolean
71
+ // IS_NOT_NULL?: boolean
72
+ // }
73
+ export type ColumnContent = ColumnTypeMap[keyof ColumnTypeMap]
36
74
 
75
+ export type columnContent = ColumnContent
37
76
  export interface compoisteColumn {
38
77
  type: "UNIQUE" | "INDEX"
39
78
  columnName: string[]