Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Workers: Fix SequelizeDatabaseError - tuple concurrently updated #1458

Merged
merged 2 commits into from
Dec 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}