Skip to content

Commit

Permalink
cmd/atlas: rename cmd/atlas/lint to cmd/atlas/migratelint (#1975)
Browse files Browse the repository at this point in the history
  • Loading branch information
a8m committed Aug 11, 2023
1 parent 477c1ad commit 5d7d111
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 41 deletions.
32 changes: 16 additions & 16 deletions cmd/atlas/internal/cmdapi/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ import (
"ariga.io/atlas/cmd/atlas/internal/cloudapi"
"ariga.io/atlas/cmd/atlas/internal/cmdext"
"ariga.io/atlas/cmd/atlas/internal/cmdlog"
"ariga.io/atlas/cmd/atlas/internal/lint"
cmdmigrate "ariga.io/atlas/cmd/atlas/internal/migrate"
"ariga.io/atlas/cmd/atlas/internal/migrate/ent/revision"
"ariga.io/atlas/cmd/atlas/internal/migratelint"
"ariga.io/atlas/sql/migrate"
"ariga.io/atlas/sql/schema"
"ariga.io/atlas/sql/sqlcheck"
Expand Down Expand Up @@ -928,28 +928,28 @@ func migrateLintRun(cmd *cobra.Command, _ []string, flags migrateLintFlags) erro
if err != nil {
return err
}
var detect lint.ChangeDetector
var detect migratelint.ChangeDetector
switch {
case flags.latest == 0 && flags.gitBase == "":
return fmt.Errorf("--%s or --%s is required", flagLatest, flagGitBase)
case flags.latest > 0 && flags.gitBase != "":
return fmt.Errorf("--%s and --%s are mutually exclusive", flagLatest, flagGitBase)
case flags.latest > 0:
detect = lint.LatestChanges(dir, int(flags.latest))
detect = migratelint.LatestChanges(dir, int(flags.latest))
case flags.gitBase != "":
detect, err = lint.NewGitChangeDetector(
detect, err = migratelint.NewGitChangeDetector(
dir,
lint.WithWorkDir(flags.gitDir),
lint.WithBase(flags.gitBase),
lint.WithMigrationsPath(dir.(interface{ Path() string }).Path()),
migratelint.WithWorkDir(flags.gitDir),
migratelint.WithBase(flags.gitBase),
migratelint.WithMigrationsPath(dir.(interface{ Path() string }).Path()),
)
if err != nil {
return err
}
}
format := lint.DefaultTemplate
format := migratelint.DefaultTemplate
if f := flags.logFormat; f != "" {
format, err = template.New("format").Funcs(lint.TemplateFuncs).Parse(f)
format, err = template.New("format").Funcs(migratelint.TemplateFuncs).Parse(f)
if err != nil {
return fmt.Errorf("parse format: %w", err)
}
Expand All @@ -962,19 +962,19 @@ func migrateLintRun(cmd *cobra.Command, _ []string, flags migrateLintFlags) erro
if err != nil {
return err
}
r := &lint.Runner{
r := &migratelint.Runner{
Dev: dev,
Dir: dir,
ChangeDetector: detect,
ReportWriter: &lint.TemplateWriter{
ReportWriter: &migratelint.TemplateWriter{
T: format,
W: cmd.OutOrStdout(),
},
Analyzers: az,
}
err = r.Run(cmd.Context())
// Print the error in case it was not printed before.
cmd.SilenceErrors = errors.As(err, &lint.SilentError{})
cmd.SilenceErrors = errors.As(err, &migratelint.SilentError{})
cmd.SilenceUsage = cmd.SilenceErrors
return err
}
Expand Down Expand Up @@ -1085,10 +1085,6 @@ func migrateSetRun(cmd *cobra.Command, args []string, flags migrateSetFlags) (re
if err != nil {
return err
}
files, err := dir.Files()
if err != nil {
return err
}
client, err := sqlclient.Open(ctx, flags.url)
if err != nil {
return err
Expand Down Expand Up @@ -1134,6 +1130,10 @@ func migrateSetRun(cmd *cobra.Command, args []string, flags migrateSetFlags) (re
if err != nil {
return err
}
files, err := dir.Files()
if err != nil {
return err
}
var version string
switch n := len(args); {
// Prevent the case where 'migrate set' is called without a version on
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// This source code is licensed under the Apache 2.0 license found
// in the LICENSE file in the root directory of this source tree.

package lint
package migratelint

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// This source code is licensed under the Apache 2.0 license found
// in the LICENSE file in the root directory of this source tree.

package lint_test
package migratelint_test

import (
"context"
Expand All @@ -15,7 +15,7 @@ import (
"testing"
"time"

"ariga.io/atlas/cmd/atlas/internal/lint"
"ariga.io/atlas/cmd/atlas/internal/migratelint"
"ariga.io/atlas/sql/migrate"
"ariga.io/atlas/sql/schema"
"ariga.io/atlas/sql/sqlclient"
Expand Down Expand Up @@ -51,7 +51,7 @@ func TestGitChangeDetector(t *testing.T) {
// Test change detector.
dir, err := migrate.NewLocalDir(mdir)
require.NoError(t, err)
cs, err := lint.NewGitChangeDetector(dir, lint.WithWorkDir(root))
cs, err := migratelint.NewGitChangeDetector(dir, migratelint.WithWorkDir(root))
require.NoError(t, err)
base, feat, err := cs.DetectChanges(context.Background())
require.NoError(t, err)
Expand All @@ -75,7 +75,7 @@ func TestGitChangeDetector(t *testing.T) {
require.Equal(t, "6_new.sql", feat[3].Name())

// Compare feature and feature-1.
cs, err = lint.NewGitChangeDetector(dir, lint.WithWorkDir(root), lint.WithBase("feature"))
cs, err = migratelint.NewGitChangeDetector(dir, migratelint.WithWorkDir(root), migratelint.WithBase("feature"))
require.NoError(t, err)
base, feat, err = cs.DetectChanges(context.Background())
require.NoError(t, err)
Expand All @@ -94,22 +94,22 @@ func TestLatestChanges(t *testing.T) {
testFile{name: "1.sql", content: "CREATE TABLE t1 (id INT)"},
testFile{name: "2.sql", content: "CREATE TABLE t2 (id INT)\nDROP TABLE users"},
}
base, feat, err := lint.LatestChanges(testDir{files: files}, 0).DetectChanges(context.Background())
base, feat, err := migratelint.LatestChanges(testDir{files: files}, 0).DetectChanges(context.Background())
require.NoError(t, err)
require.Equal(t, files, base)
require.Empty(t, feat)

base, feat, err = lint.LatestChanges(testDir{files: files}, 2).DetectChanges(context.Background())
base, feat, err = migratelint.LatestChanges(testDir{files: files}, 2).DetectChanges(context.Background())
require.NoError(t, err)
require.Empty(t, base)
require.Equal(t, files, feat)

base, feat, err = lint.LatestChanges(testDir{files: files}, -1).DetectChanges(context.Background())
base, feat, err = migratelint.LatestChanges(testDir{files: files}, -1).DetectChanges(context.Background())
require.NoError(t, err)
require.Empty(t, base)
require.Equal(t, files, feat)

base, feat, err = lint.LatestChanges(testDir{files: files}, 1).DetectChanges(context.Background())
base, feat, err = migratelint.LatestChanges(testDir{files: files}, 1).DetectChanges(context.Background())
require.NoError(t, err)
require.Equal(t, files[:1], base)
require.Equal(t, files[1:], feat)
Expand All @@ -120,7 +120,7 @@ func TestDevLoader_LoadChanges(t *testing.T) {
c, err := sqlclient.Open(ctx, "sqlite://ci?mode=memory&cache=shared&_fk=1")
require.NoError(t, err)
defer c.Close()
l := &lint.DevLoader{Dev: c}
l := &migratelint.DevLoader{Dev: c}
diff, err := l.LoadChanges(ctx, nil, nil)
require.NoError(t, err)
require.Empty(t, diff.Files)
Expand All @@ -130,7 +130,7 @@ func TestDevLoader_LoadChanges(t *testing.T) {
}, nil)
require.Error(t, err)
require.Nil(t, diff)
fr := err.(*lint.FileError)
fr := err.(*migratelint.FileError)
require.Equal(t, `executing statement: near "INVALID": syntax error`, fr.Err.Error())
require.Equal(t, 5, fr.Pos)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// This source code is licensed under the Apache 2.0 license found
// in the LICENSE file in the root directory of this source tree.

package lint
package migratelint

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// This source code is licensed under the Apache 2.0 license found
// in the LICENSE file in the root directory of this source tree.

package lint_test
package migratelint_test

import (
"bytes"
Expand All @@ -12,7 +12,7 @@ import (
"testing"
"text/template"

"ariga.io/atlas/cmd/atlas/internal/lint"
"ariga.io/atlas/cmd/atlas/internal/migratelint"
"ariga.io/atlas/sql/migrate"
"ariga.io/atlas/sql/sqlcheck"
"ariga.io/atlas/sql/sqlclient"
Expand All @@ -25,7 +25,7 @@ func TestRunner_Run(t *testing.T) {
b := &bytes.Buffer{}
c, err := sqlclient.Open(ctx, "sqlite://run?mode=memory&cache=shared&_fk=1")
require.NoError(t, err)
r := &lint.Runner{
r := &migratelint.Runner{
Dir: testDir{},
Dev: c,
ChangeDetector: testDetector{
Expand All @@ -39,8 +39,8 @@ func TestRunner_Run(t *testing.T) {
Analyzers: []sqlcheck.Analyzer{
&testAnalyzer{},
},
ReportWriter: &lint.TemplateWriter{
T: lint.DefaultTemplate,
ReportWriter: &migratelint.TemplateWriter{
T: migratelint.DefaultTemplate,
W: b,
},
}
Expand All @@ -59,8 +59,8 @@ func TestRunner_Run(t *testing.T) {
`, b.String())

b.Reset()
r.ReportWriter.(*lint.TemplateWriter).T = template.Must(template.New("").
Funcs(lint.TemplateFuncs).
r.ReportWriter.(*migratelint.TemplateWriter).T = template.Must(template.New("").
Funcs(migratelint.TemplateFuncs).
Parse(`
Env:
{{ .Env.Driver }}, {{ .Env.Dir }}
Expand Down
12 changes: 6 additions & 6 deletions cmd/atlas/x/x.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,34 @@ import (
"context"
"errors"

"ariga.io/atlas/cmd/atlas/internal/lint"
"ariga.io/atlas/cmd/atlas/internal/migratelint"
"ariga.io/atlas/sql/migrate"
"ariga.io/atlas/sql/sqlcheck"
"ariga.io/atlas/sql/sqlclient"
)

// Exposes the lint reporting types.
type (
FileReport = lint.FileReport
SummaryReport = lint.SummaryReport
FileReport = migratelint.FileReport
SummaryReport = migratelint.SummaryReport
)

// ErrEmptyReport is returned when the report is empty.
var ErrEmptyReport = errors.New("empty report")

// lintLatest runs the lint command on the latest changes (files) in the given directory.
func lintLatest(ctx context.Context, dev *sqlclient.Client, dir migrate.Dir, latest int, az []sqlcheck.Analyzer) (report *SummaryReport, err error) {
r := lint.Runner{
r := migratelint.Runner{
Dev: dev,
Dir: dir,
Analyzers: az,
ChangeDetector: lint.LatestChanges(dir, latest),
ChangeDetector: migratelint.LatestChanges(dir, latest),
ReportWriter: reporterFunc(func(r *SummaryReport) error {
report = r
return nil
}),
}
if err = r.Run(ctx); err != nil && !errors.As(err, &lint.SilentError{}) {
if err = r.Run(ctx); err != nil && !errors.As(err, &migratelint.SilentError{}) {
return nil, err
}
if report == nil {
Expand Down

0 comments on commit 5d7d111

Please sign in to comment.