Skip to content

Commit

Permalink
Add headers to examples
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew-farries committed Dec 6, 2024
1 parent 6d2d5e3 commit 4eff062
Show file tree
Hide file tree
Showing 22 changed files with 94 additions and 4 deletions.
12 changes: 12 additions & 0 deletions docs/operations/add_column.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -41,29 +41,41 @@ Default values are subject to the usual rules for quoting SQL expressions. In pa

## Examples

### Add multiple columns

Add three new columns to the `products` table. Each column addition is a separate operation:

<ExampleSnippet example="03_add_column.json" language="json" />

### Add column with `up` SQL

Add a new column to the `users` table and define `up` SQL to ensure that the new column is populated with a value when a row is added to the old version of the table:

<ExampleSnippet example="06_add_column_to_sql_table.json" language="json" />

### Add column without `up` SQL

Add a new column to the `reviews` table. There is no need for `up` SQL because the column has a default:

<ExampleSnippet example="17_add_rating_column.json" language="json" />

### Add column with `CHECK` constraint

Add a new column to the `people` table with a `CHECK` constraint defined:

<ExampleSnippet
example="26_add_column_with_check_constraint.json"
language="json"
/>

### Add column with comment

Add a new column to the `people` table with a comment defined on the new column:

<ExampleSnippet example="30_add_column_simple_up.json" language="json" />

### Add column with a user defined type

Add a new column to the `fruits` table that has an enum type, defined in an earlier migration:

<ExampleSnippet example="41_add_enum_column.json" language="json" />
2 changes: 2 additions & 0 deletions docs/operations/alter_column/add_check_constraint.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ The `up` SQL expression is used to migrate values from the column in the old sch

## Examples

### Add a `CHECK` constraint

Add a `CHECK` constraint to the `title` column in the `posts` table.

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:
Expand Down
2 changes: 2 additions & 0 deletions docs/operations/alter_column/add_foreign_key.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ description: Add a foreign key constraint to a column.

## Examples

### Add a foreign key constraint

Add a `FOREIGN KEY` constraint to the `user_id` column in the `posts` table:

<ExampleSnippet example="21_add_foreign_key_constraint.json" language="json" />
2 changes: 2 additions & 0 deletions docs/operations/alter_column/add_not_null_constraint.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ Use `up` to migrate values from the nullable column in the old schema view to th

## Examples

### Add a `NOT NULL` constraint

Add a `NOT NULL` constraint to the `review` column in the `reviews` table.

<ExampleSnippet example="16_set_nullable.json" language="json" />
2 changes: 2 additions & 0 deletions docs/operations/alter_column/add_unique_constraint.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ Use the `up` SQL expression to migrate values from the old non-unique column in

## Examples

### Add a `UNIQUE` constraint

Add a `UNIQUE` constraint to the `review` column in the `reviews` table. The `up` SQL appends a random suffix to ensure uniqueness:

<ExampleSnippet example="15_set_column_unique.json" language="json" />
4 changes: 4 additions & 0 deletions docs/operations/alter_column/change_comment.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@ The comment is added directly to the column on migration start.

## Examples

### Alter many column properties

An alter column migration performs many operations, including setting a comment:

<ExampleSnippet example="35_alter_column_multiple.json" language="json" />

### Remove a comment

To remove a comment from a column set `comment` to `NULL`:

<ExampleSnippet example="36_set_comment_to_null.json" language="json" />
4 changes: 4 additions & 0 deletions docs/operations/alter_column/change_default.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ To remove a column default, set the `default` field to `NULL`.

## Examples

### Make multiple column changes

An alter column migration that makes multiple changes including setting the default:

<ExampleSnippet example="35_alter_column_multiple.json" language="json" />

### Drop a column default

Drop a default by setting the `default` field to `null`.

<ExampleSnippet example="46_alter_column_drop_default.json" language="json" />
2 changes: 2 additions & 0 deletions docs/operations/alter_column/change_type.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ Use the `down` SQL expression to do data conversion in the other direction; from

## Examples

### Change column type

Change the type of the `rating` column on the `reviews` table:

<ExampleSnippet example="18_change_column_type.json" language="json" />
2 changes: 2 additions & 0 deletions docs/operations/alter_column/drop_not_null_constraint.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ description: Drop not null operations drop a `NOT NULL` constraint from a column

## Examples

### Remove `NOT NULL` from a column

Remove `NOT NULL` from the `title` column in the `posts` table:

<ExampleSnippet example="31_unset_not_null.json" language="json" />
2 changes: 2 additions & 0 deletions docs/operations/alter_column/rename_column.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ In the new schema version, the column will have its new name. In the old schema

## Examples

### Rename a column

Rename the `role` column in the `employees` table:

<ExampleSnippet example="13_rename_column.json" language="json" />
6 changes: 6 additions & 0 deletions docs/operations/create_constraint.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,20 @@ An `up` and `down` SQL expression is required for each column covered by the con

## Examples

### Add a `UNIQUE` constraint

Add a multi-column unique constraint on the `tickets` table:

<ExampleSnippet example="44_add_table_unique_constraint.json" language="json" />

### Add a `CHECK` constraint

Add a check constraint on the `sellers_name` and `sellers_zip` fields on the `ticket` table. The `up` SQL expression ensures that pairs of values not meeting the new constraint on the old columns are data migrated to values that meet the new constraint in the new columns:

<ExampleSnippet example="45_add_table_check_constraint.json" language="json" />

### Add a `FOREIGN KEY` constraint

Add a multi-column foreign key constraint to the the `tickets` table. The `up` SQL expressions here don't do any data transformation:

<ExampleSnippet
Expand Down
8 changes: 8 additions & 0 deletions docs/operations/create_index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,29 @@ description: A create index operation creates a new index on a set of columns.

## Examples

### Create a `btree` index

Create a `btree` index on the `name` column in the `fruits` table:

<ExampleSnippet example="10_create_index.json" language="json" />

### Create a partial index

Create a partial index on the `id` column in the `fruits` table:

<ExampleSnippet example="37_create_partial_index.json" language="json" />

### Create an index with storage parameters

Set storage parameters and index method:

<ExampleSnippet
example="38_create_hash_index_with_fillfactor.json"
language="json"
/>

### Create a unique index

Create a unique index:

<ExampleSnippet example="42_create_unique_index.json" language="json" />
24 changes: 21 additions & 3 deletions docs/operations/create_table.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -42,33 +42,49 @@ Default values are subject to the usual rules for quoting SQL expressions. In pa

## Examples

### Create multiple tables

Create multiple tables. Each table is a separate operation in the migration:

<ExampleSnippet example="01_create_tables.json" language="json" />

### Create one table

Create one table:

<ExampleSnippet example="02_create_another_table.json" language="json" />

### Create one table (2)

Create one table:

<ExampleSnippet example="08_create_fruits_table.json" language="json" />

### Create one table (3)

Create one table:

<ExampleSnippet example="20_create_posts_table.json" language="json" />

### Create a table with a comment

Create a table with a comment on the table:

<ExampleSnippet example="12_create_employees_table.json" language="json" />

### Create a table with nullable and non-nullable columns

Create a table with a mix of nullable and non-nullable columns:

<ExampleSnippet example="14_add_reviews_table.json" language="json" />

### Create a table with a foreign key

Create a table with a foreign key constraint defined on a column:

<ExampleSnippet example="19_create_orders_table.json" language="json" />

Create a table:

<ExampleSnippet example="20_create_posts_table.json" language="json" />
### Create a table with a `CHECK` constraint

Create a table with a `CHECK` constraint on one column:

Expand All @@ -77,6 +93,8 @@ Create a table with a `CHECK` constraint on one column:
language="json"
/>

### Create a table with column defaults

Create a table with different `DEFAULT` values:

<ExampleSnippet example="28_different_defaults.json" language="json" />
4 changes: 3 additions & 1 deletion docs/operations/drop_column.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ The `down` field above is required in order to backfill the previous version of

## Examples

Drop a column with a default value - if a new row is inserted against the new schema without a `price` column, the old schema `price` column will be set to `0`.
### Drop a column

Drop a column - if a new row is inserted against the new schema without a `price` column, the old schema `price` column will be set to `0`.

<ExampleSnippet example="09_drop_column.json" language="json" />
6 changes: 6 additions & 0 deletions docs/operations/drop_constraint.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,20 @@ Only `CHECK`, `FOREIGN KEY`, and `UNIQUE` constraints can be dropped.

## Examples

### Drop a `CHECK` constraint:

Drop a `CHECK` constraint:

<ExampleSnippet example="23_drop_check_constraint.json" language="json" />

### Drop a `FOREIGN KEY` constraint:

Drop a `FOREIGN KEY` constraint:

<ExampleSnippet example="24_drop_foreign_key_constraint.json" language="json" />

### Drop a `UNIQUE` constraint:

Drop a `UNIQUE` constraint:

<ExampleSnippet example="27_drop_unique_constraint.json" language="json" />
2 changes: 2 additions & 0 deletions docs/operations/drop_index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ description: A drop index operation drops an index from a table.

## Examples

### Drop an index

Drop an index defined on the `fruits` table:

<ExampleSnippet example="11_drop_index.json" language="json" />
2 changes: 2 additions & 0 deletions docs/operations/drop_multi_column_constraint.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ The new versions of the columns will no longer have the constraint, but in the o

## Examples

### Drop a `CHECK` constraint

Drop a `CHECK` constraint defined on the `tickets` table.

<ExampleSnippet example="48_drop_tickets_check.json" language="json" />
2 changes: 2 additions & 0 deletions docs/operations/drop_table.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ The table is not visible in the new version of the schema created when the migra

## Examples

### Drop a table

Drop the products table:

<ExampleSnippet example="07_drop_table.json" language="json" />
4 changes: 4 additions & 0 deletions docs/operations/raw_sql.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,14 @@ The `onComplete` flag will make this operation run the `up` expression on the co

## Examples

### Create a table with a raw SQL migration

A raw SQL migration to create a table:

<ExampleSnippet example="05_sql.json" language="json" />

### Run a SQL migration on migration complete

A raw SQL migration run on migration completion rather than start.

<ExampleSnippet example="32_sql_on_complete.json" language="json" />
2 changes: 2 additions & 0 deletions docs/operations/rename_constraint.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ The constraint is renamed on migration completion; it retains its old name durin

## Examples

### Rename a `CHECK` constraint

Rename a `CHECK` constraint:

<ExampleSnippet example="33_rename_check_constraint.json" language="json" />
2 changes: 2 additions & 0 deletions docs/operations/rename_table.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ The table itself is renamed on migration completion.

## Examples

### Rename a table

Rename the customers table:

<ExampleSnippet example="04_rename_table.json" language="json" />
2 changes: 2 additions & 0 deletions docs/operations/set_replica_identity.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,6 @@ description: A set replica identity operation sets the replica identity for a ta

## Examples

### Set replica identity

<ExampleSnippet example="29_set_replica_identity.json" language="json" />

0 comments on commit 4eff062

Please sign in to comment.