--source include/not_embedded.inc
--source include/have_debug.inc
--source include/not_embedded.inc
--source include/have_myisam_or_maria_default.inc

disable_query_log;
call mtr.add_suppression("Backup:");
call mtr.add_suppression("Cannot create backup engine for storage engine");
enable_query_log;

#
# This test script tests the behaviour reported in BUG#34721
#
# Test uses error injection activated with 'backup_test_dummy_be_factory' keyword.
# The code is injected into has_native_backup() function in backup_info.cc.
# When activated, it overwrites the native MyISAM backup factory with a dummy one 
# which never creates a driver. This change is permanent and can not be reversed by 
# disabling the error injection code. The only way to restore the original situation 
# is by restarting the server. Therefore:
#
# 1. This test should have .opt file so that a separate server instance is run for 
# it.
#
# 2. The order of operations in the test is relevant: we must first check the 
# standard behaviour and only then activate the injected debug code.
#

--disable_warnings
DROP DATABASE IF EXISTS db1;

let $MYSQLD_BACKUPDIR= `select @@backupdir`;

--error 0,1
remove_file $MYSQLD_BACKUPDIR/db1.bak;
--enable_warnings

CREATE DATABASE db1;
USE db1;
CREATE TABLE t1 (a int);
INSERT INTO t1 VALUES (1),(2),(3);

# First check the normal behaviour when server is not modified.

# make sure that injected code is disabled
SET SESSION debug="-d,";
SELECT @@debug;

# BACKUP should succeed without a warning.

--replace_column 1 #
BACKUP DATABASE db1 TO 'db1.bak';
SHOW WARNINGS;

# See what engines were used for BACKUP

SELECT max(backup_id) INTO @id FROM mysql.backup_history 
WHERE command LIKE 'BACKUP DATABASE db1 %';

# Note: right now default engine will be used (as below) because there is no
# native backup for MyISAM. But when it is added, the SELECT below should list native 
# MYISAM engine.

SELECT drivers FROM mysql.backup_history WHERE backup_id=@id;

# Now see what happens when injected code is activated and MyISAM uses the dummy
# factory function.

SET SESSION debug="+d,backup_test_dummy_be_factory";
SELECT @@debug;

# The following BACKUP command should generate a warning but otherwise
# should complete using the default backup driver.
# We disable warnings temporarily and show them with SHOW WARNINGS instead. This is to 
# avoid showing message codes (which change sometimes)

--disable_warnings
remove_file $MYSQLD_BACKUPDIR/db1.bak;
--replace_column 1 #
BACKUP DATABASE db1 TO 'db1.bak';
--enable_warnings

# Don't show warning message code
--replace_column 2 #
SHOW WARNINGS;

# See what engines were used for BACKUP

SELECT max(backup_id) INTO @id FROM mysql.backup_history 
WHERE command LIKE 'BACKUP DATABASE db1 %';

SELECT drivers FROM mysql.backup_history WHERE backup_id=@id;

# check that we can restore from the created image.

--replace_column 1 #
RESTORE FROM 'db1.bak' OVERWRITE;
SHOW TABLES IN db1;


# Clean up.

DROP DATABASE db1;
SET GLOBAL debug="-d,";
--remove_file $MYSQLD_BACKUPDIR/db1.bak
