Repository: ariga/atlas
Pull request #2355 (2023-12-09 10:36:01)

sql: move exclude helpers from sql/internal to sql/schema
This will be used in the cmd packages


Patch:
diff --git a/sql/mysql/inspect.go b/sql/mysql/inspect.go
index 52e6f725..3d5c6b60 100644
--- a/sql/mysql/inspect.go
+++ b/sql/mysql/inspect.go
@@ -50,7 +50,7 @@ func (i *inspect) InspectRealm(ctx context.Context, opts *schema.InspectRealmOpt
 			}
 		}
 	}
-	return sqlx.ExcludeRealm(r, opts.Exclude)
+	return schema.ExcludeRealm(r, opts.Exclude)
 }
 
 // InspectSchema returns schema descriptions of the tables in the given schema.
@@ -86,7 +86,7 @@ func (i *inspect) InspectSchema(ctx context.Context, name string, opts *schema.I
 			return nil, err
 		}
 	}
-	return sqlx.ExcludeSchema(r.Schemas[0], opts.Exclude)
+	return schema.ExcludeSchema(r.Schemas[0], opts.Exclude)
 }
 
 func (i *inspect) inspectTables(ctx context.Context, r *schema.Realm, opts *schema.InspectOptions) error {
diff --git a/sql/postgres/inspect.go b/sql/postgres/inspect.go
index 7bee61e9..94bf5dff 100644
--- a/sql/postgres/inspect.go
+++ b/sql/postgres/inspect.go
@@ -72,7 +72,7 @@ func (i *inspect) InspectRealm(ctx context.Context, opts *schema.InspectRealmOpt
 			return nil, err
 		}
 	}
-	return sqlx.ExcludeRealm(r, opts.Exclude)
+	return schema.ExcludeRealm(r, opts.Exclude)
 }
 
 // InspectSchema returns schema descriptions of the tables in the given schema.
@@ -129,7 +129,7 @@ func (i *inspect) InspectSchema(ctx context.Context, name string, opts *schema.I
 	if err := i.inspectDeps(ctx, r, opts); err != nil {
 		return nil, err
 	}
-	return sqlx.ExcludeSchema(r.Schemas[0], opts.Exclude)
+	return schema.ExcludeSchema(r.Schemas[0], opts.Exclude)
 }
 
 func (i *inspect) inspectTables(ctx context.Context, r *schema.Realm, opts *schema.InspectOptions) error {
diff --git a/sql/internal/sqlx/exclude.go b/sql/schema/exclude.go
similarity index 81%
rename from sql/internal/sqlx/exclude.go
rename to sql/schema/exclude.go
index da4d9df3..5d7b48cc 100644
--- a/sql/internal/sqlx/exclude.go
+++ b/sql/schema/exclude.go
@@ -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 sqlx
+package schema
 
 import (
 	"encoding/csv"
@@ -10,16 +10,14 @@ import (
 	"path/filepath"
 	"regexp"
 	"strings"
-
-	"ariga.io/atlas/sql/schema"
 )
 
 // ExcludeRealm filters resources in the realm based on the given patterns.
-func ExcludeRealm(r *schema.Realm, patterns []string) (*schema.Realm, error) {
+func ExcludeRealm(r *Realm, patterns []string) (*Realm, error) {
 	if len(patterns) == 0 {
 		return r, nil
 	}
-	var schemas []*schema.Schema
+	var schemas []*Schema
 	globs, err := split(patterns)
 	if err != nil {
 		return nil, err
@@ -36,7 +34,7 @@ Filter:
 			}
 			if match {
 				// In case there is a match, and it is
-				// a single glob we exclude this schema.
+				// a single glob we exclude this 
 				if len(g) == 1 {
 					continue Filter
 				}
@@ -52,7 +50,7 @@ Filter:
 }
 
 // ExcludeSchema filters resources in the schema based on the given patterns.
-func ExcludeSchema(s *schema.Schema, patterns []string) (*schema.Schema, error) {
+func ExcludeSchema(s *Schema, patterns []string) (*Schema, error) {
 	if len(patterns) == 0 {
 		return s, nil
 	}
@@ -89,9 +87,9 @@ func split(patterns []string) ([][]string, error) {
 	return globs, nil
 }
 
-func excludeS(s *schema.Schema, glob []string) error {
+func excludeS(s *Schema, glob []string) error {
 	if globT, exclude := excludeType(typeT, glob[0]); exclude {
-		var tables []*schema.Table
+		var tables []*Table
 		for _, t := range s.Tables {
 			match, err := filepath.Match(globT, t.Name)
 			if err != nil {
@@ -113,7 +111,7 @@ func excludeS(s *schema.Schema, glob []string) error {
 		s.Tables = tables
 	}
 	if globV, exclude := excludeType(typeV, glob[0]); exclude {
-		var views []*schema.View
+		var views []*View
 		for _, v := range s.Views {
 			match, err := filepath.Match(globV, v.Name)
 			if err != nil {
@@ -133,7 +131,7 @@ func excludeS(s *schema.Schema, glob []string) error {
 	}
 	if globF, exclude := excludeType(typeFn, glob[0]); exclude {
 		var err error
-		s.Funcs, err = filter(s.Funcs, func(f *schema.Func) (bool, error) {
+		s.Funcs, err = filter(s.Funcs, func(f *Func) (bool, error) {
 			return filepath.Match(globF, f.Name)
 		})
 		if err != nil {
@@ -142,7 +140,7 @@ func excludeS(s *schema.Schema, glob []string) error {
 	}
 	if globP, exclude := excludeType(typePr, glob[0]); exclude {
 		var err error
-		s.Procs, err = filter(s.Procs, func(p *schema.Proc) (bool, error) {
+		s.Procs, err = filter(s.Procs, func(p *Proc) (bool, error) {
 			return filepath.Match(globP, p.Name)
 		})
 		if err != nil {
@@ -152,11 +150,11 @@ func excludeS(s *schema.Schema, glob []string) error {
 	return nil
 }
 
-func excludeT(t *schema.Table, pattern string) (err error) {
-	ex := make(map[*schema.Index]struct{})
-	ef := make(map[*schema.ForeignKey]struct{})
+func excludeT(t *Table, pattern string) (err error) {
+	ex := make(map[*Index]struct{})
+	ef := make(map[*ForeignKey]struct{})
 	if p, exclude := excludeType(typeC, pattern); exclude {
-		t.Columns, err = filter(t.Columns, func(c *schema.Column) (bool, error) {
+		t.Columns, err = filter(t.Columns, func(c *Column) (bool, error) {
 			match, err := filepath.Match(p, c.Name)
 			if !match || err != nil {
 				return false, err
@@ -171,7 +169,7 @@ func excludeT(t *schema.Table, pattern string) (err error) {
 		})
 	}
 	if p, exclude := excludeType(typeI, pattern); exclude {
-		t.Indexes, err = filter(t.Indexes, func(idx *schema.Index) (bool, error) {
+		t.Indexes, err = filter(t.Indexes, func(idx *Index) (bool, error) {
 			if _, ok := ex[idx]; ok {
 				return true, nil
 			}
@@ -179,7 +177,7 @@ func excludeT(t *schema.Table, pattern string) (err error) {
 		})
 	}
 	if p, exclude := excludeType(typeF, pattern); exclude {
-		t.ForeignKeys, err = filter(t.ForeignKeys, func(fk *schema.ForeignKey) (bool, error) {
+		t.ForeignKeys, err = filter(t.ForeignKeys, func(fk *ForeignKey) (bool, error) {
 			if _, ok := ef[fk]; ok {
 				return true, nil
 			}
@@ -187,8 +185,8 @@ func excludeT(t *schema.Table, pattern string) (err error) {
 		})
 	}
 	if p, exclude := excludeType(typeK, pattern); exclude {
-		t.Attrs, err = filter(t.Attrs, func(a schema.Attr) (bool, error) {
-			c, ok := a.(*schema.Check)
+		t.Attrs, err = filter(t.Attrs, func(a Attr) (bool, error) {
+			c, ok := a.(*Check)
 			if !ok {
 				return false, nil
 			}
@@ -202,9 +200,9 @@ func excludeT(t *schema.Table, pattern string) (err error) {
 	return
 }
 
-func excludeV(t *schema.View, pattern string) (err error) {
+func excludeV(t *View, pattern string) (err error) {
 	if p, exclude := excludeType(typeC, pattern); exclude {
-		t.Columns, err = filter(t.Columns, func(c *schema.Column) (bool, error) {
+		t.Columns, err = filter(t.Columns, func(c *Column) (bool, error) {
 			match, err := filepath.Match(p, c.Name)
 			if !match || err != nil {
 				return false, err
diff --git a/sql/sqlite/inspect.go b/sql/sqlite/inspect.go
index 3bfbce20..c8331c8e 100644
--- a/sql/sqlite/inspect.go
+++ b/sql/sqlite/inspect.go
@@ -54,7 +54,7 @@ func (i *inspect) InspectRealm(ctx context.Context, opts *schema.InspectRealmOpt
 			return nil, err
 		}
 	}
-	return sqlx.ExcludeRealm(r, opts.Exclude)
+	return schema.ExcludeRealm(r, opts.Exclude)
 }
 
 // InspectSchema returns schema descriptions of the tables in the given schema.
@@ -96,7 +96,7 @@ func (i *inspect) InspectSchema(ctx context.Context, name string, opts *schema.I
 			return nil, err
 		}
 	}
-	return sqlx.ExcludeSchema(r.Schemas[0], opts.Exclude)
+	return schema.ExcludeSchema(r.Schemas[0], opts.Exclude)
 }
 
 func (i *inspect) inspectTable(ctx context.Context, t *schema.Table) error {


Test patch:
diff --git a/sql/internal/sqlx/exclude_test.go b/sql/schema/exclude_test.go
similarity index 71%
rename from sql/internal/sqlx/exclude_test.go
rename to sql/schema/exclude_test.go
index 0fd0c26b..34ce92f5 100644
--- a/sql/internal/sqlx/exclude_test.go
+++ b/sql/schema/exclude_test.go
@@ -2,18 +2,16 @@
 // 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 sqlx
+package schema
 
 import (
 	"testing"
 
-	"ariga.io/atlas/sql/schema"
-
 	"github.com/stretchr/testify/require"
 )
 
 func TestExcludeRealm_Schemas(t *testing.T) {
-	r := schema.NewRealm(schema.New("s1"), schema.New("s2"), schema.New("s3"))
+	r := NewRealm(New("s1"), New("s2"), New("s3"))
 	r, err := ExcludeRealm(r, []string{"s4"})
 	require.NoError(t, err)
 	require.Len(t, r.Schemas, 3)
@@ -28,7 +26,7 @@ func TestExcludeRealm_Schemas(t *testing.T) {
 	require.NoError(t, err)
 	require.Empty(t, r.Schemas)
 
-	r = schema.NewRealm(schema.New("s1"), schema.New("s2"), schema.New("s3"))
+	r = NewRealm(New("s1"), New("s2"), New("s3"))
 	r, err = ExcludeRealm(r, []string{"s*.*", "s*.*.*"})
 	require.NoError(t, err)
 	require.Len(t, r.Schemas, 3)
@@ -38,19 +36,19 @@ func TestExcludeRealm_Schemas(t *testing.T) {
 }
 
 func TestExcludeRealm_Tables(t *testing.T) {
-	r := schema.NewRealm(
-		schema.New("s0"),
-		schema.New("s1").AddTables(
-			schema.NewTable("t1"),
+	r := NewRealm(
+		New("s0"),
+		New("s1").AddTables(
+			NewTable("t1"),
 		),
-		schema.New("s2").AddTables(
-			schema.NewTable("t1"),
-			schema.NewTable("t2"),
+		New("s2").AddTables(
+			NewTable("t1"),
+			NewTable("t2"),
 		),
-		schema.New("s3").AddTables(
-			schema.NewTable("t1"),
-			schema.NewTable("t2"),
-			schema.NewTable("t3"),
+		New("s3").AddTables(
+			NewTable("t1"),
+			NewTable("t2"),
+			NewTable("t3"),
 		),
 	)
 	r, err := ExcludeRealm(r, []string{"s4"})
@@ -95,23 +93,23 @@ func TestExcludeRealm_Tables(t *testing.T) {
 }
 
 func TestExcludeRealm_Selector(t *testing.T) {
-	r := schema.NewRealm(
-		schema.New("s1").
+	r := NewRealm(
+		New("s1").
 			AddTables(
-				schema.NewTable("t1"),
+				NewTable("t1"),
 			).
 			AddViews(
-				schema.NewView("v1", "SELECT * FROM t1"),
+				NewView("v1", "SELECT * FROM t1"),
 			).
 			AddFuncs(
-				&schema.Func{Name: "f1"},
-				&schema.Func{Name: "f2"},
-				&schema.Func{Name: "f3"},
+				&Func{Name: "f1"},
+				&Func{Name: "f2"},
+				&Func{Name: "f3"},
 			).
 			AddProcs(
-				&schema.Proc{Name: "p1"},
-				&schema.Proc{Name: "p2"},
-				&schema.Proc{Name: "p3"},
+				&Proc{Name: "p1"},
+				&Proc{Name: "p2"},
+				&Proc{Name: "p3"},
 			),
 	)
 	r, err := ExcludeRealm(r, []string{"*.*[type=table]"})
@@ -135,9 +133,9 @@ func TestExcludeRealm_Selector(t *testing.T) {
 	require.Empty(t, r.Schemas[0].Procs)
 
 	r.Schemas[0].AddTables(
-		schema.NewTable("t1"),
-		schema.NewTable("t2"),
-		schema.NewTable("t3"),
+		NewTable("t1"),
+		NewTable("t2"),
+		NewTable("t3"),
 	)
 	r, err = ExcludeRealm(r, []string{"*.*[type=view]"})
 	require.NoError(t, err)
@@ -149,7 +147,7 @@ func TestExcludeRealm_Selector(t *testing.T) {
 	require.Len(t, r.Schemas[0].Tables, 1)
 
 	r.Schemas[0].AddViews(
-		schema.NewView("v1", "SELECT * FROM t1"),
+		NewView("v1", "SELECT * FROM t1"),
 	)
 	r, err = ExcludeRealm(r, []string{"*.*[type=view|table]"})
 	require.NoError(t, err)
@@ -158,10 +156,10 @@ func TestExcludeRealm_Selector(t *testing.T) {
 
 	r.Schemas[0].
 		AddTables(
-			schema.NewTable("t1").AddColumns(schema.NewColumn("c1")).AddIndexes(schema.NewIndex("i1")).AddChecks(schema.NewCheck().SetName("k1")),
+			NewTable("t1").AddColumns(NewColumn("c1")).AddIndexes(NewIndex("i1")).AddChecks(NewCheck().SetName("k1")),
 		).
 		AddViews(
-			schema.NewView("v1", "SELECT * FROM t1").AddColumns(schema.NewColumn("c1")),
+			NewView("v1", "SELECT * FROM t1").AddColumns(NewColumn("c1")),
 		)
 	r, err = ExcludeRealm(r, []string{"*.*[type=table].*1[type=column|check]"})
 	require.NoError(t, err)
@@ -177,9 +175,9 @@ func TestExcludeRealm_Selector(t *testing.T) {
 }
 
 func TestExcludeRealm_Checks(t *testing.T) {
-	r := schema.NewRealm(
-		schema.New("s1").AddTables(
-			schema.NewTable("t1").AddChecks(schema.NewCheck().SetName("c1")),
+	r := NewRealm(
+		New("s1").AddTables(
+			NewTable("t1").AddChecks(NewCheck().SetName("c1")),
 		),
 	)
 	r, err := ExcludeRealm(r, []string{"s1.t1.c1"})
@@ -188,40 +186,40 @@ func TestExcludeRealm_Checks(t *testing.T) {
 }
 
 func TestExcludeRealm_Columns(t *testing.T) {
-	r := schema.NewRealm(
-		schema.New("s1").AddTables(
-			func() *schema.Table {
-				t := schema.NewTable("t1").AddColumns(schema.NewColumn("c1"))
-				t.AddIndexes(schema.NewIndex("i1").AddColumns(t.Columns[0]))
+	r := NewRealm(
+		New("s1").AddTables(
+			func() *Table {
+				t := NewTable("t1").AddColumns(NewColumn("c1"))
+				t.AddIndexes(NewIndex("i1").AddColumns(t.Columns[0]))
 				return t
 			}(),
 		),
-		schema.New("s2").AddTables(
-			func() *schema.Table {
-				t := schema.NewTable("t1").AddColumns(schema.NewColumn("c1"))
-				t.AddIndexes(schema.NewIndex("i1").AddColumns(t.Columns[0]))
+		New("s2").AddTables(
+			func() *Table {
+				t := NewTable("t1").AddColumns(NewColumn("c1"))
+				t.AddIndexes(NewIndex("i1").AddColumns(t.Columns[0]))
 				return t
 			}(),
-			func() *schema.Table {
-				t := schema.NewTable("t2").AddColumns(schema.NewColumn("c1"), schema.NewColumn("c2"))
-				t.AddIndexes(schema.NewIndex("i1").AddColumns(t.Columns[0]), schema.NewIndex("i2").AddColumns(t.Columns[1]))
+			func() *Table {
+				t := NewTable("t2").AddColumns(NewColumn("c1"), NewColumn("c2"))
+				t.AddIndexes(NewIndex("i1").AddColumns(t.Columns[0]), NewIndex("i2").AddColumns(t.Columns[1]))
 				return t
 			}(),
 		),
-		schema.New("s3").AddTables(
-			func() *schema.Table {
-				t := schema.NewTable("t1").AddColumns(schema.NewColumn("c1"))
-				t.AddIndexes(schema.NewIndex("i1").AddColumns(t.Columns[0]))
+		New("s3").AddTables(
+			func() *Table {
+				t := NewTable("t1").AddColumns(NewColumn("c1"))
+				t.AddIndexes(NewIndex("i1").AddColumns(t.Columns[0]))
 				return t
 			}(),
-			func() *schema.Table {
-				t := schema.NewTable("t2").AddColumns(schema.NewColumn("c1"), schema.NewColumn("c2"))
-				t.AddIndexes(schema.NewIndex("i1").AddColumns(t.Columns[0]), schema.NewIndex("i2").AddColumns(t.Columns[1]))
+			func() *Table {
+				t := NewTable("t2").AddColumns(NewColumn("c1"), NewColumn("c2"))
+				t.AddIndexes(NewIndex("i1").AddColumns(t.Columns[0]), NewIndex("i2").AddColumns(t.Columns[1]))
 				return t
 			}(),
-			func() *schema.Table {
-				t := schema.NewTable("t3").AddColumns(schema.NewColumn("c1"), schema.NewColumn("c2"), schema.NewColumn("c3"))
-				t.AddIndexes(schema.NewIndex("i1").AddColumns(t.Columns[0]), schema.NewIndex("i2").AddColumns(t.Columns[1]))
+			func() *Table {
+				t := NewTable("t3").AddColumns(NewColumn("c1"), NewColumn("c2"), NewColumn("c3"))
+				t.AddIndexes(NewIndex("i1").AddColumns(t.Columns[0]), NewIndex("i2").AddColumns(t.Columns[1]))
 				return t
 			}(),
 		),
@@ -277,10 +275,10 @@ func TestExcludeRealm_Columns(t *testing.T) {
 }
 
 func TestExcludeSchema(t *testing.T) {
-	r := schema.NewRealm(
-		schema.New("s1").AddTables(
-			schema.NewTable("t1"),
-			schema.NewTable("t2"),
+	r := NewRealm(
+		New("s1").AddTables(
+			NewTable("t1"),
+			NewTable("t2"),
 		),
 	)
 	_, err := ExcludeSchema(r.Schemas[0], []string{"t2"})