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

Add support for PostgreSQL ENUMs (and portable named types) #171

Merged
merged 3 commits into from
Mar 4, 2021
Merged

Conversation

emk
Copy link
Contributor

@emk emk commented Mar 1, 2021

This is a fairly major change, and it breaks the output format for
dbcrossbar-schema (so this is a semver bump). Highlists of this
change include:

  • Parsing and printing CREATE TYPE name AS ENUM(...) statements.
  • Creating ENUMs in the database if they don't exist. Wrapping the existing dbcrossbar-schema output format in { "named_types": [], "tables": [old_table_format] }.

Internally, this requires:

  • Passing portable Schema around instead of Table in most places,
    so that we have access to named_types. - Adding DataType::Named
    to refer to a named type. - Adding DataType::OneOf to refer to a
    string enumeration. - Lots of changes to the postgres-sql and
    postgres drivers.

We designed DataType::Named and DataType::OneOf the way we did in
order to later support the following planned features:

  • dbcrossbar-ts should be able to convert type and interface
    declarations into named types. - We want to support PostgreSQL
    native structure types, as an alternative to encoding structures as
    jsonb options.

Note that one_of and named support is fairly limited. Some drivers
will always recursively resolve all named types (like BigQuery), but
PostgreSQL only accepts one_of and named if they correspond to a
CREATE TYPE name AS ENUM(...) statement. These restrictions will be
relaxed over time as we figure out how to handle these cases
correctly, but we want to start by allowing only those cases we know
how to handle cleanly.

Other interesting PostgreSQL changes include:

  • TableName is now PgName, because it's used by types and tables.

Many thanks to Tom Caruso for pairing on a tricky set of tests and
test fixes, and to Forrest Wallace and Seamus Abshere for providing
feedback on the design.

This is a fairly major change, and it breaks the output format for
`dbcrossbar-schema` (so this is a semver bump). Highlists of this
change include:

- Parsing and printing `CREATE TYPE name AS ENUM(...)` statements.
- Creating `ENUM`s in the database if they don't exist.  Wrapping the
- existing `dbcrossbar-schema` output format in `{ "named_types": [],
- "tables": [old_table_format] }`.

Internally, this requires:

- Passing portable `Schema` around instead of `Table` in most places,
  so that we have access to `named_types`.  - Adding `DataType::Named`
  to refer to a named type.  - Adding `DataType::OneOf` to refer to a
  string enumeration.  - Lots of changes to the `postgres-sql` and
  `postgres` drivers.

We designed `DataType::Named` and `DataType::OneOf` the way we did in
order to later support the following planned features:

- `dbcrossbar-ts` should be able to convert `type` and `interface`
  declarations into named types.  - We want to support PostgreSQL
  native structure types, as an alternative to encoding structures as
  `jsonb` options.

Note that `one_of` and `named` support is fairly limited. Some drivers
will always recursively resolve all named types (like BigQuery), but
PostgreSQL only accepts `one_of` and `named` if they correspond to a
`CREATE TYPE name AS ENUM(...)` statement. These restrictions will be
relaxed over time as we figure out how to handle these cases
correctly, but we want to start by allowing only those cases we know
how to handle cleanly.

Other interesting PostgreSQL changes include:

- `TableName` is now `PgName`, because it's used by types and tables.

Many thanks to Tom Caruso for pairing on a tricky set of tests and
test fixes, and to Forrest Wallace and Seamus Abshere for providing
feedback on the design.
@emk emk changed the title named enums Add support for PostgreSQL ENUMs (and portable named types) Mar 3, 2021
@emk emk marked this pull request as ready for review March 3, 2021 15:04
emk added 2 commits March 3, 2021 10:22
This applies changes suggested for the following advisories:

RUSTSEC-2020-0146
RUSTSEC-2021-0020
RUSTSEC-2021-0023
@emk emk merged commit 2cd2f28 into master Mar 4, 2021
@emk emk deleted the named_enums branch March 4, 2021 15:44
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.

None yet

2 participants