Add support for PostgreSQL-style CREATE TYPE#281
Open
Niols wants to merge 2 commits into
Open
Conversation
PostgreSQL (and standard SQL) uses `ALTER COLUMN` instead of MySQL's `CHANGE COLUMN` / `MODIFY COLUMN` for modifying existing columns. This adds parsing and schema evaluation for the five main sub-commands: - `ALTER COLUMN col TYPE newtype` - `ALTER COLUMN col SET NOT NULL` - `ALTER COLUMN col DROP NOT NULL` - `ALTER COLUMN col SET DEFAULT expr` - `ALTER COLUMN col DROP DEFAULT` These can be combined with other actions in a single `ALTER TABLE` statement, eg.: ``` ALTER TABLE t DROP COLUMN old_col, ALTER COLUMN col TYPE SMALLINT, ALTER COLUMN col SET NOT NULL; ``` Migration generation (inverse actions + SQL fragments) is also supported for all five variants.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR builds on top of #276. Although they are in practice independent changes, they both aim at improving PostgreSQL support in sqlgg, and I have added the new commit on top of the previous one. When reviewing, I recommend making sure to only read the last commit, or, even better, to wait before #276 is merged before reviewing anything here.
I hope the change is welcome; please let me know if there is anything that you would want me to do differently, I will be happy to update my PR.
Target
PostgreSQL allows declaring a named type with
CREATE TYPE, then referring to it elsewhere. This is most useful for enums:and in fact necessary, as inline
ENUMis just not accepted. Unlike MySQL's inlineENUM(...)columns, this is a separate statement that introduces a name to be looked up later. It cannot be handled at the leaf of the type grammar alone: it requires a registry that one statement populates and a later statement consults.Change
The main change is the new module
lib/types.ml, similar tolib/tables.ml(albeit much simpler) but for types. Encountering a newCREATE TYPEadds to a global registry found in that module; the counterpartDROP TYPEremoves from it, and we look types up during analysis ofCREATE TABLE. The rest of the change is what one expects, namely adding those syntaxes to the grammar and following the new types everywhere. I add some Cram-style tests for the feature intest/cram/create_type.t.Limitations
CreateType/DropTypecarry no source position, so the dialect-mismatch error reads... atwith an empty tail.DROP TYPE IF EXISTSparses, but the flag is discarded, so it still errors on missing names. This matches the pre-existing behaviour ofDROP TABLE IF EXISTS.DROP TYPE fooiffoois used in a table somewhere; I did not add such checks in this PR.AS ENUM (...)is supported; the other PostgreSQL forms (composite, range, base, shell) are not.CREATE DOMAINstatement, which allows aliasing existing types with some extra constraints is not supported.