--
--   Licensed to the Apache Software Foundation (ASF) under one or more
--   contributor license agreements.  See the NOTICE file distributed with
--   this work for additional information regarding copyright ownership.
--   The ASF licenses this file to You under the Apache License, Version 2.0
--   (the "License"); you may not use this file except in compliance with
--   the License.  You may obtain a copy of the License at
--
--      http://www.apache.org/licenses/LICENSE-2.0
--
--   Unless required by applicable law or agreed to in writing, software
--   distributed under the License is distributed on an "AS IS" BASIS,
--   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
--   See the License for the specific language governing permissions and
--   limitations under the License.
--

CREATE PROCEDURE RENAME_FILE(LOCATION VARCHAR(32000), NAME VARCHAR(32000), NEW_NAME  VARCHAR(32000)) DYNAMIC RESULT SETS 0 LANGUAGE JAVA EXTERNAL NAME 'org.apache.derbyTesting.functionTests.util.FTFileUtil.renameFile' PARAMETER STYLE JAVA;

values SYSCS_UTIL.SYSCS_GET_DATABASE_PROPERTY('derby.storage.logArchiveMode');
--check whether log archive mode  enabling method is working
call SYSCS_UTIL.SYSCS_BACKUP_DATABASE_AND_ENABLE_LOG_ARCHIVE_MODE(
    'extinout/mybackup', 0);
values SYSCS_UTIL.SYSCS_GET_DATABASE_PROPERTY('derby.storage.logArchiveMode');
--check whether the logArchive Mode is persistent across boots
disconnect;


values SYSCS_UTIL.SYSCS_GET_DATABASE_PROPERTY('derby.storage.logArchiveMode');

--check whether log archive mode  disabling method is working
call SYSCS_UTIL.SYSCS_DISABLE_LOG_ARCHIVE_MODE(1);
values SYSCS_UTIL.SYSCS_GET_DATABASE_PROPERTY('derby.storage.logArchiveMode');
--check whether the logArchive Mode disabling persistent across boots
disconnect;


values SYSCS_UTIL.SYSCS_GET_DATABASE_PROPERTY('derby.storage.logArchiveMode');
-- reenable the log archive mode again to see whether the 
-- disabling has any side effects.
call SYSCS_UTIL.SYSCS_BACKUP_DATABASE_AND_ENABLE_LOG_ARCHIVE_MODE(
    'extinout/mybackup', 0);
values SYSCS_UTIL.SYSCS_GET_DATABASE_PROPERTY('derby.storage.logArchiveMode');
disconnect;


values SYSCS_UTIL.SYSCS_GET_DATABASE_PROPERTY('derby.storage.logArchiveMode');
--END OF PROPERTY ARCHIVE CHECKS

---PERFORM DIFFERENT TYPES OF RESTORE
create table t1(a int ) ;
insert into t1 values(1) ;
insert into t1 values(2) ;
insert into t1 values(3 ) ;
call SYSCS_UTIL.SYSCS_BACKUP_DATABASE_AND_ENABLE_LOG_ARCHIVE_MODE(
    'extinout/mybackup', 0);
insert into t1 values(4) ;
insert into t1 values(5);
insert into t1 values(6);

disconnect;

select * from t1 ;
insert into t1 values(7);
insert into t1 values(8);
insert into t1 values(9);
--take a backup again
call SYSCS_UTIL.SYSCS_BACKUP_DATABASE_AND_ENABLE_LOG_ARCHIVE_MODE(
    'extinout/mybackup', 1);
insert into t1 values(10);
insert into t1 values(11);
insert into t1 values(12);

disconnect;

select * from t1 ;
insert into t1 values(10);
insert into t1 values(11);
insert into t1 values(12);
insert into t1 values(13);
insert into t1 values(14);
insert into t1 values(15);
call SYSCS_UTIL.SYSCS_BACKUP_DATABASE_AND_ENABLE_LOG_ARCHIVE_MODE(
    'extinout/mybackup', 1);

disconnect;

select * from t1;
insert into t1 values(16);
insert into t1 values(17);
insert into t1 values(18);
call SYSCS_UTIL.SYSCS_BACKUP_DATABASE_AND_ENABLE_LOG_ARCHIVE_MODE(
    'extinout/mybackup', 1);

disconnect;

values SUBSTR(SYSCS_UTIL.SYSCS_GET_DATABASE_PROPERTY('logDevice'), LOCATE('crwombatlog',SYSCS_UTIL.SYSCS_GET_DATABASE_PROPERTY('logDevice')),11);
select * from t1;
insert into t1 values(19);
insert into t1 values(20);
insert into t1 values(21);

disconnect;

values SUBSTR(SYSCS_UTIL.SYSCS_GET_DATABASE_PROPERTY('logDevice'), LOCATE('crwombatlog',SYSCS_UTIL.SYSCS_GET_DATABASE_PROPERTY('logDevice')),11);

select * from t1;
insert into t1 values(22);
insert into t1 values(23);
insert into t1 values(24);

disconnect;



values SUBSTR(SYSCS_UTIL.SYSCS_GET_DATABASE_PROPERTY('logDevice'), LOCATE('wombatlog',SYSCS_UTIL.SYSCS_GET_DATABASE_PROPERTY('logDevice')),9);

select * from t1 ;
insert into t1 values(19);
insert into t1 values(20);
insert into t1 values(21);
call SYSCS_UTIL.SYSCS_BACKUP_DATABASE_AND_ENABLE_LOG_ARCHIVE_MODE(
    'extinout/mybackup', 1);

disconnect;

values SUBSTR(SYSCS_UTIL.SYSCS_GET_DATABASE_PROPERTY('logDevice'), LOCATE('wombatlog',SYSCS_UTIL.SYSCS_GET_DATABASE_PROPERTY('logDevice')),9);
select * from t1;
insert into t1 values(22);
insert into t1 values(23);
insert into t1 values(24);

disconnect;

values SUBSTR(SYSCS_UTIL.SYSCS_GET_DATABASE_PROPERTY('logDevice'), LOCATE('wombatlog',SYSCS_UTIL.SYSCS_GET_DATABASE_PROPERTY('logDevice')),9);

select * from t1;
autocommit off;
insert into t1 values(25);
insert into t1 values(26);
insert into t1 values(27);
rollback;

disconnect;


values SUBSTR(SYSCS_UTIL.SYSCS_GET_DATABASE_PROPERTY('logDevice'), LOCATE('wombatlog',SYSCS_UTIL.SYSCS_GET_DATABASE_PROPERTY('logDevice')),9);
select * from t1 ;
insert into t1 values(25);
insert into t1 values(26);
insert into t1 values(27);

disconnect;


call RENAME_FILE(null,'extinout/wombatlog','extinout/wombatlogmoved');
disconnect;

values SUBSTR(SYSCS_UTIL.SYSCS_GET_DATABASE_PROPERTY('logDevice'), LOCATE('wombatlogmoved',SYSCS_UTIL.SYSCS_GET_DATABASE_PROPERTY('logDevice')),14);

select * from t1 ;
insert into t1 values(30);
insert into t1 values(31);
insert into t1 values(32);

disconnect;

values SUBSTR(SYSCS_UTIL.SYSCS_GET_DATABASE_PROPERTY('logDevice'), LOCATE('wombatlogmoved',SYSCS_UTIL.SYSCS_GET_DATABASE_PROPERTY('logDevice')),14);
select * from t1 ;
insert into t1 values(33);
insert into t1 values(34);
insert into t1 values(35);
--take a fresh backup again with moved log device.
call SYSCS_UTIL.SYSCS_BACKUP_DATABASE_AND_ENABLE_LOG_ARCHIVE_MODE(
    'extinout/mybackup', 1);

disconnect;

values SUBSTR(SYSCS_UTIL.SYSCS_GET_DATABASE_PROPERTY('logDevice'), LOCATE('wombatlogmoved',SYSCS_UTIL.SYSCS_GET_DATABASE_PROPERTY('logDevice')),14);
select * from t1;
insert into t1 values(36);
insert into t1 values(37);
insert into t1 values(38);

disconnect;

values SYSCS_UTIL.SYSCS_GET_DATABASE_PROPERTY('logDevice');
select * from t1;
insert into t1 values(39);
insert into t1 values(40);
insert into t1 values(41);
call SYSCS_UTIL.SYSCS_BACKUP_DATABASE_AND_ENABLE_LOG_ARCHIVE_MODE(
    'extinout/mybackup', 1);

disconnect;

values SYSCS_UTIL.SYSCS_GET_DATABASE_PROPERTY('logDevice');
call SYSCS_UTIL.SYSCS_BACKUP_DATABASE_AND_ENABLE_LOG_ARCHIVE_MODE(
    'extinout/mybackup', 1);

disconnect;


call SYSCS_UTIL.SYSCS_DISABLE_LOG_ARCHIVE_MODE(1);
select * from t1;
insert into t1 values(42);
insert into t1 values(43);
insert into t1 values(44);
call SYSCS_UTIL.SYSCS_BACKUP_DATABASE('extinout/mybackup');
--following inserted values should not be there
--when we do restore from the above backup.
insert into t1 values(45);
insert into t1 values(46);
insert into t1 values(47);

disconnect;

select * from t1;
insert into t1 values(45);
insert into t1 values(46);
insert into t1 values(47);
call SYSCS_UTIL.SYSCS_BACKUP_DATABASE('extinout/mybackup');

disconnect;

select * from t1;
insert into t1 values(48);
insert into t1 values(49);
insert into t1 values(50);

disconnect;

call SYSCS_UTIL.SYSCS_BACKUP_DATABASE_AND_ENABLE_LOG_ARCHIVE_MODE(
    'extinout/mybackup', 1);

disconnect;











call RENAME_FILE('extinout/mybackup/wombat/','service.properties','service.properties.old');

call RENAME_FILE('extinout/mybackup/wombat/','service.properties.old','service.properties');
call RENAME_FILE('extinout/mybackup/wombat/','log','log.old');

call RENAME_FILE('extinout/mybackup/wombat/','log.old','log');
call RENAME_FILE('extinout/mybackup/wombat/','seg0','data.old');

call RENAME_FILE('extinout/mybackup/wombat/','data.old','seg0');
--try error cases with createFrom;if root created is not getting cleaned up,
--next createFrom call will fail with DBLOCATION/wombat exist error.
call RENAME_FILE('extinout/mybackup/wombat/','service.properties','service.properties.old');

call RENAME_FILE('extinout/mybackup/wombat/','service.properties.old','service.properties');
call RENAME_FILE('extinout/mybackup/wombat/','log','log.old');

call RENAME_FILE('extinout/mybackup/wombat/','log.old','log');
call RENAME_FILE('extinout/mybackup/wombat/','seg0','data.old');

call RENAME_FILE('extinout/mybackup/wombat/','data.old','seg0');

drop procedure RENAME_FILE;






