-- http://download.oracle.com/docs/cd/E11882_01/server.112/e10707/rarrepsite.htm

-- na obou databazich:

--
-- AS SYSTEM
--

-- zprostredkovatel synchronizace
CREATE USER repadmin IDENTIFIED BY repadmin;

-- odpovidajici prava
BEGIN
   DBMS_REPCAT_ADMIN.GRANT_ADMIN_ANY_SCHEMA (username => 'repadmin');
END;
/

GRANT COMMENT ANY TABLE TO repadmin;
GRANT LOCK ANY TABLE TO repadmin;
GRANT SELECT ANY DICTIONARY TO repadmin;

-- registrace propagatora
BEGIN
   DBMS_DEFER_SYS.REGISTER_PROPAGATOR (
      username => 'repadmin');
END;
/

-- registrace receivera
BEGIN
   DBMS_REPCAT_ADMIN.REGISTER_USER_REPGROUP (
      username => 'repadmin',
      privilege_type => 'receiver',
      list_of_gnames => NULL);
END;
/

--
-- AS REPADMIN
--

BEGIN
   DBMS_DEFER_SYS.SCHEDULE_PURGE (
      next_date => SYSDATE,
      interval => 'SYSDATE + 1/24',
      delay_seconds => 0);
END;
/

-- linky mezi databazemi
CREATE DATABASE LINK semora{1|2} CONNECT TO repadmin IDENTIFIED BY repadmin USING 'semora{1|2};

-- push operace

BEGIN
   DBMS_DEFER_SYS.SCHEDULE_PUSH (
      destination => 'semora2',
      interval => 'SYSDATE + (1/144)',
      next_date => SYSDATE,
      parallelism => 1,
      execution_seconds => 1500,
      delay_seconds => 1200);
END;
/

BEGIN
   DBMS_DEFER_SYS.SCHEDULE_PUSH (
      destination => 'semora1',
      interval => 'SYSDATE + (1/144)',
      next_date => SYSDATE,
      parallelism => 1,
      execution_seconds => 1500,
      delay_seconds => 1200);
END;
/

---------------------

--
-- AS KOP
--

create table replikovana(
  id number(5)
    constraint replikovana_pk
      primary key,
  txt varchar2(20) not null
  );

--
-- AS REPADMIN
--

-- master grupy (v semora1)

BEGIN
   DBMS_REPCAT.CREATE_MASTER_REPGROUP (gname => 'test_rg');
END;
/

BEGIN
   DBMS_REPCAT.CREATE_MASTER_REPOBJECT (
      gname => 'test_rg',
      type => 'TABLE',
      oname => 'REPLIKOVANA',
      sname => 'KOP',
      use_existing_object => TRUE,
      copy_rows => FALSE);
END;
/

BEGIN
   DBMS_REPCAT.CREATE_MASTER_REPOBJECT (
      gname => 'test_rg',
      type => 'INDEX',
      oname => 'REPLIKOVANA_PK',
      sname => 'KOP',
      use_existing_object => TRUE,
      copy_rows => FALSE);
END;
/

BEGIN
   DBMS_REPCAT.ADD_MASTER_DATABASE (
      gname => 'test_rg',
      master => 'semora2',
      use_existing_objects => TRUE,
      copy_rows => FALSE,
      propagation_mode => 'ASYNCHRONOUS');
END;
/

SELECT COUNT(*) FROM DBA_REPCATLOG WHERE GNAME = 'TEST_RG';

-- replikacni support

BEGIN
    DBMS_REPCAT.GENERATE_REPLICATION_SUPPORT (
      sname => 'KOP',
      oname => 'REPLIKOVANA',
      type => 'TABLE',
      min_communication => TRUE);
END;
/

BEGIN
   DBMS_REPCAT.RESUME_MASTER_ACTIVITY (
      gname => 'test_rg');
END;
/

--------

BEGIN
   DBMS_REPCAT.SUSPEND_MASTER_ACTIVITY (
      gname => 'test_rg');
END;
/

BEGIN
   DBMS_REPCAT.ALTER_MASTER_PROPAGATION (
      gname => 'test_rg',
      master => 'semora2',
      dblink_list => null,
      propagation_mode => 'SYNCHRONOUS');
END;
/

BEGIN
   DBMS_REPCAT.RESUME_MASTER_ACTIVITY (
      gname => 'test_rg');
END;
/

