Skip to content

Commit

Permalink
gormschema: support custom gorm.Config option (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
karolis-arbaciauskas authored Oct 5, 2023
1 parent be2c441 commit 85e5b3e
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- name: Run Go linters
uses: golangci/golangci-lint-action@v3
with:
args: --verbose
args: --verbose --timeout=5m
skip-pkg-cache: true
unit-tests:
runs-on: ubuntu-latest
Expand Down
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,21 @@ env "gorm" {
}
```

### Additional Configuration

To supply custom `gorm.Config{}` object to the provider use the [Go Program Mode](#as-go-file) with
the `WithConfig` option. For example, to disable foreign keys:

```go
loader := New("sqlite", WithConfig(
&gorm.Config{
DisableForeignKeyConstraintWhenMigrating: true,
},
))
```

For a full list of options, see the [GORM documentation](https://gorm.io/docs/gorm_config.html).

### Usage

Once you have the provider installed, you can use it to apply your GORM schema to the database:
Expand All @@ -127,7 +142,7 @@ target database.

#### Diff

Atlas supports a [version migration](https://atlasgo.io/concepts/declarative-vs-versioned#versioned-migrations)
Atlas supports a [versioned migration](https://atlasgo.io/concepts/declarative-vs-versioned#versioned-migrations)
workflow, where each change to the database is versioned and recorded in a migration file. You can use the
`atlas migrate diff` command to automatically generate a migration file that will migrate the database
from its latest revision to the current GORM schema.
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module ariga.io/atlas-provider-gorm
go 1.20

require (
ariga.io/atlas-go-sdk v0.0.0-20230709063453-1058d6508503
ariga.io/atlas-go-sdk v0.1.1-0.20231001054405-7edfcfc14f1c
github.com/alecthomas/kong v0.7.1
github.com/stretchr/testify v1.8.4
golang.org/x/tools v0.10.0
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
ariga.io/atlas-go-sdk v0.0.0-20230709063453-1058d6508503 h1:D12EzAAjhL7xEJk5jFnkKYUXVrDQ4mh0IjB8vqTmo8I=
ariga.io/atlas-go-sdk v0.0.0-20230709063453-1058d6508503/go.mod h1:fwi5nIOFLedo6CqZ0a172dhykLWBnoD25bqmZhvW948=
ariga.io/atlas-go-sdk v0.1.0 h1:sSPV26Lv2DIzbc+oZxgnAqYgKEwBFU0FsFDUUnUNHHs=
ariga.io/atlas-go-sdk v0.1.0/go.mod h1:738TvNdlvLnkRhB+euXvrhKPJkeV+LPoVa4xarUGCaQ=
ariga.io/atlas-go-sdk v0.1.1-0.20231001054405-7edfcfc14f1c h1:jvi4KB/7DmYYT+Wy2TFImccaBU0+dw7V8Un67NDGuio=
ariga.io/atlas-go-sdk v0.1.1-0.20231001054405-7edfcfc14f1c/go.mod h1:MLvZ9QwZx1KhI6+8XguxHPUPm0/PTTUr46S5GQAe9WI=
github.com/alecthomas/assert/v2 v2.1.0 h1:tbredtNcQnoSd3QBhQWI7QZ3XHOVkw1Moklp2ojoH/0=
github.com/alecthomas/kong v0.7.1 h1:azoTh0IOfwlAX3qN9sHWTxACE2oV8Bg2gAwBsMwDQY4=
github.com/alecthomas/kong v0.7.1/go.mod h1:n1iCIO2xS46oE8ZfYCNDqdR0b0wZNrXAIAqro/2132U=
Expand Down
29 changes: 23 additions & 6 deletions gormschema/gorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,32 @@ import (
)

// New returns a new Loader.
func New(dialect string) *Loader {
return &Loader{dialect: dialect}
func New(dialect string, opts ...Option) *Loader {
l := &Loader{dialect: dialect, config: &gorm.Config{}}
for _, opt := range opts {
opt(l)
}
return l
}

// Loader is a Loader for gorm schema.
type Loader struct {
dialect string
type (
// Loader is a Loader for gorm schema.
Loader struct {
dialect string
config *gorm.Config
}
// Option configures the Loader.
Option func(*Loader)
)

// WithConfig sets the gorm config.
func WithConfig(cfg *gorm.Config) Option {
return func(l *Loader) {
l.config = cfg
}
}

// Load loads the models and returns the DDL statements representing the schema.
func (l *Loader) Load(models ...any) (string, error) {
var di gorm.Dialector
switch l.dialect {
Expand Down Expand Up @@ -52,7 +69,7 @@ func (l *Loader) Load(models ...any) (string, error) {
default:
return "", fmt.Errorf("unsupported engine: %s", l.dialect)
}
db, err := gorm.Open(di, &gorm.Config{})
db, err := gorm.Open(di, l.config)
if err != nil {
return "", err
}
Expand Down
22 changes: 22 additions & 0 deletions gormschema/gorm_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package gormschema

import (
"testing"

"ariga.io/atlas-provider-gorm/internal/testdata/models"
"github.com/stretchr/testify/require"
"gorm.io/gorm"
)

func TestConfig(t *testing.T) {
l := New("sqlite", WithConfig(
&gorm.Config{
DisableForeignKeyConstraintWhenMigrating: true,
},
))
sql, err := l.Load(models.Pet{}, models.User{})
require.NoError(t, err)
require.Contains(t, sql, "CREATE TABLE `pets`")
require.Contains(t, sql, "CREATE TABLE `users`")
require.NotContains(t, sql, "FOREIGN KEY")
}

0 comments on commit 85e5b3e

Please sign in to comment.