diff --git a/src/main/resources/db/migration/V6.1__Schema_data_migration_audit.sql b/src/main/resources/db/migration/V6.1__Schema_data_migration_audit.sql new file mode 100644 index 0000000000000000000000000000000000000000..85d87957104192ef25df13afc465c6fea6a5365f --- /dev/null +++ b/src/main/resources/db/migration/V6.1__Schema_data_migration_audit.sql @@ -0,0 +1,1666 @@ +-- -------------------------------------------------------------------------------- +-- About +-- migration script +-- postgresql 9.6.7 +-- Content +-- structures +-- names +-- data structures level 1 +-- systemgroup + audit +-- discipline + audit +-- data structures level 2 +-- system + audit +-- devicegroup + audit +-- data structures level 3 +-- subsystem + audit +-- devicetype + audit +-- data names +-- systemgroup_id and not devicetype_id + audit +-- system_id and not devicetype_id + audit +-- subsystem_id and not devicetype_id + audit +-- systemgroup_id and devicetype_id + audit +-- system_id and devicetype_id + audit +-- subsystem_id and devicetype_id + audit +-- index +-- sequence +-- primary key +-- foreign key +-- function +-- Note +-- about audit tables and data +-- considered temporary +-- notice prefix "tmp" and similar +-- order of items is important +-- -------------------------------------------------------------------------------- +-- data structures (parent_id) +-- data names +-- selection of proper id to refer to - systemgroup, system, subsystem, devicetype +-- exclude content (with latest) before latest +-- exclude content (with latest) after latest (cancelled, rejected) +-- keep most recent content (without latest) (to have most recent in line of uuid without latest) +-- <--> +-- select * from structure s +-- where ( +-- not (exists (select s2.id from structure s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select s2.id from structure s2 where s2."uuid" = s."uuid" and s2.latest=true)) +-- and not (exists (select s2.id from structure s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id > (select s2.id from structure s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.status in ('CANCELLED', 'REJECTED')) +-- and not (not exists (select s2.id from structure s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select max(s2.id) from structure s2 where s2."uuid" = s."uuid" and s2.latest=false)) +-- ) +-- -------------------------------------------------------------------------------- +-- retrieve parent id for audit structure +-- 1 or 2 or 3 +-- 1. get id from parent that is approved and was processed prior to child or within 10 seconds +-- 2. get id from parent that is approved and was requested prior to child or within 10 minutes +-- 3. get id from parent that was requested prior to child or within 10 minutes +-- <--> +-- coalesce ( +-- (select max(s3.id) from parent s3 where s3."uuid" = s.parent_uuid and s3.status = 'APPROVED' and s3.processed < (s.processed + interval '10 seconds')), +-- (select max(s3.id) from parent s3 where s3."uuid" = s.parent_uuid and s3.status = 'APPROVED' and s3.requested < (s.processed + interval '10 minutes')), +-- (select max(s3.id) from parent s3 where s3."uuid" = s.parent_uuid and s3.requested < (s.processed + interval '10 minutes')) +-- ) +-- -------------------------------------------------------------------------------- +-- non-audit tables contain valid +-- audit tables contain obsolete +-- -------------------------------------------------------------------------------- + +-- -------------------------------------------------------------------------------- +-- structures +-- -------------------------------------------------------------------------------- +CREATE TABLE IF NOT EXISTS tmp_systemgroup ( + id bigint NOT NULL, + version integer, + uuid text NOT NULL, + mnemonic text, + mnemonic_equivalence text, + ordering integer, + description text, + status text, + -- latest boolean NOT NULL, + deleted boolean NOT NULL, + requested timestamp without time zone, + requested_by text, + requested_comment text, + processed timestamp without time zone, + processed_by text, + processed_comment text +); + +CREATE TABLE IF NOT EXISTS tmp_system ( + id bigint NOT NULL, + version integer, + uuid text NOT NULL, + parent_id bigint NOT NULL, + mnemonic text, + mnemonic_equivalence text, + ordering integer, + description text, + status text, + -- latest boolean NOT NULL, + deleted boolean NOT NULL, + requested timestamp without time zone, + requested_by text, + requested_comment text, + processed timestamp without time zone, + processed_by text, + processed_comment text +); + +CREATE TABLE IF NOT EXISTS tmp_subsystem ( + id bigint NOT NULL, + version integer, + uuid text NOT NULL, + parent_id bigint NOT NULL, + mnemonic text, + mnemonic_equivalence text, + ordering integer, + description text, + status text, + -- latest boolean NOT NULL, + deleted boolean NOT NULL, + requested timestamp without time zone, + requested_by text, + requested_comment text, + processed timestamp without time zone, + processed_by text, + processed_comment text +); + +-- -------------------------------------------------------------------------------- +CREATE TABLE IF NOT EXISTS tmp_discipline ( + id bigint NOT NULL, + version integer, + uuid text NOT NULL, + mnemonic text, + mnemonic_equivalence text, + ordering integer, + description text, + status text, + -- latest boolean NOT NULL, + deleted boolean NOT NULL, + requested timestamp without time zone, + requested_by text, + requested_comment text, + processed timestamp without time zone, + processed_by text, + processed_comment text +); + +CREATE TABLE IF NOT EXISTS tmp_devicegroup ( + id bigint NOT NULL, + version integer, + uuid text NOT NULL, + parent_id bigint NOT NULL, + mnemonic text, + mnemonic_equivalence text, + ordering integer, + description text, + status text, + -- latest boolean NOT NULL, + deleted boolean NOT NULL, + requested timestamp without time zone, + requested_by text, + requested_comment text, + processed timestamp without time zone, + processed_by text, + processed_comment text +); + +CREATE TABLE IF NOT EXISTS tmp_devicetype ( + id bigint NOT NULL, + version integer, + uuid text NOT NULL, + parent_id bigint NOT NULL, + mnemonic text, + mnemonic_equivalence text, + ordering integer, + description text, + status text, + -- latest boolean NOT NULL, + deleted boolean NOT NULL, + requested timestamp without time zone, + requested_by text, + requested_comment text, + processed timestamp without time zone, + processed_by text, + processed_comment text +); + +-- -------------------------------------------------------------------------------- +-- name +-- -------------------------------------------------------------------------------- +CREATE TABLE IF NOT EXISTS tmp_name ( + id bigint NOT NULL, + version integer, + uuid text NOT NULL, + systemgroup_id bigint, + system_id bigint, + subsystem_id bigint, + devicetype_id bigint, + instance_index text, + convention_name text, + convention_name_equivalence text, + description text, + status text, + -- latest boolean NOT NULL, + deleted boolean NOT NULL, + requested timestamp without time zone, + requested_by text, + requested_comment text, + processed timestamp without time zone, + processed_by text, + processed_comment text +); + +-- -------------------------------------------------------------------------------- +-- audit +-- -------------------------------------------------------------------------------- + +CREATE TABLE IF NOT EXISTS tmp_audit_structure ( + audit_id bigint NOT NULL, + audit_version integer, + audit_table text NOT NULL, + audit_operation text, + id bigint NOT NULL, + version integer, + uuid text NOT NULL, + parent_id bigint, + mnemonic text, + mnemonic_equivalence text, + ordering integer, + description text, + status text, + -- latest boolean NOT NULL, + deleted boolean NOT NULL, + requested timestamp without time zone, + requested_by text, + requested_comment text, + processed timestamp without time zone, + processed_by text, + processed_comment text +); + +CREATE TABLE IF NOT EXISTS tmp_audit_name ( + audit_id bigint NOT NULL, + audit_version integer, + audit_table text NOT NULL, + audit_operation text, + id bigint NOT NULL, + version integer, + uuid text NOT NULL, + systemgroup_id bigint, + system_id bigint, + subsystem_id bigint, + devicetype_id bigint, + instance_index text, + convention_name text, + convention_name_equivalence text, + description text, + status text, + -- latest boolean NOT NULL, + deleted boolean NOT NULL, + requested timestamp without time zone, + requested_by text, + requested_comment text, + processed timestamp without time zone, + processed_by text, + processed_comment text +); + +-- -------------------------------------------------------------------------------- +-- data structures level 1 +-- systemgroup + audit +-- discipline + audit +-- -------------------------------------------------------------------------------- +-- from systemgroup +-- from discipline +-- -------------------------------------------------------------------------------- +insert into tmp_systemgroup ( + id, + version, + uuid, + mnemonic, + mnemonic_equivalence, + ordering, + description, + status, + -- latest, + deleted, + requested, + requested_by, + requested_comment, + processed, + processed_by, + processed_comment +) +select +s.id, s."version", s.uuid, s.mnemonic, s.mnemonic_equivalence, s.ordering, s.description, s.status, s.deleted, +s.requested, s.requested_by, s.requested_comment, s.processed, s.processed_by, s.processed_comment from systemgroup s +where ( + not (exists (select s2.id from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select s2.id from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=true)) + and not (exists (select s2.id from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id > (select s2.id from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.status in ('CANCELLED', 'REJECTED')) + and not (not exists (select s2.id from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select max(s2.id) from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=false)) +); + +insert into tmp_audit_structure ( + audit_id, + audit_version, + audit_table, + audit_operation, + id, + version, + uuid, + parent_id, + mnemonic, + mnemonic_equivalence, + ordering, + description, + status, + -- latest, + deleted, + requested, + requested_by, + requested_comment, + processed, + processed_by, + processed_comment +) +select s.id, s."version", 'systemgroup', null, +s.id, s.version, s.uuid, null, s.mnemonic, s.mnemonic_equivalence, s.ordering, s.description, s.status, s.deleted, +s.requested, s.requested_by, s.requested_comment, s.processed, s.processed_by, s.processed_comment from systemgroup s +where not ( + not (exists (select s2.id from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select s2.id from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=true)) + and not (exists (select s2.id from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id > (select s2.id from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.status in ('CANCELLED', 'REJECTED')) + and not (not exists (select s2.id from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select max(s2.id) from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=false)) +); + +-- -------------------------------------------------------------------------------- +insert into tmp_discipline ( + id, + version, + uuid, + mnemonic, + mnemonic_equivalence, + ordering, + description, + status, + -- latest, + deleted, + requested, + requested_by, + requested_comment, + processed, + processed_by, + processed_comment +) +select +s.id, s."version", s.uuid, s.mnemonic, s.mnemonic_equivalence, s.ordering, s.description, s.status, s.deleted, +s.requested, s.requested_by, s.requested_comment, s.processed, s.processed_by, s.processed_comment from discipline s +where ( + not (exists (select s2.id from discipline s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select s2.id from discipline s2 where s2."uuid" = s."uuid" and s2.latest=true)) + and not (exists (select s2.id from discipline s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id > (select s2.id from discipline s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.status in ('CANCELLED', 'REJECTED')) + and not (not exists (select s2.id from discipline s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select max(s2.id) from discipline s2 where s2."uuid" = s."uuid" and s2.latest=false)) +); + +insert into tmp_audit_structure ( + audit_id, + audit_version, + audit_table, + audit_operation, + id, + version, + uuid, + parent_id, + mnemonic, + mnemonic_equivalence, + ordering, + description, + status, + -- latest, + deleted, + requested, + requested_by, + requested_comment, + processed, + processed_by, + processed_comment +) +select s.id, s."version", 'discipline', null, +s.id, s.version, s.uuid, null, s.mnemonic, s.mnemonic_equivalence, s.ordering, s.description, s.status, s.deleted, +s.requested, s.requested_by, s.requested_comment, s.processed, s.processed_by, s.processed_comment from discipline s +where not ( + not (exists (select s2.id from discipline s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select s2.id from discipline s2 where s2."uuid" = s."uuid" and s2.latest=true)) + and not (exists (select s2.id from discipline s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id > (select s2.id from discipline s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.status in ('CANCELLED', 'REJECTED')) + and not (not exists (select s2.id from discipline s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select max(s2.id) from discipline s2 where s2."uuid" = s."uuid" and s2.latest=false)) +); + +-- -------------------------------------------------------------------------------- +-- data structures level 2 +-- system + audit +-- devicegroup + audit +-- -------------------------------------------------------------------------------- +-- from system +-- from devicegroup +-- -------------------------------------------------------------------------------- +insert into tmp_system ( + id, + version, + uuid, + parent_id, + mnemonic, + mnemonic_equivalence, + ordering, + description, + status, + -- latest, + deleted, + requested, + requested_by, + requested_comment, + processed, + processed_by, + processed_comment +) +select +s.id, s."version", s.uuid, +(select sg.id from systemgroup sg +where sg.uuid = s.parent_uuid and ( + not (exists (select s2.id from systemgroup s2 where s2."uuid" = sg."uuid" and s2.latest=true) and sg.id < (select s2.id from systemgroup s2 where s2."uuid" = sg."uuid" and s2.latest=true)) + and not (exists (select s2.id from systemgroup s2 where s2."uuid" = sg."uuid" and s2.latest=true) and sg.id > (select s2.id from systemgroup s2 where s2."uuid" = sg."uuid" and s2.latest=true) and sg.status in ('CANCELLED', 'REJECTED')) + and not (not exists (select s2.id from systemgroup s2 where s2."uuid" = sg."uuid" and s2.latest=true) and sg.id < (select max(s2.id) from systemgroup s2 where s2."uuid" = sg."uuid" and s2.latest=false)) +)), +s.mnemonic, s.mnemonic_equivalence, s.ordering, s.description, s.status, s.deleted, +s.requested, s.requested_by, s.requested_comment, s.processed, s.processed_by, s.processed_comment from system s +where ( + not (exists (select s2.id from system s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select s2.id from system s2 where s2."uuid" = s."uuid" and s2.latest=true)) + and not (exists (select s2.id from system s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id > (select s2.id from system s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.status in ('CANCELLED', 'REJECTED')) + and not (not exists (select s2.id from system s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select max(s2.id) from system s2 where s2."uuid" = s."uuid" and s2.latest=false)) +); + +insert into tmp_audit_structure ( + audit_id, + audit_version, + audit_table, + audit_operation, + id, + version, + uuid, + parent_id, + mnemonic, + mnemonic_equivalence, + ordering, + description, + status, + -- latest, + deleted, + requested, + requested_by, + requested_comment, + processed, + processed_by, + processed_comment +) +select s.id, s."version", 'system', null, +s.id, s.version, s.uuid, +(coalesce ( +(select max(s3.id) from systemgroup s3 where s3."uuid" = s.parent_uuid and s3.status = 'APPROVED' and s3.processed < (s.processed + interval '10 seconds')), +(select max(s3.id) from systemgroup s3 where s3."uuid" = s.parent_uuid and s3.status = 'APPROVED' and s3.requested < (s.processed + interval '10 minutes')), +(select max(s3.id) from systemgroup s3 where s3."uuid" = s.parent_uuid and s3.requested < (s.processed + interval '10 minutes')) +)), +s.mnemonic, s.mnemonic_equivalence, s.ordering, s.description, s.status, s.deleted, +s.requested, s.requested_by, s.requested_comment, s.processed, s.processed_by, s.processed_comment from system s +where not ( + not (exists (select s2.id from system s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select s2.id from system s2 where s2."uuid" = s."uuid" and s2.latest=true)) + and not (exists (select s2.id from system s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id > (select s2.id from system s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.status in ('CANCELLED', 'REJECTED')) + and not (not exists (select s2.id from system s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select max(s2.id) from system s2 where s2."uuid" = s."uuid" and s2.latest=false)) +); + +-- -------------------------------------------------------------------------------- +insert into tmp_devicegroup ( + id, + version, + uuid, + parent_id, + mnemonic, + mnemonic_equivalence, + ordering, + description, + status, + -- latest, + deleted, + requested, + requested_by, + requested_comment, + processed, + processed_by, + processed_comment +) +select +s.id, s."version", s.uuid, +(select di.id from discipline di +where di.uuid = s.parent_uuid and ( + not (exists (select s2.id from discipline s2 where s2."uuid" = di."uuid" and s2.latest=true) and di.id < (select s2.id from discipline s2 where s2."uuid" = di."uuid" and s2.latest=true)) + and not (exists (select s2.id from discipline s2 where s2."uuid" = di."uuid" and s2.latest=true) and di.id > (select s2.id from discipline s2 where s2."uuid" = di."uuid" and s2.latest=true) and di.status in ('CANCELLED', 'REJECTED')) + and not (not exists (select s2.id from discipline s2 where s2."uuid" = di."uuid" and s2.latest=true) and di.id < (select max(s2.id) from discipline s2 where s2."uuid" = di."uuid" and s2.latest=false)) +)), +s.mnemonic, s.mnemonic_equivalence, s.ordering, s.description, s.status, s.deleted, +s.requested, s.requested_by, s.requested_comment, s.processed, s.processed_by, s.processed_comment from devicegroup s +where ( + not (exists (select s2.id from devicegroup s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select s2.id from devicegroup s2 where s2."uuid" = s."uuid" and s2.latest=true)) + and not (exists (select s2.id from devicegroup s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id > (select s2.id from devicegroup s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.status in ('CANCELLED', 'REJECTED')) + and not (not exists (select s2.id from devicegroup s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select max(s2.id) from devicegroup s2 where s2."uuid" = s."uuid" and s2.latest=false)) +); + +insert into tmp_audit_structure ( + audit_id, + audit_version, + audit_table, + audit_operation, + id, + version, + uuid, + parent_id, + mnemonic, + mnemonic_equivalence, + ordering, + description, + status, + -- latest, + deleted, + requested, + requested_by, + requested_comment, + processed, + processed_by, + processed_comment +) +select s.id, s."version", 'devicegroup', null, +s.id, s.version, s.uuid, +(coalesce ( +(select max(s3.id) from discipline s3 where s3."uuid" = s.parent_uuid and s3.status = 'APPROVED' and s3.processed < (s.processed + interval '10 seconds')), +(select max(s3.id) from discipline s3 where s3."uuid" = s.parent_uuid and s3.status = 'APPROVED' and s3.requested < (s.processed + interval '10 minutes')), +(select max(s3.id) from discipline s3 where s3."uuid" = s.parent_uuid and s3.requested < (s.processed + interval '10 minutes')) +)), +s.mnemonic, s.mnemonic_equivalence, s.ordering, s.description, s.status, s.deleted, +s.requested, s.requested_by, s.requested_comment, s.processed, s.processed_by, s.processed_comment from devicegroup s +where not ( + not (exists (select s2.id from devicegroup s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select s2.id from devicegroup s2 where s2."uuid" = s."uuid" and s2.latest=true)) + and not (exists (select s2.id from devicegroup s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id > (select s2.id from devicegroup s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.status in ('CANCELLED', 'REJECTED')) + and not (not exists (select s2.id from devicegroup s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select max(s2.id) from devicegroup s2 where s2."uuid" = s."uuid" and s2.latest=false)) +); + +-- -------------------------------------------------------------------------------- +-- data structures level 3 +-- subsystem + audit +-- devicetype + audit +-- -------------------------------------------------------------------------------- +-- from subsystem +-- from devicetype +-- -------------------------------------------------------------------------------- +insert into tmp_subsystem ( + id, + version, + uuid, + parent_id, + mnemonic, + mnemonic_equivalence, + ordering, + description, + status, + -- latest, + deleted, + requested, + requested_by, + requested_comment, + processed, + processed_by, + processed_comment +) +select +s.id, s."version", s.uuid, +(select sys.id from system sys +where sys.uuid = s.parent_uuid and ( + not (exists (select s2.id from system s2 where s2."uuid" = sys."uuid" and s2.latest=true) and sys.id < (select s2.id from system s2 where s2."uuid" = sys."uuid" and s2.latest=true)) + and not (exists (select s2.id from system s2 where s2."uuid" = sys."uuid" and s2.latest=true) and sys.id > (select s2.id from system s2 where s2."uuid" = sys."uuid" and s2.latest=true) and sys.status in ('CANCELLED', 'REJECTED')) + and not (not exists (select s2.id from system s2 where s2."uuid" = sys."uuid" and s2.latest=true) and sys.id < (select max(s2.id) from system s2 where s2."uuid" = sys."uuid" and s2.latest=false)) +)), +s.mnemonic, s.mnemonic_equivalence, s.ordering, s.description, s.status, s.deleted, +s.requested, s.requested_by, s.requested_comment, s.processed, s.processed_by, s.processed_comment from subsystem s +where ( + not (exists (select s2.id from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select s2.id from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=true)) + and not (exists (select s2.id from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id > (select s2.id from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.status in ('CANCELLED', 'REJECTED')) + and not (not exists (select s2.id from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select max(s2.id) from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=false)) +); + +insert into tmp_audit_structure ( + audit_id, + audit_version, + audit_table, + audit_operation, + id, + version, + uuid, + parent_id, + mnemonic, + mnemonic_equivalence, + ordering, + description, + status, + -- latest, + deleted, + requested, + requested_by, + requested_comment, + processed, + processed_by, + processed_comment +) +select s.id, s."version", 'subsystem', null, +s.id, s.version, s.uuid, +(coalesce ( +(select max(s3.id) from "system" s3 where s3."uuid" = s.parent_uuid and s3.status = 'APPROVED' and s3.processed < (s.processed + interval '10 seconds')), +(select max(s3.id) from "system" s3 where s3."uuid" = s.parent_uuid and s3.status = 'APPROVED' and s3.requested < (s.processed + interval '10 minutes')), +(select max(s3.id) from "system" s3 where s3."uuid" = s.parent_uuid and s3.requested < (s.processed + interval '10 minutes')) +)), +s.mnemonic, s.mnemonic_equivalence, s.ordering, s.description, s.status, s.deleted, +s.requested, s.requested_by, s.requested_comment, s.processed, s.processed_by, s.processed_comment from subsystem s +where not ( + not (exists (select s2.id from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select s2.id from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=true)) + and not (exists (select s2.id from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id > (select s2.id from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.status in ('CANCELLED', 'REJECTED')) + and not (not exists (select s2.id from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select max(s2.id) from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=false)) +); + +-- -------------------------------------------------------------------------------- +insert into tmp_devicetype ( + id, + version, + uuid, + parent_id, + mnemonic, + mnemonic_equivalence, + ordering, + description, + status, + -- latest, + deleted, + requested, + requested_by, + requested_comment, + processed, + processed_by, + processed_comment +) +select +s.id, s."version", s.uuid, +(select dg.id from devicegroup dg +where dg.uuid = s.parent_uuid and ( + not (exists (select s2.id from devicegroup s2 where s2."uuid" = dg."uuid" and s2.latest=true) and dg.id < (select s2.id from devicegroup s2 where s2."uuid" = dg."uuid" and s2.latest=true)) + and not (exists (select s2.id from devicegroup s2 where s2."uuid" = dg."uuid" and s2.latest=true) and dg.id > (select s2.id from devicegroup s2 where s2."uuid" = dg."uuid" and s2.latest=true) and dg.status in ('CANCELLED', 'REJECTED')) + and not (not exists (select s2.id from devicegroup s2 where s2."uuid" = dg."uuid" and s2.latest=true) and dg.id < (select max(s2.id) from devicegroup s2 where s2."uuid" = dg."uuid" and s2.latest=false)) +)), +s.mnemonic, s.mnemonic_equivalence, s.ordering, s.description, s.status, s.deleted, +s.requested, s.requested_by, s.requested_comment, s.processed, s.processed_by, s.processed_comment from devicetype s +where ( + not (exists (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true)) + and not (exists (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id > (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.status in ('CANCELLED', 'REJECTED')) + and not (not exists (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select max(s2.id) from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=false)) +); + +insert into tmp_audit_structure ( + audit_id, + audit_version, + audit_table, + audit_operation, + id, + version, + uuid, + parent_id, + mnemonic, + mnemonic_equivalence, + ordering, + description, + status, + -- latest, + deleted, + requested, + requested_by, + requested_comment, + processed, + processed_by, + processed_comment +) +select s.id, s."version", 'devicetype', null, +s.id, s.version, s.uuid, +(coalesce ( +(select max(s3.id) from devicegroup s3 where s3."uuid" = s.parent_uuid and s3.status = 'APPROVED' and s3.processed < (s.processed + interval '10 seconds')), +(select max(s3.id) from devicegroup s3 where s3."uuid" = s.parent_uuid and s3.status = 'APPROVED' and s3.requested < (s.processed + interval '10 minutes')), +(select max(s3.id) from devicegroup s3 where s3."uuid" = s.parent_uuid and s3.requested < (s.processed + interval '10 minutes')) +)), +s.mnemonic, s.mnemonic_equivalence, s.ordering, s.description, s.status, s.deleted, +s.requested, s.requested_by, s.requested_comment, s.processed, s.processed_by, s.processed_comment from devicetype s +where not ( + not (exists (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true)) + and not (exists (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id > (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.status in ('CANCELLED', 'REJECTED')) + and not (not exists (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select max(s2.id) from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=false)) +); + +-- -------------------------------------------------------------------------------- +-- data names +-- 1) systemgroup_id and not devicetype_id + audit +-- 2) system_id and not devicetype_id + audit +-- 3) subsystem_id and not devicetype_id + audit +-- 4) systemgroup_id and devicetype_id + audit +-- 5) system_id and devicetype_id + audit +-- 6) subsystem_id and devicetype_id + audit +-- -------------------------------------------------------------------------------- +-- from systemgroup +-- from systemg +-- from subsystem +-- from systemgroup + devicetype +-- from systemg + devicetype +-- from subsystem + devicetype +-- -------------------------------------------------------------------------------- +-- 1) +insert into tmp_name ( + id, + version, + uuid, + systemgroup_id, + system_id, + subsystem_id, + devicetype_id, + instance_index, + convention_name, + convention_name_equivalence, + description, + status, + -- latest, + deleted, + requested, + requested_by, + requested_comment, + processed, + processed_by, + processed_comment +) +select n.id, n."version", n.uuid, +(select s.id from systemgroup s +where s.uuid = n.systemgroup_uuid and ( + not (exists (select s2.id from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select s2.id from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=true)) + and not (exists (select s2.id from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id > (select s2.id from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.status in ('CANCELLED', 'REJECTED')) + and not (not exists (select s2.id from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select max(s2.id) from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=false)) +)), +null, +null, +null, +n.instance_index, n.convention_name, n.convention_name_equivalence, n.description, n.status, n.deleted, +n.requested, n.requested_by, n.requested_comment, n.processed, n.processed_by, n.processed_comment from name n +where not (n.latest = false) +and n.systemgroup_uuid is not null and n.system_uuid is null and n.subsystem_uuid is null and n.devicetype_uuid is null; + +insert into tmp_audit_name ( + audit_id, + audit_version, + audit_table, + audit_operation, + id, + version, + uuid, + systemgroup_id, + system_id, + subsystem_id, + devicetype_id, + instance_index, + convention_name, + convention_name_equivalence, + description, + status, + -- latest, + deleted, + requested, + requested_by, + requested_comment, + processed, + processed_by, + processed_comment +) +select n.id, n."version", 'name', null, +n.id, n."version", n.uuid, +(select s.id from systemgroup s +where s.uuid = n.systemgroup_uuid and ( + not (exists (select s2.id from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select s2.id from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=true)) + and not (exists (select s2.id from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id > (select s2.id from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.status in ('CANCELLED', 'REJECTED')) + and not (not exists (select s2.id from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select max(s2.id) from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=false)) +)), +null, +null, +null, +n.instance_index, n.convention_name, n.convention_name_equivalence, n.description, n.status, n.deleted, +n.requested, n.requested_by, n.requested_comment, n.processed, n.processed_by, n.processed_comment from name n +where +(n.latest = false) and +n.systemgroup_uuid is not null and n.system_uuid is null and n.subsystem_uuid is null and n.devicetype_uuid is null; + +-- -------------------------------------------------------------------------------- +-- 2) +insert into tmp_name ( + id, + version, + uuid, + systemgroup_id, + system_id, + subsystem_id, + devicetype_id, + instance_index, + convention_name, + convention_name_equivalence, + description, + status, + -- latest, + deleted, + requested, + requested_by, + requested_comment, + processed, + processed_by, + processed_comment +) +select n.id, n."version", n.uuid, +null, +(select s.id from system s +where s.uuid = n.system_uuid and ( + not (exists (select s2.id from system s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select s2.id from system s2 where s2."uuid" = s."uuid" and s2.latest=true)) + and not (exists (select s2.id from system s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id > (select s2.id from system s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.status in ('CANCELLED', 'REJECTED')) + and not (not exists (select s2.id from system s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select max(s2.id) from system s2 where s2."uuid" = s."uuid" and s2.latest=false)) +)), +null, +null, +n.instance_index, n.convention_name, n.convention_name_equivalence, n.description, n.status, n.deleted, +n.requested, n.requested_by, n.requested_comment, n.processed, n.processed_by, n.processed_comment from name n +where not (n.latest = false) +and n.systemgroup_uuid is null and n.system_uuid is not null and n.subsystem_uuid is null and n.devicetype_uuid is null; + +insert into tmp_audit_name ( + audit_id, + audit_version, + audit_table, + audit_operation, + id, + version, + uuid, + systemgroup_id, + system_id, + subsystem_id, + devicetype_id, + instance_index, + convention_name, + convention_name_equivalence, + description, + status, + -- latest, + deleted, + requested, + requested_by, + requested_comment, + processed, + processed_by, + processed_comment +) +select n.id, n."version", 'name', null, +n.id, n."version", n.uuid, +null, +(select s.id from system s +where s.uuid = n.system_uuid and ( + not (exists (select s2.id from system s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select s2.id from system s2 where s2."uuid" = s."uuid" and s2.latest=true)) + and not (exists (select s2.id from system s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id > (select s2.id from system s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.status in ('CANCELLED', 'REJECTED')) + and not (not exists (select s2.id from system s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select max(s2.id) from system s2 where s2."uuid" = s."uuid" and s2.latest=false)) +)), +null, +null, +n.instance_index, n.convention_name, n.convention_name_equivalence, n.description, n.status, n.deleted, +n.requested, n.requested_by, n.requested_comment, n.processed, n.processed_by, n.processed_comment from name n +where +(n.latest = false) and +n.systemgroup_uuid is null and n.system_uuid is not null and n.subsystem_uuid is null and n.devicetype_uuid is null; + +-- -------------------------------------------------------------------------------- +-- 3) +insert into tmp_name ( + id, + version, + uuid, + systemgroup_id, + system_id, + subsystem_id, + devicetype_id, + instance_index, + convention_name, + convention_name_equivalence, + description, + status, + -- latest, + deleted, + requested, + requested_by, + requested_comment, + processed, + processed_by, + processed_comment +) +select n.id, n."version", n.uuid, +null, +null, +(select s.id from subsystem s +where s.uuid = n.subsystem_uuid and ( + not (exists (select s2.id from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select s2.id from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=true)) + and not (exists (select s2.id from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id > (select s2.id from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.status in ('CANCELLED', 'REJECTED')) + and not (not exists (select s2.id from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select max(s2.id) from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=false)) +)), +null, +n.instance_index, n.convention_name, n.convention_name_equivalence, n.description, n.status, n.deleted, +n.requested, n.requested_by, n.requested_comment, n.processed, n.processed_by, n.processed_comment from name n +where not (n.latest = false) +and n.systemgroup_uuid is null and n.system_uuid is null and n.subsystem_uuid is not null and n.devicetype_uuid is null; + +insert into tmp_audit_name ( + audit_id, + audit_version, + audit_table, + audit_operation, + id, + version, + uuid, + systemgroup_id, + system_id, + subsystem_id, + devicetype_id, + instance_index, + convention_name, + convention_name_equivalence, + description, + status, + -- latest, + deleted, + requested, + requested_by, + requested_comment, + processed, + processed_by, + processed_comment +) +select n.id, n."version", 'name', null, +n.id, n."version", n.uuid, +null, +null, +(select s.id from subsystem s +where s.uuid = n.subsystem_uuid and ( + not (exists (select s2.id from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select s2.id from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=true)) + and not (exists (select s2.id from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id > (select s2.id from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.status in ('CANCELLED', 'REJECTED')) + and not (not exists (select s2.id from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select max(s2.id) from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=false)) +)), +null, +n.instance_index, n.convention_name, n.convention_name_equivalence, n.description, n.status, n.deleted, +n.requested, n.requested_by, n.requested_comment, n.processed, n.processed_by, n.processed_comment from name n +where +(n.latest = false) and +n.systemgroup_uuid is null and n.system_uuid is null and n.subsystem_uuid is not null and n.devicetype_uuid is null; + +-- -------------------------------------------------------------------------------- +-- 4) +insert into tmp_name ( + id, + version, + uuid, + systemgroup_id, + system_id, + subsystem_id, + devicetype_id, + instance_index, + convention_name, + convention_name_equivalence, + description, + status, + -- latest, + deleted, + requested, + requested_by, + requested_comment, + processed, + processed_by, + processed_comment +) +select n.id, n."version", n.uuid, +(select s.id from systemgroup s +where s.uuid = n.systemgroup_uuid and ( + not (exists (select s2.id from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select s2.id from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=true)) + and not (exists (select s2.id from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id > (select s2.id from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.status in ('CANCELLED', 'REJECTED')) + and not (not exists (select s2.id from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select max(s2.id) from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=false)) +)), +null, +null, +(select s.id from devicetype s +where s.uuid = n.devicetype_uuid and ( + not (exists (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true)) + and not (exists (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id > (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.status in ('CANCELLED', 'REJECTED')) + and not (not exists (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select max(s2.id) from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=false)) +)), +n.instance_index, n.convention_name, n.convention_name_equivalence, n.description, n.status, n.deleted, +n.requested, n.requested_by, n.requested_comment, n.processed, n.processed_by, n.processed_comment from name n +where not (n.latest = false) +and n.systemgroup_uuid is not null and n.system_uuid is null and n.subsystem_uuid is null and n.devicetype_uuid is not null; + +insert into tmp_audit_name ( + audit_id, + audit_version, + audit_table, + audit_operation, + id, + version, + uuid, + systemgroup_id, + system_id, + subsystem_id, + devicetype_id, + instance_index, + convention_name, + convention_name_equivalence, + description, + status, + -- latest, + deleted, + requested, + requested_by, + requested_comment, + processed, + processed_by, + processed_comment +) +select n.id, n."version", 'name', null, +n.id, n."version", n.uuid, +(select s.id from systemgroup s +where s.uuid = n.systemgroup_uuid and ( + not (exists (select s2.id from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select s2.id from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=true)) + and not (exists (select s2.id from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id > (select s2.id from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.status in ('CANCELLED', 'REJECTED')) + and not (not exists (select s2.id from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select max(s2.id) from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=false)) +)), +null, +null, +(select s.id from devicetype s +where s.uuid = n.devicetype_uuid and ( + not (exists (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true)) + and not (exists (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id > (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.status in ('CANCELLED', 'REJECTED')) + and not (not exists (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select max(s2.id) from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=false)) +)), +n.instance_index, n.convention_name, n.convention_name_equivalence, n.description, n.status, n.deleted, +n.requested, n.requested_by, n.requested_comment, n.processed, n.processed_by, n.processed_comment from name n +where +(n.latest = false) and +n.systemgroup_uuid is not null and n.system_uuid is null and n.subsystem_uuid is null and n.devicetype_uuid is not null; + +-- -------------------------------------------------------------------------------- +-- 5) +insert into tmp_name ( + id, + version, + uuid, + systemgroup_id, + system_id, + subsystem_id, + devicetype_id, + instance_index, + convention_name, + convention_name_equivalence, + description, + status, + -- latest, + deleted, + requested, + requested_by, + requested_comment, + processed, + processed_by, + processed_comment +) +select n.id, n."version", n.uuid, +null, +(select s.id from system s +where s.uuid = n.system_uuid and ( + not (exists (select s2.id from system s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select s2.id from system s2 where s2."uuid" = s."uuid" and s2.latest=true)) + and not (exists (select s2.id from system s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id > (select s2.id from system s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.status in ('CANCELLED', 'REJECTED')) + and not (not exists (select s2.id from system s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select max(s2.id) from system s2 where s2."uuid" = s."uuid" and s2.latest=false)) +)), +null, +(select s.id from devicetype s +where s.uuid = n.devicetype_uuid and ( + not (exists (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true)) + and not (exists (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id > (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.status in ('CANCELLED', 'REJECTED')) + and not (not exists (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select max(s2.id) from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=false)) +)), +n.instance_index, n.convention_name, n.convention_name_equivalence, n.description, n.status, n.deleted, +n.requested, n.requested_by, n.requested_comment, n.processed, n.processed_by, n.processed_comment from name n +where not (n.latest = false) +and n.systemgroup_uuid is null and n.system_uuid is not null and n.subsystem_uuid is null and n.devicetype_uuid is not null; + +insert into tmp_audit_name ( + audit_id, + audit_version, + audit_table, + audit_operation, + id, + version, + uuid, + systemgroup_id, + system_id, + subsystem_id, + devicetype_id, + instance_index, + convention_name, + convention_name_equivalence, + description, + status, + -- latest, + deleted, + requested, + requested_by, + requested_comment, + processed, + processed_by, + processed_comment +) +select n.id, n."version", 'name', null, +n.id, n."version", n.uuid, +null, +(select s.id from system s +where s.uuid = n.system_uuid and ( + not (exists (select s2.id from system s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select s2.id from system s2 where s2."uuid" = s."uuid" and s2.latest=true)) + and not (exists (select s2.id from system s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id > (select s2.id from system s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.status in ('CANCELLED', 'REJECTED')) + and not (not exists (select s2.id from system s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select max(s2.id) from system s2 where s2."uuid" = s."uuid" and s2.latest=false)) +)), +null, +(select s.id from devicetype s +where s.uuid = n.devicetype_uuid and ( + not (exists (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true)) + and not (exists (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id > (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.status in ('CANCELLED', 'REJECTED')) + and not (not exists (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select max(s2.id) from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=false)) +)), +n.instance_index, n.convention_name, n.convention_name_equivalence, n.description, n.status, n.deleted, +n.requested, n.requested_by, n.requested_comment, n.processed, n.processed_by, n.processed_comment from name n +where +(n.latest = false) and +n.systemgroup_uuid is null and n.system_uuid is not null and n.subsystem_uuid is null and n.devicetype_uuid is not null; + +-- -------------------------------------------------------------------------------- +-- 6) +insert into tmp_name ( + id, + version, + uuid, + systemgroup_id, + system_id, + subsystem_id, + devicetype_id, + instance_index, + convention_name, + convention_name_equivalence, + description, + status, + -- latest, + deleted, + requested, + requested_by, + requested_comment, + processed, + processed_by, + processed_comment +) +select n.id, n."version", n.uuid, +null, +null, +(select s.id from subsystem s +where s.uuid = n.subsystem_uuid and ( + not (exists (select s2.id from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select s2.id from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=true)) + and not (exists (select s2.id from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id > (select s2.id from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.status in ('CANCELLED', 'REJECTED')) + and not (not exists (select s2.id from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select max(s2.id) from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=false)) +)), +(select s.id from devicetype s +where s.uuid = n.devicetype_uuid and ( + not (exists (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true)) + and not (exists (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id > (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.status in ('CANCELLED', 'REJECTED')) + and not (not exists (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select max(s2.id) from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=false)) +)), +n.instance_index, n.convention_name, n.convention_name_equivalence, n.description, n.status, n.deleted, +n.requested, n.requested_by, n.requested_comment, n.processed, n.processed_by, n.processed_comment from name n +where not (n.latest = false) +and n.systemgroup_uuid is null and n.system_uuid is null and n.subsystem_uuid is not null and n.devicetype_uuid is not null; + +insert into tmp_audit_name ( + audit_id, + audit_version, + audit_table, + audit_operation, + id, + version, + uuid, + systemgroup_id, + system_id, + subsystem_id, + devicetype_id, + instance_index, + convention_name, + convention_name_equivalence, + description, + status, + -- latest, + deleted, + requested, + requested_by, + requested_comment, + processed, + processed_by, + processed_comment +) +select n.id, n."version", 'name', null, +n.id, n."version", n.uuid, +null, +null, +(select s.id from subsystem s +where s.uuid = n.subsystem_uuid and ( + not (exists (select s2.id from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select s2.id from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=true)) + and not (exists (select s2.id from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id > (select s2.id from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.status in ('CANCELLED', 'REJECTED')) + and not (not exists (select s2.id from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select max(s2.id) from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=false)) +)), +(select s.id from devicetype s +where s.uuid = n.devicetype_uuid and ( + not (exists (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true)) + and not (exists (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id > (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.status in ('CANCELLED', 'REJECTED')) + and not (not exists (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select max(s2.id) from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=false)) +)), +n.instance_index, n.convention_name, n.convention_name_equivalence, n.description, n.status, n.deleted, +n.requested, n.requested_by, n.requested_comment, n.processed, n.processed_by, n.processed_comment from name n +where +(n.latest = false) and +n.systemgroup_uuid is null and n.system_uuid is null and n.subsystem_uuid is not null and n.devicetype_uuid is not null; + +-- -------------------------------------------------------------------------------- +-- index +-- -------------------------------------------------------------------------------- +CREATE INDEX tmp_systemgroup_id_idx ON tmp_systemgroup (id); +CREATE INDEX tmp_systemgroup_uuid_idx ON tmp_systemgroup (uuid); +CREATE INDEX tmp_systemgroup_mnemonic_idx ON tmp_systemgroup (mnemonic); +CREATE INDEX tmp_systemgroup_status_idx ON tmp_systemgroup (status); +CREATE INDEX tmp_systemgroup_deleted_idx ON tmp_systemgroup (deleted); + +CREATE INDEX tmp_system_id_idx ON tmp_system (id); +CREATE INDEX tmp_system_uuid_idx ON tmp_system (uuid); +CREATE INDEX tmp_system_parent_id_idx ON tmp_system (parent_id); +CREATE INDEX tmp_system_mnemonic_idx ON tmp_system (mnemonic); +CREATE INDEX tmp_system_status_idx ON tmp_system (status); +CREATE INDEX tmp_system_deleted_idx ON tmp_system (deleted); + +CREATE INDEX tmp_subsystem_id_idx ON tmp_subsystem (id); +CREATE INDEX tmp_subsystem_uuid_idx ON tmp_subsystem (uuid); +CREATE INDEX tmp_subsystem_parent_id_idx ON tmp_subsystem (parent_id); +CREATE INDEX tmp_subsystem_mnemonic_idx ON tmp_subsystem (mnemonic); +CREATE INDEX tmp_subsystem_status_idx ON tmp_subsystem (status); +CREATE INDEX tmp_subsystem_deleted_idx ON tmp_subsystem (deleted); + +CREATE INDEX tmp_discipline_id_idx ON tmp_discipline (id); +CREATE INDEX tmp_discipline_uuid_idx ON tmp_discipline (uuid); +CREATE INDEX tmp_discipline_mnemonic_idx ON tmp_discipline (mnemonic); +CREATE INDEX tmp_discipline_status_idx ON tmp_discipline (status); +CREATE INDEX tmp_discipline_deleted_idx ON tmp_discipline (deleted); + +CREATE INDEX tmp_devicegroup_id_idx ON tmp_devicegroup (id); +CREATE INDEX tmp_devicegroup_uuid_idx ON tmp_devicegroup (uuid); +CREATE INDEX tmp_devicegroup_parent_id_idx ON tmp_devicegroup (parent_id); +CREATE INDEX tmp_devicegroup_mnemonic_idx ON tmp_devicegroup (mnemonic); +CREATE INDEX tmp_devicegroup_status_idx ON tmp_devicegroup (status); +CREATE INDEX tmp_devicegroup_deleted_idx ON tmp_devicegroup (deleted); + +CREATE INDEX tmp_devicetype_id_idx ON tmp_devicetype (id); +CREATE INDEX tmp_devicetype_uuid_idx ON tmp_devicetype (uuid); +CREATE INDEX tmp_devicetype_parent_id_idx ON tmp_devicetype (parent_id); +CREATE INDEX tmp_devicetype_mnemonic_idx ON tmp_devicetype (mnemonic); +CREATE INDEX tmp_devicetype_status_idx ON tmp_devicetype (status); +CREATE INDEX tmp_devicetype_deleted_idx ON tmp_devicetype (deleted); + +CREATE INDEX tmp_name_id_idx ON tmp_name (id); +CREATE INDEX tmp_name_uuid_idx ON tmp_name (uuid); +CREATE INDEX tmp_name_systemgroup_id_idx ON tmp_name (systemgroup_id); +CREATE INDEX tmp_name_system_id_idx ON tmp_name (system_id); +CREATE INDEX tmp_name_subsystem_id_idx ON tmp_name (subsystem_id); +CREATE INDEX tmp_name_devicetype_id_idx ON tmp_name (devicetype_id); +CREATE INDEX tmp_name_convention_name_idx ON tmp_name (convention_name); +CREATE INDEX tmp_name_status_idx ON tmp_name (status); +CREATE INDEX tmp_name_deleted_idx ON tmp_name (deleted); + +CREATE INDEX tmp_audit_structure_audit_id_idx ON tmp_audit_structure (audit_id); +CREATE INDEX tmp_audit_structure_audit_table_idx ON tmp_audit_structure (audit_table); +CREATE INDEX tmp_audit_structure_audit_operation_idx ON tmp_audit_structure (audit_operation); +CREATE INDEX tmp_audit_structure_id_idx ON tmp_audit_structure (id); +CREATE INDEX tmp_audit_structure_uuid_idx ON tmp_audit_structure (uuid); +CREATE INDEX tmp_audit_structure_parent_id_idx ON tmp_audit_structure (parent_id); +CREATE INDEX tmp_audit_structure_mnemonic_idx ON tmp_audit_structure (mnemonic); +CREATE INDEX tmp_audit_structure_status_idx ON tmp_audit_structure (status); +CREATE INDEX tmp_audit_structure_deleted_idx ON tmp_audit_structure (deleted); + +CREATE INDEX tmp_audit_name_audit_id_idx ON tmp_audit_name (audit_id); +CREATE INDEX tmp_audit_name_audit_table_idx ON tmp_audit_name (audit_table); +CREATE INDEX tmp_audit_name_audit_operation_idx ON tmp_audit_name (audit_operation); +CREATE INDEX tmp_audit_name_id_idx ON tmp_audit_name (id); +CREATE INDEX tmp_audit_name_uuid_idx ON tmp_audit_name (uuid); +CREATE INDEX tmp_audit_name_systemgroup_id_idx ON tmp_audit_name (systemgroup_id); +CREATE INDEX tmp_audit_name_system_id_idx on tmp_audit_name (system_id); +CREATE INDEX tmp_audit_name_subsystem_id_idx ON tmp_audit_name (subsystem_id); +CREATE INDEX tmp_audit_name_devicetype_id_idx ON tmp_audit_name (devicetype_id); +CREATE INDEX tmp_audit_name_convention_name_idx ON tmp_audit_name (convention_name); +CREATE INDEX tmp_audit_name_status_idx ON tmp_audit_name (status); +CREATE INDEX tmp_audit_name_deleted_idx ON tmp_audit_name (deleted); + +-- -------------------------------------------------------------------------------- +-- sequence +-- -------------------------------------------------------------------------------- +CREATE SEQUENCE tmp_systemgroup_id_seq + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; +CREATE SEQUENCE tmp_system_id_seq + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; +CREATE SEQUENCE tmp_subsystem_id_seq + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; +CREATE SEQUENCE tmp_discipline_id_seq + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; +CREATE SEQUENCE tmp_devicegroup_id_seq + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; +CREATE SEQUENCE tmp_devicetype_id_seq + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; +CREATE SEQUENCE tmp_name_id_seq + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; +CREATE SEQUENCE tmp_audit_structure_audit_id_seq + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; +CREATE SEQUENCE tmp_audit_name_audit_id_seq + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + +SELECT setval('tmp_systemgroup_id_seq', (select max(id) from tmp_systemgroup)); +SELECT setval('tmp_system_id_seq', (select max(id) from tmp_system)); +SELECT setval('tmp_subsystem_id_seq', (select max(id) from tmp_subsystem)); +SELECT setval('tmp_discipline_id_seq', (select max(id) from tmp_discipline)); +SELECT setval('tmp_devicegroup_id_seq', (select max(id) from tmp_devicegroup)); +SELECT setval('tmp_devicetype_id_seq', (select max(id) from tmp_devicetype)); +SELECT setval('tmp_name_id_seq', (select max(id) from tmp_name)); +SELECT setval('tmp_audit_structure_audit_id_seq', (select max(audit_id) from tmp_audit_structure)); +SELECT setval('tmp_audit_name_audit_id_seq', (select max(audit_id) from tmp_audit_name)); + +ALTER SEQUENCE tmp_systemgroup_id_seq OWNED BY tmp_systemgroup.id; +ALTER SEQUENCE tmp_system_id_seq OWNED BY tmp_system.id; +ALTER SEQUENCE tmp_subsystem_id_seq OWNED BY tmp_subsystem.id; +ALTER SEQUENCE tmp_discipline_id_seq OWNED BY tmp_discipline.id; +ALTER SEQUENCE tmp_devicegroup_id_seq OWNED BY tmp_devicegroup.id; +ALTER SEQUENCE tmp_devicetype_id_seq OWNED BY tmp_devicetype.id; +ALTER SEQUENCE tmp_name_id_seq OWNED BY tmp_name.id; +ALTER SEQUENCE tmp_audit_structure_audit_id_seq OWNED BY tmp_audit_structure.audit_id; +ALTER SEQUENCE tmp_audit_name_audit_id_seq OWNED BY tmp_audit_name.audit_id; + +ALTER TABLE ONLY tmp_systemgroup ALTER COLUMN id SET DEFAULT nextval('tmp_systemgroup_id_seq'::regclass); +ALTER TABLE ONLY tmp_system ALTER COLUMN id SET DEFAULT nextval('tmp_system_id_seq'::regclass); +ALTER TABLE ONLY tmp_subsystem ALTER COLUMN id SET DEFAULT nextval('tmp_subsystem_id_seq'::regclass); +ALTER TABLE ONLY tmp_discipline ALTER COLUMN id SET DEFAULT nextval('tmp_discipline_id_seq'::regclass); +ALTER TABLE ONLY tmp_devicegroup ALTER COLUMN id SET DEFAULT nextval('tmp_devicegroup_id_seq'::regclass); +ALTER TABLE ONLY tmp_devicetype ALTER COLUMN id SET DEFAULT nextval('tmp_devicetype_id_seq'::regclass); +ALTER TABLE ONLY tmp_name ALTER COLUMN id SET DEFAULT nextval('tmp_name_id_seq'::regclass); +ALTER TABLE ONLY tmp_audit_structure ALTER COLUMN audit_id SET DEFAULT nextval('tmp_audit_structure_audit_id_seq'::regclass); +ALTER TABLE ONLY tmp_audit_name ALTER COLUMN audit_id SET DEFAULT nextval('tmp_audit_name_audit_id_seq'::regclass); + +-- -------------------------------------------------------------------------------- +-- primary key +-- -------------------------------------------------------------------------------- +ALTER TABLE tmp_systemgroup ADD CONSTRAINT tmp_systemgroup_pk PRIMARY KEY (id); +ALTER TABLE tmp_system ADD CONSTRAINT tmp_system_pk PRIMARY KEY (id); +ALTER TABLE tmp_subsystem ADD CONSTRAINT tmp_subsystem_pk PRIMARY KEY (id); +ALTER TABLE tmp_discipline ADD CONSTRAINT tmp_discipline_pk PRIMARY KEY (id); +ALTER TABLE tmp_devicegroup ADD CONSTRAINT tmp_devicegroup_pk PRIMARY KEY (id); +ALTER TABLE tmp_devicetype ADD CONSTRAINT tmp_devicetype_pk PRIMARY KEY (id); +ALTER TABLE tmp_name ADD CONSTRAINT tmp_name_pk PRIMARY KEY (id); +ALTER TABLE tmp_audit_structure ADD CONSTRAINT tmp_audit_structure_pk PRIMARY KEY (audit_id); +ALTER TABLE tmp_audit_name ADD CONSTRAINT tmp_audit_name_pk PRIMARY KEY (audit_id); + +-- -------------------------------------------------------------------------------- +-- foreign key +-- -------------------------------------------------------------------------------- +ALTER TABLE ONLY tmp_system ADD CONSTRAINT fk_system_systemgroup FOREIGN KEY (parent_id) REFERENCES tmp_systemgroup(id); +ALTER TABLE ONLY tmp_subsystem ADD CONSTRAINT fk_subsystem_system FOREIGN KEY (parent_id) REFERENCES tmp_system(id); +ALTER TABLE ONLY tmp_devicegroup ADD CONSTRAINT fk_devicegroup_discipline FOREIGN KEY (parent_id) REFERENCES tmp_discipline(id); +ALTER TABLE ONLY tmp_devicetype ADD CONSTRAINT fk_devicetype_devicegroup FOREIGN KEY (parent_id) REFERENCES tmp_devicegroup(id); +ALTER TABLE ONLY tmp_name ADD CONSTRAINT fk_name_systemgroup FOREIGN KEY (systemgroup_id) REFERENCES tmp_systemgroup(id); +ALTER TABLE ONLY tmp_name ADD CONSTRAINT fk_name_system FOREIGN KEY (system_id) REFERENCES tmp_system(id); +ALTER TABLE ONLY tmp_name ADD CONSTRAINT fk_name_subsystem FOREIGN KEY (subsystem_id) REFERENCES tmp_subsystem(id); +ALTER TABLE ONLY tmp_name ADD CONSTRAINT fk_name_devicetype FOREIGN KEY (devicetype_id) REFERENCES tmp_devicetype(id); + +-- -------------------------------------------------------------------------------- +-- function +-- -------------------------------------------------------------------------------- +-- same as V4 +-- get_mnemonic_path_system_structure(convention_name text) +-- get_mnemonic_path_device_structure(convention_name text) +-- get_instance_index(convention_name text) + +CREATE OR REPLACE FUNCTION tmp_get_mnemonic_path_system_structure(convention_name text) + RETURNS text + LANGUAGE plpgsql +AS +$$ +DECLARE + pos int; +BEGIN + pos = strpos(convention_name, ':'); + IF pos > 0 THEN + RETURN substr(convention_name, 1, pos-1); + END IF; + RETURN convention_name; +END; +$$; + +CREATE OR REPLACE FUNCTION tmp_get_mnemonic_path_device_structure(convention_name text) + RETURNS text + LANGUAGE plpgsql +AS +$$ +DECLARE + pos int; + mnemonic_path text; + nbr_delimiters int; +BEGIN + pos = strpos(convention_name, ':'); + IF pos > 0 THEN + mnemonic_path = substr(convention_name, pos+1); + nbr_delimiters = array_length(string_to_array(mnemonic_path, '-'), 1) - 1; + IF nbr_delimiters = 2 then + mnemonic_path = reverse(mnemonic_path); + mnemonic_path = substr(mnemonic_path, strpos(mnemonic_path, '-')+1); + RETURN reverse(mnemonic_path); + ELSIF nbr_delimiters = 1 then + return mnemonic_path; + ELSE + RETURN null; + END IF; + END IF; + RETURN null; +END; +$$; + +CREATE OR REPLACE FUNCTION tmp_get_instance_index(convention_name text) + RETURNS text + LANGUAGE plpgsql +AS +$$ +DECLARE + len int; + pos int; + mnemonic_path text; + nbr_delimiters int; +BEGIN + pos = strpos(convention_name, ':'); + IF pos > 0 THEN + mnemonic_path = substr(convention_name, pos+1); + nbr_delimiters = array_length(string_to_array(mnemonic_path, '-'), 1) - 1; + IF nbr_delimiters = 2 then + mnemonic_path = reverse(mnemonic_path); + len = length(mnemonic_path); + pos = strpos(mnemonic_path, '-'); + mnemonic_path = reverse(mnemonic_path); + RETURN substr(mnemonic_path, len - pos + 2); + ELSE + RETURN null; + END IF; + END IF; + RETURN null; +END; +$$; + +CREATE OR REPLACE FUNCTION tmp_get_parent_uuid_system(system_id bigint) + RETURNS text + LANGUAGE plpgsql +AS +$$ +DECLARE + parent_uuid text; +BEGIN + select uuid into parent_uuid from tmp_systemgroup where id = (select parent_id from tmp_system where id = system_id); + + if parent_uuid is not null then + return parent_uuid; + else + return null; + end if; +END; +$$; + +CREATE OR REPLACE FUNCTION tmp_get_parent_uuid_subsystem(subsystem_id bigint) + RETURNS text + LANGUAGE plpgsql +AS +$$ +DECLARE + parent_uuid text; +BEGIN + select uuid into parent_uuid from tmp_system where id = (select parent_id from tmp_subsystem where id = subsystem_id); + + if parent_uuid is not null then + return parent_uuid; + else + return null; + end if; +END; +$$; + +CREATE OR REPLACE FUNCTION tmp_get_parent_uuid_devicegroup(devicegroup_id bigint) + RETURNS text + LANGUAGE plpgsql +AS +$$ +DECLARE + parent_uuid text; +BEGIN + select uuid into parent_uuid from tmp_discipline where id = (select parent_id from tmp_devicegroup where id = devicegroup_id); + + if parent_uuid is not null then + return parent_uuid; + else + return null; + end if; +END; +$$; + +CREATE OR REPLACE FUNCTION tmp_get_parent_uuid_devicetype(devicetype_id bigint) + RETURNS text + LANGUAGE plpgsql +AS +$$ +DECLARE + parent_uuid text; +BEGIN + select uuid into parent_uuid from tmp_devicegroup where id = (select parent_id from tmp_devicetype where id = devicetype_id); + + if parent_uuid is not null then + return parent_uuid; + else + return null; + end if; +END; +$$; + +CREATE OR REPLACE FUNCTION tmp_get_mnemonic_path_system(system_id bigint) + RETURNS text + LANGUAGE plpgsql +AS +$$ +DECLARE + system_mnemonic text; +BEGIN + select mnemonic into system_mnemonic from tmp_system where id = system_id; + + if system_mnemonic is not null then + return system_mnemonic; + else + return null; + end if; +END; +$$; + +CREATE OR REPLACE FUNCTION tmp_get_mnemonic_path_subsystem(subsystem_id bigint) + RETURNS text + LANGUAGE plpgsql +AS +$$ +DECLARE + subsystem_mnemonic text; + system_id bigint; + system_mnemonic text; +BEGIN + select parent_id, mnemonic into system_id, subsystem_mnemonic from tmp_subsystem where id = subsystem_id; + select mnemonic into system_mnemonic from tmp_system where id = system_id; + + if system_mnemonic is not null and subsystem_mnemonic is not null then + return concat(system_mnemonic, '-', subsystem_mnemonic); + else + return null; + end if; +END; +$$; + +CREATE OR REPLACE FUNCTION tmp_get_mnemonic_path_devicegroup(devicegroup_id bigint) + RETURNS text + LANGUAGE plpgsql +AS +$$ +DECLARE + discipline_id bigint; + discipline_mnemonic text; +BEGIN + select parent_id into discipline_id from tmp_devicegroup where id = devicegroup_id; + select mnemonic into discipline_mnemonic from tmp_discipline where id = discipline_id; + + if discipline_mnemonic is not null then + return discipline_mnemonic; + else + return null; + end if; +END; +$$; + +CREATE OR REPLACE FUNCTION tmp_get_mnemonic_path_devicetype(devicetype_id bigint) + RETURNS text + LANGUAGE plpgsql +AS +$$ +DECLARE + devicetype_mnemonic text; + devicegroup_id bigint; + discipline_id bigint; + discipline_mnemonic text; +BEGIN + select parent_id, mnemonic into devicegroup_id, devicetype_mnemonic from tmp_devicetype where id = devicetype_id; + select parent_id, mnemonic into discipline_id from tmp_devicegroup where id = devicegroup_id; + select mnemonic into discipline_mnemonic from tmp_discipline where id = discipline_id; + + if discipline_mnemonic is not null and devicetype_mnemonic is not null then + return concat(discipline_mnemonic, '-', devicetype_mnemonic); + elsif devicetype_mnemonic is not null then + return devicetype_mnemonic; + else + return null; + end if; +END; +$$; diff --git a/src/main/resources/db/migration/V6.2__Schema_data_migration_audit.sql b/src/main/resources/db/migration/V6.2__Schema_data_migration_audit.sql new file mode 100644 index 0000000000000000000000000000000000000000..6e2118c94342bf2babc7dc089ee8cd8a9e2b2944 --- /dev/null +++ b/src/main/resources/db/migration/V6.2__Schema_data_migration_audit.sql @@ -0,0 +1,1672 @@ +-- -------------------------------------------------------------------------------- +-- About +-- migration script +-- postgresql 9.6.7 +-- Content +-- structures +-- names +-- data structures level 1 +-- systemgroup + audit +-- discipline + audit +-- data structures level 2 +-- system + audit +-- devicegroup + audit +-- data structures level 3 +-- subsystem + audit +-- devicetype + audit +-- data names +-- systemgroup_id and not devicetype_id + audit +-- system_id and not devicetype_id + audit +-- subsystem_id and not devicetype_id + audit +-- systemgroup_id and devicetype_id + audit +-- system_id and devicetype_id + audit +-- subsystem_id and devicetype_id + audit +-- index +-- sequence +-- primary key +-- foreign key +-- function +-- Note +-- about audit tables and data +-- considered temporary +-- notice prefix "tmp" and similar +-- order of items is important +-- -------------------------------------------------------------------------------- +-- data structures (parent_id) +-- data names +-- selection of proper id to refer to - systemgroup, system, subsystem, devicetype +-- exclude content (with latest) before latest +-- exclude content (with latest) after latest (cancelled, rejected) +-- keep most recent content (without latest) (to have most recent in line of uuid without latest) +-- <--> +-- select * from structure s +-- where ( +-- not (exists (select s2.id from structure s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select s2.id from structure s2 where s2."uuid" = s."uuid" and s2.latest=true)) +-- and not (exists (select s2.id from structure s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id > (select s2.id from structure s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.status in ('CANCELLED', 'REJECTED')) +-- and not (not exists (select s2.id from structure s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select max(s2.id) from structure s2 where s2."uuid" = s."uuid" and s2.latest=false)) +-- ) +-- -------------------------------------------------------------------------------- +-- retrieve parent id for audit structure +-- 1 or 2 or 3 +-- 1. get id from parent that is approved and was processed prior to child or within 10 seconds +-- 2. get id from parent that is approved and was requested prior to child or within 10 minutes +-- 3. get id from parent that was requested prior to child or within 10 minutes +-- <--> +-- coalesce ( +-- (select max(s3.id) from parent s3 where s3."uuid" = s.parent_uuid and s3.status = 'APPROVED' and s3.processed < (s.processed + interval '10 seconds')), +-- (select max(s3.id) from parent s3 where s3."uuid" = s.parent_uuid and s3.status = 'APPROVED' and s3.requested < (s.processed + interval '10 minutes')), +-- (select max(s3.id) from parent s3 where s3."uuid" = s.parent_uuid and s3.requested < (s.processed + interval '10 minutes')) +-- ) +-- -------------------------------------------------------------------------------- +-- non-audit tables contain valid +-- audit tables contain all +-- -------------------------------------------------------------------------------- + +-- -------------------------------------------------------------------------------- +-- structures +-- -------------------------------------------------------------------------------- +CREATE TABLE IF NOT EXISTS tmp_systemgroup ( + id bigint NOT NULL, + version integer, + uuid text NOT NULL, + mnemonic text, + mnemonic_equivalence text, + ordering integer, + description text, + status text, + -- latest boolean NOT NULL, + deleted boolean NOT NULL, + requested timestamp without time zone, + requested_by text, + requested_comment text, + processed timestamp without time zone, + processed_by text, + processed_comment text +); + +CREATE TABLE IF NOT EXISTS tmp_system ( + id bigint NOT NULL, + version integer, + uuid text NOT NULL, + parent_id bigint NOT NULL, + mnemonic text, + mnemonic_equivalence text, + ordering integer, + description text, + status text, + -- latest boolean NOT NULL, + deleted boolean NOT NULL, + requested timestamp without time zone, + requested_by text, + requested_comment text, + processed timestamp without time zone, + processed_by text, + processed_comment text +); + +CREATE TABLE IF NOT EXISTS tmp_subsystem ( + id bigint NOT NULL, + version integer, + uuid text NOT NULL, + parent_id bigint NOT NULL, + mnemonic text, + mnemonic_equivalence text, + ordering integer, + description text, + status text, + -- latest boolean NOT NULL, + deleted boolean NOT NULL, + requested timestamp without time zone, + requested_by text, + requested_comment text, + processed timestamp without time zone, + processed_by text, + processed_comment text +); + +-- -------------------------------------------------------------------------------- +CREATE TABLE IF NOT EXISTS tmp_discipline ( + id bigint NOT NULL, + version integer, + uuid text NOT NULL, + mnemonic text, + mnemonic_equivalence text, + ordering integer, + description text, + status text, + -- latest boolean NOT NULL, + deleted boolean NOT NULL, + requested timestamp without time zone, + requested_by text, + requested_comment text, + processed timestamp without time zone, + processed_by text, + processed_comment text +); + +CREATE TABLE IF NOT EXISTS tmp_devicegroup ( + id bigint NOT NULL, + version integer, + uuid text NOT NULL, + parent_id bigint NOT NULL, + mnemonic text, + mnemonic_equivalence text, + ordering integer, + description text, + status text, + -- latest boolean NOT NULL, + deleted boolean NOT NULL, + requested timestamp without time zone, + requested_by text, + requested_comment text, + processed timestamp without time zone, + processed_by text, + processed_comment text +); + +CREATE TABLE IF NOT EXISTS tmp_devicetype ( + id bigint NOT NULL, + version integer, + uuid text NOT NULL, + parent_id bigint NOT NULL, + mnemonic text, + mnemonic_equivalence text, + ordering integer, + description text, + status text, + -- latest boolean NOT NULL, + deleted boolean NOT NULL, + requested timestamp without time zone, + requested_by text, + requested_comment text, + processed timestamp without time zone, + processed_by text, + processed_comment text +); + +-- -------------------------------------------------------------------------------- +-- name +-- -------------------------------------------------------------------------------- +CREATE TABLE IF NOT EXISTS tmp_name ( + id bigint NOT NULL, + version integer, + uuid text NOT NULL, + systemgroup_id bigint, + system_id bigint, + subsystem_id bigint, + devicetype_id bigint, + instance_index text, + convention_name text, + convention_name_equivalence text, + description text, + status text, + -- latest boolean NOT NULL, + deleted boolean NOT NULL, + requested timestamp without time zone, + requested_by text, + requested_comment text, + processed timestamp without time zone, + processed_by text, + processed_comment text +); + +-- -------------------------------------------------------------------------------- +-- audit +-- -------------------------------------------------------------------------------- + +CREATE TABLE IF NOT EXISTS tmp_audit_structure ( + audit_id bigint NOT NULL, + audit_version integer, + audit_table text NOT NULL, + audit_operation text, + id bigint NOT NULL, + version integer, + uuid text NOT NULL, + parent_id bigint, + mnemonic text, + mnemonic_equivalence text, + ordering integer, + description text, + status text, + -- latest boolean NOT NULL, + deleted boolean NOT NULL, + requested timestamp without time zone, + requested_by text, + requested_comment text, + processed timestamp without time zone, + processed_by text, + processed_comment text +); + +CREATE TABLE IF NOT EXISTS tmp_audit_name ( + audit_id bigint NOT NULL, + audit_version integer, + audit_table text NOT NULL, + audit_operation text, + id bigint NOT NULL, + version integer, + uuid text NOT NULL, + systemgroup_id bigint, + system_id bigint, + subsystem_id bigint, + devicetype_id bigint, + instance_index text, + convention_name text, + convention_name_equivalence text, + description text, + status text, + -- latest boolean NOT NULL, + deleted boolean NOT NULL, + requested timestamp without time zone, + requested_by text, + requested_comment text, + processed timestamp without time zone, + processed_by text, + processed_comment text +); + +-- -------------------------------------------------------------------------------- +-- data structures level 1 +-- systemgroup + audit +-- discipline + audit +-- -------------------------------------------------------------------------------- +-- from systemgroup +-- from discipline +-- -------------------------------------------------------------------------------- +insert into tmp_systemgroup ( + id, + version, + uuid, + mnemonic, + mnemonic_equivalence, + ordering, + description, + status, + -- latest, + deleted, + requested, + requested_by, + requested_comment, + processed, + processed_by, + processed_comment +) +select +s.id, s."version", s.uuid, s.mnemonic, s.mnemonic_equivalence, s.ordering, s.description, s.status, s.deleted, +s.requested, s.requested_by, s.requested_comment, s.processed, s.processed_by, s.processed_comment from systemgroup s +where ( + not (exists (select s2.id from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select s2.id from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=true)) + and not (exists (select s2.id from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id > (select s2.id from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.status in ('CANCELLED', 'REJECTED')) + and not (not exists (select s2.id from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select max(s2.id) from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=false)) +); + +insert into tmp_audit_structure ( + audit_id, + audit_version, + audit_table, + audit_operation, + id, + version, + uuid, + parent_id, + mnemonic, + mnemonic_equivalence, + ordering, + description, + status, + -- latest, + deleted, + requested, + requested_by, + requested_comment, + processed, + processed_by, + processed_comment +) +select s.id, s."version", 'systemgroup', null, +s.id, s.version, s.uuid, null, s.mnemonic, s.mnemonic_equivalence, s.ordering, s.description, s.status, s.deleted, +s.requested, s.requested_by, s.requested_comment, s.processed, s.processed_by, s.processed_comment from systemgroup s +-- where not ( +-- not (exists (select s2.id from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select s2.id from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=true)) +-- and not (exists (select s2.id from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id > (select s2.id from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.status in ('CANCELLED', 'REJECTED')) +-- and not (not exists (select s2.id from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select max(s2.id) from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=false)) +-- ) +; + +-- -------------------------------------------------------------------------------- +insert into tmp_discipline ( + id, + version, + uuid, + mnemonic, + mnemonic_equivalence, + ordering, + description, + status, + -- latest, + deleted, + requested, + requested_by, + requested_comment, + processed, + processed_by, + processed_comment +) +select +s.id, s."version", s.uuid, s.mnemonic, s.mnemonic_equivalence, s.ordering, s.description, s.status, s.deleted, +s.requested, s.requested_by, s.requested_comment, s.processed, s.processed_by, s.processed_comment from discipline s +where ( + not (exists (select s2.id from discipline s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select s2.id from discipline s2 where s2."uuid" = s."uuid" and s2.latest=true)) + and not (exists (select s2.id from discipline s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id > (select s2.id from discipline s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.status in ('CANCELLED', 'REJECTED')) + and not (not exists (select s2.id from discipline s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select max(s2.id) from discipline s2 where s2."uuid" = s."uuid" and s2.latest=false)) +); + +insert into tmp_audit_structure ( + audit_id, + audit_version, + audit_table, + audit_operation, + id, + version, + uuid, + parent_id, + mnemonic, + mnemonic_equivalence, + ordering, + description, + status, + -- latest, + deleted, + requested, + requested_by, + requested_comment, + processed, + processed_by, + processed_comment +) +select s.id, s."version", 'discipline', null, +s.id, s.version, s.uuid, null, s.mnemonic, s.mnemonic_equivalence, s.ordering, s.description, s.status, s.deleted, +s.requested, s.requested_by, s.requested_comment, s.processed, s.processed_by, s.processed_comment from discipline s +-- where not ( +-- not (exists (select s2.id from discipline s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select s2.id from discipline s2 where s2."uuid" = s."uuid" and s2.latest=true)) +-- and not (exists (select s2.id from discipline s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id > (select s2.id from discipline s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.status in ('CANCELLED', 'REJECTED')) +-- and not (not exists (select s2.id from discipline s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select max(s2.id) from discipline s2 where s2."uuid" = s."uuid" and s2.latest=false)) +-- ) +; + +-- -------------------------------------------------------------------------------- +-- data structures level 2 +-- system + audit +-- devicegroup + audit +-- -------------------------------------------------------------------------------- +-- from system +-- from devicegroup +-- -------------------------------------------------------------------------------- +insert into tmp_system ( + id, + version, + uuid, + parent_id, + mnemonic, + mnemonic_equivalence, + ordering, + description, + status, + -- latest, + deleted, + requested, + requested_by, + requested_comment, + processed, + processed_by, + processed_comment +) +select +s.id, s."version", s.uuid, +(select sg.id from systemgroup sg +where sg.uuid = s.parent_uuid and ( + not (exists (select s2.id from systemgroup s2 where s2."uuid" = sg."uuid" and s2.latest=true) and sg.id < (select s2.id from systemgroup s2 where s2."uuid" = sg."uuid" and s2.latest=true)) + and not (exists (select s2.id from systemgroup s2 where s2."uuid" = sg."uuid" and s2.latest=true) and sg.id > (select s2.id from systemgroup s2 where s2."uuid" = sg."uuid" and s2.latest=true) and sg.status in ('CANCELLED', 'REJECTED')) + and not (not exists (select s2.id from systemgroup s2 where s2."uuid" = sg."uuid" and s2.latest=true) and sg.id < (select max(s2.id) from systemgroup s2 where s2."uuid" = sg."uuid" and s2.latest=false)) +)), +s.mnemonic, s.mnemonic_equivalence, s.ordering, s.description, s.status, s.deleted, +s.requested, s.requested_by, s.requested_comment, s.processed, s.processed_by, s.processed_comment from system s +where ( + not (exists (select s2.id from system s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select s2.id from system s2 where s2."uuid" = s."uuid" and s2.latest=true)) + and not (exists (select s2.id from system s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id > (select s2.id from system s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.status in ('CANCELLED', 'REJECTED')) + and not (not exists (select s2.id from system s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select max(s2.id) from system s2 where s2."uuid" = s."uuid" and s2.latest=false)) +); + +insert into tmp_audit_structure ( + audit_id, + audit_version, + audit_table, + audit_operation, + id, + version, + uuid, + parent_id, + mnemonic, + mnemonic_equivalence, + ordering, + description, + status, + -- latest, + deleted, + requested, + requested_by, + requested_comment, + processed, + processed_by, + processed_comment +) +select s.id, s."version", 'system', null, +s.id, s.version, s.uuid, +(coalesce ( +(select max(s3.id) from systemgroup s3 where s3."uuid" = s.parent_uuid and s3.status = 'APPROVED' and s3.processed < (s.processed + interval '10 seconds')), +(select max(s3.id) from systemgroup s3 where s3."uuid" = s.parent_uuid and s3.status = 'APPROVED' and s3.requested < (s.processed + interval '10 minutes')), +(select max(s3.id) from systemgroup s3 where s3."uuid" = s.parent_uuid and s3.requested < (s.processed + interval '10 minutes')) +)), +s.mnemonic, s.mnemonic_equivalence, s.ordering, s.description, s.status, s.deleted, +s.requested, s.requested_by, s.requested_comment, s.processed, s.processed_by, s.processed_comment from system s +-- where not ( +-- not (exists (select s2.id from system s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select s2.id from system s2 where s2."uuid" = s."uuid" and s2.latest=true)) +-- and not (exists (select s2.id from system s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id > (select s2.id from system s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.status in ('CANCELLED', 'REJECTED')) +-- and not (not exists (select s2.id from system s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select max(s2.id) from system s2 where s2."uuid" = s."uuid" and s2.latest=false)) +-- ) +; + +-- -------------------------------------------------------------------------------- +insert into tmp_devicegroup ( + id, + version, + uuid, + parent_id, + mnemonic, + mnemonic_equivalence, + ordering, + description, + status, + -- latest, + deleted, + requested, + requested_by, + requested_comment, + processed, + processed_by, + processed_comment +) +select +s.id, s."version", s.uuid, +(select di.id from discipline di +where di.uuid = s.parent_uuid and ( + not (exists (select s2.id from discipline s2 where s2."uuid" = di."uuid" and s2.latest=true) and di.id < (select s2.id from discipline s2 where s2."uuid" = di."uuid" and s2.latest=true)) + and not (exists (select s2.id from discipline s2 where s2."uuid" = di."uuid" and s2.latest=true) and di.id > (select s2.id from discipline s2 where s2."uuid" = di."uuid" and s2.latest=true) and di.status in ('CANCELLED', 'REJECTED')) + and not (not exists (select s2.id from discipline s2 where s2."uuid" = di."uuid" and s2.latest=true) and di.id < (select max(s2.id) from discipline s2 where s2."uuid" = di."uuid" and s2.latest=false)) +)), +s.mnemonic, s.mnemonic_equivalence, s.ordering, s.description, s.status, s.deleted, +s.requested, s.requested_by, s.requested_comment, s.processed, s.processed_by, s.processed_comment from devicegroup s +where ( + not (exists (select s2.id from devicegroup s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select s2.id from devicegroup s2 where s2."uuid" = s."uuid" and s2.latest=true)) + and not (exists (select s2.id from devicegroup s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id > (select s2.id from devicegroup s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.status in ('CANCELLED', 'REJECTED')) + and not (not exists (select s2.id from devicegroup s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select max(s2.id) from devicegroup s2 where s2."uuid" = s."uuid" and s2.latest=false)) +); + +insert into tmp_audit_structure ( + audit_id, + audit_version, + audit_table, + audit_operation, + id, + version, + uuid, + parent_id, + mnemonic, + mnemonic_equivalence, + ordering, + description, + status, + -- latest, + deleted, + requested, + requested_by, + requested_comment, + processed, + processed_by, + processed_comment +) +select s.id, s."version", 'devicegroup', null, +s.id, s.version, s.uuid, +(coalesce ( +(select max(s3.id) from discipline s3 where s3."uuid" = s.parent_uuid and s3.status = 'APPROVED' and s3.processed < (s.processed + interval '10 seconds')), +(select max(s3.id) from discipline s3 where s3."uuid" = s.parent_uuid and s3.status = 'APPROVED' and s3.requested < (s.processed + interval '10 minutes')), +(select max(s3.id) from discipline s3 where s3."uuid" = s.parent_uuid and s3.requested < (s.processed + interval '10 minutes')) +)), +s.mnemonic, s.mnemonic_equivalence, s.ordering, s.description, s.status, s.deleted, +s.requested, s.requested_by, s.requested_comment, s.processed, s.processed_by, s.processed_comment from devicegroup s +-- where not ( +-- not (exists (select s2.id from devicegroup s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select s2.id from devicegroup s2 where s2."uuid" = s."uuid" and s2.latest=true)) +-- and not (exists (select s2.id from devicegroup s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id > (select s2.id from devicegroup s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.status in ('CANCELLED', 'REJECTED')) +-- and not (not exists (select s2.id from devicegroup s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select max(s2.id) from devicegroup s2 where s2."uuid" = s."uuid" and s2.latest=false)) +-- ) +; + +-- -------------------------------------------------------------------------------- +-- data structures level 3 +-- subsystem + audit +-- devicetype + audit +-- -------------------------------------------------------------------------------- +-- from subsystem +-- from devicetype +-- -------------------------------------------------------------------------------- +insert into tmp_subsystem ( + id, + version, + uuid, + parent_id, + mnemonic, + mnemonic_equivalence, + ordering, + description, + status, + -- latest, + deleted, + requested, + requested_by, + requested_comment, + processed, + processed_by, + processed_comment +) +select +s.id, s."version", s.uuid, +(select sys.id from system sys +where sys.uuid = s.parent_uuid and ( + not (exists (select s2.id from system s2 where s2."uuid" = sys."uuid" and s2.latest=true) and sys.id < (select s2.id from system s2 where s2."uuid" = sys."uuid" and s2.latest=true)) + and not (exists (select s2.id from system s2 where s2."uuid" = sys."uuid" and s2.latest=true) and sys.id > (select s2.id from system s2 where s2."uuid" = sys."uuid" and s2.latest=true) and sys.status in ('CANCELLED', 'REJECTED')) + and not (not exists (select s2.id from system s2 where s2."uuid" = sys."uuid" and s2.latest=true) and sys.id < (select max(s2.id) from system s2 where s2."uuid" = sys."uuid" and s2.latest=false)) +)), +s.mnemonic, s.mnemonic_equivalence, s.ordering, s.description, s.status, s.deleted, +s.requested, s.requested_by, s.requested_comment, s.processed, s.processed_by, s.processed_comment from subsystem s +where ( + not (exists (select s2.id from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select s2.id from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=true)) + and not (exists (select s2.id from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id > (select s2.id from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.status in ('CANCELLED', 'REJECTED')) + and not (not exists (select s2.id from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select max(s2.id) from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=false)) +); + +insert into tmp_audit_structure ( + audit_id, + audit_version, + audit_table, + audit_operation, + id, + version, + uuid, + parent_id, + mnemonic, + mnemonic_equivalence, + ordering, + description, + status, + -- latest, + deleted, + requested, + requested_by, + requested_comment, + processed, + processed_by, + processed_comment +) +select s.id, s."version", 'subsystem', null, +s.id, s.version, s.uuid, +(coalesce ( +(select max(s3.id) from "system" s3 where s3."uuid" = s.parent_uuid and s3.status = 'APPROVED' and s3.processed < (s.processed + interval '10 seconds')), +(select max(s3.id) from "system" s3 where s3."uuid" = s.parent_uuid and s3.status = 'APPROVED' and s3.requested < (s.processed + interval '10 minutes')), +(select max(s3.id) from "system" s3 where s3."uuid" = s.parent_uuid and s3.requested < (s.processed + interval '10 minutes')) +)), +s.mnemonic, s.mnemonic_equivalence, s.ordering, s.description, s.status, s.deleted, +s.requested, s.requested_by, s.requested_comment, s.processed, s.processed_by, s.processed_comment from subsystem s +-- where not ( +-- not (exists (select s2.id from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select s2.id from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=true)) +-- and not (exists (select s2.id from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id > (select s2.id from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.status in ('CANCELLED', 'REJECTED')) +-- and not (not exists (select s2.id from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select max(s2.id) from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=false)) +-- ) +; + +-- -------------------------------------------------------------------------------- +insert into tmp_devicetype ( + id, + version, + uuid, + parent_id, + mnemonic, + mnemonic_equivalence, + ordering, + description, + status, + -- latest, + deleted, + requested, + requested_by, + requested_comment, + processed, + processed_by, + processed_comment +) +select +s.id, s."version", s.uuid, +(select dg.id from devicegroup dg +where dg.uuid = s.parent_uuid and ( + not (exists (select s2.id from devicegroup s2 where s2."uuid" = dg."uuid" and s2.latest=true) and dg.id < (select s2.id from devicegroup s2 where s2."uuid" = dg."uuid" and s2.latest=true)) + and not (exists (select s2.id from devicegroup s2 where s2."uuid" = dg."uuid" and s2.latest=true) and dg.id > (select s2.id from devicegroup s2 where s2."uuid" = dg."uuid" and s2.latest=true) and dg.status in ('CANCELLED', 'REJECTED')) + and not (not exists (select s2.id from devicegroup s2 where s2."uuid" = dg."uuid" and s2.latest=true) and dg.id < (select max(s2.id) from devicegroup s2 where s2."uuid" = dg."uuid" and s2.latest=false)) +)), +s.mnemonic, s.mnemonic_equivalence, s.ordering, s.description, s.status, s.deleted, +s.requested, s.requested_by, s.requested_comment, s.processed, s.processed_by, s.processed_comment from devicetype s +where ( + not (exists (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true)) + and not (exists (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id > (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.status in ('CANCELLED', 'REJECTED')) + and not (not exists (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select max(s2.id) from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=false)) +); + +insert into tmp_audit_structure ( + audit_id, + audit_version, + audit_table, + audit_operation, + id, + version, + uuid, + parent_id, + mnemonic, + mnemonic_equivalence, + ordering, + description, + status, + -- latest, + deleted, + requested, + requested_by, + requested_comment, + processed, + processed_by, + processed_comment +) +select s.id, s."version", 'devicetype', null, +s.id, s.version, s.uuid, +(coalesce ( +(select max(s3.id) from devicegroup s3 where s3."uuid" = s.parent_uuid and s3.status = 'APPROVED' and s3.processed < (s.processed + interval '10 seconds')), +(select max(s3.id) from devicegroup s3 where s3."uuid" = s.parent_uuid and s3.status = 'APPROVED' and s3.requested < (s.processed + interval '10 minutes')), +(select max(s3.id) from devicegroup s3 where s3."uuid" = s.parent_uuid and s3.requested < (s.processed + interval '10 minutes')) +)), +s.mnemonic, s.mnemonic_equivalence, s.ordering, s.description, s.status, s.deleted, +s.requested, s.requested_by, s.requested_comment, s.processed, s.processed_by, s.processed_comment from devicetype s +-- where not ( +-- not (exists (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true)) +-- and not (exists (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id > (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.status in ('CANCELLED', 'REJECTED')) +-- and not (not exists (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select max(s2.id) from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=false)) +-- ) +; + +-- -------------------------------------------------------------------------------- +-- data names +-- 1) systemgroup_id and not devicetype_id + audit +-- 2) system_id and not devicetype_id + audit +-- 3) subsystem_id and not devicetype_id + audit +-- 4) systemgroup_id and devicetype_id + audit +-- 5) system_id and devicetype_id + audit +-- 6) subsystem_id and devicetype_id + audit +-- -------------------------------------------------------------------------------- +-- from systemgroup +-- from systemg +-- from subsystem +-- from systemgroup + devicetype +-- from systemg + devicetype +-- from subsystem + devicetype +-- -------------------------------------------------------------------------------- +-- 1) +insert into tmp_name ( + id, + version, + uuid, + systemgroup_id, + system_id, + subsystem_id, + devicetype_id, + instance_index, + convention_name, + convention_name_equivalence, + description, + status, + -- latest, + deleted, + requested, + requested_by, + requested_comment, + processed, + processed_by, + processed_comment +) +select n.id, n."version", n.uuid, +(select s.id from systemgroup s +where s.uuid = n.systemgroup_uuid and ( + not (exists (select s2.id from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select s2.id from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=true)) + and not (exists (select s2.id from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id > (select s2.id from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.status in ('CANCELLED', 'REJECTED')) + and not (not exists (select s2.id from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select max(s2.id) from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=false)) +)), +null, +null, +null, +n.instance_index, n.convention_name, n.convention_name_equivalence, n.description, n.status, n.deleted, +n.requested, n.requested_by, n.requested_comment, n.processed, n.processed_by, n.processed_comment from name n +where not (n.latest = false) +and n.systemgroup_uuid is not null and n.system_uuid is null and n.subsystem_uuid is null and n.devicetype_uuid is null; + +insert into tmp_audit_name ( + audit_id, + audit_version, + audit_table, + audit_operation, + id, + version, + uuid, + systemgroup_id, + system_id, + subsystem_id, + devicetype_id, + instance_index, + convention_name, + convention_name_equivalence, + description, + status, + -- latest, + deleted, + requested, + requested_by, + requested_comment, + processed, + processed_by, + processed_comment +) +select n.id, n."version", 'name', null, +n.id, n."version", n.uuid, +(select s.id from systemgroup s +where s.uuid = n.systemgroup_uuid and ( + not (exists (select s2.id from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select s2.id from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=true)) + and not (exists (select s2.id from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id > (select s2.id from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.status in ('CANCELLED', 'REJECTED')) + and not (not exists (select s2.id from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select max(s2.id) from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=false)) +)), +null, +null, +null, +n.instance_index, n.convention_name, n.convention_name_equivalence, n.description, n.status, n.deleted, +n.requested, n.requested_by, n.requested_comment, n.processed, n.processed_by, n.processed_comment from name n +where +-- (n.latest = false) and +n.systemgroup_uuid is not null and n.system_uuid is null and n.subsystem_uuid is null and n.devicetype_uuid is null; + +-- -------------------------------------------------------------------------------- +-- 2) +insert into tmp_name ( + id, + version, + uuid, + systemgroup_id, + system_id, + subsystem_id, + devicetype_id, + instance_index, + convention_name, + convention_name_equivalence, + description, + status, + -- latest, + deleted, + requested, + requested_by, + requested_comment, + processed, + processed_by, + processed_comment +) +select n.id, n."version", n.uuid, +null, +(select s.id from system s +where s.uuid = n.system_uuid and ( + not (exists (select s2.id from system s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select s2.id from system s2 where s2."uuid" = s."uuid" and s2.latest=true)) + and not (exists (select s2.id from system s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id > (select s2.id from system s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.status in ('CANCELLED', 'REJECTED')) + and not (not exists (select s2.id from system s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select max(s2.id) from system s2 where s2."uuid" = s."uuid" and s2.latest=false)) +)), +null, +null, +n.instance_index, n.convention_name, n.convention_name_equivalence, n.description, n.status, n.deleted, +n.requested, n.requested_by, n.requested_comment, n.processed, n.processed_by, n.processed_comment from name n +where not (n.latest = false) +and n.systemgroup_uuid is null and n.system_uuid is not null and n.subsystem_uuid is null and n.devicetype_uuid is null; + +insert into tmp_audit_name ( + audit_id, + audit_version, + audit_table, + audit_operation, + id, + version, + uuid, + systemgroup_id, + system_id, + subsystem_id, + devicetype_id, + instance_index, + convention_name, + convention_name_equivalence, + description, + status, + -- latest, + deleted, + requested, + requested_by, + requested_comment, + processed, + processed_by, + processed_comment +) +select n.id, n."version", 'name', null, +n.id, n."version", n.uuid, +null, +(select s.id from system s +where s.uuid = n.system_uuid and ( + not (exists (select s2.id from system s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select s2.id from system s2 where s2."uuid" = s."uuid" and s2.latest=true)) + and not (exists (select s2.id from system s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id > (select s2.id from system s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.status in ('CANCELLED', 'REJECTED')) + and not (not exists (select s2.id from system s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select max(s2.id) from system s2 where s2."uuid" = s."uuid" and s2.latest=false)) +)), +null, +null, +n.instance_index, n.convention_name, n.convention_name_equivalence, n.description, n.status, n.deleted, +n.requested, n.requested_by, n.requested_comment, n.processed, n.processed_by, n.processed_comment from name n +where +-- (n.latest = false) and +n.systemgroup_uuid is null and n.system_uuid is not null and n.subsystem_uuid is null and n.devicetype_uuid is null; + +-- -------------------------------------------------------------------------------- +-- 3) +insert into tmp_name ( + id, + version, + uuid, + systemgroup_id, + system_id, + subsystem_id, + devicetype_id, + instance_index, + convention_name, + convention_name_equivalence, + description, + status, + -- latest, + deleted, + requested, + requested_by, + requested_comment, + processed, + processed_by, + processed_comment +) +select n.id, n."version", n.uuid, +null, +null, +(select s.id from subsystem s +where s.uuid = n.subsystem_uuid and ( + not (exists (select s2.id from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select s2.id from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=true)) + and not (exists (select s2.id from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id > (select s2.id from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.status in ('CANCELLED', 'REJECTED')) + and not (not exists (select s2.id from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select max(s2.id) from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=false)) +)), +null, +n.instance_index, n.convention_name, n.convention_name_equivalence, n.description, n.status, n.deleted, +n.requested, n.requested_by, n.requested_comment, n.processed, n.processed_by, n.processed_comment from name n +where not (n.latest = false) +and n.systemgroup_uuid is null and n.system_uuid is null and n.subsystem_uuid is not null and n.devicetype_uuid is null; + +insert into tmp_audit_name ( + audit_id, + audit_version, + audit_table, + audit_operation, + id, + version, + uuid, + systemgroup_id, + system_id, + subsystem_id, + devicetype_id, + instance_index, + convention_name, + convention_name_equivalence, + description, + status, + -- latest, + deleted, + requested, + requested_by, + requested_comment, + processed, + processed_by, + processed_comment +) +select n.id, n."version", 'name', null, +n.id, n."version", n.uuid, +null, +null, +(select s.id from subsystem s +where s.uuid = n.subsystem_uuid and ( + not (exists (select s2.id from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select s2.id from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=true)) + and not (exists (select s2.id from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id > (select s2.id from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.status in ('CANCELLED', 'REJECTED')) + and not (not exists (select s2.id from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select max(s2.id) from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=false)) +)), +null, +n.instance_index, n.convention_name, n.convention_name_equivalence, n.description, n.status, n.deleted, +n.requested, n.requested_by, n.requested_comment, n.processed, n.processed_by, n.processed_comment from name n +where +-- (n.latest = false) and +n.systemgroup_uuid is null and n.system_uuid is null and n.subsystem_uuid is not null and n.devicetype_uuid is null; + +-- -------------------------------------------------------------------------------- +-- 4) +insert into tmp_name ( + id, + version, + uuid, + systemgroup_id, + system_id, + subsystem_id, + devicetype_id, + instance_index, + convention_name, + convention_name_equivalence, + description, + status, + -- latest, + deleted, + requested, + requested_by, + requested_comment, + processed, + processed_by, + processed_comment +) +select n.id, n."version", n.uuid, +(select s.id from systemgroup s +where s.uuid = n.systemgroup_uuid and ( + not (exists (select s2.id from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select s2.id from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=true)) + and not (exists (select s2.id from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id > (select s2.id from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.status in ('CANCELLED', 'REJECTED')) + and not (not exists (select s2.id from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select max(s2.id) from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=false)) +)), +null, +null, +(select s.id from devicetype s +where s.uuid = n.devicetype_uuid and ( + not (exists (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true)) + and not (exists (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id > (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.status in ('CANCELLED', 'REJECTED')) + and not (not exists (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select max(s2.id) from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=false)) +)), +n.instance_index, n.convention_name, n.convention_name_equivalence, n.description, n.status, n.deleted, +n.requested, n.requested_by, n.requested_comment, n.processed, n.processed_by, n.processed_comment from name n +where not (n.latest = false) +and n.systemgroup_uuid is not null and n.system_uuid is null and n.subsystem_uuid is null and n.devicetype_uuid is not null; + +insert into tmp_audit_name ( + audit_id, + audit_version, + audit_table, + audit_operation, + id, + version, + uuid, + systemgroup_id, + system_id, + subsystem_id, + devicetype_id, + instance_index, + convention_name, + convention_name_equivalence, + description, + status, + -- latest, + deleted, + requested, + requested_by, + requested_comment, + processed, + processed_by, + processed_comment +) +select n.id, n."version", 'name', null, +n.id, n."version", n.uuid, +(select s.id from systemgroup s +where s.uuid = n.systemgroup_uuid and ( + not (exists (select s2.id from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select s2.id from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=true)) + and not (exists (select s2.id from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id > (select s2.id from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.status in ('CANCELLED', 'REJECTED')) + and not (not exists (select s2.id from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select max(s2.id) from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=false)) +)), +null, +null, +(select s.id from devicetype s +where s.uuid = n.devicetype_uuid and ( + not (exists (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true)) + and not (exists (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id > (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.status in ('CANCELLED', 'REJECTED')) + and not (not exists (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select max(s2.id) from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=false)) +)), +n.instance_index, n.convention_name, n.convention_name_equivalence, n.description, n.status, n.deleted, +n.requested, n.requested_by, n.requested_comment, n.processed, n.processed_by, n.processed_comment from name n +where +-- (n.latest = false) and +n.systemgroup_uuid is not null and n.system_uuid is null and n.subsystem_uuid is null and n.devicetype_uuid is not null; + +-- -------------------------------------------------------------------------------- +-- 5) +insert into tmp_name ( + id, + version, + uuid, + systemgroup_id, + system_id, + subsystem_id, + devicetype_id, + instance_index, + convention_name, + convention_name_equivalence, + description, + status, + -- latest, + deleted, + requested, + requested_by, + requested_comment, + processed, + processed_by, + processed_comment +) +select n.id, n."version", n.uuid, +null, +(select s.id from system s +where s.uuid = n.system_uuid and ( + not (exists (select s2.id from system s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select s2.id from system s2 where s2."uuid" = s."uuid" and s2.latest=true)) + and not (exists (select s2.id from system s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id > (select s2.id from system s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.status in ('CANCELLED', 'REJECTED')) + and not (not exists (select s2.id from system s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select max(s2.id) from system s2 where s2."uuid" = s."uuid" and s2.latest=false)) +)), +null, +(select s.id from devicetype s +where s.uuid = n.devicetype_uuid and ( + not (exists (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true)) + and not (exists (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id > (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.status in ('CANCELLED', 'REJECTED')) + and not (not exists (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select max(s2.id) from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=false)) +)), +n.instance_index, n.convention_name, n.convention_name_equivalence, n.description, n.status, n.deleted, +n.requested, n.requested_by, n.requested_comment, n.processed, n.processed_by, n.processed_comment from name n +where not (n.latest = false) +and n.systemgroup_uuid is null and n.system_uuid is not null and n.subsystem_uuid is null and n.devicetype_uuid is not null; + +insert into tmp_audit_name ( + audit_id, + audit_version, + audit_table, + audit_operation, + id, + version, + uuid, + systemgroup_id, + system_id, + subsystem_id, + devicetype_id, + instance_index, + convention_name, + convention_name_equivalence, + description, + status, + -- latest, + deleted, + requested, + requested_by, + requested_comment, + processed, + processed_by, + processed_comment +) +select n.id, n."version", 'name', null, +n.id, n."version", n.uuid, +null, +(select s.id from system s +where s.uuid = n.system_uuid and ( + not (exists (select s2.id from system s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select s2.id from system s2 where s2."uuid" = s."uuid" and s2.latest=true)) + and not (exists (select s2.id from system s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id > (select s2.id from system s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.status in ('CANCELLED', 'REJECTED')) + and not (not exists (select s2.id from system s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select max(s2.id) from system s2 where s2."uuid" = s."uuid" and s2.latest=false)) +)), +null, +(select s.id from devicetype s +where s.uuid = n.devicetype_uuid and ( + not (exists (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true)) + and not (exists (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id > (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.status in ('CANCELLED', 'REJECTED')) + and not (not exists (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select max(s2.id) from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=false)) +)), +n.instance_index, n.convention_name, n.convention_name_equivalence, n.description, n.status, n.deleted, +n.requested, n.requested_by, n.requested_comment, n.processed, n.processed_by, n.processed_comment from name n +where +-- (n.latest = false) and +n.systemgroup_uuid is null and n.system_uuid is not null and n.subsystem_uuid is null and n.devicetype_uuid is not null; + +-- -------------------------------------------------------------------------------- +-- 6) +insert into tmp_name ( + id, + version, + uuid, + systemgroup_id, + system_id, + subsystem_id, + devicetype_id, + instance_index, + convention_name, + convention_name_equivalence, + description, + status, + -- latest, + deleted, + requested, + requested_by, + requested_comment, + processed, + processed_by, + processed_comment +) +select n.id, n."version", n.uuid, +null, +null, +(select s.id from subsystem s +where s.uuid = n.subsystem_uuid and ( + not (exists (select s2.id from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select s2.id from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=true)) + and not (exists (select s2.id from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id > (select s2.id from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.status in ('CANCELLED', 'REJECTED')) + and not (not exists (select s2.id from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select max(s2.id) from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=false)) +)), +(select s.id from devicetype s +where s.uuid = n.devicetype_uuid and ( + not (exists (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true)) + and not (exists (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id > (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.status in ('CANCELLED', 'REJECTED')) + and not (not exists (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select max(s2.id) from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=false)) +)), +n.instance_index, n.convention_name, n.convention_name_equivalence, n.description, n.status, n.deleted, +n.requested, n.requested_by, n.requested_comment, n.processed, n.processed_by, n.processed_comment from name n +where not (n.latest = false) +and n.systemgroup_uuid is null and n.system_uuid is null and n.subsystem_uuid is not null and n.devicetype_uuid is not null; + +insert into tmp_audit_name ( + audit_id, + audit_version, + audit_table, + audit_operation, + id, + version, + uuid, + systemgroup_id, + system_id, + subsystem_id, + devicetype_id, + instance_index, + convention_name, + convention_name_equivalence, + description, + status, + -- latest, + deleted, + requested, + requested_by, + requested_comment, + processed, + processed_by, + processed_comment +) +select n.id, n."version", 'name', null, +n.id, n."version", n.uuid, +null, +null, +(select s.id from subsystem s +where s.uuid = n.subsystem_uuid and ( + not (exists (select s2.id from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select s2.id from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=true)) + and not (exists (select s2.id from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id > (select s2.id from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.status in ('CANCELLED', 'REJECTED')) + and not (not exists (select s2.id from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select max(s2.id) from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=false)) +)), +(select s.id from devicetype s +where s.uuid = n.devicetype_uuid and ( + not (exists (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true)) + and not (exists (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id > (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.status in ('CANCELLED', 'REJECTED')) + and not (not exists (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id < (select max(s2.id) from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=false)) +)), +n.instance_index, n.convention_name, n.convention_name_equivalence, n.description, n.status, n.deleted, +n.requested, n.requested_by, n.requested_comment, n.processed, n.processed_by, n.processed_comment from name n +where +-- (n.latest = false) and +n.systemgroup_uuid is null and n.system_uuid is null and n.subsystem_uuid is not null and n.devicetype_uuid is not null; + +-- -------------------------------------------------------------------------------- +-- index +-- -------------------------------------------------------------------------------- +CREATE INDEX tmp_systemgroup_id_idx ON tmp_systemgroup (id); +CREATE INDEX tmp_systemgroup_uuid_idx ON tmp_systemgroup (uuid); +CREATE INDEX tmp_systemgroup_mnemonic_idx ON tmp_systemgroup (mnemonic); +CREATE INDEX tmp_systemgroup_status_idx ON tmp_systemgroup (status); +CREATE INDEX tmp_systemgroup_deleted_idx ON tmp_systemgroup (deleted); + +CREATE INDEX tmp_system_id_idx ON tmp_system (id); +CREATE INDEX tmp_system_uuid_idx ON tmp_system (uuid); +CREATE INDEX tmp_system_parent_id_idx ON tmp_system (parent_id); +CREATE INDEX tmp_system_mnemonic_idx ON tmp_system (mnemonic); +CREATE INDEX tmp_system_status_idx ON tmp_system (status); +CREATE INDEX tmp_system_deleted_idx ON tmp_system (deleted); + +CREATE INDEX tmp_subsystem_id_idx ON tmp_subsystem (id); +CREATE INDEX tmp_subsystem_uuid_idx ON tmp_subsystem (uuid); +CREATE INDEX tmp_subsystem_parent_id_idx ON tmp_subsystem (parent_id); +CREATE INDEX tmp_subsystem_mnemonic_idx ON tmp_subsystem (mnemonic); +CREATE INDEX tmp_subsystem_status_idx ON tmp_subsystem (status); +CREATE INDEX tmp_subsystem_deleted_idx ON tmp_subsystem (deleted); + +CREATE INDEX tmp_discipline_id_idx ON tmp_discipline (id); +CREATE INDEX tmp_discipline_uuid_idx ON tmp_discipline (uuid); +CREATE INDEX tmp_discipline_mnemonic_idx ON tmp_discipline (mnemonic); +CREATE INDEX tmp_discipline_status_idx ON tmp_discipline (status); +CREATE INDEX tmp_discipline_deleted_idx ON tmp_discipline (deleted); + +CREATE INDEX tmp_devicegroup_id_idx ON tmp_devicegroup (id); +CREATE INDEX tmp_devicegroup_uuid_idx ON tmp_devicegroup (uuid); +CREATE INDEX tmp_devicegroup_parent_id_idx ON tmp_devicegroup (parent_id); +CREATE INDEX tmp_devicegroup_mnemonic_idx ON tmp_devicegroup (mnemonic); +CREATE INDEX tmp_devicegroup_status_idx ON tmp_devicegroup (status); +CREATE INDEX tmp_devicegroup_deleted_idx ON tmp_devicegroup (deleted); + +CREATE INDEX tmp_devicetype_id_idx ON tmp_devicetype (id); +CREATE INDEX tmp_devicetype_uuid_idx ON tmp_devicetype (uuid); +CREATE INDEX tmp_devicetype_parent_id_idx ON tmp_devicetype (parent_id); +CREATE INDEX tmp_devicetype_mnemonic_idx ON tmp_devicetype (mnemonic); +CREATE INDEX tmp_devicetype_status_idx ON tmp_devicetype (status); +CREATE INDEX tmp_devicetype_deleted_idx ON tmp_devicetype (deleted); + +CREATE INDEX tmp_name_id_idx ON tmp_name (id); +CREATE INDEX tmp_name_uuid_idx ON tmp_name (uuid); +CREATE INDEX tmp_name_systemgroup_id_idx ON tmp_name (systemgroup_id); +CREATE INDEX tmp_name_system_id_idx ON tmp_name (system_id); +CREATE INDEX tmp_name_subsystem_id_idx ON tmp_name (subsystem_id); +CREATE INDEX tmp_name_devicetype_id_idx ON tmp_name (devicetype_id); +CREATE INDEX tmp_name_convention_name_idx ON tmp_name (convention_name); +CREATE INDEX tmp_name_status_idx ON tmp_name (status); +CREATE INDEX tmp_name_deleted_idx ON tmp_name (deleted); + +CREATE INDEX tmp_audit_structure_audit_id_idx ON tmp_audit_structure (audit_id); +CREATE INDEX tmp_audit_structure_audit_table_idx ON tmp_audit_structure (audit_table); +CREATE INDEX tmp_audit_structure_audit_operation_idx ON tmp_audit_structure (audit_operation); +CREATE INDEX tmp_audit_structure_id_idx ON tmp_audit_structure (id); +CREATE INDEX tmp_audit_structure_uuid_idx ON tmp_audit_structure (uuid); +CREATE INDEX tmp_audit_structure_parent_id_idx ON tmp_audit_structure (parent_id); +CREATE INDEX tmp_audit_structure_mnemonic_idx ON tmp_audit_structure (mnemonic); +CREATE INDEX tmp_audit_structure_status_idx ON tmp_audit_structure (status); +CREATE INDEX tmp_audit_structure_deleted_idx ON tmp_audit_structure (deleted); + +CREATE INDEX tmp_audit_name_audit_id_idx ON tmp_audit_name (audit_id); +CREATE INDEX tmp_audit_name_audit_table_idx ON tmp_audit_name (audit_table); +CREATE INDEX tmp_audit_name_audit_operation_idx ON tmp_audit_name (audit_operation); +CREATE INDEX tmp_audit_name_id_idx ON tmp_audit_name (id); +CREATE INDEX tmp_audit_name_uuid_idx ON tmp_audit_name (uuid); +CREATE INDEX tmp_audit_name_systemgroup_id_idx ON tmp_audit_name (systemgroup_id); +CREATE INDEX tmp_audit_name_system_id_idx on tmp_audit_name (system_id); +CREATE INDEX tmp_audit_name_subsystem_id_idx ON tmp_audit_name (subsystem_id); +CREATE INDEX tmp_audit_name_devicetype_id_idx ON tmp_audit_name (devicetype_id); +CREATE INDEX tmp_audit_name_convention_name_idx ON tmp_audit_name (convention_name); +CREATE INDEX tmp_audit_name_status_idx ON tmp_audit_name (status); +CREATE INDEX tmp_audit_name_deleted_idx ON tmp_audit_name (deleted); + +-- -------------------------------------------------------------------------------- +-- sequence +-- -------------------------------------------------------------------------------- +CREATE SEQUENCE tmp_systemgroup_id_seq + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; +CREATE SEQUENCE tmp_system_id_seq + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; +CREATE SEQUENCE tmp_subsystem_id_seq + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; +CREATE SEQUENCE tmp_discipline_id_seq + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; +CREATE SEQUENCE tmp_devicegroup_id_seq + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; +CREATE SEQUENCE tmp_devicetype_id_seq + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; +CREATE SEQUENCE tmp_name_id_seq + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; +CREATE SEQUENCE tmp_audit_structure_audit_id_seq + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; +CREATE SEQUENCE tmp_audit_name_audit_id_seq + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + +SELECT setval('tmp_systemgroup_id_seq', (select max(id) from tmp_systemgroup)); +SELECT setval('tmp_system_id_seq', (select max(id) from tmp_system)); +SELECT setval('tmp_subsystem_id_seq', (select max(id) from tmp_subsystem)); +SELECT setval('tmp_discipline_id_seq', (select max(id) from tmp_discipline)); +SELECT setval('tmp_devicegroup_id_seq', (select max(id) from tmp_devicegroup)); +SELECT setval('tmp_devicetype_id_seq', (select max(id) from tmp_devicetype)); +SELECT setval('tmp_name_id_seq', (select max(id) from tmp_name)); +SELECT setval('tmp_audit_structure_audit_id_seq', (select max(audit_id) from tmp_audit_structure)); +SELECT setval('tmp_audit_name_audit_id_seq', (select max(audit_id) from tmp_audit_name)); + +ALTER SEQUENCE tmp_systemgroup_id_seq OWNED BY tmp_systemgroup.id; +ALTER SEQUENCE tmp_system_id_seq OWNED BY tmp_system.id; +ALTER SEQUENCE tmp_subsystem_id_seq OWNED BY tmp_subsystem.id; +ALTER SEQUENCE tmp_discipline_id_seq OWNED BY tmp_discipline.id; +ALTER SEQUENCE tmp_devicegroup_id_seq OWNED BY tmp_devicegroup.id; +ALTER SEQUENCE tmp_devicetype_id_seq OWNED BY tmp_devicetype.id; +ALTER SEQUENCE tmp_name_id_seq OWNED BY tmp_name.id; +ALTER SEQUENCE tmp_audit_structure_audit_id_seq OWNED BY tmp_audit_structure.audit_id; +ALTER SEQUENCE tmp_audit_name_audit_id_seq OWNED BY tmp_audit_name.audit_id; + +ALTER TABLE ONLY tmp_systemgroup ALTER COLUMN id SET DEFAULT nextval('tmp_systemgroup_id_seq'::regclass); +ALTER TABLE ONLY tmp_system ALTER COLUMN id SET DEFAULT nextval('tmp_system_id_seq'::regclass); +ALTER TABLE ONLY tmp_subsystem ALTER COLUMN id SET DEFAULT nextval('tmp_subsystem_id_seq'::regclass); +ALTER TABLE ONLY tmp_discipline ALTER COLUMN id SET DEFAULT nextval('tmp_discipline_id_seq'::regclass); +ALTER TABLE ONLY tmp_devicegroup ALTER COLUMN id SET DEFAULT nextval('tmp_devicegroup_id_seq'::regclass); +ALTER TABLE ONLY tmp_devicetype ALTER COLUMN id SET DEFAULT nextval('tmp_devicetype_id_seq'::regclass); +ALTER TABLE ONLY tmp_name ALTER COLUMN id SET DEFAULT nextval('tmp_name_id_seq'::regclass); +ALTER TABLE ONLY tmp_audit_structure ALTER COLUMN audit_id SET DEFAULT nextval('tmp_audit_structure_audit_id_seq'::regclass); +ALTER TABLE ONLY tmp_audit_name ALTER COLUMN audit_id SET DEFAULT nextval('tmp_audit_name_audit_id_seq'::regclass); + +-- -------------------------------------------------------------------------------- +-- primary key +-- -------------------------------------------------------------------------------- +ALTER TABLE tmp_systemgroup ADD CONSTRAINT tmp_systemgroup_pk PRIMARY KEY (id); +ALTER TABLE tmp_system ADD CONSTRAINT tmp_system_pk PRIMARY KEY (id); +ALTER TABLE tmp_subsystem ADD CONSTRAINT tmp_subsystem_pk PRIMARY KEY (id); +ALTER TABLE tmp_discipline ADD CONSTRAINT tmp_discipline_pk PRIMARY KEY (id); +ALTER TABLE tmp_devicegroup ADD CONSTRAINT tmp_devicegroup_pk PRIMARY KEY (id); +ALTER TABLE tmp_devicetype ADD CONSTRAINT tmp_devicetype_pk PRIMARY KEY (id); +ALTER TABLE tmp_name ADD CONSTRAINT tmp_name_pk PRIMARY KEY (id); +ALTER TABLE tmp_audit_structure ADD CONSTRAINT tmp_audit_structure_pk PRIMARY KEY (audit_id); +ALTER TABLE tmp_audit_name ADD CONSTRAINT tmp_audit_name_pk PRIMARY KEY (audit_id); + +-- -------------------------------------------------------------------------------- +-- foreign key +-- -------------------------------------------------------------------------------- +ALTER TABLE ONLY tmp_system ADD CONSTRAINT fk_system_systemgroup FOREIGN KEY (parent_id) REFERENCES tmp_systemgroup(id); +ALTER TABLE ONLY tmp_subsystem ADD CONSTRAINT fk_subsystem_system FOREIGN KEY (parent_id) REFERENCES tmp_system(id); +ALTER TABLE ONLY tmp_devicegroup ADD CONSTRAINT fk_devicegroup_discipline FOREIGN KEY (parent_id) REFERENCES tmp_discipline(id); +ALTER TABLE ONLY tmp_devicetype ADD CONSTRAINT fk_devicetype_devicegroup FOREIGN KEY (parent_id) REFERENCES tmp_devicegroup(id); +ALTER TABLE ONLY tmp_name ADD CONSTRAINT fk_name_systemgroup FOREIGN KEY (systemgroup_id) REFERENCES tmp_systemgroup(id); +ALTER TABLE ONLY tmp_name ADD CONSTRAINT fk_name_system FOREIGN KEY (system_id) REFERENCES tmp_system(id); +ALTER TABLE ONLY tmp_name ADD CONSTRAINT fk_name_subsystem FOREIGN KEY (subsystem_id) REFERENCES tmp_subsystem(id); +ALTER TABLE ONLY tmp_name ADD CONSTRAINT fk_name_devicetype FOREIGN KEY (devicetype_id) REFERENCES tmp_devicetype(id); + +-- -------------------------------------------------------------------------------- +-- function +-- -------------------------------------------------------------------------------- +-- same as V4 +-- get_mnemonic_path_system_structure(convention_name text) +-- get_mnemonic_path_device_structure(convention_name text) +-- get_instance_index(convention_name text) + +CREATE OR REPLACE FUNCTION tmp_get_mnemonic_path_system_structure(convention_name text) + RETURNS text + LANGUAGE plpgsql +AS +$$ +DECLARE + pos int; +BEGIN + pos = strpos(convention_name, ':'); + IF pos > 0 THEN + RETURN substr(convention_name, 1, pos-1); + END IF; + RETURN convention_name; +END; +$$; + +CREATE OR REPLACE FUNCTION tmp_get_mnemonic_path_device_structure(convention_name text) + RETURNS text + LANGUAGE plpgsql +AS +$$ +DECLARE + pos int; + mnemonic_path text; + nbr_delimiters int; +BEGIN + pos = strpos(convention_name, ':'); + IF pos > 0 THEN + mnemonic_path = substr(convention_name, pos+1); + nbr_delimiters = array_length(string_to_array(mnemonic_path, '-'), 1) - 1; + IF nbr_delimiters = 2 then + mnemonic_path = reverse(mnemonic_path); + mnemonic_path = substr(mnemonic_path, strpos(mnemonic_path, '-')+1); + RETURN reverse(mnemonic_path); + ELSIF nbr_delimiters = 1 then + return mnemonic_path; + ELSE + RETURN null; + END IF; + END IF; + RETURN null; +END; +$$; + +CREATE OR REPLACE FUNCTION tmp_get_instance_index(convention_name text) + RETURNS text + LANGUAGE plpgsql +AS +$$ +DECLARE + len int; + pos int; + mnemonic_path text; + nbr_delimiters int; +BEGIN + pos = strpos(convention_name, ':'); + IF pos > 0 THEN + mnemonic_path = substr(convention_name, pos+1); + nbr_delimiters = array_length(string_to_array(mnemonic_path, '-'), 1) - 1; + IF nbr_delimiters = 2 then + mnemonic_path = reverse(mnemonic_path); + len = length(mnemonic_path); + pos = strpos(mnemonic_path, '-'); + mnemonic_path = reverse(mnemonic_path); + RETURN substr(mnemonic_path, len - pos + 2); + ELSE + RETURN null; + END IF; + END IF; + RETURN null; +END; +$$; + +CREATE OR REPLACE FUNCTION tmp_get_parent_uuid_system(system_id bigint) + RETURNS text + LANGUAGE plpgsql +AS +$$ +DECLARE + parent_uuid text; +BEGIN + select uuid into parent_uuid from tmp_systemgroup where id = (select parent_id from tmp_system where id = system_id); + + if parent_uuid is not null then + return parent_uuid; + else + return null; + end if; +END; +$$; + +CREATE OR REPLACE FUNCTION tmp_get_parent_uuid_subsystem(subsystem_id bigint) + RETURNS text + LANGUAGE plpgsql +AS +$$ +DECLARE + parent_uuid text; +BEGIN + select uuid into parent_uuid from tmp_system where id = (select parent_id from tmp_subsystem where id = subsystem_id); + + if parent_uuid is not null then + return parent_uuid; + else + return null; + end if; +END; +$$; + +CREATE OR REPLACE FUNCTION tmp_get_parent_uuid_devicegroup(devicegroup_id bigint) + RETURNS text + LANGUAGE plpgsql +AS +$$ +DECLARE + parent_uuid text; +BEGIN + select uuid into parent_uuid from tmp_discipline where id = (select parent_id from tmp_devicegroup where id = devicegroup_id); + + if parent_uuid is not null then + return parent_uuid; + else + return null; + end if; +END; +$$; + +CREATE OR REPLACE FUNCTION tmp_get_parent_uuid_devicetype(devicetype_id bigint) + RETURNS text + LANGUAGE plpgsql +AS +$$ +DECLARE + parent_uuid text; +BEGIN + select uuid into parent_uuid from tmp_devicegroup where id = (select parent_id from tmp_devicetype where id = devicetype_id); + + if parent_uuid is not null then + return parent_uuid; + else + return null; + end if; +END; +$$; + +CREATE OR REPLACE FUNCTION tmp_get_mnemonic_path_system(system_id bigint) + RETURNS text + LANGUAGE plpgsql +AS +$$ +DECLARE + system_mnemonic text; +BEGIN + select mnemonic into system_mnemonic from tmp_system where id = system_id; + + if system_mnemonic is not null then + return system_mnemonic; + else + return null; + end if; +END; +$$; + +CREATE OR REPLACE FUNCTION tmp_get_mnemonic_path_subsystem(subsystem_id bigint) + RETURNS text + LANGUAGE plpgsql +AS +$$ +DECLARE + subsystem_mnemonic text; + system_id bigint; + system_mnemonic text; +BEGIN + select parent_id, mnemonic into system_id, subsystem_mnemonic from tmp_subsystem where id = subsystem_id; + select mnemonic into system_mnemonic from tmp_system where id = system_id; + + if system_mnemonic is not null and subsystem_mnemonic is not null then + return concat(system_mnemonic, '-', subsystem_mnemonic); + else + return null; + end if; +END; +$$; + +CREATE OR REPLACE FUNCTION tmp_get_mnemonic_path_devicegroup(devicegroup_id bigint) + RETURNS text + LANGUAGE plpgsql +AS +$$ +DECLARE + discipline_id bigint; + discipline_mnemonic text; +BEGIN + select parent_id into discipline_id from tmp_devicegroup where id = devicegroup_id; + select mnemonic into discipline_mnemonic from tmp_discipline where id = discipline_id; + + if discipline_mnemonic is not null then + return discipline_mnemonic; + else + return null; + end if; +END; +$$; + +CREATE OR REPLACE FUNCTION tmp_get_mnemonic_path_devicetype(devicetype_id bigint) + RETURNS text + LANGUAGE plpgsql +AS +$$ +DECLARE + devicetype_mnemonic text; + devicegroup_id bigint; + discipline_id bigint; + discipline_mnemonic text; +BEGIN + select parent_id, mnemonic into devicegroup_id, devicetype_mnemonic from tmp_devicetype where id = devicetype_id; + select parent_id, mnemonic into discipline_id from tmp_devicegroup where id = devicegroup_id; + select mnemonic into discipline_mnemonic from tmp_discipline where id = discipline_id; + + if discipline_mnemonic is not null and devicetype_mnemonic is not null then + return concat(discipline_mnemonic, '-', devicetype_mnemonic); + elsif devicetype_mnemonic is not null then + return devicetype_mnemonic; + else + return null; + end if; +END; +$$;