# This test verifies that we're populating the Constraint field of an error
# generated by a foreign key constraint violation.

# Prepare the environment.
send
Query {"String": "DROP TABLE IF EXISTS child, parent"}
----

until ignore=NoticeResponse
ReadyForQuery
----
{"Type":"CommandComplete","CommandTag":"DROP TABLE"}
{"Type":"ReadyForQuery","TxStatus":"I"}

send
Query {"String": "CREATE TABLE parent (p INT PRIMARY KEY)"}
----

until
ReadyForQuery
----
{"Type":"CommandComplete","CommandTag":"CREATE TABLE"}
{"Type":"ReadyForQuery","TxStatus":"I"}

send
Query {"String": "CREATE TABLE child (c INT PRIMARY KEY, p INT)"}
----

until
ReadyForQuery
----
{"Type":"CommandComplete","CommandTag":"CREATE TABLE"}
{"Type":"ReadyForQuery","TxStatus":"I"}

send
Query {"String": "ALTER TABLE child ADD CONSTRAINT fk_p_ref_parent FOREIGN KEY (p) REFERENCES parent(p)"}
----

until
ReadyForQuery
----
{"Type":"CommandComplete","CommandTag":"ALTER TABLE"}
{"Type":"ReadyForQuery","TxStatus":"I"}

send
Query {"String": "INSERT INTO child VALUES (1,1)"}
----

until
ErrorResponse
ReadyForQuery
----
{"Type":"ErrorResponse","Code":"23503","ConstraintName":"fk_p_ref_parent"}
{"Type":"ReadyForQuery","TxStatus":"I"}

send
Query {"String": "ALTER TABLE child DROP CONSTRAINT fk_p_ref_parent"}
----

until
ReadyForQuery
----
{"Type":"CommandComplete","CommandTag":"ALTER TABLE"}
{"Type":"ReadyForQuery","TxStatus":"I"}

# Test with a constraint name that requires quoting.
send
Query {"String": "ALTER TABLE child ADD CONSTRAINT \"foo bar\" FOREIGN KEY (p) REFERENCES parent(p)"}
----

until
ReadyForQuery
----
{"Type":"CommandComplete","CommandTag":"ALTER TABLE"}
{"Type":"ReadyForQuery","TxStatus":"I"}

send
Query {"String": "INSERT INTO child VALUES (1,1)"}
----

until
ErrorResponse
ReadyForQuery
----
{"Type":"ErrorResponse","Code":"23503","ConstraintName":"foo bar"}
{"Type":"ReadyForQuery","TxStatus":"I"}

send
Parse {"Name": "s0", "Query": "select $1,$2,$3", "ParameterOIDs":[1043, 1043, 1043]}
Bind {"DestinationPortal": "p0", "PreparedStatement": "s0", "ParameterFormatCodes": [0], "ResultFormatCodes": [0,1,2,3,4,5], "Parameters": [{"text":"whitebear"}, {"text":"blackbear"}, {"text":"brownbear"}]}
Execute {"Portal": "p0"}
Sync
----

until
ErrorResponse
ReadyForQuery
----
{"Type":"ParseComplete"}
{"Type":"ErrorResponse","Code":"08P01"}
{"Type":"ReadyForQuery","TxStatus":"I"}
