fix: improve DDL parsing for CHECK/CONSTRAINT/FOREIGN KEY #207
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.
What did this pull request do?
Currently, the parsing of the DDL ignores some columns (if they start with
check
orconstraint
) and add ones that aren't really column (if you have a constraint withoutCONSTRAINT
in front, which is valid). This PR focuses on supporting such cases.FOREIGN KEY
Example:
CREATE TABLE Docs (ID int NOT NULL,UserID int NOT NULL,FOREIGN KEY (UserID) REFERENCES Users(ID))
Explanation: As you don't need to have
CONSTRAINT
to specify a constraint, it is possible to have a DB which has been created without them and the current parser will consider that a column. This will of course fail when trying to recreate the table as theINSERT INTO () SELECT
generated is invalid.Solution: Because it contains a space, it can easily be detected, just like
PRIMARY KEY
.CHECK
Example:
CREATE TABLE Docs (ID int NOT NULL,Checksum text NOT NULL)
Explanation: The current parser will ignore any column which starts with
CHECK
, this works when it is actually a check (e.g.CHECK (Age > 25)
) but fails if you just have a column that happens to be named something likechecksum
(which is valid).Solution: instead of just checking for a prefix, we use a regexp to detect if something looks like
CHECK (
(including any number or whitespace betweenCHECK
and(
).CONSTRAINT
Same deal as
CHECK
, the only difference is the final regexp which is a bit more complex, checking thatCONSTRAINT
is followed by a name.