Skip to content

Commit 95d8794

Browse files
authored
Add JSON docs back with tabs (#787)
Method: vscode find and replace regex: ![Screenshot 2025-04-10 at 16 01 57@2x](https://github.com/user-attachments/assets/aba4685e-a1e1-4e28-9663-6308a06dc53d) Copied and pasted from the red diffs: https://github.com/xataio/pgroll/pull/773/files AI'd updown.mdx Check the preview link for a demo
1 parent 97f632f commit 95d8794

20 files changed

+559
-13
lines changed

docs/guides/updown.mdx

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ With the following records:
4646
We are adding a new check constraint to the table to make sure all first names are capitalized. We inspected our table and know
4747
that the check is on the application side. Thus, all data coming from it are valid.
4848

49+
<YamlJsonTabs>
4950
```yaml
5051
operations:
5152
- create_constraint:
@@ -60,6 +61,30 @@ operations:
6061
down:
6162
name: name
6263
```
64+
```json
65+
{
66+
"operations": [
67+
{
68+
"create_constraint": {
69+
"name": "capitalized_name",
70+
"table": "employee",
71+
"type": "check",
72+
"columns": [
73+
"name"
74+
],
75+
"check": "name = INITCAP(name)",
76+
"up": {
77+
"name": "name"
78+
},
79+
"down": {
80+
"name": "name"
81+
}
82+
}
83+
}
84+
]
85+
}
86+
```
87+
</YamlJsonTabs>
6388

6489
Notice that both `up` and `down` migrations are set to `name` for `name` column. Thus, `pgroll` preserves the values in the column `name`.
6590

@@ -79,6 +104,7 @@ In this example we are changing the type of a column from float to int.
79104

80105
Each person's salary is stored in a float. We are converting the `salary` column from float to integer using `trunc`.
81106

107+
<YamlJsonTabs>
82108
```yaml
83109
operations:
84110
- alter_column:
@@ -88,6 +114,23 @@ operations:
88114
up: trunc(salary)
89115
down: salary
90116
```
117+
```json
118+
{
119+
"operations": [
120+
{
121+
"alter_column": {
122+
"table": "employee",
123+
"column": "salary",
124+
"type": "integer",
125+
"up": "trunc(salary)",
126+
"down": "salary"
127+
}
128+
}
129+
]
130+
}
131+
132+
```
133+
</YamlJsonTabs>
91134

92135
After the migration, the `salary` column contains only integers.
93136

@@ -103,6 +146,7 @@ After the migration, the `salary` column contains only integers.
103146

104147
We are adding a new column that cannot be null named `location`. We are setting it to `New York` using the `up` expression.
105148

149+
<YamlJsonTabs>
106150
```yaml
107151
operations:
108152
- add_column:
@@ -113,6 +157,24 @@ operations:
113157
type: text
114158
nullable: false
115159
```
160+
```json
161+
{
162+
"operations": [
163+
{
164+
"add_column": {
165+
"table": "employee",
166+
"up": "'New York'",
167+
"column": {
168+
"name": "location",
169+
"type": "text",
170+
"nullable": false
171+
}
172+
}
173+
}
174+
]
175+
}
176+
```
177+
</YamlJsonTabs>
116178

117179
The table is backfilled:
118180

@@ -128,6 +190,7 @@ The table is backfilled:
128190

129191
We are adding a not null constraint to the `email` column. If the `email` column is not set, we are concatenating the column `nick` and `@company.com`.
130192

193+
<YamlJsonTabs>
131194
```yaml
132195
operations:
133196
- alter_column:
@@ -137,6 +200,22 @@ operations:
137200
up: SELECT CASE WHEN email IS NULL THEN nick || '@company.com' ELSE email END
138201
down: email
139202
```
203+
```json
204+
{
205+
"operations": [
206+
{
207+
"alter_column": {
208+
"table": "employee",
209+
"column": "email",
210+
"nullable": false,
211+
"up": "SELECT CASE WHEN email IS NULL THEN nick || '@company.com' ELSE email END",
212+
"down": "email"
213+
}
214+
}
215+
]
216+
}
217+
```
218+
</YamlJsonTabs>
140219

141220
After the migration is complete, everyone will have an email address:
142221

@@ -177,6 +256,7 @@ Then we can call this function from the `up` migration:
177256

178257
The complete migration is the following:
179258

259+
<YamlJsonTabs>
180260
```yaml
181261
operations:
182262
- create_constraint:
@@ -197,6 +277,30 @@ operations:
197277
down:
198278
bio: bio
199279
```
280+
```json
281+
{
282+
"operations": [
283+
{
284+
"create_constraint": {
285+
"name": "limited_required_bio",
286+
"table": "employee",
287+
"type": "check",
288+
"columns": [
289+
"bio"
290+
],
291+
"check": "length(bio) > 15 AND length(bio) < 100",
292+
"up": {
293+
"bio": "CASE WHEN bio IS NULL THEN 'this employee did not provide a bio' WHEN length(bio) <= 15 THEN bio || SELECT random_bio(16-length(bio)) WHEN length(bio) >= 100 THEN left(bio, 96) || '...' ELSE bio END"
294+
},
295+
"down": {
296+
"bio": "bio"
297+
}
298+
}
299+
}
300+
]
301+
}
302+
```
303+
</YamlJsonTabs>
200304

201305
The `bio` column for `Alice` now contains extra 5 characters (`oivgd`) after the migration is complete:
202306

docs/operations/add_column.mdx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ description: An add column operation creates a new column on an existing table.
55

66
## Structure
77

8+
<YamlJsonTabs>
89
```yaml
910
add_column:
1011
table: name of table to which the column should be added
@@ -26,6 +27,34 @@ add_column:
2627
column: name of referenced column
2728
on_delete: ON DELETE behaviour, can be CASCADE, SET NULL, RESTRICT, or NO ACTION. Default is NO ACTION
2829
```
30+
```json
31+
{
32+
"add_column": {
33+
"table": "name of table to which the column should be added",
34+
"up": "SQL expression",
35+
"column": {
36+
"name": "name of column",
37+
"type": "postgres type",
38+
"comment": "postgres comment for the column",
39+
"nullable": true|false,
40+
"unique": true|false,
41+
"pk": true|false,
42+
"default": "default value for the column",
43+
"check": {
44+
"name": "name of check constraint",
45+
"constraint": "constraint expression"
46+
},
47+
"references": {
48+
"name": "name of foreign key constraint",
49+
"table": "name of referenced table",
50+
"column": "name of referenced column",
51+
"on_delete": "ON DELETE behaviour, can be CASCADE, SET NULL, RESTRICT, or NO ACTION. Default is NO ACTION",
52+
}
53+
}
54+
}
55+
}
56+
```
57+
</YamlJsonTabs>
2958

3059
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.
3160

docs/operations/alter_column/add_check_constraint.mdx

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ description: An add check constraint operation adds a `CHECK` constraint to a co
55

66
## Structure
77

8+
<YamlJsonTabs>
89
```yaml
910
alter_column:
1011
table: table name
@@ -15,14 +16,29 @@ alter_column:
1516
up: SQL expression
1617
down: SQL expression
1718
```
19+
```json
20+
{
21+
"alter_column": {
22+
"table": "table name",
23+
"column": "column name",
24+
"check": {
25+
"name": "check constraint name",
26+
"constraint": "constraint expression"
27+
},
28+
"up": "SQL expression",
29+
"down": "SQL expression"
30+
}
31+
}
32+
```
33+
</YamlJsonTabs>
1834

1935
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.
2036

2137
## Examples
2238

2339
### Add a `CHECK` constraint
2440

25-
Add a `CHECK` constraint to the `title` column in the `posts` table.
41+
Add a `CHECK` constraint to the `title` column in the `posts` table.
2642

2743
The `up` SQL migrates values to ensure they meet the constraint. The `down` SQL copies values without modification from column in the new schema version to the column in the old schema version:
2844

docs/operations/alter_column/add_foreign_key.mdx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ description: Add a foreign key constraint to a column.
55

66
## Structure
77

8+
<YamlJsonTabs>
89
```yaml
910
alter_column:
1011
table: table name
@@ -17,6 +18,23 @@ alter_column:
1718
up: SQL expression
1819
down: SQL expression
1920
```
21+
```json
22+
{
23+
"alter_column": {
24+
"table": "table name",
25+
"column": "column name",
26+
"references": {
27+
"name": "name of foreign key reference",
28+
"table": "name of referenced table",
29+
"column": "name of referenced column",
30+
"on_delete": "ON DELETE behaviour, can be CASCADE, SET NULL, RESTRICT, or NO ACTION. Default is NO ACTION"
31+
},
32+
"up": "SQL expression",
33+
"down": "SQL expression"
34+
}
35+
}
36+
```
37+
</YamlJsonTabs>
2038

2139
## Examples
2240

docs/operations/alter_column/add_not_null_constraint.mdx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ description: Add not null operations add a `NOT NULL` constraint to a column.
55

66
## Structure
77

8+
<YamlJsonTabs>
89
```yaml
910
alter_column:
1011
table: table name
@@ -13,6 +14,18 @@ alter_column:
1314
up: SQL expression
1415
down: SQL expression
1516
```
17+
```json
18+
{
19+
"alter_column": {
20+
"table": "table name",
21+
"column": "column name",
22+
"nullable": false,
23+
"up": "SQL expression",
24+
"down": "SQL expression"
25+
}
26+
}
27+
```
28+
</YamlJsonTabs>
1629

1730
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.
1831

docs/operations/alter_column/add_unique_constraint.mdx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ description: Add unique operations add a `UNIQUE` constraint to a column.
55

66
## Structure
77

8+
<YamlJsonTabs>
89
```yaml
910
alter_column:
1011
table: table name
@@ -14,6 +15,20 @@ alter_column:
1415
up: SQL expression
1516
down: SQL expression
1617
```
18+
```json
19+
{
20+
"alter_column": {
21+
"table": "table name",
22+
"column": "column name",
23+
"unique": {
24+
"name": "name of unique constraint"
25+
},
26+
"up": "SQL expression",
27+
"down": "SQL expression"
28+
}
29+
}
30+
```
31+
</YamlJsonTabs>
1732

1833
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.
1934

docs/operations/alter_column/change_comment.mdx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ description: A change comment operation changes the comment on a column.
55

66
## Structure
77

8+
<YamlJsonTabs>
89
```yaml
910
alter_column:
1011
table: table name
@@ -13,14 +14,26 @@ alter_column:
1314
up: SQL expression
1415
down: SQL expression
1516
```
17+
```json
18+
{
19+
"alter_column": {
20+
"table": "table name",
21+
"column": "column name",
22+
"comment": "new comment for column" | null,
23+
"up": "SQL expression",
24+
"down": "SQL expression"
25+
}
26+
}
27+
```
28+
</YamlJsonTabs>
1629

1730
The comment is added directly to the column on migration start.
1831

1932
## Examples
2033

2134
### Alter many column properties
2235

23-
An alter column migration performs many operations, including setting a comment:
36+
An alter column migration performs many operations, including setting a comment:
2437

2538
<ExampleSnippet example="35_alter_column_multiple.yaml" languange="yaml" />
2639

0 commit comments

Comments
 (0)