#!/usr/bin/env bats load $BATS_TEST_DIRNAME/helper/common.bash load $BATS_TEST_DIRNAME/helper/sql-diff.bash setup() { setup_common } teardown() { assert_feature_version teardown_common } @test "sql-diff: output reconciles INSERT query" { dolt checkout -b firstbranch dolt sql < query dolt checkout firstbranch dolt sql < query cat query dolt add test dolt commit -m "Reconciled with newbranch" # confirm that both branches have the same content run dolt diff -r sql firstbranch newbranch [ "$status" -eq 0 ] [ "$output" = "" ] } @test "sql-diff: output reconciles UPDATE query" { dolt checkout -b firstbranch dolt sql < query dolt checkout firstbranch dolt sql < query rm query dolt add test dolt commit -m "Reconciled with newbranch" # confirm that both branches have the same content run dolt diff -r sql firstbranch newbranch [ "$status" -eq 0 ] [ "$output" = "" ] } @test "sql-diff: output reconciles DELETE query" { dolt checkout -b firstbranch dolt sql < query dolt checkout firstbranch dolt sql < query rm query dolt add test dolt commit -m "Reconciled with newbranch" # confirm that both branches have the same content run dolt diff -r sql firstbranch newbranch [ "$status" -eq 0 ] [ "$output" = "" ] } @test "sql-diff: output reconciles change to PRIMARY KEY field in row" { dolt checkout -b firstbranch dolt sql < query dolt checkout firstbranch dolt sql < query rm query dolt add test dolt commit -m "Reconciled with newbranch" # confirm that both branches have the same content run dolt diff -r sql firstbranch newbranch [ "$status" -eq 0 ] [ "$output" = "" ] } @test "sql-diff: output reconciles column rename" { dolt checkout -b firstbranch dolt sql < query dolt checkout firstbranch dolt sql < query cat query dolt add test dolt commit -m "Reconciled with newbranch" # confirm that both branches have the same content run dolt diff -r sql firstbranch newbranch [ "$status" -eq 0 ] [ "$output" = "" ] } @test "sql-diff: output reconciles DROP column query" { dolt checkout -b firstbranch dolt sql < query dolt checkout firstbranch cat query dolt sql < query dolt add test dolt commit -m "Reconciled with newbranch" # confirm that both branches have the same content run dolt diff -r sql firstbranch newbranch [ "$status" -eq 0 ] [ "$output" = "" ] } @test "sql-diff: output reconciles ADD column query" { dolt checkout -b firstbranch dolt sql < query dolt checkout firstbranch dolt sql < query rm query dolt add test dolt commit -m "Reconciled with newbranch" # confirm that both branches have the same content run dolt diff -r sql firstbranch newbranch [ "$status" -eq 0 ] [ "$output" = "" ] } @test "sql-diff: reconciles CREATE TABLE" { dolt checkout -b firstbranch dolt checkout -b newbranch dolt sql < query echo "----------------------" cat query echo "----------------------" dolt checkout firstbranch dolt sql < query dolt add test dolt commit -m "Reconciled with newbranch" # confirm that both branches have the same content run dolt diff -r sql firstbranch newbranch [ "$status" -eq 0 ] [ "$output" = "" ] } @test "sql-diff: includes row INSERTSs to new tables after CREATE TABLE" { dolt checkout -b firstbranch dolt checkout -b newbranch dolt sql < query dolt checkout firstbranch cat query dolt sql < query dolt add test dolt commit -m "Reconciled with newbranch" # confirm that both branches have the same content run dolt diff -r sql firstbranch newbranch [ "$status" -eq 0 ] [ "$output" = "" ] } @test "sql-diff: reconciles DROP TABLE" { dolt checkout -b firstbranch dolt sql < query dolt checkout firstbranch cat query dolt sql < query dolt add test dolt commit -m "Reconciled with newbranch" # confirm that both branches have the same content run dolt diff -r sql firstbranch newbranch [ "$status" -eq 0 ] [ "$output" = "" ] } @test "sql-diff: reconciles RENAME TABLE" { dolt checkout -b firstbranch dolt sql < query dolt checkout firstbranch cat query dolt sql < query dolt add . dolt commit -m "Reconciled with newbranch" # confirm that both branches have the same content run dolt diff -r sql firstbranch newbranch [ "$status" -eq 0 ] [ "$output" = "" ] grep 'RENAME' query } @test "sql-diff: reconciles RENAME TABLE with schema changes" { dolt checkout -b firstbranch dolt sql < query grep 'RENAME' query dolt checkout firstbranch cat query dolt sql < query dolt add . dolt commit -m "Reconciled with newbranch" # confirm that both branches have the same content # Note: For RENAME TABLE with schema changes, data diffs are skipped due to incompatible schemas # So we expect there to still be data differences after applying schema changes run dolt diff -r sql firstbranch newbranch [ "$status" -eq 0 ] # The output should contain the data differences that couldn't be included in the SQL diff [[ "$output" =~ "INSERT INTO" ]] || false } @test "sql-diff: reconciles CREATE/ALTER/DROP VIEW" { dolt sql -q 'create table test (pk int not null primary key)' dolt sql -q 'create view double_view as select pk*2 from test' run dolt diff -r sql [ "$status" -eq 0 ] [[ "$output" =~ "create view double_view" ]] || false } @test "sql-diff: diff sql recreates tables with all types" { dolt checkout -b firstbranch dolt checkout -b newbranch dolt sql < query dolt checkout firstbranch dolt sql < query rm query dolt add test dolt commit -m "Reconciled with newbranch" # confirm that both branches have the same content dolt diff -r sql firstbranch newbranch run dolt diff -r sql firstbranch newbranch [ "$status" -eq 0 ] [ "$output" = "" ] } @test "sql-diff: supports all types" { dolt checkout -b firstbranch dolt sql < patch.sql newbranch dolt checkout firstbranch dolt sql < patch.sql rm patch.sql dolt add . dolt commit -m "Reconciled with newbranch" # confirm that both branches have the same content run dolt diff -r sql firstbranch newbranch [ "$status" -eq 0 ] [ "$output" = "" ] done } run_2pk5col_ints() { local query_name=$1 # Initial setup dolt checkout -b firstbranch dolt sql < patch.sql newbranch dolt checkout firstbranch dolt sql < patch.sql rm patch.sql dolt add . dolt commit -m "Reconciled with newbranch" # Confirm branches are identical run dolt diff -r sql firstbranch newbranch [ "$status" -eq 0 ] [ "$output" = "" ] } @test "sql-diff: supports multiple primary keys (delete)" { run_2pk5col_ints "delete" } @test "sql-diff: supports multiple primary keys (add)" { run_2pk5col_ints "add" } @test "sql-diff: supports multiple primary keys (update)" { run_2pk5col_ints "update" } @test "sql-diff: supports multiple primary keys (single_pk_update)" { run_2pk5col_ints "single_pk_update" } @test "sql-diff: supports multiple primary keys (all_pk_update)" { run_2pk5col_ints "all_pk_update" } @test "sql-diff: supports multiple primary keys (create_table)" { run_2pk5col_ints "create_table" } @test "sql-diff: escapes values for MySQL string literals" { # https://dev.mysql.com/doc/refman/8.0/en/string-literals.html dolt sql < patch.sql run dolt sql < patch.sql [ "$status" -eq 0 ] run dolt diff -r sql main [ "$status" -eq 0 ] [ "$output" = "" ] } @test "sql-diff: sql diff ignores dolt docs" { echo "This is a README" > README.md run dolt diff -r sql [ "$status" -eq 0 ] skip "Have to decide how to treat dolty_docs in diff -r sql" [[ ! "$output" =~ "dolt_docs" ]] || false; } @test "sql-diff: handles NULL cells" { dolt sql < expected <<'EOF' DELETE FROM `test` WHERE `pk`=4; INSERT INTO `test` (`pk`,`c1`,`c2`) VALUES (7,8,9); EOF # We can't do direct bash comparisons because of newlines, so use diff to compare # Diff returns non-zero unless empty cat > actual < expected <<'EOF' create view v1 as select "hello" from test; EOF cat > actual < expected <<'EOF' create trigger tr1 before insert on test for each row set new.c1 = new.c1 + 1; EOF cat > actual < expected < actual < expected <<'EOF' DELETE FROM `test` WHERE `pk`=4; INSERT INTO `test` (`pk`,`c1`,`c2`) VALUES (7,8,9); create view v1 as select "hello" from test; EOF cat > actual < expected <<'EOF' DELETE FROM `test` WHERE `pk`=4; INSERT INTO `test` (`pk`,`c1`,`c2`) VALUES (7,8,9); create trigger tr1 before insert on test for each row set new.c1 = new.c1 + 1; create view v1 as select "hello" from test; EOF cat > actual < expected <<'EOF' DROP TRIGGER `tr1`; create trigger tr1 before insert on test for each row set new.c1 = new.c1 + 100; DROP VIEW `v1`; create view v1 as select "goodbye" from test; EOF cat > actual <