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

autoschema changes: Create enum db type #113

Merged
merged 7 commits into from
Aug 23, 2020
Merged

Conversation

lolopinto
Copy link
Owner

@lolopinto lolopinto commented Aug 22, 2020

These are changes to autoschema to make postgres enum type work

postgres has support for built in enum type which may be preferred to using strings. This provides database validation/enforcement that only valid values can be input as opposed to leaving it to the ent-or data layer.

2 major downsides of postgres enum types:

  • can't remove values without changing types and going through a whole slew of complicated steps.
  • adding a new value can't be done in a transaction with other changes.

Luckily, alembic added support for autocommit_block which makes it possible to have a non-transactional block within a migration script.

Because of the following recommendation while using autocommit_block, transaction_per_migration flag is now enabled for all migrations:

It is recommended that when an application includes migrations with
"autocommit" blocks, that
:paramref:.EnvironmentContext.transaction_per_migration be used
so that the calling environment is tuned to expect short per-file
migrations whether or not one of them has an autocommit block.

A future option can be provided to disable this. Or the code can be smarter to only use this when we detect an enum type. For now, the default option for all types has been changed to support this.

This includes tests that validates the following:

  • adding an enum type works
  • adding new values after the fact (in different positions) work
  • trying to remove a value throws an exception

Test Plan: python -m pytest -sv ( I have pytest issues so only that way works)

@lolopinto lolopinto changed the title Create enum db type autoschema changes: Create enum db type Aug 22, 2020
Base automatically changed from create-enum-type to master August 22, 2020 23:49
@lolopinto
Copy link
Owner Author

Useful info:

Links:

Commands:

  • \dT+ rainbow_type to see the enum
  • select unnest(enum_range(enum_first(null::rainbow_type))); to list values in order

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

Successfully merging this pull request may close these issues.

1 participant