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

Postgres Native Enum with * schema #5456

Closed
5 tasks done
PrestonGiorgianni opened this issue Apr 15, 2024 · 2 comments
Closed
5 tasks done

Postgres Native Enum with * schema #5456

PrestonGiorgianni opened this issue Apr 15, 2024 · 2 comments

Comments

@PrestonGiorgianni
Copy link

Describe the bug

While trying to use the schema: * option to allow multiple postgres schemas to be created with the same classes, I have noticed that the update command does not generate the proper Enum types. There are all generated as *.${nativeEnumName} instead of ${options.schema}.${nativeEnumName}.

I have tracked the issue down to this code block, where meta.schema is * and thus that is what is used in generating the sql. I am unfamiliar with the project and how best this could be fixed.

output

create type "*"."axis_move_axis_type_enum" as enum ();

desired output

create type "test"."axis_move_axis_type_enum" as enum ();

if (prop.nativeEnumName) {
let key = prop.nativeEnumName;
const s = meta.schema ?? schema.name;
if (s && s !== platform.getDefaultSchemaName()) {
key = s + '.' + key;
}
nativeEnums[key] = {
name: prop.nativeEnumName,
schema: meta.schema ?? schema.name,
items: prop.items?.map(val => '' + val) ?? [],
};
}

additional context

the create sql function also has issues with the * schema. It generates this sql, where the enum has the schema in it twice, but the create table references the enum differently.

create type "test"."test.axis_move_axis_type_enum" as enum ();
create table "test"."axis_move" ("id" serial primary key, "axis_type" axis_move_axis_type_enum not null);

Reproduction

//Type def
import { Entity, Enum, PrimaryKey, Property } from "@mikro-orm/core";

export enum AxisType {
...
}

@Entity({tableName: "axis_move", schema: "*"})
export class AxisMove {
  @Enum({
    type: 'AxisType',
    name: "axis_type",
    nativeEnumName: "axis_move_axis_type_enum",
  })
  axisType: "linear" | "rotary";
...
}

// schema generator
import { MikroORM } from '@mikro-orm/core';
import config from './config.js';

const orm = await MikroORM.init(config);
const schemaGenerator = orm.getSchemaGenerator();

console.log(await schemaGenerator.getUpdateSchemaSQL({
  schema: 'test',
}));
orm.close();

What driver are you using?

@mikro-orm/postgresql

MikroORM version

6.2.1

Node.js version

v20.11.1

Operating system

No response

Validations

@B4nan
Copy link
Member

B4nan commented Apr 18, 2024

I was looking into this yesterday, and the challenging part is diffing combined with support for enums in a specific schema (for entity in wildcard schema). Will need to give it a bit more time, worst case I'll implement at least the support for wildcard enums in general, as that should be rather easy (and I got that mostly already implemented locally).

@B4nan B4nan closed this as completed in e183de3 Apr 18, 2024
@B4nan
Copy link
Member

B4nan commented Apr 18, 2024

not perfect, but it should cover your use case

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants