8000 Support multiple 'alter column' sub operations by andrew-farries · Pull Request #338 · xataio/pgroll · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Support multiple 'alter column' sub operations #338

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

Merged
merged 7 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,8 @@ Example **add column** migrations:

An alter column operation alters the properties of a column. The operation supports several sub-operations, described below.

An alter column operation may contain multiple sub-operations. For example, a single alter column operation may rename a column, change its type, and add a check constraint.

#### Rename column

A rename column operation renames a column.
Expand Down
22 changes: 22 additions & 0 deletions examples/34_create_events_table.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "34_create_events_table",
"operations": [
{
"create_table": {
"name": "events",
"columns": [
{
"name": "id",
"type": "serial",
"pk": true
},
{
"name": "name",
"type": "varchar(255)",
"nullable": true
}
]
}
}
]
}
23 changes: 23 additions & 0 deletions examples/35_alter_column_multiple.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "35_alter_column_multiple",
"operations": [
{
"alter_column": {
"table": "events",
"column": "name",
"name": "event_name",
"type": "text",
"nullable": false,
"unique": {
"name": "events_event_name_unique"
},
"check": {
"name": "event_name_length",
"constraint": "length(name) > 3"
},
"up": "(SELECT CASE WHEN name IS NULL OR LENGTH(name) <= 3 THEN 'placeholder' ELSE name END)",
"down": "name"
}
}
]
}
2 changes: 1 addition & 1 deletion pkg/jsonschema/testdata/alter-column-3.txtar
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
This is an invalid 'alter_column' migration.
It sets `name` but also sets `up` and `down`.
It sets only `name` but also sets `up` and `down`.

-- alter_column.json --
{
Expand Down
2 changes: 1 addition & 1 deletion pkg/jsonschema/testdata/alter-column-4.txtar
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
This is an invalid 'alter_column' migration.
It sets `name` but also sets `up`.
It sets only `name` but also sets `up`.

-- alter_column.json --
{
Expand Down
2 changes: 1 addition & 1 deletion pkg/jsonschema/testdata/alter-column-5.txtar
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
This is an invalid 'alter_column' migration.
It sets `name` but also sets `down`.
It sets only `name` but also sets `down`.

-- alter_column.json --
{
Expand Down
8000 4 changes: 2 additions & 2 deletions pkg/jsonschema/testdata/alter-column-6.txtar
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
This is an invalid 'alter_column' migration.
This is a valid 'alter_column' migration.
It sets both `name` and `nullable`.

-- alter_column.json --
Expand All @@ -19,4 +19,4 @@ It sets both `name` and `nullable`.
}

-- valid --
false
true
4 changes: 2 additions & 2 deletions pkg/jsonschema/testdata/alter-column-7.txtar
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
This is an invalid 'alter_column' migration.
It sets both `name` and `nullable`.
This is a invalid 'alter_column' migration.
It sets both `name` and `nullable` but does not set `up` or `down`.

-- alter_column.json --
{
Expand Down
4 changes: 2 additions & 2 deletions pkg/jsonschema/testdata/alter-column-8.txtar
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
This is an invalid 'alter_column' migration.
This is a valid 'alter_column' migration.
It sets both `type` and `nullable`.

-- alter_column.json --
Expand All @@ -19,4 +19,4 @@ It sets both `type` and `nullable`.
}

-- valid --
false
true
18 changes: 18 additions & 0 deletions pkg/jsonschema/testdata/alter-column-9.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
This is an invalid 'alter_column' migration.
It specifies a table and column but no changes.

-- alter_column.json --
{
"name": "migration_name",
"operations": [
{
"alter_column": {
"table": "reviews",
"column": "review"
}
}
]
}

-- valid --
false
17 changes: 9 additions & 8 deletions pkg/migrations/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,6 @@ func (e NoDownSQLAllowedError) Error() string {
return "down SQL is not allowed for this operation"
}

type MultipleAlterColumnChangesError struct {
Changes int
}

func (e MultipleAlterColumnChangesError) Error() string {
return fmt.Sprintf("alter column operations require exactly one change, found %d", e.Changes)
}

type BackfillNotPossibleError struct {
Table string
}
Expand Down Expand Up @@ -199,3 +191,12 @@ func (e InvalidOnDeleteSettingError) Error() string {
e.Setting,
)
}

type AlterColumnNoChangesError struct {
Table string
Column string
}

func (e AlterColumnNoChangesError) Error() string {
return fmt.Sprintf("alter column %q on table %q requires at least one change", e.Column, e.Table)
}
Loading
0