Skip to content

Commit

Permalink
sql: fix the match type reporting in information_schema.referential_c…
Browse files Browse the repository at this point in the history
…onstraints

This is the info_schema complement to cockroachdb#35052.

Release note (bug fix): CockroachDB now properly reports the composite
foreign key match type in `information_schema.referential_constraints`.
  • Loading branch information
knz committed Mar 9, 2019
1 parent efc57fa commit b7fe419
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
15 changes: 10 additions & 5 deletions pkg/sql/information_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -559,9 +559,11 @@ var (
matchOptionPartial = tree.NewDString("PARTIAL")
matchOptionNone = tree.NewDString("NONE")

// Avoid unused warning for constants.
_ = matchOptionPartial
_ = matchOptionNone
matchOptionMap = map[sqlbase.ForeignKeyReference_Match]tree.Datum{
sqlbase.ForeignKeyReference_SIMPLE: matchOptionNone,
sqlbase.ForeignKeyReference_FULL: matchOptionFull,
sqlbase.ForeignKeyReference_PARTIAL: matchOptionPartial,
}

refConstraintRuleNoAction = tree.NewDString("NO ACTION")
refConstraintRuleRestrict = tree.NewDString("RESTRICT")
Expand Down Expand Up @@ -627,15 +629,18 @@ CREATE TABLE information_schema.referential_constraints (
if err != nil {
return err
}

var matchType tree.Datum = tree.DNull
if r, ok := matchOptionMap[fk.Match]; ok {
matchType = r
}
return addRow(
dbNameStr, // constraint_catalog
scNameStr, // constraint_schema
tree.NewDString(fk.Name), // constraint_name
dbNameStr, // unique_constraint_catalog
scNameStr, // unique_constraint_schema
tree.NewDString(refIndex.Name), // unique_constraint_name
matchOptionFull, // match_option
matchType, // match_option
dStringForFKAction(fk.OnUpdate), // update_rule
dStringForFKAction(fk.OnDelete), // delete_rule
tbNameStr, // table_name
Expand Down
32 changes: 30 additions & 2 deletions pkg/sql/logictest/testdata/logic_test/information_schema
Original file line number Diff line number Diff line change
Expand Up @@ -997,8 +997,8 @@ query TTTTTTTTTTT colnames
SELECT * FROM information_schema.referential_constraints WHERE constraint_schema = 'public' ORDER BY TABLE_NAME, CONSTRAINT_NAME
----
constraint_catalog constraint_schema constraint_name unique_constraint_catalog unique_constraint_schema unique_constraint_name match_option update_rule delete_rule table_name referenced_table_name
constraint_column public fk constraint_column public t1_a_key FULL NO ACTION RESTRICT t2 t1
constraint_column public fk2 constraint_column public index_key FULL CASCADE NO ACTION t3 t1
constraint_column public fk constraint_column public t1_a_key NONE NO ACTION RESTRICT t2 t1
constraint_column public fk2 constraint_column public index_key NONE CASCADE NO ACTION t3 t1

statement ok
DROP DATABASE constraint_column CASCADE
Expand Down Expand Up @@ -1961,3 +1961,31 @@ role_name
testuser

user root

subtest fk_match_type

statement ok
CREATE DATABASE dfk; SET database=dfk

statement ok
CREATE TABLE v(x INT, y INT, UNIQUE (x,y))

statement ok
CREATE TABLE w(
a INT, b INT, c INT, d INT,
FOREIGN KEY (a,b) REFERENCES v(x,y) MATCH FULL,
FOREIGN KEY (c,d) REFERENCES v(x,y) MATCH SIMPLE
);

query TTTT
SELECT constraint_name, table_name, referenced_table_name, match_option
FROM information_schema.referential_constraints
----
fk_a_ref_v w v FULL
fk_c_ref_v w v NONE

statement ok
SET database = test

statement ok
DROP DATABASE dfk CASCADE

0 comments on commit b7fe419

Please sign in to comment.