diff --git a/packages/node-core/src/indexer/store.service.ts b/packages/node-core/src/indexer/store.service.ts index 1a47b180e4..f7ce9ce717 100644 --- a/packages/node-core/src/indexer/store.service.ts +++ b/packages/node-core/src/indexer/store.service.ts @@ -46,6 +46,7 @@ import { makeTriggerName, createSchemaTriggerFunction, createSchemaTrigger, + enumNameToHash, } from '../utils'; import {Metadata, MetadataFactory, MetadataRepo, PoiFactory, PoiRepo, ProofOfIndex} from './entities'; import {StoreOperations} from './StoreOperations'; @@ -122,7 +123,7 @@ export class StoreService { for (const e of this.modelsRelations.enums) { // We shouldn't set the typename to e.name because it could potentially create SQL injection, // using a replacement at the type name location doesn't work. - const enumTypeName = `${schema}_enum_${this.enumNameToHash(e.name)}`; + const enumTypeName = `${schema}_enum_${enumNameToHash(e.name)}`; const [results] = await this.sequelize.query( `select e.enumlabel as enum_value @@ -391,10 +392,6 @@ export class StoreService { }); } - enumNameToHash(enumName: string): string { - return blake2AsHex(enumName).substr(2, 10); - } - setTransaction(tx: Transaction): void { this.tx = tx; tx.afterCommit(() => (this.tx = undefined)); diff --git a/packages/node-core/src/utils/sync-helper.ts b/packages/node-core/src/utils/sync-helper.ts index eb3b52ce4c..2e6a5e6247 100644 --- a/packages/node-core/src/utils/sync-helper.ts +++ b/packages/node-core/src/utils/sync-helper.ts @@ -174,3 +174,7 @@ export function createSchemaTriggerFunction(schema: string): string { END; $$ LANGUAGE plpgsql;`; } + +export function enumNameToHash(enumName: string): string { + return blake2AsHex(enumName).substr(2, 10); +} diff --git a/packages/node/src/subcommands/forceClean.service.ts b/packages/node/src/subcommands/forceClean.service.ts index bf66a20dc2..eaaacac28d 100644 --- a/packages/node/src/subcommands/forceClean.service.ts +++ b/packages/node/src/subcommands/forceClean.service.ts @@ -7,8 +7,11 @@ import { getLogger, NodeConfig, getExistingProjectSchema, + enumNameToHash, } from '@subql/node-core'; +import { getAllEntitiesRelations } from '@subql/utils'; import { QueryTypes, Sequelize } from 'sequelize'; +import { SubqueryProject } from '../configure/SubqueryProject'; const logger = getLogger('Force-clean'); @@ -17,6 +20,7 @@ export class ForceCleanService { constructor( private readonly sequelize: Sequelize, private readonly nodeConfig: NodeConfig, + private readonly project: SubqueryProject, ) {} async forceClean(): Promise { @@ -28,6 +32,7 @@ export class ForceCleanService { logger.error('Unable to locate schema'); throw new Error('Schema does not exist.'); } + const modelsRelation = getAllEntitiesRelations(this.project.schema); try { // drop existing project schema and metadata table @@ -36,6 +41,16 @@ export class ForceCleanService { benchmark: false, }); + // drop all related enums + await Promise.all( + modelsRelation.enums.map(async (e) => { + const enumTypeName = `${schema}_enum_${enumNameToHash(e.name)}`; + await this.sequelize.query(` + DROP TYPE "${enumTypeName}"; + `); + }), + ); + // remove schema from subquery table (might not exist) await this.sequelize.query( ` DELETE