@@ -534,7 +534,7 @@ var UpdateScriptTests = []ScriptTest{
534
534
Name : "UPDATE join – multiple tables, with trigger" ,
535
535
SetUpScript : []string {
536
536
"CREATE TABLE a (id INT PRIMARY KEY, x INT);" ,
537
- "CREATE TABLE b (id INT PRIMARY KEY, y INT);" ,
537
+ "CREATE TABLE b (pk INT PRIMARY KEY, y INT);" ,
538
538
"CREATE TABLE logbook (entry TEXT);" ,
539
539
`CREATE TRIGGER trig_a AFTER UPDATE ON a FOR EACH ROW
540
540
BEGIN
@@ -550,13 +550,10 @@ var UpdateScriptTests = []ScriptTest{
550
550
Assertions : []ScriptTestAssertion {
551
551
{
552
552
Query : `UPDATE a
553
- JOIN b ON a.id = 5 AND b.id = 6
553
+ JOIN b ON a.id = 5 AND b.pk = 6
554
554
SET a.x = 101, b.y = 201;` ,
555
555
},
556
556
{
557
- // TODO: UPDATE ... JOIN does not properly apply triggers when multiple tables are being updated,
558
- // and will currently only apply triggers from one of the tables.
559
- Skip : true ,
560
557
Query : "SELECT * FROM logbook ORDER BY entry;" ,
561
558
Expected : []sql.Row {
562
559
{"a updated" },
@@ -565,6 +562,82 @@ var UpdateScriptTests = []ScriptTest{
565
562
},
566
563
},
567
564
},
565
+ {
566
+ Dialect : "mysql" ,
567
+ Name : "UPDATE join – multiple tables with triggers that reference row values" ,
568
+ SetUpScript : []string {
569
+ "create table customers (id int primary key, name text, tier text)" ,
570
+ "create table orders (order_id int primary key, customer_id int, status text)" ,
571
+ "create table trigger_log (msg text)" ,
572
+ `CREATE TRIGGER after_orders_update after update on orders for each row
573
+ begin
574
+ insert into trigger_log (msg) values(
575
+ concat('Order ', OLD.order_id, ' status changed from ', OLD.status, ' to ', NEW.status));
576
+ end;` ,
577
+ `Create trigger after_customers_update after update on customers for each row
578
+ begin
579
+ insert into trigger_log (msg) values(
580
+ concat('Customer ', OLD.id, ' tier changed from ', OLD.tier, ' to ', NEW.tier));
581
+ end;` ,
582
+ "insert into customers values(1, 'Alice', 'silver'), (2, 'Bob', 'gold');" ,
583
+ "insert into orders values (101, 1, 'pending'), (102, 2, 'pending');" ,
584
+ "update customers c join orders o on c.id = o.customer_id " +
585
+ "set c.tier = 'platinum', o.status = 'shipped' where o.status = 'pending'" ,
586
+ },
587
+ Assertions : []ScriptTestAssertion {
588
+ {
589
+ Query : "SELECT * FROM trigger_log order by msg;" ,
590
+ Expected : []sql.Row {
591
+ {"Customer 1 tier changed from silver to platinum" },
592
+ {"Customer 2 tier changed from gold to platinum" },
593
+ {"Order 101 status changed from pending to shipped" },
594
+ {"Order 102 status changed from pending to shipped" },
595
+ },
596
+ },
597
+ },
598
+ },
599
+ {
600
+ Dialect : "mysql" ,
601
+ Name : "UPDATE join – multiple tables with same column names with triggers" ,
602
+ SetUpScript : []string {
603
+ "create table customers (id int primary key, name text, tier text)" ,
604
+ "create table orders (id int primary key, customer_id int, status text)" ,
605
+ "create table trigger_log (msg text)" ,
606
+ `CREATE TRIGGER after_orders_update after update on orders for each row
607
+ begin
608
+ insert into trigger_log (msg) values(
609
+ concat('Order ', OLD.id, ' status changed from ', OLD.status, ' to ', NEW.status));
610
+ end;` ,
611
+ `Create trigger after_customers_update after update on customers for each row
612
+ begin
613
+ insert into trigger_log (msg) values(
614
+ concat('Customer ', OLD.id, ' tier changed from ', OLD.tier, ' to ', NEW.tier));
615
+ end;` ,
616
+ "insert into customers values(1, 'Alice', 'silver'), (2, 'Bob', 'gold');" ,
617
+ "insert into orders values (101, 1, 'pending'), (102, 2, 'pending');" ,
618
+ },
619
+ Assertions : []ScriptTestAssertion {
620
+ {
621
+ Query : "update customers c join orders o on c.id = o.customer_id " +
622
+ "set c.tier = 'platinum', o.status = 'shipped' where o.status = 'pending'" ,
623
+ // TODO: we shouldn't expect an error once we're able to handle conflicting column names
624
+ // https://github.com/dolthub/dolt/issues/9403
625
+ ExpectedErrStr : "Unable to apply triggers when joined tables have columns with the same name" ,
626
+ },
627
+ {
628
+ // TODO: unskip once we're able to handle conflicting column names
629
+ // https://github.com/dolthub/dolt/issues/9403
630
+ Skip : true ,
631
+ Query : "SELECT * FROM trigger_log order by msg;" ,
632
+ Expected : []sql.Row {
633
+ {"Customer 1 tier changed from silver to platinum" },
634
+ {"Customer 2 tier changed from gold to platinum" },
635
+ {"Order 101 status changed from pending to shipped" },
636
+ {"Order 102 status changed from pending to shipped" },
637
+ },
638
+ },
639
+ },
640
+ },
568
641
}
569
642
570
643
var SpatialUpdateTests = []WriteQueryTest {
0 commit comments