Skip to content

Commit

Permalink
improve force clean with deprecated enum (#1541)
Browse files Browse the repository at this point in the history
  • Loading branch information
jiqiang90 authored Mar 5, 2023
1 parent a24b5db commit 921e3fd
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
12 changes: 2 additions & 10 deletions packages/node-core/src/indexer/store.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import {
modelsTypeToModelAttributes,
SmartTags,
smartTags,
getEnumDeprecated,
} from '../utils';
import {Metadata, MetadataFactory, MetadataRepo, PoiFactory, PoiRepo, ProofOfIndex} from './entities';
import {StoreOperations} from './StoreOperations';
Expand Down Expand Up @@ -178,17 +179,8 @@ export class StoreService {
{replacements: [enumTypeName, schema]}
);

// If enum has created before and not under schema, still following the original logic
// This logic should be deprecated for new project
const enumTypeNameDeprecated = `${schema}_enum_${enumNameToHash(e.name)}`;
const [resultsDeprecated] = await this.sequelize.query(
`select e.enumlabel as enum_value
from pg_type t
join pg_enum e on t.oid = e.enumtypid
where t.typname = ?
order by enumsortorder;`,
{replacements: [enumTypeNameDeprecated]}
);
const resultsDeprecated = await getEnumDeprecated(this.sequelize, enumTypeNameDeprecated);
if (resultsDeprecated.length !== 0) {
results = resultsDeprecated;
type = `"${enumTypeNameDeprecated}"`;
Expand Down
12 changes: 12 additions & 0 deletions packages/node-core/src/utils/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,15 @@ export function transformBypassBlocks(bypassBlocks: (number | string)[]): number
export function cleanedBatchBlocks(bypassBlocks: number[], currentBlockBatch: number[]): number[] {
return without(currentBlockBatch, ...transformBypassBlocks(bypassBlocks));
}

export async function getEnumDeprecated(sequelize: Sequelize, enumTypeNameDeprecated: string): Promise<unknown[]> {
const [resultsDeprecated] = await sequelize.query(
`select e.enumlabel as enum_value
from pg_type t
join pg_enum e on t.oid = e.enumtypid
where t.typname = ?
order by enumsortorder;`,
{replacements: [enumTypeNameDeprecated]}
);
return resultsDeprecated;
}
15 changes: 12 additions & 3 deletions packages/node/src/subcommands/forceClean.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
NodeConfig,
getExistingProjectSchema,
enumNameToHash,
getEnumDeprecated,
} from '@subql/node-core';
import { getAllEntitiesRelations } from '@subql/utils';
import { QueryTypes, Sequelize } from 'sequelize';
Expand Down Expand Up @@ -45,10 +46,18 @@ export class ForceCleanService {
// Deprecate, now enums are moved under schema, drop schema will remove project enums
await Promise.all(
modelsRelation.enums.map(async (e) => {
const enumTypeName = `${schema}_enum_${enumNameToHash(e.name)}`;
await this.sequelize.query(`
DROP TYPE "${enumTypeName}";
const enumTypeNameDeprecated = `${schema}_enum_${enumNameToHash(
e.name,
)}`;
const resultsDeprecated = await getEnumDeprecated(
this.sequelize,
enumTypeNameDeprecated,
);
if (resultsDeprecated.length !== 0) {
await this.sequelize.query(`
DROP TYPE "${enumTypeNameDeprecated}";
`);
}
}),
);

Expand Down

0 comments on commit 921e3fd

Please sign in to comment.