Skip to content

Commit

Permalink
Workers: Fix SequelizeDatabaseError - tuple concurrently updated (#1458)
Browse files Browse the repository at this point in the history
* add flag for hot-schema reload on node, ensure query is on main thread

* remove flags
  • Loading branch information
bz888 authored Dec 12, 2022
1 parent 3f21af4 commit f736386
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
28 changes: 18 additions & 10 deletions packages/node-core/src/indexer/store.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,24 @@ export class StoreService {
);
}

async initHotSchemaReloadQueries(schema: string): Promise<void> {
/* 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<void> {
const enumTypeMap = new Map<string, string>();
Expand Down Expand Up @@ -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);
Expand Down
7 changes: 6 additions & 1 deletion packages/node/src/indexer/project.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -179,6 +181,9 @@ export class ProjectService {
return schema;
}

private async initHotSchemaReload(): Promise<void> {
await initHotSchemaReload(this.schema, this.storeService);
}
private async initDbSchema(): Promise<void> {
await initDbSchema(this.project, this.schema, this.storeService);
}
Expand Down
7 changes: 7 additions & 0 deletions packages/node/src/utils/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
await storeService.initHotSchemaReloadQueries(schema);
}

0 comments on commit f736386

Please sign in to comment.