ariga / atlas-provider-gorm

GORM Provider for https://atlasgo.io
Apache License 2.0
53 stars 13 forks source link

gormschema: support custom gorm.Config option #14

Closed karolis-arbaciauskas closed 12 months ago

karolis-arbaciauskas commented 1 year ago

Issue

The proposed changes allow passing configuration to gorschema as a second argument. Currently, it is only possible to set one argument, DisableMigrationForeignKeyConstraint, as a boolean. This will disable foreign key constraints when creating migrations. Platforms like The PlanetScale, which uses Vitess behind the scenes, does not support foreign key constraints. For more information on this topic, please refer to the following link: https://planetscale.com/docs/learn/operating-without-foreign-key-constraints.

To disable foreign key constraints in the migrations, you can configure Atlas config or code.

data "external_schema" "gorm" {
  program = [
    "go",
    "run",
    "-mod=mod",
    "ariga.io/atlas-provider-gorm",
    "load",
    "--path", "./path/to/models",
    "--dialect", "mysql", // | postgres | sqlite
    "--disable-migration-foreign-key-constraint",
  ]
}

When used as a Go file, use the following code:

package main

import (
  "io"
  "os"

  "ariga.io/atlas-provider-gorm/gormschema"
  "github.com/<yourorg>/<yourrepo>/path/to/models"
)

func main() {
  stmts, err := gormschema.New("mysql", gormschema.WithForeignKeyConstraintDisabled()).Load(&models.User{}, &models.Pet{})
  if err != nil {
    fmt.Fprintf(os.Stderr, "failed to load gorm schema: %v\n", err)
    os.Exit(1)
  }
  io.WriteString(os.Stdout, stmts)
}
karolis-arbaciauskas commented 1 year ago

Hi @karolis-arbaciauskas

Thanks for working on this. I think the solution will be more robust and support more usecases if the gormschema constructor just accepts a gorm.Config{}. This will enable users to use this tool with any possible GORM configuration.

To make the API more aesthetic, we can use something similar to https://golang.cafe/blog/golang-functional-options-pattern.html

Thanks @rotemtam for the suggestions. I have updated the PR according to your recommendations.

karolis-arbaciauskas commented 1 year ago

@rotemtam @a8m 👋 Would it be possible for you to review the pull request sometime soon?