Skip to content

Commit

Permalink
Move enum under schema (#1527)
Browse files Browse the repository at this point in the history
* need fix query get enum

* fix enum query, clean all enums

* add logic to handle original enum type
  • Loading branch information
jiqiang90 authored Mar 2, 2023
1 parent 7dd15d1 commit 82b028c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
26 changes: 20 additions & 6 deletions packages/node-core/src/indexer/store.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,19 +169,33 @@ 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_${enumNameToHash(e.name)}`;
const enumTypeName = enumNameToHash(e.name);
let type = `"${schema}".${enumTypeName}`;
let [results] = await this.sequelize.query(
`SELECT pg_enum.enumlabel as enum_value
FROM pg_type t JOIN pg_enum ON pg_enum.enumtypid = t.oid JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace
WHERE t.typname = ? AND n.nspname = ? order by enumsortorder;`,
{replacements: [enumTypeName, schema]}
);

const [results] = await this.sequelize.query(
// 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: [enumTypeName]}
{replacements: [enumTypeNameDeprecated]}
);
if (resultsDeprecated.length !== 0) {
results = resultsDeprecated;
type = `"${enumTypeNameDeprecated}"`;
}

if (results.length === 0) {
await this.sequelize.query(`CREATE TYPE "${enumTypeName}" as ENUM (${e.values.map(() => '?').join(',')});`, {
await this.sequelize.query(`CREATE TYPE ${type} as ENUM (${e.values.map(() => '?').join(',')});`, {
replacements: e.values,
});
} else {
Expand Down Expand Up @@ -211,9 +225,9 @@ export class StoreService {
const comment = this.sequelize.escape(
`@enum\\n@enumName ${e.name}${e.description ? `\\n ${e.description}` : ''}`
);
await this.sequelize.query(`COMMENT ON TYPE "${enumTypeName}" IS E${comment}`);
await this.sequelize.query(`COMMENT ON TYPE ${type} IS E${comment}`);
}
enumTypeMap.set(e.name, `"${enumTypeName}"`);
enumTypeMap.set(e.name, type);
}
const extraQueries = [];
// Function need to create ahead of triggers
Expand Down
3 changes: 2 additions & 1 deletion packages/node/src/subcommands/forceClean.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ export class ForceCleanService {
benchmark: false,
});

// drop all related enums
// TODO, remove this soon, once original enum are cleaned
// 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)}`;
Expand Down

0 comments on commit 82b028c

Please sign in to comment.