gg-mysql-connector 1.0.93 → 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;
|
|
@@ -28,7 +28,7 @@ class MyDBMigrator {
|
|
|
28
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
|
}
|
package/dist/src/myModel.d.ts
CHANGED
|
@@ -16,7 +16,8 @@ export interface columnContent {
|
|
|
16
16
|
IS_INDEX?: boolean;
|
|
17
17
|
IS_NOT_NULL?: boolean;
|
|
18
18
|
}
|
|
19
|
-
export interface
|
|
19
|
+
export interface compoisteColumn {
|
|
20
|
+
type: "UNIQUE" | "INDEX";
|
|
20
21
|
columnName: string[];
|
|
21
22
|
}
|
|
22
23
|
export interface MyModel {
|
|
@@ -24,7 +25,7 @@ export interface MyModel {
|
|
|
24
25
|
tableName: string;
|
|
25
26
|
columns: [...columnContent[]];
|
|
26
27
|
meaning?: string;
|
|
27
|
-
|
|
28
|
+
compoisteColumn?: compoisteColumn[];
|
|
28
29
|
}
|
|
29
30
|
export interface MyViewModel {
|
|
30
31
|
viewName: string;
|
package/package.json
CHANGED
|
@@ -46,10 +46,7 @@ export default class MyDBMigrator {
|
|
|
46
46
|
)
|
|
47
47
|
await this.alterForeignKey(row.tableName, col)
|
|
48
48
|
}
|
|
49
|
-
await this.
|
|
50
|
-
row.tableName,
|
|
51
|
-
row.compoisteUniqueColumn
|
|
52
|
-
)
|
|
49
|
+
await this.alterColumnCombination(row.tableName, row.compoisteColumn)
|
|
53
50
|
}
|
|
54
51
|
console.log("migrate table done")
|
|
55
52
|
}
|
|
@@ -65,18 +62,19 @@ export default class MyDBMigrator {
|
|
|
65
62
|
return data.length ? true : false
|
|
66
63
|
}
|
|
67
64
|
|
|
68
|
-
private
|
|
69
|
-
compositeUniqueColumn: Unarray<MyModel["
|
|
65
|
+
private getCompositeName(
|
|
66
|
+
compositeUniqueColumn: Unarray<MyModel["compoisteColumn"]>
|
|
70
67
|
) {
|
|
71
|
-
|
|
68
|
+
if (compositeUniqueColumn?.type === "INDEX")
|
|
69
|
+
return `index_${compositeUniqueColumn?.columnName.join("_")}`
|
|
70
|
+
if (compositeUniqueColumn?.type === "UNIQUE")
|
|
71
|
+
return `unique_${compositeUniqueColumn?.columnName.join("_")}`
|
|
72
72
|
}
|
|
73
73
|
private async isCompositeUniqueColumnExist(
|
|
74
74
|
tableName: string,
|
|
75
|
-
|
|
75
|
+
compositeColumn: Unarray<MyModel["compoisteColumn"]>
|
|
76
76
|
) {
|
|
77
|
-
const compositeUniqueName = this.
|
|
78
|
-
compositeUniqueColumn
|
|
79
|
-
)
|
|
77
|
+
const compositeUniqueName = this.getCompositeName(compositeColumn)
|
|
80
78
|
const sql = `SHOW INDEX FROM ${tableName} WHERE Non_unique = 0 AND Key_name like "${compositeUniqueName}"`
|
|
81
79
|
const data = (await this.MyDB.query(sql)) as any[]
|
|
82
80
|
// console.log("sql", sql)
|
|
@@ -111,9 +109,9 @@ export default class MyDBMigrator {
|
|
|
111
109
|
}
|
|
112
110
|
}
|
|
113
111
|
}
|
|
114
|
-
private async
|
|
112
|
+
private async alterColumnCombination(
|
|
115
113
|
tableName: string,
|
|
116
|
-
uniqueColumnCombination: MyModel["
|
|
114
|
+
uniqueColumnCombination: MyModel["compoisteColumn"]
|
|
117
115
|
) {
|
|
118
116
|
if (uniqueColumnCombination?.length) {
|
|
119
117
|
for (let row of uniqueColumnCombination) {
|
|
@@ -122,15 +120,14 @@ export default class MyDBMigrator {
|
|
|
122
120
|
// check is exist, then drop it first
|
|
123
121
|
if (isCompositeUniqueNameExist) {
|
|
124
122
|
await this.MyDB.query(
|
|
125
|
-
`ALTER TABLE ${tableName} DROP INDEX ${this.
|
|
126
|
-
row
|
|
127
|
-
)}`
|
|
123
|
+
`ALTER TABLE ${tableName} DROP INDEX ${this.getCompositeName(row)}`
|
|
128
124
|
)
|
|
129
125
|
}
|
|
130
|
-
const
|
|
126
|
+
const type = row.type === "UNIQUE" ? `${row.type} KEY` : "INDEX"
|
|
127
|
+
const compositeUniqueName = this.getCompositeName(row)
|
|
131
128
|
const temp = row.columnName.join(",")
|
|
132
129
|
await this.MyDB.query(
|
|
133
|
-
`ALTER TABLE ${tableName} ADD
|
|
130
|
+
`ALTER TABLE ${tableName} ADD ${type} ${compositeUniqueName} (${temp})`
|
|
134
131
|
)
|
|
135
132
|
}
|
|
136
133
|
}
|
package/src/myModel.ts
CHANGED
|
@@ -34,15 +34,17 @@ export interface columnContent {
|
|
|
34
34
|
IS_NOT_NULL?: boolean
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
-
export interface
|
|
37
|
+
export interface compoisteColumn {
|
|
38
|
+
type: "UNIQUE" | "INDEX"
|
|
38
39
|
columnName: string[]
|
|
39
40
|
}
|
|
41
|
+
|
|
40
42
|
export interface MyModel {
|
|
41
43
|
isSeed?: boolean
|
|
42
44
|
tableName: string
|
|
43
45
|
columns: [...columnContent[]]
|
|
44
46
|
meaning?: string
|
|
45
|
-
|
|
47
|
+
compoisteColumn?: compoisteColumn[]
|
|
46
48
|
}
|
|
47
49
|
export interface MyViewModel {
|
|
48
50
|
viewName: string
|