diff --git a/packages/node-core/src/indexer/store.service.ts b/packages/node-core/src/indexer/store.service.ts index 8738b4359b..6c59e56901 100644 --- a/packages/node-core/src/indexer/store.service.ts +++ b/packages/node-core/src/indexer/store.service.ts @@ -118,6 +118,24 @@ export class StoreService { ); } + async initHotSchemaReloadQueries(schema: string): Promise { + /* These SQL queries are to allow hot-schema reload on query service */ + const schemaTriggerName = hashName(schema, 'schema_trigger', this.metaDataRepo.tableName); + const schemaTriggers = await getTriggers(this.sequelize, schemaTriggerName); + + try { + await Promise.all([ + await this.sequelize.query(`${createSchemaTriggerFunction(schema)}`), + schemaTriggers.length === 0 ?? + (await this.sequelize.query(` + ${createSchemaTrigger(schema, this.metaDataRepo.tableName)} + `)), + ]); + } catch (e) { + logger.error(`Failed to init Hot schema reload`); + } + } + // eslint-disable-next-line complexity async syncSchema(schema: string): Promise { const enumTypeMap = new Map(); @@ -277,16 +295,6 @@ export class StoreService { this.subqueryProject.network.chainId ); - /* These SQL queries are to allow hot-schema reload on query service */ - extraQueries.push(createSchemaTriggerFunction(schema)); - const schemaTriggerName = hashName(schema, 'schema_trigger', this.metaDataRepo.tableName); - - const schemaTriggers = await getTriggers(this.sequelize, schemaTriggerName); - - if (schemaTriggers.length === 0) { - extraQueries.push(createSchemaTrigger(schema, this.metaDataRepo.tableName)); - } - await this.sequelize.sync(); await this.setMetadata('historicalStateEnabled', this.historical); diff --git a/packages/node/src/indexer/project.service.ts b/packages/node/src/indexer/project.service.ts index 41ea7ed04b..af17c8f4c8 100644 --- a/packages/node/src/indexer/project.service.ts +++ b/packages/node/src/indexer/project.service.ts @@ -23,7 +23,7 @@ import { SubqlProjectDs, SubqueryProject, } from '../configure/SubqueryProject'; -import { initDbSchema } from '../utils/project'; +import { initDbSchema, initHotSchemaReload } from '../utils/project'; import { reindex } from '../utils/reindex'; import { ApiService } from './api.service'; import { DsProcessorService } from './ds-processor.service'; @@ -106,6 +106,8 @@ export class ProjectService { this.metadataRepo = await this.ensureMetadata(); this.dynamicDsService.init(this.metadataRepo); + await this.initHotSchemaReload(); + if (this.nodeConfig.proofOfIndex) { const blockOffset = await this.getMetadataBlockOffset(); void this.setBlockOffset(Number(blockOffset)); @@ -179,6 +181,9 @@ export class ProjectService { return schema; } + private async initHotSchemaReload(): Promise { + await initHotSchemaReload(this.schema, this.storeService); + } private async initDbSchema(): Promise { await initDbSchema(this.project, this.schema, this.storeService); } diff --git a/packages/node/src/utils/project.ts b/packages/node/src/utils/project.ts index 6c4053da69..e35336b785 100644 --- a/packages/node/src/utils/project.ts +++ b/packages/node/src/utils/project.ts @@ -309,3 +309,10 @@ export async function initDbSchema( const modelsRelation = getAllEntitiesRelations(project.schema); await storeService.init(modelsRelation, schema); } + +export async function initHotSchemaReload( + schema: string, + storeService: StoreService, +): Promise { + await storeService.initHotSchemaReloadQueries(schema); +}