#=============================================================================================
# @TITLE			Recovery - Index - check table
#
# @NUMBER			WDBM_FKT_02021
#
# @DESCRIPTION		First will be create a bad index to make a recovery necessary and
# @					possible. Next will be click the link for "Index Recovery" and the first
# @					mask will be checked. Then will be clicked on the "Select" link to select
# @					a possible index for recreating. Checking the "Back" link and running the
# @					recovery.
#
# @EXP_RESULT		The index recovery will work fine and the test run through.
#
# @REQUIREMENTS		Test: WDBM_FKT_01000; WDBM_FKT_00100; WDBM_FKT_01010;
#=============================================================================================

static index_table_items_ok_1 = 1;
static index_table_items_ok_2 = 1;
static index_table_items [] = {
								{"Owner","Table Name","Index Name"},
								{"DBA","BADIDXTEST","BADIDXTEST1"},
								{"DBA","BADIDXTEST","BADIDXTEST2"}
							  };
#=============================================================================================
# @STEP 1			Creating table with content.
#=============================================================================================
invoke_application("..\\..\\..\\DBM_Scripte\\" & database & "\\create_table.cmd","","",SW_SHOW);

wait(10);
#=============================================================================================
# @STEP 2			Running complete backup.
#=============================================================================================
set_window (menu_window, 6);

if (version == "7.3.00") {
	web_link_click("Complete");
}
if (version == "7.4.03" ||
	version == "7.4.04") {
	web_link_click("Complete Data");
}

set_window (work_window, 4);
web_link_click("Add Medium");

set_window (work_window, 9);
edit_set("Name","Data_1");
edit_set("Location","D:\\database\\backup\\data_1");
button_set("Overwrite",ON);
web_link_click("OK");

wait(2);

set_window (work_window, 3);
web_link_click("Data_1");
set_window (work_window, 2);
web_link_click("Start");
wait(60);
set_window (work_window, 3);
web_link_click("Close");
#=============================================================================================
# @STEP 3			Creating indexes.
#=============================================================================================
invoke_application("..\\..\\..\\DBM_Scripte\\" & database & "\\BadIndexes.cmd","","",SW_SHOW);

wait(10);
#=============================================================================================
# @STEP 4			Restore complete backup.
#=============================================================================================
set_window (state_window, 1);
web_link_click("Admin");

for(i=0;i<100;i++) {

	if (obj_exists("Cold",0) == 0) {
		wait(5);
	} else {
		break;
	}
}

set_window (menu_window, 2);
web_link_click("Database_1");

set_window (work_window, 2);
button_set("RecoveryType",ON);
web_link_click("OK");
wait(3);
#=============================================================================================
# @STEP 4.2			Restoring complete backup.
#=============================================================================================
set_window (work_window, 2);
web_link_click("OK");
wait(3);
set_window (work_window, 1);
web_link_click("Start");
wait(30);
#=============================================================================================
# @STEP 4.3			Restart of database.
#=============================================================================================
set_window (work_window, 7);
web_link_click("Restart");

for(i=0;i<100;i++) {

	L_RC = obj_exists("Cold",0);
	
	if (L_RC == 0) {
		break;
	}
}
#=============================================================================================
# @STEP 5			Checking for bad indexes.
#=============================================================================================
set_window (menu_window, 7);

if (version == "7.3.00") {
	web_link_click("Index");
}
if (version == "7.4.03" ||
	version == "7.4.04") {
	web_link_click("Indexes");
}

set_window (work_window, 1);
web_link_click("Select");

wait(2);
#=============================================================================================
# @STEP 5.1			Selecting the bad indexes and running recreate.
#=============================================================================================
set_window (work_window, 4);

for(n=1;n<4;n++) {
	for(i=2;i<5;i++) {
		tbl_get_cell_data("Owner","#"&n,"#"&i,cell_value_1);

		if (cell_value_1 != index_table_items [n-1,i-2]) {
			index_table_items_ok_1 = 0;
			break;
		}
	}
}

if (index_table_items_ok_1 == 0) {
	tl_step("Table for bad indexes","1","is not correct");
} else {
	tl_step("Table for bad indexes","0","is correct");
}

if (obj_exists("Item",0) == 0 &&
	obj_exists("Item_1",0) == 0) {
	
	button_set("Item",ON);
	button_set("Item_1",ON);
	web_link_click("Recreate");
#=============================================================================================
# @STEP 5.2			Checking if bad indexes still exist.
#=============================================================================================
	set_window (work_window, 2);
	web_link_click("Back");
	set_window (work_window, 1);
	web_link_click("Select");
	
	wait(5);
	set_window (work_window, 4);
	tbl_get_rows_count("Owner",rows_count);
	
	if (rows_count == 1) {
		tl_step("Bad indexes","0","there are no indexes for recovery");
	} else {
		tl_step("Bad indexes","1","there are still indexes for recovery");
	}


	for(n=1;n<2;n++) {
		for(i=2;i<5;i++) {
			tbl_get_cell_data("Owner","#"&n,"#"&i,cell_value_2);
	
			if (cell_value_2 != index_table_items [n-1,i-2]) {
				index_table_items_ok_2 = 0;
				break;
			}
		}
	}

	if (index_table_items_2 == 0) {
		tl_step("Table for bad indexes","1","After recovery is not correct");
	} else {
		tl_step("Table for bad indexes","0","After recovery is correct");
	}
} else {
	tl_step("There are no bad indexes","1","Recover Index");
}
#=============================================================================================
# @STEP 6			Delete table and indexes.
#=============================================================================================
invoke_application("..\\..\\..\\DBM_Scripte\\" & database & "\\drop_table_indexes.cmd","","",SW_SHOW);
