2022-11-16 12:53:58 +08:00
|
|
|
import { InheritedCollection } from './inherited-collection';
|
|
|
|
import lodash from 'lodash';
|
|
|
|
import { Sequelize } from 'sequelize';
|
|
|
|
|
|
|
|
export class SyncRunner {
|
|
|
|
static async syncInheritModel(model: any, options: any) {
|
|
|
|
const { transaction } = options;
|
|
|
|
|
|
|
|
const inheritedCollection = model.collection as InheritedCollection;
|
|
|
|
const db = inheritedCollection.context.database;
|
|
|
|
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) {
|
|
|
|
throw new Error("Inherit model can't be created without parents");
|
|
|
|
}
|
|
|
|
|
2022-11-16 12:53:58 +08:00
|
|
|
const parentTables = parents.map((parent) => parent.model.tableName);
|
|
|
|
|
|
|
|
const tableName = model.getTableName();
|
|
|
|
|
|
|
|
const attributes = model.tableAttributes;
|
|
|
|
|
|
|
|
const childAttributes = lodash.pickBy(attributes, (value) => {
|
|
|
|
return !value.inherit;
|
|
|
|
});
|
|
|
|
|
|
|
|
let maxSequenceVal = 0;
|
|
|
|
let maxSequenceName;
|
|
|
|
|
|
|
|
if (childAttributes.id && childAttributes.id.autoIncrement) {
|
|
|
|
for (const parent of parentTables) {
|
|
|
|
const sequenceNameResult = await queryInterface.sequelize.query(
|
2022-11-17 20:16:03 +08:00
|
|
|
`SELECT column_default FROM information_schema.columns WHERE
|
|
|
|
table_name='${parent}' and "column_name" = 'id';`,
|
2022-11-16 12:53:58 +08:00
|
|
|
{
|
|
|
|
transaction,
|
|
|
|
},
|
|
|
|
);
|
2022-11-18 20:46:55 +08:00
|
|
|
|
|
|
|
if (!sequenceNameResult[0].length) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-11-17 20:16:03 +08:00
|
|
|
const columnDefault = sequenceNameResult[0][0]['column_default'];
|
|
|
|
|
|
|
|
if (!columnDefault) {
|
|
|
|
throw new Error(`Can't find sequence name of ${parent}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
const regex = new RegExp(/nextval\('(\w+)\'.*\)/);
|
|
|
|
const match = regex.exec(columnDefault);
|
|
|
|
|
|
|
|
const sequenceName = match[1];
|
2022-11-16 12:53:58 +08:00
|
|
|
|
|
|
|
const sequenceCurrentValResult = await queryInterface.sequelize.query(
|
|
|
|
`select last_value from ${sequenceName}`,
|
|
|
|
{
|
|
|
|
transaction,
|
|
|
|
},
|
|
|
|
);
|
2022-11-17 20:16:03 +08:00
|
|
|
const sequenceCurrentVal = parseInt(sequenceCurrentValResult[0][0]['last_value']);
|
2022-11-16 12:53:58 +08:00
|
|
|
|
|
|
|
if (sequenceCurrentVal > maxSequenceVal) {
|
|
|
|
maxSequenceName = sequenceName;
|
|
|
|
maxSequenceVal = sequenceCurrentVal;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.createTable(tableName, childAttributes, options, model, parentTables);
|
|
|
|
|
2022-11-17 20:16:03 +08:00
|
|
|
if (maxSequenceName) {
|
|
|
|
const parentsDeep = Array.from(db.inheritanceMap.getParents(inheritedCollection.name)).map(
|
|
|
|
(parent) => db.getCollection(parent).model.tableName,
|
|
|
|
);
|
2022-11-16 12:53:58 +08:00
|
|
|
|
2022-11-17 20:16:03 +08:00
|
|
|
const sequenceTables = [...parentsDeep, tableName];
|
2022-11-16 12:53:58 +08:00
|
|
|
|
2022-11-17 20:16:03 +08:00
|
|
|
for (const sequenceTable of sequenceTables) {
|
|
|
|
await queryInterface.sequelize.query(
|
|
|
|
`alter table "${sequenceTable}" alter column id set default nextval('${maxSequenceName}')`,
|
|
|
|
{
|
|
|
|
transaction,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
2022-11-16 12:53:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (options.alter) {
|
|
|
|
const columns = await queryInterface.describeTable(tableName, options);
|
|
|
|
|
|
|
|
for (const columnName in childAttributes) {
|
|
|
|
if (!columns[columnName]) {
|
|
|
|
await queryInterface.addColumn(tableName, columnName, childAttributes[columnName], options);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static async createTable(tableName, attributes, options, model, parentTables) {
|
|
|
|
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(
|
|
|
|
';',
|
|
|
|
` INHERITS (${parentTables.map((t) => `"${t}"`).join(', ')});`,
|
|
|
|
);
|
|
|
|
|
|
|
|
return await model.sequelize.query(sql, options);
|
|
|
|
}
|
|
|
|
}
|