diff --git a/packages/core/database/src/__tests__/inhertits/collection-inherits.test.ts b/packages/core/database/src/__tests__/inhertits/collection-inherits.test.ts index 315cebf059..2a98ef844f 100644 --- a/packages/core/database/src/__tests__/inhertits/collection-inherits.test.ts +++ b/packages/core/database/src/__tests__/inhertits/collection-inherits.test.ts @@ -84,6 +84,35 @@ describe.runIf(isPg())('collection inherits', () => { expect(err).toBeUndefined(); }); + it('should emit afterSync event', async () => { + const Root = db.collection({ + name: 'root', + fields: [ + { name: 'name', type: 'string' }, + { + name: 'bs', + type: 'hasMany', + target: 'b', + foreignKey: 'root_id', + }, + ], + }); + + const Child = db.collection({ + name: 'child', + inherits: ['root'], + }); + + const fn = vi.fn(); + db.on('child.afterSync', (options) => { + fn(); + }); + + await db.sync(); + + expect(fn).toBeCalled(); + }); + it('should append __collection with eager load', async () => { const Root = db.collection({ name: 'root', diff --git a/packages/core/database/src/inherited-sync-runner.ts b/packages/core/database/src/inherited-sync-runner.ts index f4971c3ef9..e5060dcb5d 100644 --- a/packages/core/database/src/inherited-sync-runner.ts +++ b/packages/core/database/src/inherited-sync-runner.ts @@ -13,6 +13,7 @@ import lodash from 'lodash'; export class InheritedSyncRunner { static async syncInheritModel(model: any, options: any) { const { transaction } = options; + options.hooks = options.hooks === undefined ? true : !!options.hooks; const inheritedCollection = model.collection as InheritedCollection; const db = inheritedCollection.context.database; @@ -62,10 +63,10 @@ export class InheritedSyncRunner { for (const parent of parents) { const sequenceNameResult = await queryInterface.sequelize.query( `SELECT column_default - FROM information_schema.columns - WHERE table_name = '${parent.model.tableName}' - and table_schema = '${parent.collectionSchema()}' - and "column_name" = 'id';`, + FROM information_schema.columns + WHERE table_name = '${parent.model.tableName}' + and table_schema = '${parent.collectionSchema()}' + and "column_name" = 'id';`, { transaction, }, @@ -87,7 +88,7 @@ export class InheritedSyncRunner { const sequenceName = match[1]; const sequenceCurrentValResult = await queryInterface.sequelize.query( `select last_value - from ${sequenceName}`, + from ${sequenceName}`, { transaction, }, @@ -117,10 +118,10 @@ export class InheritedSyncRunner { const schemaName = sequenceTable.schema; const idColumnSql = `SELECT column_name - FROM information_schema.columns - WHERE table_name = '${tableName}' - and column_name = 'id' - and table_schema = '${schemaName}'; + FROM information_schema.columns + WHERE table_name = '${tableName}' + and column_name = 'id' + and table_schema = '${schemaName}'; `; const idColumnQuery = await queryInterface.sequelize.query(idColumnSql, { @@ -133,7 +134,7 @@ export class InheritedSyncRunner { await queryInterface.sequelize.query( `alter table ${db.utils.quoteTable(sequenceTable)} - alter column id set default nextval('${maxSequenceName}')`, + alter column id set default nextval('${maxSequenceName}')`, { transaction, }, @@ -153,6 +154,14 @@ export class InheritedSyncRunner { } } } + + if (options.hooks) { + await model.runHooks('afterSync', { + ...options, + modelName: model.name, + transaction, + }); + } } static async createTable(tableName, attributes, options, model, parents) {