-- for OpenDOAR
-- COPY existing rows
COPY (SELECT * FROM dsm_datasource_organization WHERE datasource like 'opendoar%') TO '/tmp/opendoar_datasource_orgs.csv' WITH DELIMITER ';' CSV HEADER;
COPY (SELECT * FROM dsm_organizations WHERE id like 'opendoar%') TO '/tmp/opendoar_orgs.csv' WITH DELIMITER ';' CSV HEADER;
COPY (SELECT * FROM dsm_organizationpids  WHERE organization like 'opendoar%') TO '/tmp/opendoar_orgs_pids.csv' WITH DELIMITER ';' CSV HEADER;

-- delete existing rows
ALTER TABLE dsm_datasources DISABLE TRIGGER ALL;
ALTER TABLE dsm_organizations DISABLE TRIGGER ALL;
ALTER TABLE dsm_organizationpids DISABLE TRIGGER ALL;
ALTER TABLE dsm_datasource_organization DISABLE TRIGGER ALL;

DELETE from dsm_datasource_organization where datasource like 'opendoar%';
DELETE from dsm_organizations where id like 'opendoar%';
DELETE from dsm_organizationpids where organization like 'opendoar%';

ALTER TABLE dsm_datasources ENABLE TRIGGER ALL;
ALTER TABLE dsm_organizations ENABLE TRIGGER ALL;
ALTER TABLE dsm_organizationpids ENABLE TRIGGER ALL;
ALTER TABLE dsm_datasource_organization ENABLE TRIGGER ALL;

-- run the patch workflow

-- now for re3data
-- COPY existing rows
COPY (SELECT * FROM dsm_datasource_organization WHERE datasource like 're3data%') TO '/tmp/re3data_datasource_orgs.csv' WITH DELIMITER ';' CSV HEADER;
COPY (SELECT * FROM dsm_organizations WHERE id like 're3data%') TO '/tmp/re3data_orgs.csv' WITH DELIMITER ';' CSV HEADER;
COPY (SELECT * FROM dsm_organizationpids  WHERE organization like 're3data%') TO '/tmp/re3data_orgs_pids.csv' WITH DELIMITER ';' CSV HEADER;

-- delete existing rows
ALTER TABLE dsm_datasources DISABLE TRIGGER ALL;
ALTER TABLE dsm_organizations DISABLE TRIGGER ALL;
ALTER TABLE dsm_organizationpids DISABLE TRIGGER ALL;
ALTER TABLE dsm_datasource_organization DISABLE TRIGGER ALL;

DELETE from dsm_datasource_organization where datasource like 're3data%';
DELETE from dsm_organizations where id like 're3data%';
DELETE from dsm_organizationpids where organization like 're3data%';

ALTER TABLE dsm_datasources ENABLE TRIGGER ALL;
ALTER TABLE dsm_organizations ENABLE TRIGGER ALL;
ALTER TABLE dsm_organizationpids ENABLE TRIGGER ALL;
ALTER TABLE dsm_datasource_organization ENABLE TRIGGER ALL;

-- run the patch workflow

