Skip to content

Commit

Permalink
controllers: replace fs.FS with migrate.Dir (#174)
Browse files Browse the repository at this point in the history
  • Loading branch information
giautm authored Apr 19, 2024
1 parent 19618ec commit 87247e5
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 41 deletions.
33 changes: 10 additions & 23 deletions controllers/atlasmigration_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import (
"errors"
"fmt"
"io"
"io/fs"
"net/url"
"strings"

Expand Down Expand Up @@ -76,7 +75,7 @@ type (
migrationData struct {
EnvName string
URL *url.URL
Dir fs.FS
Dir migrate.Dir
Cloud *cloud
RevisionsSchema string
Baseline string
Expand Down Expand Up @@ -302,9 +301,15 @@ func (r *AtlasMigrationReconciler) extractData(ctx context.Context, res *dbv1alp
if err != nil {
return nil, err
}
data.Dir = mapFS(cfgMap.Data)
data.Dir, err = memDir(cfgMap.Data)
if err != nil {
return nil, err
}
case d.Local != nil:
data.Dir = mapFS(d.Local)
data.Dir, err = memDir(d.Local)
if err != nil {
return nil, err
}
default:
return nil, errors.New("no directory specified")
}
Expand Down Expand Up @@ -335,7 +340,7 @@ func (d *migrationData) hash() (string, error) {
h.Write([]byte(d.Cloud.RemoteDir.Tag))
case d.Dir != nil:
// Hash local directory
hf, err := checkSumDir(d.Dir)
hf, err := d.Dir.Checksum()
if err != nil {
return "", err
}
Expand Down Expand Up @@ -383,21 +388,3 @@ func (c *cloud) hasRemoteDir() bool {
}
return c.RemoteDir != nil && c.RemoteDir.Name != ""
}

func checkSumDir(src fs.FS) (migrate.HashFile, error) {
names, err := fs.Glob(src, "*.sql")
if err != nil {
return nil, err
}
dir := &migrate.MemDir{}
for _, name := range names {
data, err := fs.ReadFile(src, name)
if err != nil {
return nil, err
}
if err = dir.WriteFile(name, data); err != nil {
return nil, err
}
}
return dir.Checksum()
}
8 changes: 4 additions & 4 deletions controllers/atlasmigration_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -710,9 +710,9 @@ func TestWatcher_enabled(t *testing.T) {
func TestDefaultTemplate(t *testing.T) {
migrate := &migrationData{
URL: must(url.Parse("sqlite://file2/?mode=memory")),
Dir: mapFS(map[string]string{
Dir: must(memDir(map[string]string{
"1.sql": "CREATE TABLE foo (id INT PRIMARY KEY);",
}),
})),
}
var fileContent bytes.Buffer
require.NoError(t, migrate.render(&fileContent))
Expand All @@ -729,7 +729,7 @@ env {
func TestBaselineTemplate(t *testing.T) {
migrate := &migrationData{
URL: must(url.Parse("sqlite://file2/?mode=memory")),
Dir: mapFS(map[string]string{}),
Dir: must(memDir(map[string]string{})),
Baseline: "20230412003626",
}
var fileContent bytes.Buffer
Expand Down Expand Up @@ -903,7 +903,7 @@ func (t *migrationTest) addMigrationScript(name, content string) {
cm.Data[name] = content
t.k8s.put(&cm)

sum, err := checkSumDir(mapFS(cm.Data))
sum, err := must(memDir(cm.Data)).Checksum()
require.NoError(t, err)
atlasSum, err := sum.MarshalText()
require.NoError(t, err)
Expand Down
14 changes: 8 additions & 6 deletions controllers/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@ import (
"embed"
"errors"
"fmt"
"io/fs"
"regexp"
"strings"
"testing/fstest"
"text/template"
"time"

"ariga.io/atlas/sql/migrate"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -104,12 +103,15 @@ func getSecretValue(ctx context.Context, r client.Reader, ns string, selector *c
return string(secret.Data[selector.Key]), nil
}

func mapFS(m map[string]string) fs.FS {
f := fstest.MapFS{}
// memDir creates a memory directory from the given map.
func memDir(m map[string]string) (migrate.Dir, error) {
f := &migrate.MemDir{}
for key, value := range m {
f[key] = &fstest.MapFile{Data: []byte(value)}
if err := f.WriteFile(key, []byte(value)); err != nil {
return nil, err
}
}
return f
return f, nil
}

// isSQLErr returns true if the error is a SQL error.
Expand Down
8 changes: 6 additions & 2 deletions controllers/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,14 @@ func (r *AtlasSchemaReconciler) lint(ctx context.Context, wd *atlas.WorkingDir,
"unable to remove temporary directory", "dir", dir)
}
}()
err = wd.CopyFS(lintDirName, mapFS(map[string]string{
dir, err := memDir(map[string]string{
"1.sql": current,
"2.sql": strings.Join(plan.Changes.Pending, ";\n"),
}))
})
if err != nil {
return err
}
err = wd.CopyFS(lintDirName, dir)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ go 1.22
toolchain go1.22.0

require (
ariga.io/atlas v0.20.1-0.20240321075817-75fd3b1accbf
ariga.io/atlas-go-sdk v0.5.3
ariga.io/atlas v0.21.2-0.20240418081819-02b3f6239b04
ariga.io/atlas-go-sdk v0.5.4-0.20240419053913-865efa3e6f8f
github.com/stretchr/testify v1.9.0
golang.org/x/mod v0.17.0
k8s.io/api v0.29.4
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
ariga.io/atlas v0.20.1-0.20240321075817-75fd3b1accbf h1:jpDQmb4YyO6SADrfS2+13p+dKOBfNJB80K8sTLQMr9g=
ariga.io/atlas v0.20.1-0.20240321075817-75fd3b1accbf/go.mod h1:VPlcXdd4w2KqKnH54yEZcry79UAhpaWaxEsmn5JRNoE=
ariga.io/atlas-go-sdk v0.5.3 h1:KlLWPUnKm/gv3iaUDXAwUfQPZuEFbrAcqDIAFIImXZ0=
ariga.io/atlas-go-sdk v0.5.3/go.mod h1:wCso3QwMboXPUD5vNjBPDc3z086Ix3kfooanvcdlwV4=
ariga.io/atlas v0.21.2-0.20240418081819-02b3f6239b04 h1:YF3qiqtnhn+y4tfhZKTfZKfizpjqHYt7rWPUb+eA4ZA=
ariga.io/atlas v0.21.2-0.20240418081819-02b3f6239b04/go.mod h1:VPlcXdd4w2KqKnH54yEZcry79UAhpaWaxEsmn5JRNoE=
ariga.io/atlas-go-sdk v0.5.4-0.20240419053913-865efa3e6f8f h1:KlImyCEkKSsoFVOzxOnsraDzdt95g0Sqewt8sjJcbrY=
ariga.io/atlas-go-sdk v0.5.4-0.20240419053913-865efa3e6f8f/go.mod h1:9Q+/04PVyJHUse1lEE9Kp6E18xj/6mIzaUTcWYSjSnQ=
github.com/DATA-DOG/go-sqlmock v1.5.0 h1:Shsta01QNfFxHCfpW6YH2STWB0MudeXXEWMr20OEh60=
github.com/DATA-DOG/go-sqlmock v1.5.0/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
github.com/agext/levenshtein v1.2.3 h1:YB2fHEn0UJagG8T1rrWknE3ZQzWM06O8AMAatNn7lmo=
Expand Down

0 comments on commit 87247e5

Please sign in to comment.