Repository: ariga/atlas
Pull request #2810 (2024-05-29 09:15:52)

sql/internal/sqlx: skip (some) inspect for excluded objects


Patch:
diff --git a/sql/internal/sqlx/sqlx.go b/sql/internal/sqlx/sqlx.go
index 8f4ac263..cedec817 100644
--- a/sql/internal/sqlx/sqlx.go
+++ b/sql/internal/sqlx/sqlx.go
@@ -12,6 +12,7 @@ import (
 	"fmt"
 	"io"
 	"reflect"
+	"slices"
 	"strconv"
 	"strings"
 	"unicode"
@@ -223,20 +224,27 @@ func ValuesEqual(v1, v2 []string) bool {
 
 // ModeInspectSchema returns the InspectMode or its default.
 func ModeInspectSchema(o *schema.InspectOptions) schema.InspectMode {
-	if o == nil || o.Mode == 0 {
-		return schema.InspectSchemas | schema.InspectTables | schema.InspectViews | schema.InspectFuncs |
-			schema.InspectTypes | schema.InspectObjects | schema.InspectTriggers
+	if o != nil && o.Mode != 0 {
+		return o.Mode
 	}
-	return o.Mode
+	return modeSchemaOrAll(V(o).Exclude, "*")
 }
 
 // ModeInspectRealm returns the InspectMode or its default.
 func ModeInspectRealm(o *schema.InspectRealmOption) schema.InspectMode {
-	if o == nil || o.Mode == 0 {
-		return schema.InspectSchemas | schema.InspectTables | schema.InspectViews | schema.InspectFuncs |
-			schema.InspectTypes | schema.InspectObjects | schema.InspectTriggers
+	if o != nil && o.Mode != 0 {
+		return o.Mode
 	}
-	return o.Mode
+	return modeSchemaOrAll(V(o).Exclude, "*.*")
+}
+
+// modeSchemaOrAll returns the inspect mode based on the exclude patterns.
+func modeSchemaOrAll(exclude []string, match string) schema.InspectMode {
+	if slices.Contains(exclude, match) {
+		return schema.InspectSchemas
+	}
+	return schema.InspectSchemas | schema.InspectTables | schema.InspectViews | schema.InspectFuncs |
+		schema.InspectTypes | schema.InspectObjects | schema.InspectTriggers
 }
 
 // A Builder provides a syntactic sugar API for writing SQL statements.


Test patch:
diff --git a/sql/internal/sqlx/sqlx_test.go b/sql/internal/sqlx/sqlx_test.go
index c7fac7fc..d8f0073b 100644
--- a/sql/internal/sqlx/sqlx_test.go
+++ b/sql/internal/sqlx/sqlx_test.go
@@ -26,6 +26,17 @@ func TestModeInspectRealm(t *testing.T) {
 	m = ModeInspectRealm(&schema.InspectRealmOption{Mode: schema.InspectSchemas})
 	require.True(t, m.Is(schema.InspectSchemas))
 	require.False(t, m.Is(schema.InspectTables))
+
+	m = ModeInspectRealm(&schema.InspectRealmOption{Exclude: []string{"*.*"}})
+	require.Equal(t, schema.InspectSchemas, m)
+	m = ModeInspectRealm(&schema.InspectRealmOption{Exclude: []string{"*"}})
+	require.NotEqual(t, schema.InspectSchemas, m)
+	require.True(t, m.Is(schema.InspectSchemas))
+	require.True(t, m.Is(schema.InspectTables))
+	m = ModeInspectRealm(&schema.InspectRealmOption{Mode: schema.InspectFuncs, Exclude: []string{"*.*"}})
+	require.NotEqual(t, schema.InspectSchemas, m)
+	require.True(t, m.Is(schema.InspectFuncs))
+	require.False(t, m.Is(schema.InspectTables))
 }
 
 func TestModeInspectSchema(t *testing.T) {
@@ -40,6 +51,17 @@ func TestModeInspectSchema(t *testing.T) {
 	m = ModeInspectSchema(&schema.InspectOptions{Mode: schema.InspectSchemas})
 	require.True(t, m.Is(schema.InspectSchemas))
 	require.False(t, m.Is(schema.InspectTables))
+
+	m = ModeInspectSchema(&schema.InspectOptions{Exclude: []string{"*"}})
+	require.Equal(t, schema.InspectSchemas, m)
+	m = ModeInspectSchema(&schema.InspectOptions{Exclude: []string{"*.*"}})
+	require.NotEqual(t, schema.InspectSchemas, m)
+	require.True(t, m.Is(schema.InspectSchemas))
+	require.True(t, m.Is(schema.InspectTables))
+	m = ModeInspectSchema(&schema.InspectOptions{Mode: schema.InspectFuncs, Exclude: []string{"*"}})
+	require.NotEqual(t, schema.InspectSchemas, m)
+	require.True(t, m.Is(schema.InspectFuncs))
+	require.False(t, m.Is(schema.InspectTables))
 }
 
 func TestBuilder(t *testing.T) {