Skip to content

Commit

Permalink
dialect/sql/schema: upgrade atlas and disable sum file creation (#2400)
Browse files Browse the repository at this point in the history
* dialect/sql/schema: upgrade atlas and disable sum file creation

* dialect/sql/schema: linter
  • Loading branch information
masseelch committed Mar 15, 2022
1 parent 3f0b085 commit db1617b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 6 deletions.
6 changes: 5 additions & 1 deletion dialect/sql/schema/migrate.go
Expand Up @@ -181,7 +181,11 @@ func (m *Migrate) NamedDiff(ctx context.Context, name string, tables ...*Table)
if len(plan.Changes) == 0 {
return nil
}
return migrate.NewPlanner(nil, m.atlas.dir, migrate.WithFormatter(m.atlas.fmt)).WritePlan(plan)
opts := []migrate.PlannerOption{
migrate.WithFormatter(m.atlas.fmt),
migrate.DisableChecksum(),
}
return migrate.NewPlanner(nil, m.atlas.dir, opts...).WritePlan(plan)
}

func (m *Migrate) create(ctx context.Context, tables ...*Table) error {
Expand Down
39 changes: 35 additions & 4 deletions dialect/sql/schema/migrate_test.go
Expand Up @@ -6,11 +6,19 @@ package schema

import (
"context"
"os"
"path/filepath"
"strconv"
"testing"
"time"

"ariga.io/atlas/sql/migrate"
"entgo.io/ent/dialect"
"entgo.io/ent/dialect/sql"
"github.com/DATA-DOG/go-sqlmock"
"github.com/stretchr/testify/require"

_ "github.com/mattn/go-sqlite3"
)

func TestMigrateHookOmitTable(t *testing.T) {
Expand All @@ -25,13 +33,13 @@ func TestMigrateHookOmitTable(t *testing.T) {
WillReturnResult(sqlmock.NewResult(0, 1))
mock.ExpectCommit()

migrate, err := NewMigrate(sql.OpenDB("mysql", db), WithHooks(func(next Creator) Creator {
m, err := NewMigrate(sql.OpenDB("mysql", db), WithHooks(func(next Creator) Creator {
return CreateFunc(func(ctx context.Context, tables ...*Table) error {
return next.Create(ctx, tables[1])
})
}))
require.NoError(t, err)
err = migrate.Create(context.Background(), tables...)
err = m.Create(context.Background(), tables...)
require.NoError(t, err)
}

Expand All @@ -50,12 +58,35 @@ func TestMigrateHookAddTable(t *testing.T) {
WillReturnResult(sqlmock.NewResult(0, 1))
mock.ExpectCommit()

migrate, err := NewMigrate(sql.OpenDB("mysql", db), WithHooks(func(next Creator) Creator {
m, err := NewMigrate(sql.OpenDB("mysql", db), WithHooks(func(next Creator) Creator {
return CreateFunc(func(ctx context.Context, tables ...*Table) error {
return next.Create(ctx, tables[0], &Table{Name: "pets"})
})
}))
require.NoError(t, err)
err = migrate.Create(context.Background(), tables...)
err = m.Create(context.Background(), tables...)
require.NoError(t, err)
}

func TestMigrate_Diff(t *testing.T) {
db, err := sql.Open(dialect.SQLite, "file:test?mode=memory&_fk=1")
require.NoError(t, err)

p := t.TempDir()
d, err := migrate.NewLocalDir(p)
require.NoError(t, err)

m, err := NewMigrate(db, WithDir(d))
require.NoError(t, err)
require.NoError(t, m.Diff(context.Background(), &Table{Name: "users"}))
v := strconv.FormatInt(time.Now().Unix(), 10)
requireFileEqual(t, filepath.Join(p, v+"_changes.up.sql"), "CREATE TABLE `users` (, PRIMARY KEY ());\n")
requireFileEqual(t, filepath.Join(p, v+"_changes.down.sql"), "DROP TABLE `users`;\n")
require.NoFileExists(t, filepath.Join(p, "atlas.sum"))
}

func requireFileEqual(t *testing.T, name, contents string) {
c, err := os.ReadFile(name)
require.NoError(t, err)
require.Equal(t, contents, string(c))
}
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -3,7 +3,7 @@ module entgo.io/ent
go 1.17

require (
ariga.io/atlas v0.3.8-0.20220313134928-770640fc02bf
ariga.io/atlas v0.3.8-0.20220314111236-b2171e04c5b2
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
2 changes: 2 additions & 0 deletions go.sum
Expand Up @@ -2,6 +2,8 @@ ariga.io/atlas v0.3.7-0.20220303204946-787354f533c3 h1:fjG4oFCQEfGrRi0QoxWcH2OO2
ariga.io/atlas v0.3.7-0.20220303204946-787354f533c3/go.mod h1:yWGf4VPiD4SW83+kAqzD624txN9VKoJC+bpVXr2pKJA=
ariga.io/atlas v0.3.8-0.20220313134928-770640fc02bf h1:bAt5AUvr91QI8yXHME6qTsMTNM4BtfSB3M9o1cmt51E=
ariga.io/atlas v0.3.8-0.20220313134928-770640fc02bf/go.mod h1:ipw7dUlFanAylr9nvs8lCvOUC8hFG6PGd/gtr+uJMvk=
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=
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
cloud.google.com/go v0.38.0/go.mod h1:990N+gfupTy94rShfmMCWGDn0LpTmnzTp2qbd1dvSRU=
Expand Down

0 comments on commit db1617b

Please sign in to comment.