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

entc/integration/custom_id: update atlas and fix issue in integration tests #2424

Merged
merged 1 commit into from Mar 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 24 additions & 13 deletions entc/integration/customid/customid_test.go
Expand Up @@ -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"
Expand All @@ -22,8 +24,8 @@ 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"

"github.com/go-sql-driver/mysql"
"github.com/google/uuid"
_ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3"
Expand All @@ -48,7 +50,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)
})
Expand All @@ -58,18 +60,21 @@ 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)
CustomID(t, client)
BytesID(t, client)
Expand All @@ -81,7 +86,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)
}
Expand Down Expand Up @@ -234,8 +239,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...)
})
}

Expand Down
4 changes: 2 additions & 2 deletions go.mod
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down