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;
|
|
|
|
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(
|
|
|
|
`Inherit model ${inheritedCollection.name} can't be created without parents, parents option is ${lodash
|
|
|
|
.castArray(inheritedCollection.options.inherits)
|
|
|
|
.join(', ')}`,
|
|
|
|
);
|
2022-11-20 17:33:20 +08:00
|
|
|
}
|
|
|
|
|
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(
|
2023-01-29 15:46:53 +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}`);
|
|
|
|
}
|
|
|
|
|
2022-12-31 19:16:49 +08:00
|
|
|
const regex = new RegExp(/nextval\('("?\w+"?)\'.*\)/);
|
2022-11-17 20:16:03 +08:00
|
|
|
const match = regex.exec(columnDefault);
|
|
|
|
|
|
|
|
const sequenceName = match[1];
|
2022-11-16 12:53:58 +08:00
|
|
|
|
|
|
|
const sequenceCurrentValResult = await queryInterface.sequelize.query(
|
2023-01-29 15:46:53 +08:00
|
|
|
`select last_value
|
|
|
|
from ${sequenceName}`,
|
2022-11-16 12:53:58 +08:00
|
|
|
{
|
|
|
|
transaction,
|
|
|
|
},
|
|
|
|
);
|
2022-11-30 10:00:46 +08:00
|
|
|
|
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) {
|
2022-12-31 19:16:49 +08:00
|
|
|
const queryName = Boolean(sequenceTable.match(/[A-Z]/)) ? `"${sequenceTable}"` : sequenceTable;
|
|
|
|
|
2022-11-30 11:37:59 +08:00
|
|
|
const idColumnQuery = await queryInterface.sequelize.query(
|
|
|
|
`
|
2023-01-29 15:46:53 +08:00
|
|
|
SELECT true
|
|
|
|
FROM pg_attribute
|
|
|
|
WHERE attrelid = '${queryName}'::regclass -- cast to a registered class (table)
|
2022-11-30 11:37:59 +08:00
|
|
|
AND attname = 'id'
|
2023-01-29 15:46:53 +08:00
|
|
|
AND NOT attisdropped
|
|
|
|
`,
|
2022-11-30 11:37:59 +08:00
|
|
|
{
|
|
|
|
transaction,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
if (idColumnQuery[0].length == 0) {
|
|
|
|
continue;
|
2022-11-30 10:00:46 +08:00
|
|
|
}
|
2022-11-30 11:37:59 +08:00
|
|
|
|
|
|
|
await queryInterface.sequelize.query(
|
2023-01-29 15:46:53 +08:00
|
|
|
`alter table "${sequenceTable}"
|
|
|
|
alter column id set default nextval('${maxSequenceName}')`,
|
2022-11-30 11:37:59 +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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|