-- This script is for backward compatability with old DataBlade modules.
-- The bld_* and bldi_* tables have been depreciated in favor of the sysbld prefix.
-- Note that these work the first time through, but fail harmlessly on subsequent
-- invocations.

-- Duplicate data from old style tables.
insert into sysbldmodules select * from bld_modules;

insert into sysbldscripts select * from bld_scripts;

insert into sysblddescription select * from bld_desc;

insert into sysbldclientfiles select * from client_files;

insert into sysbldclientfileos select * from clntfile_os_rel;

insert into sysbldinterfaces select * from bld_interfaces;

insert into sysbldifacprovided select * from dbi_provided;

insert into sysbldifacrequired select * from dbi_required;

merge into sysbldvendors as old using bld_vendors as new
  on old.bvendor_id = new.bvendor_id
when not matched then insert (bvendor_id, bvendor_name, blocale_id, bvendor_format, bvendor_desc)
    VALUES (new.bvendor_id, new.bvendor_name, new.blocale_id, new.bvendor_format, new.bvendor_desc);

-- bld_registered upgrade.
create view if not exists bld_registered as select * from sysbldregistered;

merge into sysbldregistered as old using bld_registered as new
  on old.bld_id = new.bld_id
when not matched then insert (bld_id)
    values (new.bld_id);

-- bldi_required upgrade.

create view if not exists bldi_required as select * from sysbldirequired;

merge into sysbldirequired as old using bldi_required as new
  on old.bld_id = new.bld_id and old.bldi_id = new.bldi_id 
when not matched then insert (bldi_id, bld_id)
    values (new.bldi_id, new.bld_id);

-- bldi_provided upgrade.

create view if not exists bldi_provided as select * from sysbldiprovided;

merge into sysbldiprovided as old using bldi_provided as new
   on  old.bldi_id = new.bldi_id 
 when not matched then insert (bldi_id, bld_id)
    values (new.bldi_id, new.bld_id);

