Skip to content
Merged
80 changes: 80 additions & 0 deletions cmd/plan/external_db_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"path/filepath"
"testing"

"github.com/pgplex/pgschema/cmd/util"
"github.com/pgplex/pgschema/internal/postgres"
"github.com/pgplex/pgschema/testutil"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -282,3 +283,82 @@ func TestExternalDatabase_SchemaIsolation(t *testing.T) {
schema2 := extDB2.GetSchemaName()
assert.NotEqual(t, schema1, schema2, "temporary schemas should have unique names")
}

// TestExternalDatabase_ExtensionOperatorInPublicPlansClean reproduces issue #697:
// a view in the public schema that uses an operator owned by an extension
// installed in public (hstore's ->) must plan as a no-op against the database
// it was dumped from when the desired state is built on an external plan
// database.
func TestExternalDatabase_ExtensionOperatorInPublicPlansClean(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
}

targetDB := testutil.SetupPostgres(t)
defer targetDB.Stop()
planDB := testutil.SetupPostgres(t)
defer planDB.Stop()

targetConn, targetHost, targetPort, targetDatabase, targetUser, targetPassword := testutil.ConnectToPostgres(t, targetDB)
defer targetConn.Close()
// Operators pgschema does not manage, present on both databases:
// - public.+(integer, integer) duplicates pg_catalog's and is reachable
// only qualified: a fully shadowed name, kept as OPERATOR(public.+).
// - public.->(jsonb, text) duplicates pg_catalog's too, but hstore's
// ->(hstore, text) shares the name and is not shadowed, so the name
// is not exempted and the hstore views below must still plan clean.
// The operator functions live outside the managed schema so the plan
// does not see them as unmanaged objects to drop.
setupSQL := `
CREATE EXTENSION hstore;
CREATE SCHEMA ops;
CREATE FUNCTION ops.int_add(integer, integer) RETURNS integer LANGUAGE sql IMMUTABLE AS 'SELECT $1 + $2';
CREATE OPERATOR public.+ (LEFTARG = integer, RIGHTARG = integer, FUNCTION = ops.int_add);
CREATE FUNCTION ops.jsonb_arrow(jsonb, text) RETURNS jsonb LANGUAGE sql IMMUTABLE AS 'SELECT $1 -> $2';
CREATE OPERATOR public.-> (LEFTARG = jsonb, RIGHTARG = text, FUNCTION = ops.jsonb_arrow);
`
// Every expression context that can carry an operator: column default and
// generated expression (deparsed under search_path = pg_catalog), CHECK
// constraint, index expression and predicate, routine parameter default,
// view and materialized view. The quoted alias must survive untouched.
schemaSQL := `
CREATE TABLE t (
id integer PRIMARY KEY,
h hstore,
d text DEFAULT ('a=>1'::hstore -> 'a'),
g text GENERATED ALWAYS AS (h -> 'a') STORED,
CONSTRAINT c CHECK ((h ? 'k') OR h IS NULL)
);
CREATE INDEX i ON t ((h -> 'b')) WHERE (h ? 'b');
CREATE FUNCTION pick(x text DEFAULT ('a=>1'::hstore -> 'a')) RETURNS text LANGUAGE sql AS 'SELECT x';
CREATE VIEW v AS SELECT id, h -> 'a' AS ha, h ? 'k' AS hk, 1 AS "OPERATOR(public.->)" FROM t;
CREATE MATERIALIZED VIEW mv AS SELECT id, h -> 'a' AS ha FROM t;
`
_, err := targetConn.Exec(setupSQL + schemaSQL)
require.NoError(t, err)

planConn, planHost, planPort, planDatabase, planUser, planPassword := testutil.ConnectToPostgres(t, planDB)
defer planConn.Close()
_, err = planConn.Exec(setupSQL)
require.NoError(t, err)

targetIR, err := util.GetIRFromDatabase(targetHost, targetPort, targetDatabase, targetUser, targetPassword, "disable", "public", "pgschema-test", nil, "")
require.NoError(t, err)
require.Equal(t, []string{"+"}, targetIR.Metadata.ShadowedOperators, "only a name whose every overload is shadowed is exempted")

file := filepath.Join(t.TempDir(), "schema.sql")
require.NoError(t, os.WriteFile(file, []byte(schemaSQL), 0644))

config := &PlanConfig{
Host: targetHost, Port: targetPort, DB: targetDatabase, User: targetUser, Password: targetPassword,
Schema: "public", File: file, ApplicationName: "pgschema-test",
PlanDBHost: planHost, PlanDBPort: planPort, PlanDBDatabase: planDatabase, PlanDBUser: planUser, PlanDBPassword: planPassword,
}
provider, err := CreateDesiredStateProvider(config)
require.NoError(t, err)
defer provider.Stop()

p, err := GeneratePlan(config, provider)
require.NoError(t, err)
require.False(t, p.HasAnyChanges(), "view using an extension operator must not be re-planned, got:\n%s", p.HumanColored(false))
}
132 changes: 115 additions & 17 deletions cmd/plan/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,11 @@ func normalizeSchemaNames(irData *ir.IR, fromSchema, toSchema string) {
// contain "toSchema.func_name(" or "::toSchema.type" which are redundant same-schema
// qualifiers. The initial normalizeIR (run by the inspector) couldn't strip these
// because it ran with the temp schema name, not the target schema. See issue #283.
stripQualifiers := newSameSchemaQualifierStripper(toSchema)
shadowedOperators := make(map[string]bool, len(irData.Metadata.ShadowedOperators))
for _, name := range irData.Metadata.ShadowedOperators {
shadowedOperators[name] = true
}
stripQualifiers := newSameSchemaQualifierStripper(toSchema, shadowedOperators)

// Normalize schema names in Schemas map
if schema, exists := irData.Schemas[fromSchema]; exists {
Expand Down Expand Up @@ -676,6 +680,11 @@ func normalizeSchemaNames(irData *ir.IR, fromSchema, toSchema string) {
}
for _, param := range fn.Parameters {
param.DataType = replaceString(param.DataType)
// Parameter defaults are deparsed like column defaults and
// compared verbatim; normalize them the same way (#697).
if param.DefaultValue != nil {
*param.DefaultValue = stripQualifiers(replaceString(*param.DefaultValue))
}
}
// Normalize function dependencies for topological sorting
for i := range fn.Dependencies {
Expand All @@ -692,6 +701,9 @@ func normalizeSchemaNames(irData *ir.IR, fromSchema, toSchema string) {
proc.Definition = replaceString(proc.Definition)
for _, param := range proc.Parameters {
param.DataType = replaceString(param.DataType)
if param.DefaultValue != nil {
*param.DefaultValue = stripQualifiers(replaceString(*param.DefaultValue))
}
}
}

Expand Down Expand Up @@ -841,44 +853,130 @@ func newSchemaStringReplacer(fromSchema, toSchema string) func(string) string {
// with the target schema, expressions may contain "schema.func_name(" or "::schema.type"
// where the qualifier matches the object's own schema. These are redundant and must be
// stripped to match how the target database's inspector would produce them. See issue #283.
func newSameSchemaQualifierStripper(schema string) func(string) string {
//
// shadowedOperators names the operators that must keep their qualifier because
// an unqualified reference would resolve to a pg_catalog operator instead (see
// ir.Metadata.ShadowedOperators).
func newSameSchemaQualifierStripper(schema string, shadowedOperators map[string]bool) func(string) string {
if schema == "" {
return func(s string) string { return s }
}
prefix := schema + "."
funcPattern := regexp.MustCompile(regexp.QuoteMeta(prefix) + `([a-zA-Z_][a-zA-Z0-9_$]*)\(`)
typePattern := regexp.MustCompile(`::` + regexp.QuoteMeta(prefix))
// The deparsers render the schema bare or in quote_ident form ("My Schema").
prefixes := []string{schema + "."}
if quoted := ir.QuoteIdentifier(schema); quoted != schema {
prefixes = append(prefixes, quoted+".")
}
quotedPrefixes := make([]string, len(prefixes))
for i, p := range prefixes {
quotedPrefixes[i] = regexp.QuoteMeta(p)
}
prefixAlternatives := `(?:` + strings.Join(quotedPrefixes, "|") + `)`
// The object name after the qualifier is bare or quote_ident form too.
funcPattern := regexp.MustCompile(prefixAlternatives + `([a-zA-Z_][a-zA-Z0-9_$]*|"(?:[^"]|"")+")\(`)
typePattern := regexp.MustCompile(`::` + prefixAlternatives)
// OPERATOR(schema.op): ruleutils qualifies an operator whenever its schema
// is not on the deparsing session's search_path. The plan database is
// inspected with only the temporary schema on the path, so an operator in
// the target schema (e.g. hstore's -> installed in public) comes out as
// OPERATOR(public.->) while the target side prints the bare operator.
// Operator names use only the characters PostgreSQL allows (issue #697).
operatorPattern := regexp.MustCompile(`OPERATOR\(` + prefixAlternatives + `([` + regexp.QuoteMeta(ir.OperatorNameChars) + `]+)\)`)
replaceQualifiers := func(s string) string {
s = funcPattern.ReplaceAllString(s, `${1}(`)
s = operatorPattern.ReplaceAllStringFunc(s, func(match string) string {
// match is "OPERATOR(" + prefix + name + ")"; the name is the
// trailing run of operator characters.
nameStart := len(match) - 1
for nameStart > 0 && strings.IndexByte(ir.OperatorNameChars, match[nameStart-1]) >= 0 {
nameStart--
}
name := match[nameStart : len(match)-1]
if shadowedOperators[name] {
return match
}
return name
})
return typePattern.ReplaceAllString(s, "::")
}
containsPrefix := func(s string) bool {
for _, p := range prefixes {
if strings.Contains(s, p) {
return true
}
}
return false
}
// quotedPrefixLen returns the length of the quote_ident-form schema prefix
// starting at s[i], or 0 when none starts there.
quotedPrefixLen := func(s string, i int) int {
for _, p := range prefixes[1:] {
if strings.HasPrefix(s[i:], p) {
return len(p)
}
}
return 0
}
// endsWithPrefix reports whether s[:i] ends with a schema qualifier, so a
// quoted identifier starting at s[i] is the qualified object's own name.
endsWithPrefix := func(s string, i int) bool {
for _, p := range prefixes {
if strings.HasSuffix(s[:i], p) {
return true
}
}
return false
}
// closingQuote returns the index of the quote that closes the literal or
// identifier opened at s[i]; a doubled quote inside is an escape.
closingQuote := func(s string, i int) int {
quote := s[i]
for i++; i < len(s); i++ {
if s[i] != quote {
continue
}
if i+1 < len(s) && s[i+1] == quote {
i++
continue
}
break
}
return min(i, len(s)-1)
}
return func(s string) string {
if s == "" || !strings.Contains(s, prefix) {
if s == "" || !containsPrefix(s) {
return s
}

// Replace only outside single-quoted literals and double-quoted
// identifiers; a doubled quote inside either is an escape. Two quoted
// tokens are not opaque: the quoted schema itself ("My Schema".) and
// a quoted object name right after a qualifier (public."My Func"().
// Both stay in the replaceable segment so the patterns can see the
// whole qualified reference.
var result strings.Builder
result.Grow(len(s))
segmentStart := 0
for i := 0; i < len(s); i++ {
if s[i] != '\'' {
quote := s[i]
if quote != '\'' && quote != '"' {
continue
}

result.WriteString(replaceQualifiers(s[segmentStart:i]))
literalStart := i
for i++; i < len(s); i++ {
if s[i] != '\'' {
if quote == '"' {
if n := quotedPrefixLen(s, i); n > 0 {
i += n - 1
continue
}
if i+1 < len(s) && s[i+1] == '\'' {
i++
if endsWithPrefix(s, i) {
i = closingQuote(s, i)
continue
}
break
}
result.WriteString(s[literalStart:min(i+1, len(s))])
segmentStart = min(i+1, len(s))

result.WriteString(replaceQualifiers(s[segmentStart:i]))
literalStart := i
i = closingQuote(s, i)
result.WriteString(s[literalStart : i+1])
segmentStart = i + 1
}
result.WriteString(replaceQualifiers(s[segmentStart:]))
return result.String()
Expand Down
96 changes: 96 additions & 0 deletions cmd/plan/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,49 @@ func TestNormalizeSchemaNames_StripsSameSchemaQualifiersFromViewDefinitions(t *t
}
}

func TestNormalizeSchemaNames_StripsQuotedSchemaQualifiers(t *testing.T) {
// A target schema that needs quoting is rendered as "My Schema". by the
// deparsers; function, cast and operator qualifiers in that form are
// stripped too, while an identifier that merely contains the token stays.
const tempSchema = "pgschema_tmp_20260326_123456_abcd1234"
irData := &ir.IR{
Metadata: ir.Metadata{ShadowedOperators: []string{"+"}},
Schemas: map[string]*ir.Schema{
tempSchema: {
Name: tempSchema,
Views: map[string]*ir.View{
"v": {
Schema: tempSchema,
Name: "v",
Definition: " SELECT \"My Schema\".nlevel(path) AS depth,\n" +
" \"My Schema\".\"Fn Name\"(path) AS named,\n" +
" path::\"My Schema\".ltree AS p,\n" +
" path::\"My Schema\".\"Type Name\" AS typed,\n" +
" h OPERATOR(\"My Schema\".->) 'a'::text AS ha,\n" +
" id OPERATOR(\"My Schema\".+) 1 AS shadowed,\n" +
" 1 AS \"x \"\"My Schema\"\".y(\"\n" +
" FROM t",
},
},
},
},
}

normalizeSchemaNames(irData, tempSchema, "My Schema")

expected := " SELECT nlevel(path) AS depth,\n" +
" \"Fn Name\"(path) AS named,\n" +
" path::ltree AS p,\n" +
" path::\"Type Name\" AS typed,\n" +
" h -> 'a'::text AS ha,\n" +
" id OPERATOR(\"My Schema\".+) 1 AS shadowed,\n" +
" 1 AS \"x \"\"My Schema\"\".y(\"\n" +
" FROM t"
if got := irData.Schemas["My Schema"].Views["v"].Definition; got != expected {
t.Fatalf("expected quoted-schema qualifiers stripped:\n%s\ngot:\n%s", expected, got)
}
}

func TestNormalizeSchemaNames_PreservesInheritedPartitionDefault(t *testing.T) {
const tempSchema = "pgschema_tmp_partition595"
childDefault, parentDefault := "public.member_default()", "public.member_default()"
Expand Down Expand Up @@ -324,3 +367,56 @@ func TestNormalizeSchemaNames_IndexAttachment(t *testing.T) {
t.Fatalf("Incorrect partition-table metadata normalization: %+v", children)
}
}

func TestNormalizeSchemaNames_StripsSameSchemaOperatorQualifiers(t *testing.T) {
// With an external plan database and target schema public, the plan side
// deparses extension operators living in public as OPERATOR(public.->),
// while the target side (inspected with public on its search_path) prints
// the bare operator. The same-schema qualifier must be stripped so the two
// definitions agree (issue #697).
const tempSchema = "pgschema_tmp_20260326_123456_abcd1234"
irData := &ir.IR{
// A custom public.+(integer, integer) duplicates pg_catalog's; ruleutils
// always qualifies it, and so must the stripped definition.
Metadata: ir.Metadata{ShadowedOperators: []string{"+"}},
Schemas: map[string]*ir.Schema{
tempSchema: {
Name: tempSchema,
Views: map[string]*ir.View{
"v": {
Schema: tempSchema,
Name: "v",
Definition: " SELECT id,\n" +
" h OPERATOR(public.->) 'a'::text AS ha,\n" +
" h OPERATOR(public.?) 'k'::text AS hk,\n" +
" t OPERATOR(public.@>) 'x'::public.ltree AS anc,\n" +
" id OPERATOR(public.+) 1 AS shadowed,\n" +
" 'OPERATOR(public.->)'::text AS literal,\n" +
" 1 AS \"OPERATOR(public.->)\",\n" +
" \"public.nlevel(\" AS \"it''s \"\"public.x(\"\"\",\n" +
" public.\"Fn Name\"(id) AS named, id::public.\"Type Name\" AS typed,\n" +
" j -> 'a'::text AS ja\n" +
" FROM t",
},
},
},
},
}

normalizeSchemaNames(irData, tempSchema, "public")

expected := " SELECT id,\n" +
" h -> 'a'::text AS ha,\n" +
" h ? 'k'::text AS hk,\n" +
" t @> 'x'::ltree AS anc,\n" +
" id OPERATOR(public.+) 1 AS shadowed,\n" +
" 'OPERATOR(public.->)'::text AS literal,\n" +
" 1 AS \"OPERATOR(public.->)\",\n" +
" \"public.nlevel(\" AS \"it''s \"\"public.x(\"\"\",\n" +
" \"Fn Name\"(id) AS named, id::\"Type Name\" AS typed,\n" +
" j -> 'a'::text AS ja\n" +
" FROM t"
if got := irData.Schemas["public"].Views["v"].Definition; got != expected {
t.Fatalf("expected operator qualifiers stripped:\n%s\ngot:\n%s", expected, got)
}
}
Loading
Loading