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