Skip to content

Transform JSON structure descriptions and examples to YAML #773

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 1 commit into from
Apr 4, 2025
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
6 changes: 3 additions & 3 deletions docs/cli/start.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ description: Start a pgroll migration
## Command

```
$ pgroll start sql/03_add_column.json
$ pgroll start sql/03_add_column.yaml
```

This starts the migration defined in the `sql/03_add_column.json` file.
This starts the migration defined in the `sql/03_add_column.yaml` file.

After starting a migration there will be two schema versions in the database; one for the old schema before the migration (e.g. `public_02_create_table`) and one for the new version with the schema changes (e.g. `public_03_add_column`). Each of these schemas merely contains views on the tables in the `public` schema.

Expand All @@ -18,7 +18,7 @@ After starting a migration there will be two schema versions in the database; on
A migration can be started and completed with one command by specifying the `--complete` flag:

```
$ pgroll start sql/03_add_column.json --complete
$ pgroll start sql/03_add_column.yaml --complete
```

This is equivalent to running `pgroll start` immediately followed by `pgroll complete`.
Expand Down
12 changes: 11 additions & 1 deletion docs/operations/README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
# Operations reference

`pgroll` migrations are specified as JSON files. All migrations follow the same basic structure:
`pgroll` migrations are specified as YAML or JSON files. All migrations follow the same basic structure:

YAML migration:

```yaml
name: "0x_migration_name"
operations: [...]
```

JSON migration:

```json
{
"name": "0x_migration_name",
"operations": [...]
}
```

46 changes: 20 additions & 26 deletions docs/operations/add_column.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,26 @@ description: An add column operation creates a new column on an existing table.

## Structure

```json
{
"add_column": {
"table": "name of table to which the column should be added",
"up": "SQL expression",
"column": {
"name": "name of column",
"type": "postgres type",
"comment": "postgres comment for the column",
"nullable": true|false,
"unique": true|false,
"pk": true|false,
"default": "default value for the column",
"check": {
"name": "name of check constraint",
"constraint": "constraint expression"
},
"references": {
"name": "name of foreign key constraint",
"table": "name of referenced table",
"column": "name of referenced column",
"on_delete": "ON DELETE behaviour, can be CASCADE, SET NULL, RESTRICT, or NO ACTION. Default is NO ACTION",
}
}
}
}
```yaml
add_column:
table: name of table to which the column should be added
up: SQL expression
column:
name: name of column
type: postgres type
comment: postgres comment for the column
nullable: true|false
unique: true|false
pk: true|false
default: default value for the column
check:
name: name of check constraint
constraint: constraint expression
references:
name: name of foreign key constraint
table: name of referenced table
column: name of referenced column
on_delete: ON DELETE behaviour, can be CASCADE, SET NULL, RESTRICT, or NO ACTION. Default is NO ACTION
```

The `up` SQL is used when a row is added to the old version of the table (ie, without the new column). In the new version of the table, the row's value for the new column is determined by the `up` SQL expression. The `up` SQL can be omitted if the new column is nullable or has a `DEFAULT` defined.
Expand Down
22 changes: 9 additions & 13 deletions docs/operations/alter_column/add_check_constraint.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,15 @@ description: An add check constraint operation adds a `CHECK` constraint to a co

## Structure

```json
{
"alter_column": {
"table": "table name",
"column": "column name",
"check": {
"name": "check constraint name",
"constraint": "constraint expression"
},
"up": "SQL expression",
"down": "SQL expression"
}
}
```yaml
alter_column:
table: table name
column: column name
check:
name: check constraint name
constraint: constraint expression
up: SQL expression
down: SQL expression
```

The `up` SQL expression is used to migrate values from the column in the old schema version that aren't subject to the constraint to values in the new schema version that are subject to the constraint.
Expand Down
26 changes: 11 additions & 15 deletions docs/operations/alter_column/add_foreign_key.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,17 @@ description: Add a foreign key constraint to a column.

## Structure

```json
{
"alter_column": {
"table": "table name",
"column": "column name",
"references": {
"name": "name of foreign key reference",
"table": "name of referenced table",
"column": "name of referenced column",
"on_delete": "ON DELETE behaviour, can be CASCADE, SET NULL, RESTRICT, or NO ACTION. Default is NO ACTION"
},
"up": "SQL expression",
"down": "SQL expression"
}
}
```yaml
alter_column:
table: table name
column: column name
references:
name: name of foreign key reference
table: name of referenced table
column: name of referenced column
on_delete: ON DELETE behaviour, can be CASCADE, SET NULL, RESTRICT, or NO ACTION. Default is NO ACTION
up: SQL expression
down: SQL expression
```

## Examples
Expand Down
17 changes: 7 additions & 10 deletions docs/operations/alter_column/add_not_null_constraint.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@ description: Add not null operations add a `NOT NULL` constraint to a column.

## Structure

```json
{
"alter_column": {
"table": "table name",
"column": "column name",
"nullable": false,
"up": "SQL expression",
"down": "SQL expression"
}
}
```yaml
alter_column:
table: table name
column: column name
nullable: false
up: SQL expression
down: SQL expression
```

Use `up` to migrate values from the nullable column in the old schema view to the `NOT NULL` column in the new schema version. `down` is used to migrate values in the other direction.
Expand Down
20 changes: 8 additions & 12 deletions docs/operations/alter_column/add_unique_constraint.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,14 @@ description: Add unique operations add a `UNIQUE` constraint to a column.

## Structure

```json
{
"alter_column": {
"table": "table name",
"column": "column name",
"unique": {
"name": "name of unique constraint"
},
"up": "SQL expression",
"down": "SQL expression"
}
}
```yaml
alter_column:
table: table name
column: column name
unique:
name: name of unique constraint
up: SQL expression
down: SQL expression
```

Use the `up` SQL expression to migrate values from the old non-unique column in the old schema to the `UNIQUE` column in the new schema.
Expand Down
17 changes: 7 additions & 10 deletions docs/operations/alter_column/change_comment.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@ description: A change comment operation changes the comment on a column.

## Structure

```json
{
"alter_column": {
"table": "table name",
"column": "column name",
"comment": "new comment for column" | null,
"up": "SQL expression",
"down": "SQL expression"
}
}
```yaml
alter_column:
table: table name
column: column name
comment: new comment for column | null
up: SQL expression
down: SQL expression
```

The comment is added directly to the column on migration start.
Expand Down
17 changes: 7 additions & 10 deletions docs/operations/alter_column/change_default.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@ description: A change default operation changes the default value of a column.

## Structure

```json
{
"alter_column": {
"table": "table name",
"column": "column name",
"default": "new default value" | null,
"up": "SQL expression",
"down": "SQL expression"
}
}
```yaml
alter_column:
table: table name
column: column name
default: new default value | null
up: SQL expression
down: SQL expression
```

The `default` expression is subject to the usual SQL quoting rules. In particular, string literals should be surrounded with `''`.
Expand Down
17 changes: 7 additions & 10 deletions docs/operations/alter_column/change_type.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@ description: A change type operation changes the type of a column.

## Structure

```json
{
"alter_column": {
"table": "table name",
"column": "column name",
"type": "new type of column",
"up": "SQL expression",
"down": "SQL expression"
}
}
```yaml
alter_column:
table: table name
column: column name
type: new type of column
up: SQL expression
down: SQL expression
```

Use the `up` SQL expression to do data conversion from the old column type to the new type. In the old schema version, the column will have its old data type; in the new version the column will have its new type.
Expand Down
17 changes: 7 additions & 10 deletions docs/operations/alter_column/drop_not_null_constraint.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@ description: Drop not null operations drop a `NOT NULL` constraint from a column

## Structure

```json
{
"alter_column": {
"table": "table name",
"column": "column name",
"nullable": true,
"up": "SQL expression",
"down": "SQL expression"
}
}
```yaml
alter_column:
table: table name
column: column name
nullable: true
up: SQL expression
down: SQL expression
```

## Examples
Expand Down
68 changes: 29 additions & 39 deletions docs/operations/create_constraint.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -9,49 +9,39 @@ description: A create constraint operation adds a new constraint to an existing

Required fields: `name`, `table`, `type`, `up`, `down`.

```json
{
"create_constraint": {
"table": "name of table",
"name": "my_unique_constraint",
"columns": ["column1", "column2"],
"type": "unique"| "check" | "foreign_key",
"check": "SQL expression for CHECK constraint",
"no_inherit": "true|false",
"references": {
"name": "name of foreign key reference",
"table": "name of referenced table",
"columns": "[names of referenced columns]",
"on_delete": "ON DELETE behaviour, can be CASCADE, SET NULL, RESTRICT, or NO ACTION. Default is NO ACTION",
"on_delete_set_columns": ["list of FKs to set", "in on delete operation on SET NULL or SET DEFAULT"],
"on_update": "ON UPDATE behaviour, can be CASCADE, SET NULL, RESTRICT, or NO ACTION. Default is NO ACTION",
"match_type": "match type, can be SIMPLE or FULL. Default is SIMPLE"
},
"up": {
"column1": "up SQL expressions for each column covered by the constraint",
...
},
"down": {
"column1": "up SQL expressions for each column covered by the constraint",
...
}
}
}
```yaml
create_constraint:
table: name of table
name: my_unique_constraint
columns: [column1, column2]
type: unique | check | foreign_key
check: SQL expression for CHECK constraint
no_inherit: true|false
references:
name: name of foreign key reference
table: name of referenced table
columns: [names of referenced columns]
on_delete: ON DELETE behaviour, can be CASCADE, SET NULL, RESTRICT, or NO ACTION. Default is NO ACTION
on_delete_set_columns: [list of FKs to set, in on delete operation on SET NULL or SET DEFAULT]
on_update: ON UPDATE behaviour, can be CASCADE, SET NULL, RESTRICT, or NO ACTION. Default is NO ACTION
match_type: match type, can be SIMPLE or FULL. Default is SIMPLE
up:
column1: up SQL expressions for each column covered by the constraint
...
down:
column1: up SQL expressions for each column covered by the constraint
...
```

An `up` and `down` SQL expression is required for each column covered by the constraint, and no other column names are permitted. For example, when adding a new constraint covering columns `a` and `b` the `up` and `down` fields should look like:

```json
{
"up": {
"a": "up SQL expression for column a",
"b": "up SQL expression for column b",
},
"down": {
"a": "down SQL expression for column a",
"b": "down SQL expression for column b",
}
}
```yaml
up:
a: up SQL expression for column a
b: up SQL expression for column b
down:
a: down SQL expression for column a
b: down SQL expression for column b
```

## Examples
Expand Down
Loading