2022-11-16 04:53:58 +00: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 02:53:04 +00:00
|
|
|
|
2022-11-16 04:53:58 +00: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 09:33:20 +00:00
|
|
|
if (!parents) {
|
2022-11-26 01:24:53 +00: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 09:33:20 +00:00
|
|
|
}
|
|
|
|
|
2023-03-10 11:16:00 +00:00
|
|
|
const tableName = inheritedCollection.getTableNameWithSchema();
|
2022-11-16 04:53:58 +00:00
|
|
|
|
|
|
|
const attributes = model.tableAttributes;
|
|
|
|
|
|
|
|
const childAttributes = lodash.pickBy(attributes, (value) => {
|
|
|
|
return !value.inherit;
|
|
|
|
});
|
|
|
|
|
|
|
|
let maxSequenceVal = 0;
|
|
|
|
let maxSequenceName;
|
|
|
|
|
2023-03-05 06:45:56 +00:00
|
|
|
// find max sequence
|
2022-11-16 04:53:58 +00:00
|
|
|
if (childAttributes.id && childAttributes.id.autoIncrement) {
|
2023-03-05 06:45:56 +00:00
|
|
|
for (const parent of parents) {
|
2022-11-16 04:53:58 +00:00
|
|
|
const sequenceNameResult = await queryInterface.sequelize.query(
|
2023-01-29 07:46:53 +00:00
|
|
|
`SELECT column_default
|
|
|
|
FROM information_schema.columns
|
2023-03-05 06:45:56 +00:00
|
|
|
WHERE table_name = '${parent.model.tableName}'
|
2023-03-10 11:16:00 +00:00
|
|
|
and table_schema = '${parent.collectionSchema()}'
|
2023-01-29 07:46:53 +00:00
|
|
|
and "column_name" = 'id';`,
|
2022-11-16 04:53:58 +00:00
|
|
|
{
|
|
|
|
transaction,
|
|
|
|
},
|
|
|
|
);
|
2022-11-18 12:46:55 +00:00
|
|
|
|
|
|
|
if (!sequenceNameResult[0].length) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-11-17 12:16:03 +00:00
|
|
|
const columnDefault = sequenceNameResult[0][0]['column_default'];
|
|
|
|
|
|
|
|
if (!columnDefault) {
|
|
|
|
throw new Error(`Can't find sequence name of ${parent}`);
|
|
|
|
}
|
|
|
|
|
2023-02-16 02:53:04 +00:00
|
|
|
const regex = new RegExp(/nextval\('(.*)'::regclass\)/);
|
2022-11-17 12:16:03 +00:00
|
|
|
const match = regex.exec(columnDefault);
|
|
|
|
|
|
|
|
const sequenceName = match[1];
|
2022-11-16 04:53:58 +00:00
|
|
|
|
|
|
|
const sequenceCurrentValResult = await queryInterface.sequelize.query(
|
2023-01-29 07:46:53 +00:00
|
|
|
`select last_value
|
|
|
|
from ${sequenceName}`,
|
2022-11-16 04:53:58 +00:00
|
|
|
{
|
|
|
|
transaction,
|
|
|
|
},
|
|
|
|
);
|
2022-11-30 02:00:46 +00:00
|
|
|
|
2022-11-17 12:16:03 +00:00
|
|
|
const sequenceCurrentVal = parseInt(sequenceCurrentValResult[0][0]['last_value']);
|
2022-11-16 04:53:58 +00:00
|
|
|
|
|
|
|
if (sequenceCurrentVal > maxSequenceVal) {
|
|
|
|
maxSequenceName = sequenceName;
|
|
|
|
maxSequenceVal = sequenceCurrentVal;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-05 06:45:56 +00:00
|
|
|
await this.createTable(tableName, childAttributes, options, model, parents);
|
2022-11-16 04:53:58 +00:00
|
|
|
|
2023-03-05 06:45:56 +00:00
|
|
|
// if we have max sequence, set it to child table
|
2022-11-17 12:16:03 +00:00
|
|
|
if (maxSequenceName) {
|
2023-03-05 06:45:56 +00:00
|
|
|
const parentsDeep = Array.from(db.inheritanceMap.getParents(inheritedCollection.name)).map((parent) =>
|
2023-03-10 11:16:00 +00:00
|
|
|
db.getCollection(parent).getTableNameWithSchema(),
|
2022-11-17 12:16:03 +00:00
|
|
|
);
|
2022-11-16 04:53:58 +00:00
|
|
|
|
2023-03-05 06:45:56 +00:00
|
|
|
const sequenceTables = [...parentsDeep, tableName];
|
2022-11-16 04:53:58 +00:00
|
|
|
|
2022-11-17 12:16:03 +00:00
|
|
|
for (const sequenceTable of sequenceTables) {
|
2023-03-05 06:45:56 +00:00
|
|
|
const tableName = sequenceTable.tableName;
|
|
|
|
const schemaName = sequenceTable.schema;
|
|
|
|
|
|
|
|
const queryName = Boolean(tableName.match(/[A-Z]/)) && !tableName.includes(`"`) ? `"${tableName}"` : tableName;
|
2022-12-31 11:16:49 +00:00
|
|
|
|
2022-11-30 03:37:59 +00:00
|
|
|
const idColumnQuery = await queryInterface.sequelize.query(
|
2023-03-05 06:45:56 +00:00
|
|
|
`SELECT column_name
|
2023-03-10 11:16:00 +00:00
|
|
|
FROM information_schema.columns
|
|
|
|
WHERE table_name = '${queryName}'
|
|
|
|
and column_name = 'id'
|
|
|
|
and table_schema = '${schemaName}';
|
2023-01-29 07:46:53 +00:00
|
|
|
`,
|
2022-11-30 03:37:59 +00:00
|
|
|
{
|
|
|
|
transaction,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
if (idColumnQuery[0].length == 0) {
|
|
|
|
continue;
|
2022-11-30 02:00:46 +00:00
|
|
|
}
|
2022-11-30 03:37:59 +00:00
|
|
|
|
|
|
|
await queryInterface.sequelize.query(
|
2023-03-05 06:45:56 +00:00
|
|
|
`alter table ${db.utils.quoteTable(sequenceTable)}
|
2023-01-29 07:46:53 +00:00
|
|
|
alter column id set default nextval('${maxSequenceName}')`,
|
2022-11-30 03:37:59 +00:00
|
|
|
{
|
|
|
|
transaction,
|
|
|
|
},
|
|
|
|
);
|
2022-11-17 12:16:03 +00:00
|
|
|
}
|
2022-11-16 04:53:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (options.alter) {
|
|
|
|
const columns = await queryInterface.describeTable(tableName, options);
|
|
|
|
|
2023-02-13 13:38:47 +00:00
|
|
|
for (const attribute in childAttributes) {
|
|
|
|
const columnName = childAttributes[attribute].field;
|
|
|
|
|
2022-11-16 04:53:58 +00:00
|
|
|
if (!columns[columnName]) {
|
|
|
|
await queryInterface.addColumn(tableName, columnName, childAttributes[columnName], options);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-05 06:45:56 +00:00
|
|
|
static async createTable(tableName, attributes, options, model, parents) {
|
2022-11-16 04:53:58 +00: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 06:45:56 +00:00
|
|
|
` INHERITS (${parents
|
2023-02-16 02:53:04 +00:00
|
|
|
.map((t) => {
|
2023-03-10 11:16:00 +00:00
|
|
|
return t.getTableNameWithSchema();
|
2023-02-16 02:53:04 +00:00
|
|
|
})
|
|
|
|
.join(', ')});`,
|
2022-11-16 04:53:58 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return await model.sequelize.query(sql, options);
|
|
|
|
}
|
|
|
|
}
|