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

remove enums on force-clean #1427

Merged
merged 1 commit into from
Nov 22, 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
7 changes: 2 additions & 5 deletions packages/node-core/src/indexer/store.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import {
makeTriggerName,
createSchemaTriggerFunction,
createSchemaTrigger,
enumNameToHash,
} from '../utils';
import {Metadata, MetadataFactory, MetadataRepo, PoiFactory, PoiRepo, ProofOfIndex} from './entities';
import {StoreOperations} from './StoreOperations';
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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));
Expand Down
4 changes: 4 additions & 0 deletions packages/node-core/src/utils/sync-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
15 changes: 15 additions & 0 deletions packages/node/src/subcommands/forceClean.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -17,6 +20,7 @@ export class ForceCleanService {
constructor(
private readonly sequelize: Sequelize,
private readonly nodeConfig: NodeConfig,
private readonly project: SubqueryProject,
) {}

async forceClean(): Promise<void> {
Expand All @@ -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
Expand All @@ -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
Expand Down