2022-11-16 12:53:58 +08:00
|
|
|
import { InheritedCollection } from './inherited-collection';
|
|
|
|
import lodash from 'lodash';
|
|
|
|
|
|
|
|
export class SyncRunner {
|
|
|
|
static async syncInheritModel(model: any, options: any) {
|
|
|
|
const { transaction } = options;
|
|
|
|
|
|
|
|
const inheritedCollection = model.collection as InheritedCollection;
|
|
|
|
const db = inheritedCollection.context.database;
|
2023-02-16 10:53:04 +08:00
|
|
|
|
2022-11-16 12:53:58 +08:00
|
|
|
const dialect = db.sequelize.getDialect();
|
|
|
|
|
|
|
|
const queryInterface = db.sequelize.getQueryInterface();
|
|
|
|
|
|
|
|
if (dialect != 'postgres') {
|
|
|
|
throw new Error('Inherit model is only supported on postgres');
|
|
|
|
}
|
|
|
|
|
|
|
|
const parents = inheritedCollection.parents;
|
|
|
|
|
2022-11-20 17:33:20 +08:00
|
|
|
if (!parents) {
|
2022-11-26 09:24:53 +08:00
|
|
|
throw new Error(
|
2023-11-21 21:36:08 +08:00
|
|
|
`Inherit model ${inheritedCollection.name} can't be created without parents, parents option is ${lodash
|
2022-11-26 09:24:53 +08:00
|
|
|
.castArray(inheritedCollection.options.inherits)
|
|
|
|
.join(', ')}`,
|
|
|
|
);
|
2022-11-20 17:33:20 +08:00
|
|
|
}
|
|
|
|
|
2023-03-10 19:16:00 +08:00
|
|
|
const tableName = inheritedCollection.getTableNameWithSchema();
|
2022-11-16 12:53:58 +08:00
|
|
|
const attributes = model.tableAttributes;
|
2024-01-08 18:59:56 +08:00
|
|
|
|
2022-11-16 12:53:58 +08:00
|
|
|
const childAttributes = lodash.pickBy(attributes, (value) => {
|
|
|
|
return !value.inherit;
|
|
|
|
});
|
|
|
|
|
2023-11-21 21:36:08 +08:00
|
|
|
if (
|
|
|
|
!(await inheritedCollection.existsInDb({
|
|
|
|
transaction,
|
|
|
|
}))
|
|
|
|
) {
|
|
|
|
let maxSequenceVal = 0;
|
|
|
|
let maxSequenceName;
|
|
|
|
|
|
|
|
// find max sequence
|
|
|
|
if (childAttributes.id && childAttributes.id.autoIncrement) {
|
|
|
|
for (const parent of parents) {
|
|
|
|
const sequenceNameResult = await queryInterface.sequelize.query(
|
|
|
|
`SELECT column_default
|
2023-01-29 15:46:53 +08:00
|
|
|
FROM information_schema.columns
|
2023-03-05 14:45:56 +08:00
|
|
|
WHERE table_name = '${parent.model.tableName}'
|
2023-03-10 19:16:00 +08:00
|
|
|
and table_schema = '${parent.collectionSchema()}'
|
2023-01-29 15:46:53 +08:00
|
|
|
and "column_name" = 'id';`,
|
2023-11-21 21:36:08 +08:00
|
|
|
{
|
|
|
|
transaction,
|
|
|
|
},
|
|
|
|
);
|
2022-11-18 20:46:55 +08:00
|
|
|
|
2023-11-21 21:36:08 +08:00
|
|
|
if (!sequenceNameResult[0].length) {
|
|
|
|
continue;
|
|
|
|
}
|
2022-11-18 20:46:55 +08:00
|
|
|
|
2023-11-21 21:36:08 +08:00
|
|
|
const columnDefault = sequenceNameResult[0][0]['column_default'];
|
2022-11-17 20:16:03 +08:00
|
|
|
|
2023-11-21 21:36:08 +08:00
|
|
|
if (!columnDefault) {
|
|
|
|
throw new Error(`Can't find sequence name of parent collection ${parent.options.name}`);
|
|
|
|
}
|
2022-11-17 20:16:03 +08:00
|
|
|
|
2023-11-21 21:36:08 +08:00
|
|
|
const regex = new RegExp(/nextval\('(.*)'::regclass\)/);
|
|
|
|
const match = regex.exec(columnDefault);
|
2022-11-17 20:16:03 +08:00
|
|
|
|
2023-11-21 21:36:08 +08:00
|
|
|
const sequenceName = match[1];
|
|
|
|
const sequenceCurrentValResult = await queryInterface.sequelize.query(
|
|
|
|
`select last_value
|
2023-01-29 15:46:53 +08:00
|
|
|
from ${sequenceName}`,
|
2023-11-21 21:36:08 +08:00
|
|
|
{
|
|
|
|
transaction,
|
|
|
|
},
|
|
|
|
);
|
2022-11-30 10:00:46 +08:00
|
|
|
|
2023-11-21 21:36:08 +08:00
|
|
|
const sequenceCurrentVal = parseInt(sequenceCurrentValResult[0][0]['last_value']);
|
2022-11-16 12:53:58 +08:00
|
|
|
|
2023-11-21 21:36:08 +08:00
|
|
|
if (sequenceCurrentVal > maxSequenceVal) {
|
|
|
|
maxSequenceName = sequenceName;
|
|
|
|
maxSequenceVal = sequenceCurrentVal;
|
|
|
|
}
|
2022-11-16 12:53:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-21 21:36:08 +08:00
|
|
|
await this.createTable(tableName, childAttributes, options, model, parents);
|
2022-11-16 12:53:58 +08:00
|
|
|
|
2023-11-21 21:36:08 +08:00
|
|
|
// if we have max sequence, set it to child table
|
|
|
|
if (maxSequenceName) {
|
|
|
|
const parentsDeep = Array.from(db.inheritanceMap.getParents(inheritedCollection.name)).map((parent) =>
|
|
|
|
db.getCollection(parent).getTableNameWithSchema(),
|
|
|
|
);
|
2022-11-16 12:53:58 +08:00
|
|
|
|
2023-11-21 21:36:08 +08:00
|
|
|
const sequenceTables = [...parentsDeep, tableName];
|
2022-11-16 12:53:58 +08:00
|
|
|
|
2023-11-21 21:36:08 +08:00
|
|
|
for (const sequenceTable of sequenceTables) {
|
|
|
|
const tableName = sequenceTable.tableName;
|
|
|
|
const schemaName = sequenceTable.schema;
|
2023-03-05 14:45:56 +08:00
|
|
|
|
2023-11-21 21:36:08 +08:00
|
|
|
const idColumnSql = `SELECT column_name
|
2023-03-10 19:16:00 +08:00
|
|
|
FROM information_schema.columns
|
2023-04-06 09:05:47 +08:00
|
|
|
WHERE table_name = '${tableName}'
|
2023-03-10 19:16:00 +08:00
|
|
|
and column_name = 'id'
|
|
|
|
and table_schema = '${schemaName}';
|
2023-04-06 09:05:47 +08:00
|
|
|
`;
|
|
|
|
|
2023-11-21 21:36:08 +08:00
|
|
|
const idColumnQuery = await queryInterface.sequelize.query(idColumnSql, {
|
|
|
|
transaction,
|
|
|
|
});
|
2022-11-30 11:37:59 +08:00
|
|
|
|
2023-11-21 21:36:08 +08:00
|
|
|
if (idColumnQuery[0].length == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
2022-11-30 11:37:59 +08:00
|
|
|
|
2023-11-21 21:36:08 +08:00
|
|
|
await queryInterface.sequelize.query(
|
|
|
|
`alter table ${db.utils.quoteTable(sequenceTable)}
|
2023-01-29 15:46:53 +08:00
|
|
|
alter column id set default nextval('${maxSequenceName}')`,
|
2023-11-21 21:36:08 +08:00
|
|
|
{
|
|
|
|
transaction,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
2022-11-17 20:16:03 +08:00
|
|
|
}
|
2022-11-16 12:53:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (options.alter) {
|
|
|
|
const columns = await queryInterface.describeTable(tableName, options);
|
|
|
|
|
2023-02-13 21:38:47 +08:00
|
|
|
for (const attribute in childAttributes) {
|
|
|
|
const columnName = childAttributes[attribute].field;
|
|
|
|
|
2022-11-16 12:53:58 +08:00
|
|
|
if (!columns[columnName]) {
|
|
|
|
await queryInterface.addColumn(tableName, columnName, childAttributes[columnName], options);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-05 14:45:56 +08:00
|
|
|
static async createTable(tableName, attributes, options, model, parents) {
|
2022-11-16 12:53:58 +08:00
|
|
|
let sql = '';
|
|
|
|
|
|
|
|
options = { ...options };
|
|
|
|
|
|
|
|
if (options && options.uniqueKeys) {
|
|
|
|
lodash.forOwn(options.uniqueKeys, (uniqueKey) => {
|
|
|
|
if (uniqueKey.customIndex === undefined) {
|
|
|
|
uniqueKey.customIndex = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (model) {
|
|
|
|
options.uniqueKeys = options.uniqueKeys || model.uniqueKeys;
|
|
|
|
}
|
|
|
|
|
|
|
|
const queryGenerator = model.queryGenerator;
|
|
|
|
|
|
|
|
attributes = lodash.mapValues(attributes, (attribute) => model.sequelize.normalizeAttribute(attribute));
|
|
|
|
|
|
|
|
attributes = queryGenerator.attributesToSQL(attributes, { table: tableName, context: 'createTable' });
|
|
|
|
|
|
|
|
sql = `${queryGenerator.createTableQuery(tableName, attributes, options)}`.replace(
|
|
|
|
';',
|
2023-03-05 14:45:56 +08:00
|
|
|
` INHERITS (${parents
|
2023-02-16 10:53:04 +08:00
|
|
|
.map((t) => {
|
2023-03-10 19:16:00 +08:00
|
|
|
return t.getTableNameWithSchema();
|
2023-02-16 10:53:04 +08:00
|
|
|
})
|
|
|
|
.join(', ')});`,
|
2022-11-16 12:53:58 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
return await model.sequelize.query(sql, options);
|
|
|
|
}
|
|
|
|
}
|