Skip to content
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

add to FAQ how to use custom types #53

Merged
merged 1 commit into from
Jun 25, 2024
Merged
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
71 changes: 71 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,77 @@ The provider supports the following databases:
).Load(&Models.Address{}, &Models.Person{})
```

* **How to handle enums and custom types?** -
The recommended way to handle custom types that are not supported by GORM such as postgres enums is to use [composite schemas](https://atlasgo.io/atlas-schema/projects#data-source-composite_schema).

First you need to define your custom type inside state file, lets call it `schema.sql`:
```sql
CREATE TYPE "status" AS ENUM ('active', 'inactive', 'deleted');
```

Next, you need to add the custom type to your GORM model using [type field tag](https://gorm.io/docs/models.html#Fields-Tags):
```diff filename ="models/player.go"
package models

import (
"gorm.io/gorm"
)

type Player struct {
gorm.Model
ID int `gorm:"primaryKey"`
+ Status string `gorm:"type:status"`
}

```
Next, you need to use schema composed of your GORM schema and `schema.sql` file, you can do it by using the next `atlas.hcl` config file:

```hcl
data "external_schema" "gorm" {
program = [
"go",
"run",
"-mod=mod",
"ariga.io/atlas-provider-gorm",
"load",
"--path", "./models",
"--dialect", "postgres",
]
}

data "composite_schema" "app" {
schema "public" {
url = "file://schema.sql"
}
schema "public" {
url = data.external_schema.gorm.url
}
}

env "composed" {
src = data.composite_schema.app.url
dev = "docker://postgres/15/dev?search_path=public"
migration {
dir = "file://migrations"
}
format {
migrate {
diff = "{{ sql . \" \" }}"
}
}
}

```
Now, when running: `atlas migrate diff --env composed` new migration file should be created, containing the custom enum type:

```sql filename ="20240623142238.sql"
-- Create enum type "status"
CREATE TYPE "status" AS ENUM ('active', 'inactive', 'deleted');
-- Modify "players" table
ALTER TABLE "players" ADD COLUMN "status" "status" NULL;
```


### License

This project is licensed under the [Apache License 2.0](LICENSE).
Loading