Skip to content

Commit

Permalink
cmd/atlas/internal/sqlparse: move parsers out (#3279)
Browse files Browse the repository at this point in the history
  • Loading branch information
a8m authored Dec 19, 2024
1 parent 0f5d486 commit 2e6aaaf
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 1,727 deletions.
134 changes: 0 additions & 134 deletions cmd/atlas/internal/migratelint/lint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package migratelint_test

import (
"context"
"errors"
"os"
"os/exec"
"path/filepath"
Expand All @@ -16,8 +15,6 @@ import (

"ariga.io/atlas/cmd/atlas/internal/migratelint"
"ariga.io/atlas/sql/migrate"
"ariga.io/atlas/sql/schema"
"ariga.io/atlas/sql/sqlclient"
_ "ariga.io/atlas/sql/sqlite"

_ "github.com/mattn/go-sqlite3"
Expand Down Expand Up @@ -133,134 +130,3 @@ func TestLatestChanges(t *testing.T) {
require.Equal(t, files[:1], base)
require.Equal(t, files[1:], feat)
}

func TestDevLoader_LoadChanges(t *testing.T) {
ctx := context.Background()
c, err := sqlclient.Open(ctx, "sqlite://ci?mode=memory&_fk=1")
require.NoError(t, err)
defer c.Close()
l := &migratelint.DevLoader{Dev: c}
diff, err := l.LoadChanges(ctx, nil, nil)
require.NoError(t, err)
require.Empty(t, diff.Files)

diff, err = l.LoadChanges(ctx, []migrate.File{
migrate.NewLocalFile("0_base.sql", []byte("---\n\nCREATE INVALID users (id INT);\n")),
}, nil)
require.Error(t, err)
require.Nil(t, diff)
var fr *migratelint.FileError
require.True(t, errors.As(err, &fr))
require.Equal(t, `executing statement: near "INVALID": syntax error`, fr.Err.Error())
require.Equal(t, 5, fr.Pos)

base := []migrate.File{
migrate.NewLocalFile("0_base.sql", []byte("CREATE TABLE users (id INT);")),
}
files := []migrate.File{
migrate.NewLocalFile("1.sql", []byte("CREATE TABLE t1 (id INT);\nINSERT INTO t1 (id) VALUES (1);")),
migrate.NewLocalFile("2.sql", []byte("CREATE TABLE t2 (id INT);\nDROP TABLE users;")),
migrate.NewLocalFile("3.sql", []byte("CREATE TABLE t3 (id INT);\nDROP TABLE t3;")),
migrate.NewLocalFile("4.sql", []byte("ALTER TABLE t2 RENAME id TO oid;")),
}
diff, err = l.LoadChanges(ctx, base, files)
require.NoError(t, err)
require.Len(t, diff.Files, 4)

// File 1.
require.Equal(t, files[0], diff.Files[0].File)
require.Len(t, diff.Files[0].Changes, 2)
require.Zero(t, diff.Files[0].Changes[0].Stmt.Pos)
require.Equal(t, "CREATE TABLE t1 (id INT);", diff.Files[0].Changes[0].Stmt.Text)
require.IsType(t, (*schema.AddTable)(nil), diff.Files[0].Changes[0].Changes[0])
require.Equal(t, "INSERT INTO t1 (id) VALUES (1);", diff.Files[0].Changes[1].Stmt.Text)
require.Empty(t, diff.Files[0].Changes[1].Changes)

// File 2.
require.Equal(t, files[1], diff.Files[1].File)
require.Len(t, diff.Files[1].Changes, 2)
require.Zero(t, diff.Files[1].Changes[0].Stmt.Pos)
require.Equal(t, "CREATE TABLE t2 (id INT);", diff.Files[1].Changes[0].Stmt.Text)
require.IsType(t, (*schema.AddTable)(nil), diff.Files[1].Changes[0].Changes[0])
require.Zero(t, diff.Files[1].Changes[0].Stmt.Pos)
require.Equal(t, "DROP TABLE users;", diff.Files[1].Changes[1].Stmt.Text)
require.IsType(t, (*schema.DropTable)(nil), diff.Files[1].Changes[1].Changes[0])

// File 3.
require.Equal(t, files[2], diff.Files[2].File)
require.IsType(t, (*schema.AddTable)(nil), diff.Files[2].Changes[0].Changes[0])
require.IsType(t, (*schema.DropTable)(nil), diff.Files[2].Changes[1].Changes[0])
require.Empty(t, diff.Files[2].Sum)

// File 3.
require.Equal(t, files[3], diff.Files[3].File)
require.IsType(t, (*schema.ModifyTable)(nil), diff.Files[3].Changes[0].Changes[0])
require.IsType(t, (*schema.RenameColumn)(nil), diff.Files[3].Changes[0].Changes[0].(*schema.ModifyTable).Changes[0])

// Changes.
changes, err := c.RealmDiff(diff.From, diff.To)
require.NoError(t, err)
require.Len(t, changes, 3)

err = c.ApplyChanges(ctx, []schema.Change{
&schema.AddTable{
T: schema.NewTable("users").AddColumns(schema.NewIntColumn("id", "int")),
},
})
require.NoError(t, err)
_, err = l.LoadChanges(ctx, base, files)
require.ErrorAs(t, err, new(*migrate.NotCleanError))
}

func TestDevLoader_LoadCheckpoints(t *testing.T) {
ctx := context.Background()
c, err := sqlclient.Open(ctx, "sqlite://ci?mode=memory&_fk=1")
require.NoError(t, err)
defer c.Close()
dir := &migrate.MemDir{}
l := &migratelint.DevLoader{Dev: c}
require.NoError(t, dir.WriteFile("1.sql", []byte("CREATE TABLE t1 (id INT);")))
require.NoError(t, dir.WriteFile("2.sql", []byte("CREATE TABLE t2 (id INT);")))
require.NoError(t, dir.WriteCheckpoint("3_checkpoint.sql", "", []byte("CREATE TABLE t1 (id INT);\nCREATE TABLE t2 (id INT);")))
require.NoError(t, dir.WriteFile("4.sql", []byte("CREATE TABLE t3 (id INT);")))

files, err := dir.Files()
require.NoError(t, err)
// Base contains a checkpoint file.
diff, err := l.LoadChanges(ctx, files[:3], files[3:])
require.NoError(t, err)
require.Len(t, diff.Files, 1)
require.Equal(t, "4.sql", diff.Files[0].File.Name())
isAddTable(t, diff.Files[0].Changes[0].Changes[0], "t3")

// Changed files contain a checkpoint file.
diff, err = l.LoadChanges(ctx, files[:2], files[2:])
require.NoError(t, err)
require.Len(t, diff.Files, 2)
require.Equal(t, "3_checkpoint.sql", diff.Files[0].File.Name())
require.Len(t, diff.Files[0].Changes, 2)
isAddTable(t, diff.Files[0].Changes[0].Changes[0], "t1")
isAddTable(t, diff.Files[0].Changes[1].Changes[0], "t2")
require.Equal(t, "4.sql", diff.Files[1].File.Name())
isAddTable(t, diff.Files[1].Changes[0].Changes[0], "t3")

// Both base and changed files contain a checkpoint file.
require.NoError(t, dir.WriteCheckpoint("5_checkpoint.sql", "", []byte("CREATE TABLE t1(id INT);\nCREATE TABLE t2(id INT);\nCREATE TABLE t3(id INT);")))
files, err = dir.Files()
require.NoError(t, err)
diff, err = l.LoadChanges(ctx, files[:3], files[3:])
require.NoError(t, err)
require.Len(t, diff.Files, 2)
require.Equal(t, "4.sql", diff.Files[0].File.Name())
isAddTable(t, diff.Files[0].Changes[0].Changes[0], "t3")
require.Equal(t, "5_checkpoint.sql", diff.Files[1].File.Name())
require.Len(t, diff.Files[1].Changes, 3)
isAddTable(t, diff.Files[1].Changes[0].Changes[0], "t1")
isAddTable(t, diff.Files[1].Changes[1].Changes[0], "t2")
isAddTable(t, diff.Files[1].Changes[2].Changes[0], "t3")
}

func isAddTable(t *testing.T, c schema.Change, name string) {
require.IsType(t, (*schema.AddTable)(nil), c)
require.Equal(t, name, c.(*schema.AddTable).T.Name)
}
215 changes: 0 additions & 215 deletions cmd/atlas/internal/sqlparse/pgparse/pgparse.go

This file was deleted.

16 changes: 14 additions & 2 deletions cmd/atlas/internal/sqlparse/pgparse/pgparse_oss.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,22 @@
package pgparse

import (
"errors"

"ariga.io/atlas/sql/migrate"
"ariga.io/atlas/sql/schema"
pgquery "github.com/pganalyze/pg_query_go/v5"
)

func FixAlterTable(_ string, _ *pgquery.AlterTableStmt, changes schema.Changes) (schema.Changes, error) {
type Parser struct{}

func (*Parser) ColumnFilledBefore([]*migrate.Stmt, *schema.Table, *schema.Column, int) (bool, error) {
return false, errors.New("unimplemented")
}

func (*Parser) CreateViewAfter([]*migrate.Stmt, string, string, int) (bool, error) {
return false, errors.New("unimplemented")
}

func (*Parser) FixChange(_ migrate.Driver, _ string, changes schema.Changes) (schema.Changes, error) {
return changes, nil // Unimplemented.
}
Loading

0 comments on commit 2e6aaaf

Please sign in to comment.