Skip to content

Commit

Permalink
Update store modelRelations and modelIndexedFields on db migrations (s…
Browse files Browse the repository at this point in the history
…ubquery#2264)

* Update store modelRelations and modelIndexedFields on db migrations

* Update changelog
  • Loading branch information
stwiname authored Feb 22, 2024
1 parent fbbc12f commit 99a4cb1
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 18 deletions.
1 change: 1 addition & 0 deletions packages/node-core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
- Fixed non-atomic schema migration execution (#2244)
- Testing Suites should run with unfinalizedBlocks `false` (#2258)
- StoreService not being fully in sync with db (#2264)

### Changed
- Improve error handling when fetching blocks (#2256)
Expand Down
7 changes: 7 additions & 0 deletions packages/node-core/src/configure/ProjectUpgrade.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ export class ProjectUpgradeSevice<P extends ISubqueryProject = ISubqueryProject>
#currentProject: P;

#storeCache?: StoreCacheService;
#storeService?: StoreService;
#schema?: string;
#initialized = false;

private config?: NodeConfig;
Expand All @@ -129,6 +131,7 @@ export class ProjectUpgradeSevice<P extends ISubqueryProject = ISubqueryProject>
this.#currentHeight = currentHeight;
this.#currentProject = this.getProject(this.#currentHeight);
}

async init(
storeService: StoreService,
currentHeight: number,
Expand All @@ -143,6 +146,8 @@ export class ProjectUpgradeSevice<P extends ISubqueryProject = ISubqueryProject>
}
this.#initialized = true;
this.#storeCache = storeService.storeCache;
this.#storeService = storeService;
this.#schema = schema;
this.config = config;

this.migrationService = new SchemaMigrationService(
Expand Down Expand Up @@ -232,6 +237,8 @@ export class ProjectUpgradeSevice<P extends ISubqueryProject = ISubqueryProject>
const modifiedModels = await this.migrationService.run(project.schema, newProject.schema, transaction);
if (modifiedModels) {
this.#storeCache?.updateModels(modifiedModels);
assert(this.#schema, 'Schema is undefined');
await this.#storeService?.updateModels(this.#schema, getAllEntitiesRelations(newProject.schema));
}
}
}
Expand Down
30 changes: 12 additions & 18 deletions packages/node-core/src/indexer/store.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,7 @@ import {
hexToU8a,
GraphQLModelsType,
} from '@subql/utils';
import {
IndexesOptions,
ModelAttributes,
ModelStatic,
Op,
QueryTypes,
Sequelize,
Transaction,
Utils,
} from '@subql/x-sequelize';
import {IndexesOptions, ModelAttributes, ModelStatic, Op, QueryTypes, Sequelize, Transaction} from '@subql/x-sequelize';
import {camelCase, flatten, upperFirst} from 'lodash';
import {NodeConfig} from '../configure';
import {
Expand All @@ -44,8 +35,6 @@ import {ISubqueryProject} from './types';
const logger = getLogger('StoreService');
const NULL_MERKEL_ROOT = hexToU8a('0x00');

type RemovedIndexes = Record<string, IndexesOptions[]>;

interface IndexField {
entityName: string;
fieldName: string;
Expand Down Expand Up @@ -165,12 +154,7 @@ export class StoreService {
logger.error(e, `Having a problem when syncing schema`);
process.exit(1);
}
try {
this._modelIndexedFields = await this.getAllIndexFields(schema);
} catch (e: any) {
logger.error(e, `Having a problem when get indexed fields`);
process.exit(1);
}
await this.updateModels(schema, modelsRelations);
}

async initHotSchemaReloadQueries(schema: string): Promise<void> {
Expand Down Expand Up @@ -221,6 +205,16 @@ export class StoreService {
await schemaMigrationService.run(null, this.subqueryProject.schema, tx);
}

async updateModels(schema: string, modelsRelations: GraphQLModelsRelationsEnums): Promise<void> {
this._modelsRelations = modelsRelations;
try {
this._modelIndexedFields = await this.getAllIndexFields(schema);
} catch (e: any) {
logger.error(e, `Having a problem when get indexed fields`);
process.exit(1);
}
}

defineModel(
model: GraphQLModelsType,
attributes: ModelAttributes<any>,
Expand Down

0 comments on commit 99a4cb1

Please sign in to comment.