tachybase_todo/packages/core/database/src/sync-runner.ts
ChengLei Shao e991b2965a
feat: collection inheritance (#1069)
* chore: test

* chore: inherited-collection class

* feat: collection inherit

* feat: collection inherit

* feat: inhertis sync runner

* test: get parents fields

* feat: collection inherit style promote

* feat: sync

* feat: sync alter table

* feat: pgOnly Test

* fix: child collection create api

* feat: replace parent field

* chore: reload parent fields

* test: reload collection test

* feat: details are displayed according to conditions

* fix: typo

* feat: inheritance map class

* chore: is parent node

* feat: display where child row created from

* fix: find with appends

* feat: add parent collection fields

* fix: create table

* feat: load fields for all children

* refactor: sync fields from parent

* test: has one field inhertis

* feat: replace child association target

* feat: should not replace child field when parent field update

* test: should update inherit field when parent field update

* feat: only the blocks directly inherited from the current data are displayed

* fix: inherit from multiple collections

* feat: only the blocks directly inherited from the current data are displayed

* fix: test

* feat: parent collection expend

* fix: test

* test: belongsToMany inherits

* test: belongsToMany inherits

* feat: block display

* feat: collection inherite

* feat: collection inherite

* feat: multiple inherits

* fix: sync runner

* feat: collection inherite

* feat: collecton inherits

* feat: cannot be modified after inheritance and saving

* feat: collection inherit for graph

* feat: collection inherits

* fix: drop inhertied field

* fix: should throw error when type conflit

* feat: output inherited fields

* feat: bulk update collection fields

* feat: collection fields

* feat: collection fields

* test: create relation with child table

* fix: test

* fix: test

* fix: test

* feat: style impove

* test: should not replace field with difference type

* feat: add text

* fix: throw error when replace field with difference type

* feat: overriding

* feat: kan bankanban group fields

* feat: calendar block fields

* feat: kan bankanban group fields

* fix: test

* feat: relationship fields

* feat: should delete child's field when parent field deleted

* feat: foreign key filter

* fix: build error & multiple inherit destory field

* fix: test

* chore: disable error

* feat: no recursive update associations (#1091)

* feat: update associations

* fix(collection-manager): should update uiSchema

* chore: flip if

* feat: mutile inherits

* feat: db dialect

* feat: inherits show by database

* chore: git hash into docker image

* fix: js gzip

* fix: dockerfile

* chore: error message

* feat: overriding

* feat: overriding

* feat: overriding

* feat: local

* feat: filter fields by interface

* fix: database logging env

* test: replace hasOne target

* feat: add view

* feat: local

* chore: enable error

* fix: update docs

Co-authored-by: katherinehhh <katherine_15995@163.com>
Co-authored-by: chenos <chenlinxh@gmail.com>
2022-11-16 12:53:58 +08:00

118 lines
3.5 KiB
TypeScript

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;
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(
`select pg_get_serial_sequence('"${parent}"', 'id')`,
{
transaction,
},
);
const sequenceName = sequenceNameResult[0][0]['pg_get_serial_sequence'];
const sequenceCurrentValResult = await queryInterface.sequelize.query(
`select last_value from ${sequenceName}`,
{
transaction,
},
);
const sequenceCurrentVal = sequenceCurrentValResult[0][0]['last_value'];
if (sequenceCurrentVal > maxSequenceVal) {
maxSequenceName = sequenceName;
maxSequenceVal = sequenceCurrentVal;
}
}
}
await this.createTable(tableName, childAttributes, options, model, parentTables);
const parentsDeep = Array.from(db.inheritanceMap.getParents(inheritedCollection.name)).map(
(parent) => db.getCollection(parent).model.tableName,
);
const sequenceTables = [...parentsDeep, tableName];
for (const sequenceTable of sequenceTables) {
await queryInterface.sequelize.query(
`alter table "${sequenceTable}" alter column id set default nextval('${maxSequenceName}')`,
{
transaction,
},
);
}
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);
}
}