Skip to content

Commit

Permalink
doc: add domains and enums to hcl spec (ariga#2200)
Browse files Browse the repository at this point in the history
  • Loading branch information
a8m authored Oct 22, 2023
1 parent fd0c720 commit 2b47d88
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 24 deletions.
38 changes: 37 additions & 1 deletion doc/md/atlas-schema/hcl-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -443,9 +443,45 @@ table "t" {
}
```

### Domain

The `domain` type is a user-defined data type that is based on an existing data type but with optional constraints
and default values. Learn more about it in the [PostgreSQL website](https://www.postgresql.org/docs/current/domains.html).

```hcl
domain "us_postal_code" {
schema = schema.public
type = text
null = true
check "us_postal_code_check" {
expr = "((VALUE ~ '^\\d{5}$'::text) OR (VALUE ~ '^\\d{5}-\\d{4}$'::text))"
}
}
domain "username" {
schema = schema.public
type = text
null = false
default = "anonymous"
check "username_length" {
expr = "(length(VALUE) > 3)"
}
}
table "users" {
schema = schema.public
column "name" {
type = username
}
column "zip" {
type = us_postal_code
}
}
```

### Enum

The `enum` type allows storing a set of enumerated values.
The `enum` type allows storing a set of enumerated values. Learn more about it in the [PostgreSQL website](https://www.postgresql.org/docs/current/datatype-enum.html).

```hcl
enum "status" {
Expand Down
118 changes: 95 additions & 23 deletions doc/md/atlas-schema/hcl.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ table "products" {
Table partitioning refers to splitting logical large tables into smaller physical ones.

:::note
Atlas currently only supports PostgreSQL. Support for the remaining dialects will be added in future versions.
Partitions are currently supported only by the PostgreSQL driver. Support for the remaining drivers will be added in future versions.
:::

```hcl
Expand Down Expand Up @@ -773,28 +773,6 @@ procedure "proc" {
}
```

## Comment

The `comment` attribute is an attribute of `schema`, `table`, `column`, and `index`.

```hcl
schema "public" {
comment = "A schema comment"
}
table "users" {
schema = schema.public
column "name" {
type = text
comment = "A column comment"
}
index "name_idx" {
columns = [column.name]
}
comment = "A table comment"
}
```

</TabItem>
<TabItem label="MySQL" value="mysql">

Expand Down Expand Up @@ -832,6 +810,100 @@ procedure "p2" {
</TabItem>
</Tabs>

## Domain

:::info BETA FEATURE
Domains are currently in beta and available to logged-in users only. To use this feature, run:
```
atlas login
```
:::

The `domain` type is a user-defined data type that is based on an existing data type but with optional constraints
and default values. Supported by PostgreSQL.

```hcl
domain "us_postal_code" {
schema = schema.public
type = text
null = true
check "us_postal_code_check" {
expr = "((VALUE ~ '^\\d{5}$'::text) OR (VALUE ~ '^\\d{5}-\\d{4}$'::text))"
}
}
domain "username" {
schema = schema.public
type = text
null = false
default = "anonymous"
check "username_length" {
expr = "(length(VALUE) > 3)"
}
}
table "users" {
schema = schema.public
column "name" {
type = username
}
column "zip" {
type = us_postal_code
}
}
schema "public" {
comment = "standard public schema"
}
```

## Enum

The `enum` type allows storing a set of enumerated values. Supported by PostgreSQL.

```hcl
enum "status" {
schema = schema.test
values = ["on", "off"]
}
table "t1" {
schema = schema.test
column "c1" {
type = enum.status
}
}
table "t2" {
schema = schema.test
column "c1" {
type = enum.status
}
}
```

## Comment

The `comment` attribute is an attribute of `schema`, `table`, `column`, and `index`.

```hcl
schema "public" {
comment = "A schema comment"
}
table "users" {
schema = schema.public
column "name" {
type = text
comment = "A column comment"
}
index "name_idx" {
columns = [column.name]
}
comment = "A table comment"
}
```

## Charset and Collation

The `charset` and `collate` are attributes of `schema`, `table` and `column` and supported by MySQL, MariaDB and PostgreSQL.
Expand Down

0 comments on commit 2b47d88

Please sign in to comment.