gg-mysql-connector 1.0.122 → 1.0.126
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/dist/src/GGMySQLConnector/GGMySQLConnector.js +3 -1
- package/dist/src/ModelGenerator/ModelGenerator.js +3 -1
- package/dist/src/MyDBMigrator/MyDBMigrator.d.ts +0 -1
- package/dist/src/MyDBMigrator/MyDBMigrator.js +39 -34
- package/dist/src/myModel.d.ts +16 -5
- package/package.json +1 -1
- package/src/GGMySQLConnector/GGMySQLConnector.ts +4 -2
- package/src/ModelGenerator/ModelGenerator.ts +5 -1
- package/src/MyDBMigrator/MyDBMigrator.ts +53 -58
- package/src/myModel.ts +42 -3
|
@@ -37,7 +37,9 @@ class GGMySQLConnector {
|
|
|
37
37
|
*/
|
|
38
38
|
async selectByDateRange(tableName, params) {
|
|
39
39
|
const { columnName, endDate, startDate } = params;
|
|
40
|
-
const result = (await this.query(
|
|
40
|
+
const result = (await this.query(
|
|
41
|
+
//@ts-ignore
|
|
42
|
+
`SELECT * FROM ${tableName} WHERE ? <= ${columnName} AND ${columnName} <= ?`, [startDate, endDate], false));
|
|
41
43
|
return result;
|
|
42
44
|
}
|
|
43
45
|
async createDatabaseIfNotExist(tempConnection) {
|
|
@@ -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 &&
|
|
140
|
+
if (foundColumn &&
|
|
141
|
+
foundColumn.DATA_TYPE === "enum" &&
|
|
142
|
+
foundColumn.POSSIBLE_VALUE) {
|
|
141
143
|
return foundColumn.POSSIBLE_VALUE;
|
|
142
144
|
}
|
|
143
145
|
else {
|
|
@@ -26,7 +26,6 @@ export default class MyDBMigrator {
|
|
|
26
26
|
private checkIsColumnDataTypeChange;
|
|
27
27
|
private alterUniqueKey;
|
|
28
28
|
private alterIndex;
|
|
29
|
-
private deleteOldIndexVersion;
|
|
30
29
|
private alterPossibleEnum;
|
|
31
30
|
migrateView_v2(viewModel: MyViewModel[]): Promise<void>;
|
|
32
31
|
private checkIsPrimaryKey;
|
|
@@ -13,12 +13,11 @@ class MyDBMigrator {
|
|
|
13
13
|
async migrateTable(model) {
|
|
14
14
|
let data = model.filter((row) => row.tableName !== "sessions" && row.tableName.search("prisma") < 0);
|
|
15
15
|
let index = 1;
|
|
16
|
+
this.onetimeLoadColumnContent = [];
|
|
17
|
+
await this.loadColumnContentIfEmpty();
|
|
16
18
|
for (let row of data) {
|
|
17
19
|
console.log(chalk_1.default.bgBlue(`${index}/${data.length} ${row.tableName}`));
|
|
18
|
-
console.log(row);
|
|
19
20
|
await this.createTableIfNotExist(row.tableName);
|
|
20
|
-
this.onetimeLoadColumnContent = [];
|
|
21
|
-
await this.loadColumnContentIfEmpty();
|
|
22
21
|
index++;
|
|
23
22
|
const indexList = (await this.MyDB.query(`SHOW INDEX FROM ${row.tableName}`));
|
|
24
23
|
for (let col of row.columns) {
|
|
@@ -27,7 +26,8 @@ class MyDBMigrator {
|
|
|
27
26
|
await this.alterDataType(row.tableName, col);
|
|
28
27
|
await this.alterUniqueKey(row.tableName, col.COLUMN_NAME, col.IS_UNIQUE, indexList);
|
|
29
28
|
await this.alterIndex(row.tableName, col.COLUMN_NAME, col.IS_INDEX, indexList);
|
|
30
|
-
|
|
29
|
+
if (col.DATA_TYPE === "enum")
|
|
30
|
+
await this.alterPossibleEnum(row.tableName, col.COLUMN_NAME, col.POSSIBLE_VALUE, col.IS_NOT_NULL);
|
|
31
31
|
await this.alterForeignKey(row.tableName, col);
|
|
32
32
|
}
|
|
33
33
|
await this.alterColumnCombination(row.tableName, row.compoisteColumn);
|
|
@@ -68,7 +68,7 @@ class MyDBMigrator {
|
|
|
68
68
|
return data.length ? true : false;
|
|
69
69
|
}
|
|
70
70
|
async alterForeignKey(tableName, col) {
|
|
71
|
-
console.log("alterForeignKey()")
|
|
71
|
+
// console.log("alterForeignKey()")
|
|
72
72
|
if (col.FOREIGN_KEY) {
|
|
73
73
|
for (const row of col.FOREIGN_KEY) {
|
|
74
74
|
const CONSTRAINT = `fk_${tableName}_${col.COLUMN_NAME}_${row.tableName}_${row.columnName}`.slice(0, 64);
|
|
@@ -104,7 +104,9 @@ class MyDBMigrator {
|
|
|
104
104
|
}
|
|
105
105
|
}
|
|
106
106
|
async alterDataType(tableName, col) {
|
|
107
|
-
|
|
107
|
+
if (col.DATA_TYPE === "enum")
|
|
108
|
+
return null;
|
|
109
|
+
// console.log("alterDataType()")
|
|
108
110
|
const AutoIncrement = col.AUTO_INCREMENT ? "AUTO_INCREMENT" : "";
|
|
109
111
|
const columnDefault = col.COLUMN_DEFAULT
|
|
110
112
|
? `DEFAULT ${col.COLUMN_DEFAULT}`
|
|
@@ -122,15 +124,20 @@ class MyDBMigrator {
|
|
|
122
124
|
}
|
|
123
125
|
}
|
|
124
126
|
async alterPrimaryKey(tableName, col) {
|
|
125
|
-
console.log("alterPrimaryKey()")
|
|
127
|
+
// console.log("alterPrimaryKey()")
|
|
126
128
|
if (col.COLUMN_NAME === "id" && col.AUTO_INCREMENT === true) {
|
|
127
|
-
const isPrimaryKey = await this.checkIsPrimaryKey(
|
|
128
|
-
|
|
129
|
+
const isPrimaryKey = await this.checkIsPrimaryKey(tableName, col.COLUMN_NAME);
|
|
130
|
+
// recheck only id
|
|
131
|
+
if (isPrimaryKey === false && col.COLUMN_NAME === "id")
|
|
129
132
|
await this.MyDB.query(`ALTER TABLE ${tableName} ADD PRIMARY KEY(id);`);
|
|
130
133
|
}
|
|
131
134
|
}
|
|
132
135
|
async createTableIfNotExist(tableName) {
|
|
133
|
-
|
|
136
|
+
const foundTable = this.onetimeLoadColumnContent.find((row) => row.TABLE_NAME === tableName);
|
|
137
|
+
if (!foundTable) {
|
|
138
|
+
await this.MyDB.query(`CREATE TABLE IF NOT EXISTS ${tableName} (id INT(11) AUTO_INCREMENT PRIMARY KEY)`);
|
|
139
|
+
this.loadColumnContentIfEmpty();
|
|
140
|
+
}
|
|
134
141
|
}
|
|
135
142
|
async columnInTableExist(tableName, columnName) {
|
|
136
143
|
await this.loadColumnContentIfEmpty();
|
|
@@ -156,6 +163,11 @@ class MyDBMigrator {
|
|
|
156
163
|
if (temp[0].DATA_TYPE !== dataType) {
|
|
157
164
|
console.log(chalk_1.default.green `${temp[0].DATA_TYPE} change to ${dataType}`);
|
|
158
165
|
return true;
|
|
166
|
+
// if (temp[0].DATA_TYPE === "enum") {
|
|
167
|
+
// return true
|
|
168
|
+
// } else {
|
|
169
|
+
// return true
|
|
170
|
+
// }
|
|
159
171
|
}
|
|
160
172
|
if (temp[0].COLUMN_DEFAULT !== columnDefault) {
|
|
161
173
|
console.log(chalk_1.default.green `${temp[0].COLUMN_DEFAULT} change to ${columnDefault}`);
|
|
@@ -184,7 +196,7 @@ class MyDBMigrator {
|
|
|
184
196
|
}
|
|
185
197
|
}
|
|
186
198
|
async alterIndex(tableName, columnName, IS_INDEX, indexList) {
|
|
187
|
-
console.log("alterIndex()")
|
|
199
|
+
// console.log("alterIndex()")
|
|
188
200
|
const isIndex = Boolean(IS_INDEX);
|
|
189
201
|
const indexIndexName = `${columnName}_index`;
|
|
190
202
|
const isFound = indexList.find((iRow) => iRow.Key_name === indexIndexName)
|
|
@@ -195,25 +207,18 @@ class MyDBMigrator {
|
|
|
195
207
|
else if (isIndex === false && isFound === true)
|
|
196
208
|
await this.MyDB.query(`DROP INDEX ${indexIndexName} ON ${tableName};`);
|
|
197
209
|
}
|
|
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
210
|
async alterPossibleEnum(tableName, columnName, possibleValue, isNotNull) {
|
|
208
|
-
console.log("alterPossibleEnum()")
|
|
211
|
+
// console.log("alterPossibleEnum()")
|
|
209
212
|
if (!possibleValue)
|
|
210
213
|
return null;
|
|
211
214
|
const data = this.onetimeLoadColumnContent.find((row) => row.COLUMN_NAME === columnName && row.TABLE_NAME === tableName);
|
|
215
|
+
const possibleValueString = possibleValue.map((row) => `'${row}'`).join(",");
|
|
216
|
+
const enumType = `enum(${possibleValueString})`;
|
|
217
|
+
// console.log("enum ->", data?.COLUMN_TYPE)
|
|
218
|
+
// console.log("enumType ->", enumType)
|
|
212
219
|
const notNull = isNotNull ? "NOT NULL" : "";
|
|
213
|
-
if (data) {
|
|
214
|
-
|
|
215
|
-
.map((row) => `'${row}'`)
|
|
216
|
-
.join(", ");
|
|
220
|
+
if (data && enumType !== data.COLUMN_TYPE) {
|
|
221
|
+
// console.log(chalk.red("alter enum"))
|
|
217
222
|
try {
|
|
218
223
|
await this.MyDB.query(`ALTER TABLE ${tableName} MODIFY COLUMN ${columnName} ENUM(${possibleValueString}) ${notNull} ;`);
|
|
219
224
|
}
|
|
@@ -261,15 +266,15 @@ class MyDBMigrator {
|
|
|
261
266
|
loopCount++;
|
|
262
267
|
}
|
|
263
268
|
}
|
|
264
|
-
async checkIsPrimaryKey(
|
|
265
|
-
let result = await this.MyDB.query(`SELECT *
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
AND TABLE_NAME='${tableName}'
|
|
269
|
-
AND COLUMN_NAME='${columnName}'`)
|
|
270
|
-
|
|
271
|
-
if (
|
|
272
|
-
if (
|
|
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")
|
|
273
278
|
return true;
|
|
274
279
|
else
|
|
275
280
|
return false;
|
package/dist/src/myModel.d.ts
CHANGED
|
@@ -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
|
-
|
|
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
|
@@ -100,9 +100,11 @@ export default class GGMySQLConnector<Main> implements ClassDBInterface<Main> {
|
|
|
100
100
|
}
|
|
101
101
|
): Promise<Main[T][]> {
|
|
102
102
|
const { columnName, endDate, startDate } = params
|
|
103
|
+
|
|
103
104
|
const result = (await this.query(
|
|
104
|
-
|
|
105
|
-
|
|
105
|
+
//@ts-ignore
|
|
106
|
+
`SELECT * FROM ${tableName} WHERE ? <= ${columnName} AND ${columnName} <= ?`,
|
|
107
|
+
[startDate, endDate],
|
|
106
108
|
false
|
|
107
109
|
)) as Main[T][]
|
|
108
110
|
return result
|
|
@@ -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 (
|
|
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,7 @@ 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"
|
|
6
7
|
export default class MyDBMigrator {
|
|
7
8
|
MyDB: ModelGenerator
|
|
8
9
|
onetimeLoadColumnContent: {
|
|
@@ -27,12 +28,12 @@ export default class MyDBMigrator {
|
|
|
27
28
|
row.tableName !== "sessions" && row.tableName.search("prisma") < 0
|
|
28
29
|
)
|
|
29
30
|
let index = 1
|
|
31
|
+
this.onetimeLoadColumnContent = []
|
|
32
|
+
await this.loadColumnContentIfEmpty()
|
|
30
33
|
for (let row of data) {
|
|
31
34
|
console.log(chalk.bgBlue(`${index}/${data.length} ${row.tableName}`))
|
|
32
|
-
console.log(row)
|
|
33
35
|
await this.createTableIfNotExist(row.tableName)
|
|
34
|
-
|
|
35
|
-
await this.loadColumnContentIfEmpty()
|
|
36
|
+
|
|
36
37
|
index++
|
|
37
38
|
|
|
38
39
|
const indexList = (await this.MyDB.query(
|
|
@@ -55,13 +56,13 @@ export default class MyDBMigrator {
|
|
|
55
56
|
col.IS_INDEX,
|
|
56
57
|
indexList
|
|
57
58
|
)
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
59
|
+
if (col.DATA_TYPE === "enum")
|
|
60
|
+
await this.alterPossibleEnum(
|
|
61
|
+
row.tableName,
|
|
62
|
+
col.COLUMN_NAME,
|
|
63
|
+
col.POSSIBLE_VALUE,
|
|
64
|
+
col.IS_NOT_NULL
|
|
65
|
+
)
|
|
65
66
|
await this.alterForeignKey(row.tableName, col)
|
|
66
67
|
}
|
|
67
68
|
await this.alterColumnCombination(row.tableName, row.compoisteColumn)
|
|
@@ -113,7 +114,7 @@ export default class MyDBMigrator {
|
|
|
113
114
|
tableName: string,
|
|
114
115
|
col: Unarray<MyModel["columns"]>
|
|
115
116
|
) {
|
|
116
|
-
console.log("alterForeignKey()")
|
|
117
|
+
// console.log("alterForeignKey()")
|
|
117
118
|
if (col.FOREIGN_KEY) {
|
|
118
119
|
for (const row of col.FOREIGN_KEY) {
|
|
119
120
|
const CONSTRAINT =
|
|
@@ -121,7 +122,6 @@ export default class MyDBMigrator {
|
|
|
121
122
|
0,
|
|
122
123
|
64
|
|
123
124
|
)
|
|
124
|
-
|
|
125
125
|
const constraintExist = await this.isConstraintNameExist(CONSTRAINT)
|
|
126
126
|
if (constraintExist) {
|
|
127
127
|
await this.MyDB.query(
|
|
@@ -166,7 +166,8 @@ export default class MyDBMigrator {
|
|
|
166
166
|
tableName: string,
|
|
167
167
|
col: Unarray<MyModel["columns"]>
|
|
168
168
|
) {
|
|
169
|
-
|
|
169
|
+
if (col.DATA_TYPE === "enum") return null
|
|
170
|
+
// console.log("alterDataType()")
|
|
170
171
|
const AutoIncrement = col.AUTO_INCREMENT ? "AUTO_INCREMENT" : ""
|
|
171
172
|
const columnDefault = col.COLUMN_DEFAULT
|
|
172
173
|
? `DEFAULT ${col.COLUMN_DEFAULT}`
|
|
@@ -199,22 +200,28 @@ export default class MyDBMigrator {
|
|
|
199
200
|
tableName: string,
|
|
200
201
|
col: Unarray<MyModel["columns"]>
|
|
201
202
|
) {
|
|
202
|
-
console.log("alterPrimaryKey()")
|
|
203
|
+
// console.log("alterPrimaryKey()")
|
|
203
204
|
if (col.COLUMN_NAME === "id" && col.AUTO_INCREMENT === true) {
|
|
204
205
|
const isPrimaryKey = await this.checkIsPrimaryKey(
|
|
205
|
-
this.MyDB.dbInfo.database,
|
|
206
206
|
tableName,
|
|
207
207
|
col.COLUMN_NAME
|
|
208
208
|
)
|
|
209
|
-
|
|
209
|
+
// recheck only id
|
|
210
|
+
if (isPrimaryKey === false && col.COLUMN_NAME === "id")
|
|
210
211
|
await this.MyDB.query(`ALTER TABLE ${tableName} ADD PRIMARY KEY(id);`)
|
|
211
212
|
}
|
|
212
213
|
}
|
|
213
214
|
|
|
214
215
|
private async createTableIfNotExist(tableName: string) {
|
|
215
|
-
|
|
216
|
-
|
|
216
|
+
const foundTable = this.onetimeLoadColumnContent.find(
|
|
217
|
+
(row) => row.TABLE_NAME === tableName
|
|
217
218
|
)
|
|
219
|
+
if (!foundTable) {
|
|
220
|
+
await this.MyDB.query(
|
|
221
|
+
`CREATE TABLE IF NOT EXISTS ${tableName} (id INT(11) AUTO_INCREMENT PRIMARY KEY)`
|
|
222
|
+
)
|
|
223
|
+
this.loadColumnContentIfEmpty()
|
|
224
|
+
}
|
|
218
225
|
}
|
|
219
226
|
|
|
220
227
|
private async columnInTableExist(tableName: string, columnName: string) {
|
|
@@ -239,12 +246,11 @@ export default class MyDBMigrator {
|
|
|
239
246
|
tableName: string,
|
|
240
247
|
columnName: string,
|
|
241
248
|
dataType: columnType,
|
|
242
|
-
columnDefault: string |
|
|
249
|
+
columnDefault: string | number | null | undefined
|
|
243
250
|
) {
|
|
244
251
|
await this.loadColumnContentIfEmpty()
|
|
245
252
|
let temp = this.onetimeLoadColumnContent.filter(
|
|
246
|
-
(row
|
|
247
|
-
row.TABLE_NAME === tableName && row.COLUMN_NAME === columnName
|
|
253
|
+
(row) => row.TABLE_NAME === tableName && row.COLUMN_NAME === columnName
|
|
248
254
|
)
|
|
249
255
|
if (columnDefault === undefined) columnDefault = "NULL"
|
|
250
256
|
if (columnDefault === null) columnDefault = "NULL"
|
|
@@ -253,6 +259,11 @@ export default class MyDBMigrator {
|
|
|
253
259
|
if (temp[0].DATA_TYPE !== dataType) {
|
|
254
260
|
console.log(chalk.green`${temp[0].DATA_TYPE} change to ${dataType}`)
|
|
255
261
|
return true
|
|
262
|
+
// if (temp[0].DATA_TYPE === "enum") {
|
|
263
|
+
// return true
|
|
264
|
+
// } else {
|
|
265
|
+
// return true
|
|
266
|
+
// }
|
|
256
267
|
}
|
|
257
268
|
if (temp[0].COLUMN_DEFAULT !== columnDefault) {
|
|
258
269
|
console.log(
|
|
@@ -296,7 +307,7 @@ export default class MyDBMigrator {
|
|
|
296
307
|
IS_INDEX: boolean | undefined,
|
|
297
308
|
indexList: { Key_name: string }[]
|
|
298
309
|
) {
|
|
299
|
-
console.log("alterIndex()")
|
|
310
|
+
// console.log("alterIndex()")
|
|
300
311
|
const isIndex = Boolean(IS_INDEX)
|
|
301
312
|
const indexIndexName = `${columnName}_index`
|
|
302
313
|
const isFound = indexList.find((iRow) => iRow.Key_name === indexIndexName)
|
|
@@ -310,38 +321,24 @@ export default class MyDBMigrator {
|
|
|
310
321
|
await this.MyDB.query(`DROP INDEX ${indexIndexName} ON ${tableName};`)
|
|
311
322
|
}
|
|
312
323
|
|
|
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
324
|
private async alterPossibleEnum(
|
|
329
325
|
tableName: string,
|
|
330
326
|
columnName: string,
|
|
331
327
|
possibleValue?: string[],
|
|
332
328
|
isNotNull?: boolean
|
|
333
329
|
) {
|
|
334
|
-
console.log("alterPossibleEnum()")
|
|
330
|
+
// console.log("alterPossibleEnum()")
|
|
335
331
|
if (!possibleValue) return null
|
|
336
332
|
const data = this.onetimeLoadColumnContent.find(
|
|
337
333
|
(row) => row.COLUMN_NAME === columnName && row.TABLE_NAME === tableName
|
|
338
334
|
)
|
|
335
|
+
const possibleValueString = possibleValue.map((row) => `'${row}'`).join(",")
|
|
336
|
+
const enumType = `enum(${possibleValueString})`
|
|
337
|
+
// console.log("enum ->", data?.COLUMN_TYPE)
|
|
338
|
+
// console.log("enumType ->", enumType)
|
|
339
339
|
const notNull = isNotNull ? "NOT NULL" : ""
|
|
340
|
-
if (data) {
|
|
341
|
-
|
|
342
|
-
.map((row) => `'${row}'`)
|
|
343
|
-
.join(", ")
|
|
344
|
-
|
|
340
|
+
if (data && enumType !== data.COLUMN_TYPE) {
|
|
341
|
+
// console.log(chalk.red("alter enum"))
|
|
345
342
|
try {
|
|
346
343
|
await this.MyDB.query(
|
|
347
344
|
`ALTER TABLE ${tableName} MODIFY COLUMN ${columnName} ENUM(${possibleValueString}) ${notNull} ;`
|
|
@@ -351,11 +348,9 @@ export default class MyDBMigrator {
|
|
|
351
348
|
`SELECT DISTINCT(${columnName}) as columnValue FROM ${tableName}`
|
|
352
349
|
)
|
|
353
350
|
const valueInColumn = data.map((dRow) => `['${dRow.columnValue}']`)
|
|
354
|
-
|
|
355
351
|
const problemValue = valueInColumn.filter(
|
|
356
352
|
(row) => !possibleValue.includes(row)
|
|
357
353
|
)
|
|
358
|
-
|
|
359
354
|
console.log(
|
|
360
355
|
chalk.bgRed.white(
|
|
361
356
|
`error table(${tableName}) - column (${columnName}) has problem value is ${problemValue.join(
|
|
@@ -406,19 +401,19 @@ export default class MyDBMigrator {
|
|
|
406
401
|
}
|
|
407
402
|
}
|
|
408
403
|
|
|
409
|
-
private async checkIsPrimaryKey(
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
if (
|
|
421
|
-
if (
|
|
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}'`)
|
|
410
|
+
|
|
411
|
+
const result2 = this.onetimeLoadColumnContent.find(
|
|
412
|
+
(row) => row.TABLE_NAME === tableName && columnName === columnName
|
|
413
|
+
)
|
|
414
|
+
|
|
415
|
+
if (result2) {
|
|
416
|
+
if (result2.COLUMN_KEY === "PRI") return true
|
|
422
417
|
else return false
|
|
423
418
|
} else return false
|
|
424
419
|
}
|
package/src/myModel.ts
CHANGED
|
@@ -17,12 +17,33 @@ export type columnType =
|
|
|
17
17
|
| "date"
|
|
18
18
|
| "time"
|
|
19
19
|
| "datetime"
|
|
20
|
-
|
|
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[]
|