#!/usr/bin/env bats load $BATS_TEST_DIRNAME/helper/common.bash load $BATS_TEST_DIRNAME/helper/query-server-common.bash setup() { setup_common } teardown() { teardown_common } @test "sql-backup: dolt_backup no argument" { run dolt sql -q "call dolt_backup()" [ "$status" -ne 0 ] [[ "$output" =~ "use 'dolt_backups' table to list backups" ]] || false run dolt sql -q "CALL dolt_backup()" [ "$status" -ne 0 ] [[ "$output" =~ "use 'dolt_backups' table to list backups" ]] || false } @test "sql-backup: dolt_backup add" { mkdir the_backup run dolt sql -q "call dolt_backup('add', 'hostedapidb-0', 'file://./the_backup')" [ "$status" -eq 0 ] run dolt backup -v [ "$status" -eq 0 ] [[ "$output" =~ "the_backup" ]] || false } @test "sql-backup: dolt_backup add cannot add remote with address of existing backup" { backupFileUrl="file://$BATS_TEST_TMPDIR/backup" dolt sql -q "call dolt_backup('add','bac1', '$backupFileUrl')" run dolt sql -q "call dolt_backup('add','rem1', '$backupFileUrl')" [ "$status" -eq 1 ] [[ "$output" =~ "address conflict with a remote: 'bac1'" ]] || false } @test "sql-backup: dolt_backup remove" { backupFileUrl="file://$BATS_TEST_TMPDIR/backup" dolt sql -q "call dolt_backup('add', 'bac1', '$backupFileUrl')" run dolt backup -v [ "$status" -eq 0 ] [ "${#lines[@]}" -eq 1 ] [[ "$output" =~ "bac1" ]] || false dolt sql -q "call dolt_backup('remove','bac1')" run dolt backup -v [ "$status" -eq 0 ] [ "${#lines[@]}" -eq 0 ] } @test "sql-backup: dolt_backup remove cannot remove non-existent backup" { run dolt backup -v [ "$status" -eq 0 ] [ "${#lines[@]}" -eq 0 ] [[ ! "$output" =~ "bac1" ]] || false run dolt sql -q "call dolt_backup('remove','bac1')" [ "$status" -eq 1 ] [[ "$output" =~ "backup 'bac1' not found" ]] || false } @test "sql-backup: dolt_backup rm" { backupFileUrl="files://$BATS_TEST_TMPDIR/backup" dolt sql -q "call dolt_backup('add', 'bac1', '$backupFileUrl')" run dolt backup -v [ "$status" -eq 0 ] [ "${#lines[@]}" -eq 1 ] [[ "$output" =~ "bac1" ]] || false dolt sql -q "call dolt_backup('rm','bac1')" run dolt backup -v [ "$status" -eq 0 ] [ "${#lines[@]}" -eq 0 ] [[ ! "$output" =~ "bac1" ]] || false } @test "sql-backup: dolt_backup restore invalid arguments" { # Not enough arguments run dolt sql -q "call dolt_backup('restore')" [ "$status" -eq 1 ] [[ "$output" =~ "usage: dolt_backup('restore', 'remote_url', 'new_db_name', ['--force'], ['--aws-region='], ['--aws-creds-type='], ['--aws-creds-file='], ['--aws-creds-profile='])" ]] || false # Not enough arguments run dolt sql -q "call dolt_backup('restore', 'file:///some_directory')" [ "$status" -eq 1 ] [[ "$output" =~ "usage: dolt_backup('restore', 'remote_url', 'new_db_name', ['--force'], ['--aws-region='], ['--aws-creds-type='], ['--aws-creds-file='], ['--aws-creds-profile='])" ]] || false # Too many arguments run dolt sql -q "call dolt_backup('restore', 'hostedapidb-0', 'file:///some_directory', 'too many')" [ "$status" -eq 1 ] [[ "$output" =~ "usage: dolt_backup('restore', 'remote_url', 'new_db_name', ['--force'], ['--aws-region='], ['--aws-creds-type='], ['--aws-creds-file='], ['--aws-creds-profile='])" ]] || false } @test "sql-backup: dolt_backup restore" { backupFileUrl="file://$BATS_TEST_TMPDIR/backups" mkdir backupsDir # Created a nested database, back it up, drop it, then restore it with a new name dolt sql -q "create database db1;" cd db1 dolt sql -q "create table t1 (pk int primary key); insert into t1 values (42); call dolt_commit('-Am', 'creating table t1');" dolt sql -q "call dolt_backup('add', 'backups', '$backupFileUrl');" dolt sql -q "call dolt_backup('sync', 'backups');" cd .. dolt sql -q "drop database db1;" dolt sql -q "call dolt_backup('restore', '$backupFileUrl', 'db2');" # Assert that db2 is present, and db1 is not run dolt sql -q "show databases;" [ "$status" -eq 0 ] [[ ! "$output" =~ "db1" ]] || false [[ "$output" =~ "db2" ]] || false # Sanity check some data in the database run dolt sql -q "use db2; select * from t1;" [ "$status" -eq 0 ] [[ "$output" =~ "42" ]] || false # Assert that db2 doesn't have any remotes cd db2 run dolt remote -v [ "$status" -eq 0 ] [ "${#lines[@]}" -eq 0 ] } @test "sql-backup: dolt_backup restore --force on current database" { backupFileUrl="file://$BATS_TEST_TMPDIR/backups" # Created a nested database, and back it up dolt sql -q "create database db1;" # We could cd into db1 but Windows does not like us touching its CWD when we drop the database when restoring. dolt sql -q "use db1; create table t1 (pk int primary key); insert into t1 values (42); call dolt_commit('-Am', 'creating table t1');" dolt sql -q "use db1; call dolt_backup('add', 'backups', '$backupFileUrl');" dolt sql -q "use db1; call dolt_backup('sync', 'backups');" # Make a new commit in db1, but don't push it to the backup dolt sql -q "use db1; update t1 set pk=100; call dolt_commit('-Am', 'updating table t1');" # Assert that without --force, we can't update an existing db from a backup run dolt sql -q "use db1; call dolt_backup('restore', '$backupFileUrl', 'db1');" [ "$status" -eq 1 ] [[ "$output" =~ "database 'db1' already exists, use '--force' to overwrite" ]] || false # Use --force to overwrite the existing database and sanity check the data run dolt sql -q "use db1; call dolt_backup('restore', '--force', '$backupFileUrl', 'db1');" [ "$status" -eq 0 ] run dolt sql -q "use db1; select * from t1;" [ "$status" -eq 0 ] [[ "$output" =~ "42" ]] || false } @test "sql-backup: dolt_backup restore --force on current database and as cwd" { skiponwindows "Windows storage system locks the terminal cwd when trying to drop database in restore procedure; this includes mounted storage in WSL" backupFileUrl="file://$BATS_TEST_TMPDIR/backup" dolt sql -q "create database db1;" cd db1 dolt sql <