From d0105a0d60548b0052e5e5f07c32b3701dffb9e7 Mon Sep 17 00:00:00 2001 From: Ariel Mashraki Date: Thu, 24 Mar 2022 17:05:05 +0200 Subject: [PATCH] entc/integration/custom_id: update atlas and fix issue in integration tests --- entc/integration/customid/customid_test.go | 45 +++++++++++++++------- go.mod | 4 +- go.sum | 3 ++ 3 files changed, 37 insertions(+), 15 deletions(-) diff --git a/entc/integration/customid/customid_test.go b/entc/integration/customid/customid_test.go index 2d86ca3337..e03686ebb7 100644 --- a/entc/integration/customid/customid_test.go +++ b/entc/integration/customid/customid_test.go @@ -13,6 +13,8 @@ import ( "testing" "entgo.io/ent/dialect" + + entsql "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/schema" "entgo.io/ent/entc/integration/customid/ent" "entgo.io/ent/entc/integration/customid/ent/blob" @@ -22,8 +24,9 @@ import ( "entgo.io/ent/entc/integration/customid/ent/user" "entgo.io/ent/entc/integration/customid/sid" "entgo.io/ent/schema/field" - "github.com/go-sql-driver/mysql" + atlas "ariga.io/atlas/sql/schema" + "github.com/go-sql-driver/mysql" "github.com/google/uuid" _ "github.com/lib/pq" _ "github.com/mattn/go-sqlite3" @@ -48,7 +51,7 @@ func TestMySQL(t *testing.T) { cfg.DBName = "custom_id" client, err := ent.Open("mysql", cfg.FormatDSN()) require.NoError(t, err, "connecting to custom_id database") - err = client.Schema.Create(context.Background(), schema.WithHooks(clearDefault, skipBytesID)) + err = client.Schema.Create(context.Background(), schema.WithHooks(clearDefault, skipBytesID), schema.WithAtlas(true)) require.NoError(t, err) CustomID(t, client) }) @@ -58,18 +61,28 @@ func TestMySQL(t *testing.T) { func TestPostgres(t *testing.T) { for version, port := range map[string]int{"10": 5430, "11": 5431, "12": 5433, "13": 5434} { t.Run(version, func(t *testing.T) { - dsn := fmt.Sprintf("host=localhost port=%d user=postgres password=pass sslmode=disable", port) + dsn := fmt.Sprintf("host=localhost port=%d user=postgres password=pass sslmode=disable dbname=test", port) db, err := sql.Open(dialect.Postgres, dsn) require.NoError(t, err) defer db.Close() - _, err = db.Exec("CREATE DATABASE custom_id") - require.NoError(t, err, "creating database") - defer db.Exec("DROP DATABASE custom_id") + _, err = db.Exec("CREATE SCHEMA IF NOT EXISTS custom_id") + require.NoError(t, err, "creating schema") + _, err = db.Exec("SET search_path TO custom_id") + require.NoError(t, err, "setting schema") + _, err = db.Exec(`CREATE EXTENSION IF NOT EXISTS "uuid-ossp" SCHEMA custom_id`) + require.NoError(t, err, "creating extension") + defer db.Exec(`DROP EXTENSION "uuid-ossp"`) + defer db.Exec("DROP SCHEMA custom_id CASCADE") - client, err := ent.Open(dialect.Postgres, dsn+" dbname=custom_id") - require.NoError(t, err, "connecting to custom_id database") - defer client.Close() - err = client.Schema.Create(context.Background()) + client := ent.NewClient(ent.Driver(entsql.OpenDB(dialect.Postgres, db))) + err = client.Schema.Create(context.Background(), schema.WithAtlas(true)) + require.NoError(t, err) + err = client.Schema.Create(context.Background(), schema.WithAtlas(true), schema.WithDiffHook(func(next schema.Differ) schema.Differ { + return schema.DiffFunc(func(current, desired *atlas.Schema) ([]atlas.Change, error) { + changes, err := next.Diff(current, desired) + return changes, err + }) + })) require.NoError(t, err) CustomID(t, client) BytesID(t, client) @@ -81,7 +94,7 @@ func TestSQLite(t *testing.T) { client, err := ent.Open("sqlite3", "file:ent?mode=memory&cache=shared&_fk=1") require.NoError(t, err) defer client.Close() - require.NoError(t, client.Schema.Create(context.Background(), schema.WithHooks(clearDefault))) + require.NoError(t, client.Schema.Create(context.Background(), schema.WithHooks(clearDefault)), schema.WithAtlas(true)) CustomID(t, client) BytesID(t, client) } @@ -234,8 +247,14 @@ func BytesID(t *testing.T, client *ent.Client) { // clearDefault clears the id's default for non-postgres dialects. func clearDefault(c schema.Creator) schema.Creator { return schema.CreateFunc(func(ctx context.Context, tables ...*schema.Table) error { - tables[1].Columns[0].Default = nil - return c.Create(ctx, tables...) + // Drop DEFAULT clause for MySQL without changing the tables. + ct := make([]*schema.Table, len(tables)) + copy(ct, tables) + *ct[1] = *tables[1] + ct[1].Columns = append([]*schema.Column(nil), tables[1].Columns...) + *ct[1].Columns[0] = *tables[1].Columns[0] + ct[1].Columns[0].Default = nil + return c.Create(ctx, ct...) }) } diff --git a/go.mod b/go.mod index 2de62cb6b8..ce9d2812d1 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module entgo.io/ent go 1.17 require ( - ariga.io/atlas v0.3.8-0.20220314111236-b2171e04c5b2 + ariga.io/atlas v0.3.8-0.20220324144249-d5d77d7dadfa github.com/DATA-DOG/go-sqlmock v1.5.0 github.com/go-openapi/inflect v0.19.0 github.com/go-sql-driver/mysql v1.6.0 @@ -39,7 +39,7 @@ require ( github.com/spf13/pflag v1.0.5 // indirect github.com/zclconf/go-cty v1.8.0 // indirect golang.org/x/mod v0.5.1 // indirect - golang.org/x/sys v0.0.0-20211205182925-97ca703d548d // indirect + golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect golang.org/x/text v0.3.7 // indirect golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect diff --git a/go.sum b/go.sum index 22b247ca6f..d6dd845b5a 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ ariga.io/atlas v0.3.8-0.20220314111236-b2171e04c5b2 h1:qbH+CDPAMsV1FIKkHGYzy2aWP9k5QAqPbi9PYZGqz60= ariga.io/atlas v0.3.8-0.20220314111236-b2171e04c5b2/go.mod h1:ipw7dUlFanAylr9nvs8lCvOUC8hFG6PGd/gtr+uJMvk= +ariga.io/atlas v0.3.8-0.20220324144249-d5d77d7dadfa h1:v+ZNqhCtR0uylXshIfghFGHTvpJrxB+9iNjWQV2UXak= +ariga.io/atlas v0.3.8-0.20220324144249-d5d77d7dadfa/go.mod h1:2aAhlHuY8tr7rPNz2MgOBTJB/cVjK0sv1VvTIqeYSws= cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/DATA-DOG/go-sqlmock v1.5.0 h1:Shsta01QNfFxHCfpW6YH2STWB0MudeXXEWMr20OEh60= @@ -150,6 +152,7 @@ golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20211205182925-97ca703d548d h1:FjkYO/PPp4Wi0EAUOVLxePm7qVW4r4ctbWpURyuOD0E= golang.org/x/sys v0.0.0-20211205182925-97ca703d548d/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=