gg-mysql-connector 1.0.92 → 1.0.94
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.
|
@@ -13,10 +13,10 @@ export default class MyDBMigrator {
|
|
|
13
13
|
constructor(connection: ModelGenerator);
|
|
14
14
|
migrateTable(model: MyModel[]): Promise<void>;
|
|
15
15
|
private isConstraintNameExist;
|
|
16
|
-
private
|
|
16
|
+
private getCompositeName;
|
|
17
17
|
private isCompositeUniqueColumnExist;
|
|
18
18
|
private alterForeignKey;
|
|
19
|
-
private
|
|
19
|
+
private alterColumnCombination;
|
|
20
20
|
private alterDataType;
|
|
21
21
|
private alterPrimaryKey;
|
|
22
22
|
private createTableIfNotExist;
|
|
@@ -25,10 +25,10 @@ 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
|
-
await this.
|
|
31
|
+
await this.alterColumnCombination(row.tableName, row.compoisteColumn);
|
|
32
32
|
}
|
|
33
33
|
console.log("migrate table done");
|
|
34
34
|
}
|
|
@@ -41,11 +41,14 @@ class MyDBMigrator {
|
|
|
41
41
|
const data = (await this.MyDB.query(sql));
|
|
42
42
|
return data.length ? true : false;
|
|
43
43
|
}
|
|
44
|
-
|
|
45
|
-
|
|
44
|
+
getCompositeName(compositeUniqueColumn) {
|
|
45
|
+
if ((compositeUniqueColumn === null || compositeUniqueColumn === void 0 ? void 0 : compositeUniqueColumn.type) === "INDEX")
|
|
46
|
+
return `index_${compositeUniqueColumn === null || compositeUniqueColumn === void 0 ? void 0 : compositeUniqueColumn.columnName.join("_")}`;
|
|
47
|
+
if ((compositeUniqueColumn === null || compositeUniqueColumn === void 0 ? void 0 : compositeUniqueColumn.type) === "UNIQUE")
|
|
48
|
+
return `unique_${compositeUniqueColumn === null || compositeUniqueColumn === void 0 ? void 0 : compositeUniqueColumn.columnName.join("_")}`;
|
|
46
49
|
}
|
|
47
|
-
async isCompositeUniqueColumnExist(tableName,
|
|
48
|
-
const compositeUniqueName = this.
|
|
50
|
+
async isCompositeUniqueColumnExist(tableName, compositeColumn) {
|
|
51
|
+
const compositeUniqueName = this.getCompositeName(compositeColumn);
|
|
49
52
|
const sql = `SHOW INDEX FROM ${tableName} WHERE Non_unique = 0 AND Key_name like "${compositeUniqueName}"`;
|
|
50
53
|
const data = (await this.MyDB.query(sql));
|
|
51
54
|
// console.log("sql", sql)
|
|
@@ -69,17 +72,18 @@ class MyDBMigrator {
|
|
|
69
72
|
}
|
|
70
73
|
}
|
|
71
74
|
}
|
|
72
|
-
async
|
|
75
|
+
async alterColumnCombination(tableName, uniqueColumnCombination) {
|
|
73
76
|
if (uniqueColumnCombination === null || uniqueColumnCombination === void 0 ? void 0 : uniqueColumnCombination.length) {
|
|
74
77
|
for (let row of uniqueColumnCombination) {
|
|
75
78
|
const isCompositeUniqueNameExist = await this.isCompositeUniqueColumnExist(tableName, row);
|
|
76
79
|
// check is exist, then drop it first
|
|
77
80
|
if (isCompositeUniqueNameExist) {
|
|
78
|
-
await this.MyDB.query(`ALTER TABLE ${tableName} DROP INDEX ${this.
|
|
81
|
+
await this.MyDB.query(`ALTER TABLE ${tableName} DROP INDEX ${this.getCompositeName(row)}`);
|
|
79
82
|
}
|
|
80
|
-
const
|
|
83
|
+
const type = row.type === "UNIQUE" ? `${row.type} KEY` : "INDEX";
|
|
84
|
+
const compositeUniqueName = this.getCompositeName(row);
|
|
81
85
|
const temp = row.columnName.join(",");
|
|
82
|
-
await this.MyDB.query(`ALTER TABLE ${tableName} ADD
|
|
86
|
+
await this.MyDB.query(`ALTER TABLE ${tableName} ADD ${type} ${compositeUniqueName} (${temp})`);
|
|
83
87
|
}
|
|
84
88
|
}
|
|
85
89
|
}
|
|
@@ -88,14 +92,15 @@ class MyDBMigrator {
|
|
|
88
92
|
const columnDefault = col.COLUMN_DEFAULT
|
|
89
93
|
? `DEFAULT ${col.COLUMN_DEFAULT}`
|
|
90
94
|
: "";
|
|
95
|
+
const notNull = col.IS_NOT_NULL ? "NOT NULL" : "";
|
|
91
96
|
const isColumnChange = await this.checkIsColumnDataTypeChange(tableName, col.COLUMN_NAME, col.DATA_TYPE, col.COLUMN_DEFAULT);
|
|
92
97
|
if (isColumnChange) {
|
|
93
98
|
const isColumnExist = await this.columnInTableExist(tableName, col.COLUMN_NAME);
|
|
94
99
|
if (!isColumnExist) {
|
|
95
|
-
await this.MyDB.query(`ALTER TABLE ${tableName} ADD COLUMN ${col.COLUMN_NAME} ${col.DATA_TYPE} ${AutoIncrement} ${columnDefault}`);
|
|
100
|
+
await this.MyDB.query(`ALTER TABLE ${tableName} ADD COLUMN ${col.COLUMN_NAME} ${col.DATA_TYPE} ${AutoIncrement} ${notNull} ${columnDefault}`);
|
|
96
101
|
}
|
|
97
102
|
else {
|
|
98
|
-
await this.MyDB.query(`ALTER TABLE ${tableName} MODIFY COLUMN ${col.COLUMN_NAME} ${col.DATA_TYPE} ${AutoIncrement} ${columnDefault}`);
|
|
103
|
+
await this.MyDB.query(`ALTER TABLE ${tableName} MODIFY COLUMN ${col.COLUMN_NAME} ${col.DATA_TYPE} ${AutoIncrement} ${notNull} ${columnDefault}`);
|
|
99
104
|
}
|
|
100
105
|
}
|
|
101
106
|
}
|
|
@@ -178,16 +183,17 @@ class MyDBMigrator {
|
|
|
178
183
|
}
|
|
179
184
|
}
|
|
180
185
|
}
|
|
181
|
-
async alterPossibleEnum(tableName, columnName, possibleValue) {
|
|
186
|
+
async alterPossibleEnum(tableName, columnName, possibleValue, isNotNull) {
|
|
182
187
|
if (!possibleValue)
|
|
183
188
|
return null;
|
|
184
189
|
const data = this.onetimeLoadColumnContent.find((row) => row.COLUMN_NAME === columnName && row.TABLE_NAME === tableName);
|
|
190
|
+
const notNull = isNotNull ? "NOT NULL" : "";
|
|
185
191
|
if (data) {
|
|
186
192
|
let possibleValueString = possibleValue
|
|
187
193
|
.map((row) => `"${row}"`)
|
|
188
194
|
.join(", ");
|
|
189
195
|
try {
|
|
190
|
-
await this.MyDB.query(`ALTER TABLE ${tableName} MODIFY COLUMN ${columnName} ENUM(${possibleValueString}) ;`);
|
|
196
|
+
await this.MyDB.query(`ALTER TABLE ${tableName} MODIFY COLUMN ${columnName} ENUM(${possibleValueString}) ${notNull} ;`);
|
|
191
197
|
}
|
|
192
198
|
catch (e) {
|
|
193
199
|
const data = await this.MyDB.query(`SELECT DISTINCT(${columnName}) as columnValue FROM ${tableName}`);
|
package/dist/src/myModel.d.ts
CHANGED
|
@@ -14,8 +14,10 @@ export interface columnContent {
|
|
|
14
14
|
}[];
|
|
15
15
|
IS_UNIQUE?: boolean;
|
|
16
16
|
IS_INDEX?: boolean;
|
|
17
|
+
IS_NOT_NULL?: boolean;
|
|
17
18
|
}
|
|
18
|
-
export interface
|
|
19
|
+
export interface compoisteColumn {
|
|
20
|
+
type: "UNIQUE" | "INDEX";
|
|
19
21
|
columnName: string[];
|
|
20
22
|
}
|
|
21
23
|
export interface MyModel {
|
|
@@ -23,7 +25,7 @@ export interface MyModel {
|
|
|
23
25
|
tableName: string;
|
|
24
26
|
columns: [...columnContent[]];
|
|
25
27
|
meaning?: string;
|
|
26
|
-
|
|
28
|
+
compoisteColumn?: compoisteColumn[];
|
|
27
29
|
}
|
|
28
30
|
export interface MyViewModel {
|
|
29
31
|
viewName: string;
|
package/package.json
CHANGED
|
@@ -41,14 +41,12 @@ 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
|
}
|
|
48
|
-
await this.
|
|
49
|
-
row.tableName,
|
|
50
|
-
row.compoisteUniqueColumn
|
|
51
|
-
)
|
|
49
|
+
await this.alterColumnCombination(row.tableName, row.compoisteColumn)
|
|
52
50
|
}
|
|
53
51
|
console.log("migrate table done")
|
|
54
52
|
}
|
|
@@ -64,18 +62,19 @@ export default class MyDBMigrator {
|
|
|
64
62
|
return data.length ? true : false
|
|
65
63
|
}
|
|
66
64
|
|
|
67
|
-
private
|
|
68
|
-
compositeUniqueColumn: Unarray<MyModel["
|
|
65
|
+
private getCompositeName(
|
|
66
|
+
compositeUniqueColumn: Unarray<MyModel["compoisteColumn"]>
|
|
69
67
|
) {
|
|
70
|
-
|
|
68
|
+
if (compositeUniqueColumn?.type === "INDEX")
|
|
69
|
+
return `index_${compositeUniqueColumn?.columnName.join("_")}`
|
|
70
|
+
if (compositeUniqueColumn?.type === "UNIQUE")
|
|
71
|
+
return `unique_${compositeUniqueColumn?.columnName.join("_")}`
|
|
71
72
|
}
|
|
72
73
|
private async isCompositeUniqueColumnExist(
|
|
73
74
|
tableName: string,
|
|
74
|
-
|
|
75
|
+
compositeColumn: Unarray<MyModel["compoisteColumn"]>
|
|
75
76
|
) {
|
|
76
|
-
const compositeUniqueName = this.
|
|
77
|
-
compositeUniqueColumn
|
|
78
|
-
)
|
|
77
|
+
const compositeUniqueName = this.getCompositeName(compositeColumn)
|
|
79
78
|
const sql = `SHOW INDEX FROM ${tableName} WHERE Non_unique = 0 AND Key_name like "${compositeUniqueName}"`
|
|
80
79
|
const data = (await this.MyDB.query(sql)) as any[]
|
|
81
80
|
// console.log("sql", sql)
|
|
@@ -110,9 +109,9 @@ export default class MyDBMigrator {
|
|
|
110
109
|
}
|
|
111
110
|
}
|
|
112
111
|
}
|
|
113
|
-
private async
|
|
112
|
+
private async alterColumnCombination(
|
|
114
113
|
tableName: string,
|
|
115
|
-
uniqueColumnCombination: MyModel["
|
|
114
|
+
uniqueColumnCombination: MyModel["compoisteColumn"]
|
|
116
115
|
) {
|
|
117
116
|
if (uniqueColumnCombination?.length) {
|
|
118
117
|
for (let row of uniqueColumnCombination) {
|
|
@@ -121,15 +120,14 @@ export default class MyDBMigrator {
|
|
|
121
120
|
// check is exist, then drop it first
|
|
122
121
|
if (isCompositeUniqueNameExist) {
|
|
123
122
|
await this.MyDB.query(
|
|
124
|
-
`ALTER TABLE ${tableName} DROP INDEX ${this.
|
|
125
|
-
row
|
|
126
|
-
)}`
|
|
123
|
+
`ALTER TABLE ${tableName} DROP INDEX ${this.getCompositeName(row)}`
|
|
127
124
|
)
|
|
128
125
|
}
|
|
129
|
-
const
|
|
126
|
+
const type = row.type === "UNIQUE" ? `${row.type} KEY` : "INDEX"
|
|
127
|
+
const compositeUniqueName = this.getCompositeName(row)
|
|
130
128
|
const temp = row.columnName.join(",")
|
|
131
129
|
await this.MyDB.query(
|
|
132
|
-
`ALTER TABLE ${tableName} ADD
|
|
130
|
+
`ALTER TABLE ${tableName} ADD ${type} ${compositeUniqueName} (${temp})`
|
|
133
131
|
)
|
|
134
132
|
}
|
|
135
133
|
}
|
|
@@ -143,6 +141,7 @@ export default class MyDBMigrator {
|
|
|
143
141
|
const columnDefault = col.COLUMN_DEFAULT
|
|
144
142
|
? `DEFAULT ${col.COLUMN_DEFAULT}`
|
|
145
143
|
: ""
|
|
144
|
+
const notNull = col.IS_NOT_NULL ? "NOT NULL" : ""
|
|
146
145
|
const isColumnChange = await this.checkIsColumnDataTypeChange(
|
|
147
146
|
tableName,
|
|
148
147
|
col.COLUMN_NAME,
|
|
@@ -156,11 +155,11 @@ export default class MyDBMigrator {
|
|
|
156
155
|
)
|
|
157
156
|
if (!isColumnExist) {
|
|
158
157
|
await this.MyDB.query(
|
|
159
|
-
`ALTER TABLE ${tableName} ADD COLUMN ${col.COLUMN_NAME} ${col.DATA_TYPE} ${AutoIncrement} ${columnDefault}`
|
|
158
|
+
`ALTER TABLE ${tableName} ADD COLUMN ${col.COLUMN_NAME} ${col.DATA_TYPE} ${AutoIncrement} ${notNull} ${columnDefault}`
|
|
160
159
|
)
|
|
161
160
|
} else {
|
|
162
161
|
await this.MyDB.query(
|
|
163
|
-
`ALTER TABLE ${tableName} MODIFY COLUMN ${col.COLUMN_NAME} ${col.DATA_TYPE} ${AutoIncrement} ${columnDefault}`
|
|
162
|
+
`ALTER TABLE ${tableName} MODIFY COLUMN ${col.COLUMN_NAME} ${col.DATA_TYPE} ${AutoIncrement} ${notNull} ${columnDefault}`
|
|
164
163
|
)
|
|
165
164
|
}
|
|
166
165
|
}
|
|
@@ -294,12 +293,14 @@ export default class MyDBMigrator {
|
|
|
294
293
|
private async alterPossibleEnum(
|
|
295
294
|
tableName: string,
|
|
296
295
|
columnName: string,
|
|
297
|
-
possibleValue?: string[]
|
|
296
|
+
possibleValue?: string[],
|
|
297
|
+
isNotNull?: boolean
|
|
298
298
|
) {
|
|
299
299
|
if (!possibleValue) return null
|
|
300
300
|
const data = this.onetimeLoadColumnContent.find(
|
|
301
301
|
(row) => row.COLUMN_NAME === columnName && row.TABLE_NAME === tableName
|
|
302
302
|
)
|
|
303
|
+
const notNull = isNotNull ? "NOT NULL" : ""
|
|
303
304
|
if (data) {
|
|
304
305
|
let possibleValueString = possibleValue
|
|
305
306
|
.map((row) => `"${row}"`)
|
|
@@ -307,7 +308,7 @@ export default class MyDBMigrator {
|
|
|
307
308
|
|
|
308
309
|
try {
|
|
309
310
|
await this.MyDB.query(
|
|
310
|
-
`ALTER TABLE ${tableName} MODIFY COLUMN ${columnName} ENUM(${possibleValueString}) ;`
|
|
311
|
+
`ALTER TABLE ${tableName} MODIFY COLUMN ${columnName} ENUM(${possibleValueString}) ${notNull} ;`
|
|
311
312
|
)
|
|
312
313
|
} catch (e) {
|
|
313
314
|
const data: { columnValue: any }[] = await this.MyDB.query(
|
package/src/myModel.ts
CHANGED
|
@@ -31,17 +31,20 @@ 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
|
-
export interface
|
|
37
|
+
export interface compoisteColumn {
|
|
38
|
+
type: "UNIQUE" | "INDEX"
|
|
37
39
|
columnName: string[]
|
|
38
40
|
}
|
|
41
|
+
|
|
39
42
|
export interface MyModel {
|
|
40
43
|
isSeed?: boolean
|
|
41
44
|
tableName: string
|
|
42
45
|
columns: [...columnContent[]]
|
|
43
46
|
meaning?: string
|
|
44
|
-
|
|
47
|
+
compoisteColumn?: compoisteColumn[]
|
|
45
48
|
}
|
|
46
49
|
export interface MyViewModel {
|
|
47
50
|
viewName: string
|