@nocobase/database 0.16.0-alpha.3 → 0.16.0-alpha.4
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/lib/sync-runner.js +54 -50
- package/package.json +4 -4
package/lib/sync-runner.js
CHANGED
|
@@ -53,72 +53,76 @@ const _SyncRunner = class _SyncRunner {
|
|
|
53
53
|
const childAttributes = import_lodash.default.pickBy(attributes, (value) => {
|
|
54
54
|
return !value.inherit;
|
|
55
55
|
});
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
56
|
+
if (!await inheritedCollection.existsInDb({
|
|
57
|
+
transaction
|
|
58
|
+
})) {
|
|
59
|
+
let maxSequenceVal = 0;
|
|
60
|
+
let maxSequenceName;
|
|
61
|
+
if (childAttributes.id && childAttributes.id.autoIncrement) {
|
|
62
|
+
for (const parent of parents) {
|
|
63
|
+
const sequenceNameResult = await queryInterface.sequelize.query(
|
|
64
|
+
`SELECT column_default
|
|
62
65
|
FROM information_schema.columns
|
|
63
66
|
WHERE table_name = '${parent.model.tableName}'
|
|
64
67
|
and table_schema = '${parent.collectionSchema()}'
|
|
65
68
|
and "column_name" = 'id';`,
|
|
66
|
-
|
|
67
|
-
|
|
69
|
+
{
|
|
70
|
+
transaction
|
|
71
|
+
}
|
|
72
|
+
);
|
|
73
|
+
if (!sequenceNameResult[0].length) {
|
|
74
|
+
continue;
|
|
68
75
|
}
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
const match = regex.exec(columnDefault);
|
|
79
|
-
const sequenceName = match[1];
|
|
80
|
-
const sequenceCurrentValResult = await queryInterface.sequelize.query(
|
|
81
|
-
`select last_value
|
|
76
|
+
const columnDefault = sequenceNameResult[0][0]["column_default"];
|
|
77
|
+
if (!columnDefault) {
|
|
78
|
+
throw new Error(`Can't find sequence name of parent collection ${parent.options.name}`);
|
|
79
|
+
}
|
|
80
|
+
const regex = new RegExp(/nextval\('(.*)'::regclass\)/);
|
|
81
|
+
const match = regex.exec(columnDefault);
|
|
82
|
+
const sequenceName = match[1];
|
|
83
|
+
const sequenceCurrentValResult = await queryInterface.sequelize.query(
|
|
84
|
+
`select last_value
|
|
82
85
|
from ${sequenceName}`,
|
|
83
|
-
|
|
84
|
-
|
|
86
|
+
{
|
|
87
|
+
transaction
|
|
88
|
+
}
|
|
89
|
+
);
|
|
90
|
+
const sequenceCurrentVal = parseInt(sequenceCurrentValResult[0][0]["last_value"]);
|
|
91
|
+
if (sequenceCurrentVal > maxSequenceVal) {
|
|
92
|
+
maxSequenceName = sequenceName;
|
|
93
|
+
maxSequenceVal = sequenceCurrentVal;
|
|
85
94
|
}
|
|
86
|
-
);
|
|
87
|
-
const sequenceCurrentVal = parseInt(sequenceCurrentValResult[0][0]["last_value"]);
|
|
88
|
-
if (sequenceCurrentVal > maxSequenceVal) {
|
|
89
|
-
maxSequenceName = sequenceName;
|
|
90
|
-
maxSequenceVal = sequenceCurrentVal;
|
|
91
95
|
}
|
|
92
96
|
}
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
const idColumnSql = `SELECT column_name
|
|
97
|
+
await this.createTable(tableName, childAttributes, options, model, parents);
|
|
98
|
+
if (maxSequenceName) {
|
|
99
|
+
const parentsDeep = Array.from(db.inheritanceMap.getParents(inheritedCollection.name)).map(
|
|
100
|
+
(parent) => db.getCollection(parent).getTableNameWithSchema()
|
|
101
|
+
);
|
|
102
|
+
const sequenceTables = [...parentsDeep, tableName];
|
|
103
|
+
for (const sequenceTable of sequenceTables) {
|
|
104
|
+
const tableName2 = sequenceTable.tableName;
|
|
105
|
+
const schemaName = sequenceTable.schema;
|
|
106
|
+
const idColumnSql = `SELECT column_name
|
|
104
107
|
FROM information_schema.columns
|
|
105
108
|
WHERE table_name = '${tableName2}'
|
|
106
109
|
and column_name = 'id'
|
|
107
110
|
and table_schema = '${schemaName}';
|
|
108
111
|
`;
|
|
109
|
-
|
|
110
|
-
transaction
|
|
111
|
-
});
|
|
112
|
-
if (idColumnQuery[0].length == 0) {
|
|
113
|
-
continue;
|
|
114
|
-
}
|
|
115
|
-
await queryInterface.sequelize.query(
|
|
116
|
-
`alter table ${db.utils.quoteTable(sequenceTable)}
|
|
117
|
-
alter column id set default nextval('${maxSequenceName}')`,
|
|
118
|
-
{
|
|
112
|
+
const idColumnQuery = await queryInterface.sequelize.query(idColumnSql, {
|
|
119
113
|
transaction
|
|
114
|
+
});
|
|
115
|
+
if (idColumnQuery[0].length == 0) {
|
|
116
|
+
continue;
|
|
120
117
|
}
|
|
121
|
-
|
|
118
|
+
await queryInterface.sequelize.query(
|
|
119
|
+
`alter table ${db.utils.quoteTable(sequenceTable)}
|
|
120
|
+
alter column id set default nextval('${maxSequenceName}')`,
|
|
121
|
+
{
|
|
122
|
+
transaction
|
|
123
|
+
}
|
|
124
|
+
);
|
|
125
|
+
}
|
|
122
126
|
}
|
|
123
127
|
}
|
|
124
128
|
if (options.alter) {
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nocobase/database",
|
|
3
|
-
"version": "0.16.0-alpha.
|
|
3
|
+
"version": "0.16.0-alpha.4",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"types": "./lib/index.d.ts",
|
|
7
7
|
"license": "Apache-2.0",
|
|
8
8
|
"dependencies": {
|
|
9
|
-
"@nocobase/logger": "0.16.0-alpha.
|
|
10
|
-
"@nocobase/utils": "0.16.0-alpha.
|
|
9
|
+
"@nocobase/logger": "0.16.0-alpha.4",
|
|
10
|
+
"@nocobase/utils": "0.16.0-alpha.4",
|
|
11
11
|
"async-mutex": "^0.3.2",
|
|
12
12
|
"cron-parser": "4.4.0",
|
|
13
13
|
"dayjs": "^1.11.8",
|
|
@@ -33,5 +33,5 @@
|
|
|
33
33
|
"url": "git+https://github.com/nocobase/nocobase.git",
|
|
34
34
|
"directory": "packages/database"
|
|
35
35
|
},
|
|
36
|
-
"gitHead": "
|
|
36
|
+
"gitHead": "98d65186a22281e59209d11a6756712a62667c07"
|
|
37
37
|
}
|