

-- *********************************************
-- Delete snapshot_file_path, io_file_path and layers_file_path columns from memories table;
-- Delete hidden_mem_path column from shows table
-- Add major and minor version number columns to memories and shows tables
-- Add default options file true/false column to options files tables
-- *********************************************

set term !! ;
execute block as begin

if( exists ( select rdb\$relation_fields.rdb\$field_name from rdb\$relation_fields 
where rdb\$relation_fields.rdb\$relation_name = 'MEMORIES' and rdb\$relation_fields.rdb\$field_name = 'SNAPSHOT_FILE_PATH'))
then execute statement 'ALTER TABLE memories DROP snapshot_file_path;';

if( exists ( select rdb\$relation_fields.rdb\$field_name from rdb\$relation_fields 
where rdb\$relation_fields.rdb\$relation_name = 'MEMORIES' and rdb\$relation_fields.rdb\$field_name = 'IO_FILE_PATH'))
then execute statement 'ALTER TABLE memories DROP io_file_path;';

if( exists ( select rdb\$relation_fields.rdb\$field_name from rdb\$relation_fields 
where rdb\$relation_fields.rdb\$relation_name = 'MEMORIES' and rdb\$relation_fields.rdb\$field_name = 'LAYERS_FILE_PATH'))
then execute statement 'ALTER TABLE memories DROP layers_file_path;';

if( not exists ( select rdb\$relation_fields.rdb\$field_name from rdb\$relation_fields 
where rdb\$relation_fields.rdb\$relation_name = 'MEMORIES' and rdb\$relation_fields.rdb\$field_name = 'CREATED'))
then execute statement 'ALTER TABLE memories ADD created TIMESTAMP;';

if( not exists ( select rdb\$relation_fields.rdb\$field_name from rdb\$relation_fields 
where rdb\$relation_fields.rdb\$relation_name = 'MEMORIES' and rdb\$relation_fields.rdb\$field_name = 'MAJOR_VERSION'))
then execute statement 'ALTER TABLE memories ADD major_version INTEGER DEFAULT -1 NOT NULL;';

if( not exists ( select rdb\$relation_fields.rdb\$field_name from rdb\$relation_fields 
where rdb\$relation_fields.rdb\$relation_name = 'MEMORIES' and rdb\$relation_fields.rdb\$field_name = 'MINOR_VERSION'))
then execute statement 'ALTER TABLE memories ADD minor_version INTEGER DEFAULT -1 NOT NULL;';

if( exists ( select rdb\$relation_fields.rdb\$field_name from rdb\$relation_fields 
where rdb\$relation_fields.rdb\$relation_name = 'SHOWS' and rdb\$relation_fields.rdb\$field_name = 'HIDDEN_MEM_PATH'))
then execute statement 'ALTER TABLE shows DROP hidden_mem_path;';

if( not exists ( select rdb\$relation_fields.rdb\$field_name from rdb\$relation_fields 
where rdb\$relation_fields.rdb\$relation_name = 'SHOWS' and rdb\$relation_fields.rdb\$field_name = 'MAJOR_VERSION'))
then execute statement 'ALTER TABLE shows ADD major_version INTEGER DEFAULT -1 NOT NULL;';

if( not exists ( select rdb\$relation_fields.rdb\$field_name from rdb\$relation_fields 
where rdb\$relation_fields.rdb\$relation_name = 'SHOWS' and rdb\$relation_fields.rdb\$field_name = 'MINOR_VERSION'))
then execute statement 'ALTER TABLE shows ADD minor_version INTEGER DEFAULT -1 NOT NULL;';

if( not exists ( select rdb\$relation_fields.rdb\$field_name from rdb\$relation_fields 
where rdb\$relation_fields.rdb\$relation_name = 'SHOWS' and rdb\$relation_fields.rdb\$field_name = 'LAST_LOADED_MEMORY'))
then execute statement 'ALTER TABLE shows ADD last_loaded_memory VARCHAR(36) UNIQUE;';

if( not exists ( select rdb\$relation_fields.rdb\$field_name from rdb\$relation_fields 
where rdb\$relation_fields.rdb\$relation_name = 'PATH_CONFIG_FILES' and rdb\$relation_fields.rdb\$field_name = 'DEFAULT_FILE'))
then execute statement 'ALTER TABLE path_config_files ADD default_file CHAR(1) DEFAULT 0 NOT NULL;';

if( not exists ( select rdb\$relation_fields.rdb\$field_name from rdb\$relation_fields 
where rdb\$relation_fields.rdb\$relation_name = 'EXT_INTERFACE_FILES' and rdb\$relation_fields.rdb\$field_name = 'DEFAULT_FILE'))
then execute statement 'ALTER TABLE ext_interface_files ADD default_file CHAR(1) DEFAULT 0 NOT NULL;';

if( not exists ( select rdb\$relation_fields.rdb\$field_name from rdb\$relation_fields 
where rdb\$relation_fields.rdb\$relation_name = 'PLUGINS_SETUP_FILES' and rdb\$relation_fields.rdb\$field_name = 'DEFAULT_FILE'))
then execute statement 'ALTER TABLE plugins_setup_files ADD default_file CHAR(1) DEFAULT 0 NOT NULL;';

if( not exists ( select rdb\$relation_fields.rdb\$field_name from rdb\$relation_fields 
where rdb\$relation_fields.rdb\$relation_name = 'MIDI_SETUP_FILES' and rdb\$relation_fields.rdb\$field_name = 'DEFAULT_FILE'))
then execute statement 'ALTER TABLE midi_setup_files ADD default_file CHAR(1) DEFAULT 0 NOT NULL;';

if( not exists ( select rdb\$relation_fields.rdb\$field_name from rdb\$relation_fields 
where rdb\$relation_fields.rdb\$relation_name = 'USER_SECTIONS_SETUP_FILES' and rdb\$relation_fields.rdb\$field_name = 'DEFAULT_FILE'))
then execute statement 'ALTER TABLE user_sections_setup_files ADD default_file CHAR(1) DEFAULT 0 NOT NULL;';

if( not exists ( select rdb\$relation_fields.rdb\$field_name from rdb\$relation_fields 
where rdb\$relation_fields.rdb\$relation_name = 'INSERTS_SETUP_FILES' and rdb\$relation_fields.rdb\$field_name = 'DEFAULT_FILE'))
then execute statement 'ALTER TABLE inserts_setup_files ADD default_file CHAR(1) DEFAULT 0 NOT NULL;';

if( not exists ( select rdb\$relation_fields.rdb\$field_name from rdb\$relation_fields 
where rdb\$relation_fields.rdb\$relation_name = 'DEFAULT_SETTINGS_FILES' and rdb\$relation_fields.rdb\$field_name = 'DEFAULT_FILE'))
then execute statement 'ALTER TABLE default_settings_files ADD default_file CHAR(1) DEFAULT 0 NOT NULL;';

if( not exists ( select rdb\$relation_fields.rdb\$field_name from rdb\$relation_fields 
where rdb\$relation_fields.rdb\$relation_name = 'MON_SELS_SETUP_FILES' and rdb\$relation_fields.rdb\$field_name = 'DEFAULT_FILE'))
then execute statement 'ALTER TABLE mon_sels_setup_files ADD default_file CHAR(1) DEFAULT 0 NOT NULL;';

if( not exists ( select rdb\$relation_fields.rdb\$field_name from rdb\$relation_fields 
where rdb\$relation_fields.rdb\$relation_name = 'TFT_SETUP_FILES' and rdb\$relation_fields.rdb\$field_name = 'DEFAULT_FILE'))
then execute statement 'ALTER TABLE tft_setup_files ADD default_file CHAR(1) DEFAULT 0 NOT NULL;';

if( not exists ( select rdb\$relation_fields.rdb\$field_name from rdb\$relation_fields 
where rdb\$relation_fields.rdb\$relation_name = 'MON_MTR_EXT_INPUT_FILES' and rdb\$relation_fields.rdb\$field_name = 'DEFAULT_FILE'))
then execute statement 'ALTER TABLE mon_mtr_ext_input_files ADD default_file CHAR(1) DEFAULT 0 NOT NULL;';

if( not exists ( select rdb\$relation_fields.rdb\$field_name from rdb\$relation_fields 
where rdb\$relation_fields.rdb\$relation_name = 'WILDS_SETUP_FILES' and rdb\$relation_fields.rdb\$field_name = 'DEFAULT_FILE'))
then execute statement 'ALTER TABLE wilds_setup_files ADD default_file CHAR(1) DEFAULT 0 NOT NULL;';

if( not exists ( select rdb\$relation_fields.rdb\$field_name from rdb\$relation_fields 
where rdb\$relation_fields.rdb\$relation_name = 'HYDRA_NETWORK_FILES' and rdb\$relation_fields.rdb\$field_name = 'DEFAULT_FILE'))
then execute statement 'ALTER TABLE hydra_network_files ADD default_file CHAR(1) DEFAULT 0 NOT NULL;';

end!!
set term ; !!
commit;

-- *********************************************
-- Set the default values for the show and memory major and minor version numbers and the memories created columns;
-- *********************************************

UPDATE memories SET created = last_saved WHERE created IS NULL;
UPDATE memories SET major_version = -1 WHERE major_version IS NULL;
UPDATE memories SET minor_version = -1 WHERE minor_version IS NULL;

UPDATE shows SET major_version = -1 WHERE major_version IS NULL;
UPDATE shows SET minor_version = -1 WHERE minor_version IS NULL;

-- *********************************************
-- Set the default options file to true for all the current default options files
-- *********************************************

UPDATE user_sections_setup_files SET default_file = 1 where file_name = 'DefaultUserSectionsSetup.uso';
UPDATE hydra_network_files SET default_file = 1 where file_name = 'DefaultHydraNetworkSetup.hno';
UPDATE default_settings_files SET default_file = 1 where file_name = 'DefaultSettingsOptions.dfo';
UPDATE mon_mtr_ext_input_files SET default_file = 1 where file_name = 'DefaultMonMtrExtInputConfig.mmo';

commit;

EOF
