@itrocks/mysql-maintainer 0.0.13 → 0.0.15
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/cjs/mysql-maintainer.d.ts +2 -1
- package/cjs/mysql-maintainer.js +20 -17
- package/esm/mysql-maintainer.d.ts +2 -1
- package/esm/mysql-maintainer.js +20 -17
- package/package.json +1 -1
|
@@ -10,7 +10,8 @@ export declare class MysqlMaintainer {
|
|
|
10
10
|
constructor(connection: Connection);
|
|
11
11
|
createImplicitTable(type1: ObjectOrType, type2: ObjectOrType): Promise<boolean>;
|
|
12
12
|
createTable(type: Type): Promise<boolean>;
|
|
13
|
+
implicitTableName(type1: ObjectOrType, type2: ObjectOrType): string;
|
|
13
14
|
manageError(error: SqlError, context: Context, sql: string | QueryOptions, values: any[]): Promise<boolean>;
|
|
14
|
-
updateContextTables(context: Context): Promise<boolean>;
|
|
15
|
+
updateContextTables(context: Context, tableName?: string): Promise<boolean>;
|
|
15
16
|
updateTable(type: Type): Promise<boolean>;
|
|
16
17
|
}
|
package/cjs/mysql-maintainer.js
CHANGED
|
@@ -24,18 +24,16 @@ const schema_diff_mysql_1 = require("@itrocks/schema-diff-mysql");
|
|
|
24
24
|
const schema_to_mysql_1 = require("@itrocks/schema-to-mysql");
|
|
25
25
|
const store_1 = require("@itrocks/store");
|
|
26
26
|
__exportStar(require("./mysql"), exports);
|
|
27
|
+
const DELETION = false;
|
|
27
28
|
class MysqlMaintainer {
|
|
28
29
|
connection;
|
|
29
30
|
constructor(connection) {
|
|
30
31
|
this.connection = connection;
|
|
31
32
|
}
|
|
32
33
|
async createImplicitTable(type1, type2) {
|
|
34
|
+
const joinTable = this.implicitTableName(type1, type2);
|
|
33
35
|
const table1 = (0, store_1.storeOf)(type1);
|
|
34
36
|
const table2 = (0, store_1.storeOf)(type2);
|
|
35
|
-
if (!table1 || !table2) {
|
|
36
|
-
throw 'Collection objects are not stored';
|
|
37
|
-
}
|
|
38
|
-
const joinTable = (0, mysql_1.joinTableName)(table1, table2);
|
|
39
37
|
const query = `CREATE TABLE \`${joinTable}\` (
|
|
40
38
|
${table1}_id int unsigned NOT NULL,
|
|
41
39
|
${table2}_id int unsigned NOT NULL,
|
|
@@ -54,24 +52,29 @@ CONSTRAINT \`${joinTable}.${table2}_id\` FOREIGN KEY (${table2}_id) REFERENCES \
|
|
|
54
52
|
const tableSchema = new reflect_to_schema_1.ReflectToTable().convert(type);
|
|
55
53
|
const schemaToMysql = new schema_to_mysql_1.SchemaToMysql();
|
|
56
54
|
const sql = schemaToMysql.sql(tableSchema);
|
|
57
|
-
console.log(sql);
|
|
58
55
|
await this.connection.query(sql);
|
|
59
56
|
return true;
|
|
60
57
|
}
|
|
58
|
+
implicitTableName(type1, type2) {
|
|
59
|
+
const table1 = (0, store_1.storeOf)(type1);
|
|
60
|
+
const table2 = (0, store_1.storeOf)(type2);
|
|
61
|
+
if (!table1 || !table2) {
|
|
62
|
+
throw 'Collection objects are not stored';
|
|
63
|
+
}
|
|
64
|
+
return (0, mysql_1.joinTableName)(table1, table2);
|
|
65
|
+
}
|
|
61
66
|
async manageError(error, context, sql, values) {
|
|
62
|
-
console.log('query', sql, values);
|
|
63
|
-
console.log('throw', error);
|
|
64
|
-
console.log('context', context);
|
|
65
67
|
switch (error.code) {
|
|
66
68
|
case 'ER_BAD_FIELD_ERROR':
|
|
67
69
|
case 'ER_CANNOT_ADD_FOREIGN':
|
|
68
70
|
return await this.updateContextTables(context);
|
|
69
71
|
case 'ER_NO_SUCH_TABLE':
|
|
70
|
-
|
|
72
|
+
const tableName = /Table '.*?\.(.*?)'/.exec(error.message)?.[1];
|
|
73
|
+
return this.updateContextTables(context, tableName);
|
|
71
74
|
}
|
|
72
75
|
return false;
|
|
73
76
|
}
|
|
74
|
-
async updateContextTables(context) {
|
|
77
|
+
async updateContextTables(context, tableName) {
|
|
75
78
|
const contexts = Array.isArray(context) ? context : [context];
|
|
76
79
|
for (const context of contexts) {
|
|
77
80
|
const type = (0, class_type_1.typeOf)(context);
|
|
@@ -80,9 +83,9 @@ CONSTRAINT \`${joinTable}.${table2}_id\` FOREIGN KEY (${table2}_id) REFERENCES \
|
|
|
80
83
|
throw 'No table name for type';
|
|
81
84
|
}
|
|
82
85
|
const exists = (await this.connection.query('SHOW TABLES LIKE ?', tableName));
|
|
83
|
-
exists.length ?
|
|
86
|
+
await (exists.length ? this.updateTable(type) : this.createTable(type));
|
|
84
87
|
}
|
|
85
|
-
if (contexts.length === 2) {
|
|
88
|
+
if ((contexts.length === 2) && (this.implicitTableName(contexts[0], contexts[1]) === tableName)) {
|
|
86
89
|
await this.createImplicitTable(contexts[0], contexts[1]);
|
|
87
90
|
}
|
|
88
91
|
return true;
|
|
@@ -96,14 +99,14 @@ CONSTRAINT \`${joinTable}.${table2}_id\` FOREIGN KEY (${table2}_id) REFERENCES \
|
|
|
96
99
|
new mysql_to_schema_1.MysqlToTable(this.connection).normalize(classTable);
|
|
97
100
|
const mysqlTable = await ((new mysql_to_schema_1.MysqlToTable(this.connection)).convert(tableName));
|
|
98
101
|
const schemaDiff = new schema_diff_1.TableDiff(mysqlTable, classTable);
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
102
|
+
if (!schemaDiff.additions.length
|
|
103
|
+
&& !schemaDiff.changes.length
|
|
104
|
+
&& (!schemaDiff.deletions.length || !DELETION)
|
|
105
|
+
&& !schemaDiff.tableChanges()) {
|
|
103
106
|
return false;
|
|
104
107
|
}
|
|
105
108
|
const schemaDiffMysql = new schema_diff_mysql_1.SchemaDiffMysql();
|
|
106
|
-
const sql = schemaDiffMysql.sql(schemaDiff);
|
|
109
|
+
const sql = schemaDiffMysql.sql(schemaDiff, DELETION);
|
|
107
110
|
await this.connection.query(sql);
|
|
108
111
|
return true;
|
|
109
112
|
}
|
|
@@ -10,7 +10,8 @@ export declare class MysqlMaintainer {
|
|
|
10
10
|
constructor(connection: Connection);
|
|
11
11
|
createImplicitTable(type1: ObjectOrType, type2: ObjectOrType): Promise<boolean>;
|
|
12
12
|
createTable(type: Type): Promise<boolean>;
|
|
13
|
+
implicitTableName(type1: ObjectOrType, type2: ObjectOrType): string;
|
|
13
14
|
manageError(error: SqlError, context: Context, sql: string | QueryOptions, values: any[]): Promise<boolean>;
|
|
14
|
-
updateContextTables(context: Context): Promise<boolean>;
|
|
15
|
+
updateContextTables(context: Context, tableName?: string): Promise<boolean>;
|
|
15
16
|
updateTable(type: Type): Promise<boolean>;
|
|
16
17
|
}
|
package/esm/mysql-maintainer.js
CHANGED
|
@@ -7,18 +7,16 @@ import { SchemaDiffMysql } from '@itrocks/schema-diff-mysql';
|
|
|
7
7
|
import { SchemaToMysql } from '@itrocks/schema-to-mysql';
|
|
8
8
|
import { storeOf } from '@itrocks/store';
|
|
9
9
|
export * from './mysql';
|
|
10
|
+
const DELETION = false;
|
|
10
11
|
export class MysqlMaintainer {
|
|
11
12
|
connection;
|
|
12
13
|
constructor(connection) {
|
|
13
14
|
this.connection = connection;
|
|
14
15
|
}
|
|
15
16
|
async createImplicitTable(type1, type2) {
|
|
17
|
+
const joinTable = this.implicitTableName(type1, type2);
|
|
16
18
|
const table1 = storeOf(type1);
|
|
17
19
|
const table2 = storeOf(type2);
|
|
18
|
-
if (!table1 || !table2) {
|
|
19
|
-
throw 'Collection objects are not stored';
|
|
20
|
-
}
|
|
21
|
-
const joinTable = joinTableName(table1, table2);
|
|
22
20
|
const query = `CREATE TABLE \`${joinTable}\` (
|
|
23
21
|
${table1}_id int unsigned NOT NULL,
|
|
24
22
|
${table2}_id int unsigned NOT NULL,
|
|
@@ -37,24 +35,29 @@ CONSTRAINT \`${joinTable}.${table2}_id\` FOREIGN KEY (${table2}_id) REFERENCES \
|
|
|
37
35
|
const tableSchema = new ReflectToTable().convert(type);
|
|
38
36
|
const schemaToMysql = new SchemaToMysql();
|
|
39
37
|
const sql = schemaToMysql.sql(tableSchema);
|
|
40
|
-
console.log(sql);
|
|
41
38
|
await this.connection.query(sql);
|
|
42
39
|
return true;
|
|
43
40
|
}
|
|
41
|
+
implicitTableName(type1, type2) {
|
|
42
|
+
const table1 = storeOf(type1);
|
|
43
|
+
const table2 = storeOf(type2);
|
|
44
|
+
if (!table1 || !table2) {
|
|
45
|
+
throw 'Collection objects are not stored';
|
|
46
|
+
}
|
|
47
|
+
return joinTableName(table1, table2);
|
|
48
|
+
}
|
|
44
49
|
async manageError(error, context, sql, values) {
|
|
45
|
-
console.log('query', sql, values);
|
|
46
|
-
console.log('throw', error);
|
|
47
|
-
console.log('context', context);
|
|
48
50
|
switch (error.code) {
|
|
49
51
|
case 'ER_BAD_FIELD_ERROR':
|
|
50
52
|
case 'ER_CANNOT_ADD_FOREIGN':
|
|
51
53
|
return await this.updateContextTables(context);
|
|
52
54
|
case 'ER_NO_SUCH_TABLE':
|
|
53
|
-
|
|
55
|
+
const tableName = /Table '.*?\.(.*?)'/.exec(error.message)?.[1];
|
|
56
|
+
return this.updateContextTables(context, tableName);
|
|
54
57
|
}
|
|
55
58
|
return false;
|
|
56
59
|
}
|
|
57
|
-
async updateContextTables(context) {
|
|
60
|
+
async updateContextTables(context, tableName) {
|
|
58
61
|
const contexts = Array.isArray(context) ? context : [context];
|
|
59
62
|
for (const context of contexts) {
|
|
60
63
|
const type = typeOf(context);
|
|
@@ -63,9 +66,9 @@ CONSTRAINT \`${joinTable}.${table2}_id\` FOREIGN KEY (${table2}_id) REFERENCES \
|
|
|
63
66
|
throw 'No table name for type';
|
|
64
67
|
}
|
|
65
68
|
const exists = (await this.connection.query('SHOW TABLES LIKE ?', tableName));
|
|
66
|
-
exists.length ?
|
|
69
|
+
await (exists.length ? this.updateTable(type) : this.createTable(type));
|
|
67
70
|
}
|
|
68
|
-
if (contexts.length === 2) {
|
|
71
|
+
if ((contexts.length === 2) && (this.implicitTableName(contexts[0], contexts[1]) === tableName)) {
|
|
69
72
|
await this.createImplicitTable(contexts[0], contexts[1]);
|
|
70
73
|
}
|
|
71
74
|
return true;
|
|
@@ -79,14 +82,14 @@ CONSTRAINT \`${joinTable}.${table2}_id\` FOREIGN KEY (${table2}_id) REFERENCES \
|
|
|
79
82
|
new MysqlToTable(this.connection).normalize(classTable);
|
|
80
83
|
const mysqlTable = await ((new MysqlToTable(this.connection)).convert(tableName));
|
|
81
84
|
const schemaDiff = new TableDiff(mysqlTable, classTable);
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
85
|
+
if (!schemaDiff.additions.length
|
|
86
|
+
&& !schemaDiff.changes.length
|
|
87
|
+
&& (!schemaDiff.deletions.length || !DELETION)
|
|
88
|
+
&& !schemaDiff.tableChanges()) {
|
|
86
89
|
return false;
|
|
87
90
|
}
|
|
88
91
|
const schemaDiffMysql = new SchemaDiffMysql();
|
|
89
|
-
const sql = schemaDiffMysql.sql(schemaDiff);
|
|
92
|
+
const sql = schemaDiffMysql.sql(schemaDiff, DELETION);
|
|
90
93
|
await this.connection.query(sql);
|
|
91
94
|
return true;
|
|
92
95
|
}
|
package/package.json
CHANGED