@@ -24,7 +24,7 @@ import (
2424 "github.com/dolthub/vitess/go/mysql"
2525)
2626
27- var UpdateTests = []WriteQueryTest {
27+ var UpdateWriteQueryTests = []WriteQueryTest {
2828 {
2929 WriteQuery : "UPDATE mytable SET s = 'updated';" ,
3030 ExpectedWriteResult : []sql.Row {{NewUpdateResult (3 , 3 )}},
@@ -470,6 +470,106 @@ var UpdateTests = []WriteQueryTest{
470470 },
471471}
472472
473+ var UpdateScriptTests = []ScriptTest {
474+ {
475+ Name : "UPDATE join – single table, with FK constraint" ,
476+ SetUpScript : []string {
477+ "CREATE TABLE customers (id INT PRIMARY KEY, name TEXT);" ,
478+ "CREATE TABLE orders (id INT PRIMARY KEY, customer_id INT, amount INT, FOREIGN KEY (customer_id) REFERENCES customers(id));" ,
479+ "INSERT INTO customers VALUES (1, 'Alice'), (2, 'Bob');" ,
480+ "INSERT INTO orders VALUES (101, 1, 50), (102, 2, 75);" ,
481+ },
482+ Assertions : []ScriptTestAssertion {
483+ {
484+ // TODO: Foreign key constraints are not honored for UDPATE ... JOIN statements
485+ Skip : true ,
486+ Query : "UPDATE orders o JOIN customers c ON o.customer_id = c.id SET o.customer_id = 123 where o.customer_id != 1;" ,
487+ ExpectedErr : sql .ErrCheckConstraintViolated ,
488+ },
489+ {
490+ Query : "SELECT * FROM orders;" ,
491+ Expected : []sql.Row {
492+ {101 , 1 , 50 }, {102 , 2 , 75 },
493+ },
494+ },
495+ },
496+ },
497+ {
498+ Name : "UPDATE join – multiple tables, with FK constraint" ,
499+ SetUpScript : []string {
500+ "CREATE TABLE parent1 (id INT PRIMARY KEY);" ,
501+ "CREATE TABLE parent2 (id INT PRIMARY KEY);" ,
502+ "CREATE TABLE child1 (id INT PRIMARY KEY, p1_id INT, FOREIGN KEY (p1_id) REFERENCES parent1(id));" ,
503+ "CREATE TABLE child2 (id INT PRIMARY KEY, p2_id INT, FOREIGN KEY (p2_id) REFERENCES parent2(id));" ,
504+ "INSERT INTO parent1 VALUES (1), (3);" ,
505+ "INSERT INTO parent2 VALUES (1), (3);" ,
506+ "INSERT INTO child1 VALUES (10, 1);" ,
507+ "INSERT INTO child2 VALUES (20, 1);" ,
508+ },
509+ Assertions : []ScriptTestAssertion {
510+ {
511+ // TODO: Foreign key constraints are not honored for UDPATE ... JOIN statements
512+ Skip : true ,
513+ Query : `UPDATE child1 c1
514+ JOIN child2 c2 ON c1.id = 10 AND c2.id = 20
515+ SET c1.p1_id = 999, c2.p2_id = 3;` ,
516+ ExpectedErr : sql .ErrForeignKeyChildViolation ,
517+ },
518+ {
519+ // TODO: Foreign key constraints are not honored for UDPATE ... JOIN statements
520+ Skip : true ,
521+ Query : `UPDATE child1 c1
522+ JOIN child2 c2 ON c1.id = 10 AND c2.id = 20
523+ SET c1.p1_id = 3, c2.p2_id = 999;` ,
524+ ExpectedErr : sql .ErrForeignKeyChildViolation ,
525+ },
526+ {
527+ Query : "SELECT * FROM child1;" ,
528+ Expected : []sql.Row {{10 , 1 }},
529+ },
530+ {
531+ Query : "SELECT * FROM child2;" ,
532+ Expected : []sql.Row {{20 , 1 }},
533+ },
534+ },
535+ },
536+ {
537+ Name : "UPDATE join – multiple tables, with trigger" ,
538+ SetUpScript : []string {
539+ "CREATE TABLE a (id INT PRIMARY KEY, x INT);" ,
540+ "CREATE TABLE b (id INT PRIMARY KEY, y INT);" ,
541+ "CREATE TABLE logbook (entry TEXT);" ,
542+ `CREATE TRIGGER trig_a AFTER UPDATE ON a FOR EACH ROW
543+ BEGIN
544+ INSERT INTO logbook VALUES ('a updated');
545+ END;` ,
546+ `CREATE TRIGGER trig_b AFTER UPDATE ON b FOR EACH ROW
547+ BEGIN
548+ INSERT INTO logbook VALUES ('b updated');
549+ END;` ,
550+ "INSERT INTO a VALUES (5, 100);" ,
551+ "INSERT INTO b VALUES (6, 200);" ,
552+ },
553+ Assertions : []ScriptTestAssertion {
554+ {
555+ Query : `UPDATE a
556+ JOIN b ON a.id = 5 AND b.id = 6
557+ SET a.x = 101, b.y = 201;` ,
558+ },
559+ {
560+ // TODO: UPDATE ... JOIN does not properly apply triggers when multiple tables are being updated,
561+ // and will currently only apply triggers from one of the tables.
562+ Skip : true ,
563+ Query : "SELECT * FROM logbook ORDER BY entry;" ,
564+ Expected : []sql.Row {
565+ {"a updated" },
566+ {"b updated" },
567+ },
568+ },
569+ },
570+ },
571+ }
572+
473573var SpatialUpdateTests = []WriteQueryTest {
474574 {
475575 WriteQuery : "UPDATE point_table SET p = point(123.456,789);" ,
0 commit comments