Pogchamp-company/alembic-postgresql-enum

Duplicate Enumerations are being created

richarddahl opened this issue · 3 comments

This library is great! appreciate the effort. I just started using it in a a new project and it was working with no issues, however, I switched to using a custom schema in my PostgreSQL 16 db.

Now all of the Enumerations I have defined (they are in a separate file called enumerations.py) get a duplicate as.ENUM statement created in the alembic revision file, e.g.

sa.Enum('PUBLIC', 'PRIVATE', 'SECRET', 'SYSTEM', name='customschema.autofilter_columsecurity').create(op.get_bind())
ALL OTHER ENUMS LISTED FOR THE FIRST TIME ...
sa.Enum('PUBLIC', 'PRIVATE', 'SECRET', 'SYSTEM', name='customschema.autofilter_columsecurity').create(op.get_bind())
ALL OTHER ENUMS LISTED A SECOND TIME ...

I added the schema name to the beginning of each enumeration name in the sqlalchemy core Table definitions, (to no avail) e.g.:
Table(
"autofilter__field",
meta_data,
[OTHER COLUMS LISTED ...]
Column(
"column_security",
ENUM(
ColumnSecurity,
name=f"{settings.DB_SCHEMA}.autofilter_columsecurity",
create_type=False,
),
default=ColumnSecurity.PUBLIC,
nullable=False,
),
schema=settings.DB_SCHEMA,
)

And here is the definition of ColumnSecurity in enumerations.py:

class ColumnSecurity(str, enum.Enum):
"""
Enumeration class representing the security levels for Field columns.

Attributes:
    PUBLIC (str): The column is publicly accessible (in accordance with row-level security enforcement).
    PRIVATE (str): The column is accessible only to the owner or the customer admin (if used) and superusers.
    SECRET (str): The column is accessible only to superusers.
    SYSTEM (str): The column is accessible only to the system.

"""

PUBLIC = "public"
PRIVATE = "private"
SECRET = "secret"
SYSTEM = "system"

As a workaround I simply create the revision and then delete the second statements, but would obviously rather it work as it did before. Not sure if I did something incorrect when making the changes to accommodate a custom schema, or if this is something that is just not supported, but any assistance is appreciated. thanks.

There is schema field on ENUM type for your situation.

Table(
    "autofilter__field",
    meta_data,
    [OTHER COLUMS LISTED ...]
    Column(
        "column_security",
        ENUM(
            ColumnSecurity,
           name="autofilter_columsecurity",
           create_type=False,
           schema=settings.DB_SCHEMA
        ),
       default=ColumnSecurity.PUBLIC,
       nullable=False,
    ),
    schema=settings.DB_SCHEMA,
)

This should fix your issue. If this is not the case, then write another comment with feedback.

Thanks, apparently I needed your eyes to read the docs, lol!