--source include/galera_cluster.inc
--source include/have_debug.inc
--source include/have_debug_sync.inc

--echo #
--echo # test phase with foreign key of varchar type
--echo #
--connection node_1
 CREATE TABLE parent (
      `id` varchar(36) COLLATE utf8_unicode_ci NOT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;


   CREATE TABLE child (
      `id` int NOT NULL,
      `parent_id` varchar(36) COLLATE utf8_unicode_ci DEFAULT NULL,
      PRIMARY KEY (`id`),
      KEY `parent_id` (`parent_id`),
      CONSTRAINT `ipallocations_ibfk_1` FOREIGN KEY (`parent_id`) REFERENCES `parent` (`id`) ON DELETE CASCADE
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;


INSERT INTO parent VALUES ('row one'), ('row two');
INSERT INTO child VALUES (1,'row one'), (2,'row two');

--connection node_2
--let $wait_condition = SELECT COUNT(*) = 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'parent'
--source include/wait_condition.inc
--let $wait_condition = SELECT COUNT(*) = 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'child'
--source include/wait_condition.inc
--let $wait_condition = SELECT COUNT(*) = 2 FROM parent;
--source include/wait_condition.inc
--let $wait_condition = SELECT COUNT(*) = 2 FROM child;
--source include/wait_condition.inc

DELETE FROM parent;

--connection node_1
--let $wait_condition = SELECT COUNT(*) = 0 FROM parent;
--source include/wait_condition.inc
--let $wait_condition = SELECT COUNT(*) = 0 FROM child;
--source include/wait_condition.inc

SELECT * FROM parent;
SELECT * FROM child;

DROP TABLE child;
DROP TABLE parent;

--echo #
--echo # test phase with MM conflict in FK cascade
--echo #

--connection node_1
set wsrep_retry_autocommit=0;
CREATE TABLE parent (
    id INT NOT NULL PRIMARY KEY
) ENGINE=InnoDB;

CREATE TABLE child (
    id INT NOT NULL PRIMARY KEY,
    j int default 0,
    parent_id INT,
    FOREIGN KEY (parent_id)
        REFERENCES parent(id)
        ON DELETE CASCADE
) ENGINE=InnoDB;

INSERT INTO parent VALUES (1);
INSERT INTO child VALUES (1,0,1);

--connection node_2
--let $wait_condition = SELECT COUNT(*) = 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'parent'
--source include/wait_condition.inc
--let $wait_condition = SELECT COUNT(*) = 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'child'
--source include/wait_condition.inc
--let $wait_condition = SELECT COUNT(*) = 1 FROM parent;
--source include/wait_condition.inc
--let $wait_condition = SELECT COUNT(*) = 1 FROM child;
--source include/wait_condition.inc

# block applier before applying
--connect node_1a, 127.0.0.1, root, , test, $NODE_MYPORT_1
SET GLOBAL debug_dbug = "d,sync.wsrep_apply_cb";

--connection node_2
DELETE FROM parent;

--connection node_1a
# wait until applier has reached the sync point
SET SESSION DEBUG_SYNC = "now WAIT_FOR sync.wsrep_apply_cb_reached";

--connection node_1
# issue conflicting write to child table, it should fail in certification
--send update child set j=2;

--connection node_1a
# release the applier
SET DEBUG_SYNC = "now SIGNAL signal.wsrep_apply_cb";
SET GLOBAL debug_dbug = "";
SET DEBUG_SYNC = "RESET";

--connection node_1
--error 0,ER_LOCK_DEADLOCK, ER_LOCK_WAIT_TIMEOUT
--reap
--let $wait_condition = SELECT COUNT(*) = 0 FROM parent;
--source include/wait_condition.inc
--let $wait_condition = SELECT COUNT(*) = 0 FROM child;
--source include/wait_condition.inc

SELECT * FROM parent;
SELECT * FROM child;

DROP TABLE child;
DROP TABLE parent;
