Repository navigation
Schema change: create_enum and add_enum_value - #10
Merged
Merged
Conversation
create_enum makes an enum type in a step that locks nothing; a column can use it later in the same plan, and its default has to be one of its labels (PostgreSQL cannot be asked about a type that does not exist yet). add_enum_value adds a label at the end or before or after another, with ADD VALUE IF NOT EXISTS, in a step of its own so later steps can use the label; it says when the label is already there and refuses an unknown type, a type that is not an enum, an unknown neighbour or both positions. A new probe reads whether a type exists, whether it is an enum and its labels in order. Type names are compared without the schema when one side has none, because PostgreSQL leaves the schema out of a type on the search path.
One creates an enum, adds a column of that type with a default and then a label after another, and checks the labels in order, the default on existing rows and that the new label can be used; one adds a label before another to an enum that exists. The probe is checked against an enum, a type that is not there and a table's row type.
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.
What changed
Two plan operations for enum types.
create_enum(type name, labels):CREATE TYPE … AS ENUM (…)in one step that locks nothing; the undo isDROP TYPE. A column can use the new type later in the same plan, and its default has to be one of the labels (PostgreSQL cannot be asked about a type that does not exist yet, so the compiler checks it itself). Refused: a name taken by a type or by a table (the two share a namespace), repeated labels, a label over 63 bytes, a schema that does not exist.add_enum_value(type name, label, optionallybeforeorafteran existing label):ALTER TYPE … ADD VALUE IF NOT EXISTS …in a step of its own, so later steps can use the label. Says so when the label is already there (including one an earlier step of the same plan added); refuses an unknown type, a type that is not an enum, an unknown neighbour, and both positions. The notes say a label can never be removed again and that no table that uses the type is locked or rewritten.public.moodandmood), because PostgreSQL leaves the schema out of a type on the search path; without that the comparison after a run would report a difference that is not one.create_enum.How it was checked
npm run check:openai).scripts/schema-change-pg.ts: create an enum, add a column of that type with a default, then add a label after another (labels in order, existing rows read the default, the new label can be used); and add a label before another to an enum that exists. The probe is checked against an enum, a type that is not there and a table's row type. Lock levels and the predicted end state are checked like every scenario.npm run test:schema-pg:versionspasses on PostgreSQL 12, 13, 14, 15, 16, 17 and 18.Reviewer notes