Skip to content

Commit

Permalink
dialect/sql/schema: option to enable atlas sum file (#2470)
Browse files Browse the repository at this point in the history
* dialect/sql/schema: option to enable atlas checksum file

* entc/integration/migrate: add integration tests for versioned migrations

* entc/integration/migrate: apply CR
  • Loading branch information
masseelch committed Apr 13, 2022
1 parent 89dc838 commit c117571
Show file tree
Hide file tree
Showing 28 changed files with 90 additions and 5,518 deletions.
8 changes: 8 additions & 0 deletions dialect/sql/schema/atlas.go
Expand Up @@ -264,6 +264,13 @@ func WithFormatter(fmt migrate.Formatter) MigrateOption {
}
}

// WithSumFile instructs atlas to generate a migration directory integrity sum file as well.
func WithSumFile() MigrateOption {
return func(m *Migrate) {
m.atlas.genSum = true
}
}

type (
// atlasOptions describes the options for atlas.
atlasOptions struct {
Expand All @@ -273,6 +280,7 @@ type (
skip ChangeKind
dir migrate.Dir
fmt migrate.Formatter
genSum bool
}

// atBuilder must be implemented by the different drivers in
Expand Down
4 changes: 3 additions & 1 deletion dialect/sql/schema/migrate.go
Expand Up @@ -186,7 +186,9 @@ func (m *Migrate) NamedDiff(ctx context.Context, name string, tables ...*Table)
}
opts := []migrate.PlannerOption{
migrate.WithFormatter(m.atlas.fmt),
migrate.DisableChecksum(),
}
if !m.atlas.genSum {
opts = append(opts, migrate.DisableChecksum())
}
return migrate.NewPlanner(nil, m.atlas.dir, opts...).WritePlan(plan)
}
Expand Down
49 changes: 49 additions & 0 deletions entc/integration/migrate/migrate_test.go
Expand Up @@ -7,11 +7,14 @@ package migrate
import (
"context"
"fmt"
"io/fs"
"math"
"path/filepath"
"strconv"
"strings"
"testing"

"ariga.io/atlas/sql/sqltool"
"entgo.io/ent/dialect"
"entgo.io/ent/dialect/sql"
"entgo.io/ent/dialect/sql/schema"
Expand All @@ -23,6 +26,7 @@ import (
"entgo.io/ent/entc/integration/migrate/entv2/customtype"
migratev2 "entgo.io/ent/entc/integration/migrate/entv2/migrate"
"entgo.io/ent/entc/integration/migrate/entv2/user"
"entgo.io/ent/entc/integration/migrate/versioned"

"ariga.io/atlas/sql/migrate"
atlas "ariga.io/atlas/sql/schema"
Expand Down Expand Up @@ -54,6 +58,10 @@ func TestMySQL(t *testing.T) {
}
NicknameSearch(t, clientv2)
TimePrecision(t, drv, "SELECT datetime_precision FROM information_schema.columns WHERE table_name = ? AND column_name = ?")

require.NoError(t, err, root.Exec(ctx, "DROP DATABASE IF EXISTS migrate", []interface{}{}, new(sql.Result)))
require.NoError(t, root.Exec(ctx, "CREATE DATABASE IF NOT EXISTS migrate", []interface{}{}, new(sql.Result)))
Versioned(t, versioned.NewClient(versioned.Driver(drv)))
})
}
}
Expand Down Expand Up @@ -157,6 +165,41 @@ func TestStorageKey(t *testing.T) {
require.Equal(t, "user_friend_id2", migratev2.FriendsTable.ForeignKeys[1].Symbol)
}

func Versioned(t *testing.T, client *versioned.Client) {
ctx := context.Background()

p := t.TempDir()
dir, err := migrate.NewLocalDir(p)
require.NoError(t, err)
require.NoError(t, client.Schema.Diff(ctx, schema.WithDir(dir)))
require.Equal(t, 2, countFiles(t, dir))

p = t.TempDir()
dir, err = migrate.NewLocalDir(p)
require.NoError(t, err)
require.NoError(t, client.Schema.Diff(ctx, schema.WithDir(dir), schema.WithFormatter(sqltool.GooseFormatter)))
require.Equal(t, 1, countFiles(t, dir))

p = t.TempDir()
dir, err = migrate.NewLocalDir(p)
require.NoError(t, err)
require.NoError(t, client.Schema.Diff(ctx, schema.WithDir(dir), schema.WithFormatter(sqltool.FlywayFormatter)))
require.Equal(t, 2, countFiles(t, dir))

p = t.TempDir()
dir, err = migrate.NewLocalDir(p)
require.NoError(t, err)
require.NoError(t, client.Schema.Diff(ctx, schema.WithDir(dir), schema.WithFormatter(sqltool.LiquibaseFormatter)))
require.Equal(t, 1, countFiles(t, dir))

p = t.TempDir()
dir, err = migrate.NewLocalDir(p)
require.NoError(t, err)
require.NoError(t, client.Schema.Diff(ctx, schema.WithDir(dir), schema.WithSumFile()))
require.Equal(t, 3, countFiles(t, dir))
require.FileExists(t, filepath.Join(p, "atlas.sum"))
}

func V1ToV2(t *testing.T, dialect string, clientv1 *entv1.Client, clientv2 *entv2.Client) {
ctx := context.Background()

Expand Down Expand Up @@ -403,3 +446,9 @@ func TimePrecision(t *testing.T, drv *sql.Driver, query string) {
func idRange(t *testing.T, id, l, h int) {
require.Truef(t, id > l && id < h, "id %s should be between %d to %d", id, l, h)
}

func countFiles(t *testing.T, d migrate.Dir) int {
files, err := fs.ReadDir(d, "")
require.NoError(t, err)
return len(files)
}
133 changes: 0 additions & 133 deletions entc/integration/migrate/versioned/car.go

This file was deleted.

53 changes: 0 additions & 53 deletions entc/integration/migrate/versioned/car/car.go

This file was deleted.

0 comments on commit c117571

Please sign in to comment.