schemainspect icon indicating copy to clipboard operation
schemainspect copied to clipboard

Exception inspecting a table or composite type referencing an enum from an extension

Open groner opened this issue 4 years ago • 6 comments

This looks similar to #59, but with enums. The error occurs a little earlier, in load_all_relations.

To reproduce this problem you need an extension containing composite type containing an enum. I couldn't find anything like that in the standard postgresql distribution, so I put together a dummy extension. This short script will create the extension, or you can grab the archive linked below and unpack it in your postgresql share dir.

pgsharedir=$(pg_config --sharedir)

cat >$pgsharedir/extension/dummy.control <<'EOF'
# dummy extension
comment = 'dummy test extension'
default_version = '0.0.1'
relocatable = true
EOF

cat >$pgsharedir/extension/dummy--0.0.1.sql <<'EOF'
SET client_min_messages = warning;
CREATE TYPE color AS ENUM (
    'blue',
    'teal',
    'aqua',
    'grey',
    'eggshell');
CREATE TYPE colorpair AS (
    a color,
    b color);
EOF

dummy-extension.tar.gz

Once the extension is installed, the following schema should reproduce the problem.

create extension dummy;
create table foo (id serial primary key, xx colorpair);
Traceback (most recent call last):
  File ".../bin/migra", line 3, in <module>
    import re
  File ".../lib/python3.6/site-packages/migra/command.py", line 108, in do_command
    status = run(args)
  File ".../lib/python3.6/site-packages/migra/command.py", line 79, in run
    m = Migration(ac0, ac1, schema=schema, exclude_schema=exclude_schema)
  File ".../lib/python3.6/site-packages/migra/migra.py", line 26, in __init__
    x_from, schema=schema, exclude_schema=exclude_schema
  File ".../lib/python3.6/site-packages/schemainspect/get.py", line 20, in get_inspector
    inspected = ic(c)
  File ".../lib/python3.6/site-packages/schemainspect/pg/obj.py", line 1060, in __init__
    super(PostgreSQL, self).__init__(c, include_internal)
  File ".../lib/python3.6/site-packages/schemainspect/inspector.py", line 25, in __init__
    self.load_all()
  File ".../lib/python3.6/site-packages/schemainspect/pg/obj.py", line 1064, in load_all
    self.load_all_relations()
  File ".../lib/python3.6/site-packages/schemainspect/pg/obj.py", line 1319, in load_all_relations
    for c in clist
  File ".../lib/python3.6/site-packages/schemainspect/pg/obj.py", line 1320, in <listcomp>
    if c.position_number
  File ".../lib/python3.6/site-packages/schemainspect/pg/obj.py", line 1302, in get_enum
    return self.enums[quoted_full_name]
KeyError: '"sterling"."rrule_freq"' 

groner avatar Jan 08 '21 19:01 groner

It just occurred to me that this might be an unusual corner case because the enum is in a composite type.

I'll try to put some steps to reproduce the problem together.

groner avatar Jan 08 '21 19:01 groner

This also occurs without the composite type, if the extension enum is used in a table.

groner avatar Jan 11 '21 14:01 groner

The following approach seems to work to get past this:

  • Add an is_extension flag to InspectedEnum
  • Modify the query in pg/sql/enums.sql to return an is_extension column instead of filtering out extensions
  • In migra, customize Changes.enums to filter out enums that have is_extension on

If this approach seems reasonable, I can put together some PRs.

groner avatar Jan 11 '21 17:01 groner

Hey, thanks heaps for filing this issue and investigating! PRs would be very welcome. If you have any questions just let me know.

djrobstep avatar Jan 12 '21 00:01 djrobstep

@groner Is there any progress on this or anything I can do to help this along? This same issue is blocking me from using migra in my project!

jaredramirez avatar Apr 14 '22 17:04 jaredramirez

@jaredramirez I haven't looked at this in over a year, but I think #63 addresses this. Not sure how up to date it is now though.

groner avatar Apr 14 '22 18:04 groner