# GNUmed database system bootstrapping config file
#-------------------------------------------------
# license: GPL v2 or later
# author: Karsten Hilbert@gmx.net

#----------------------------------
[installation]
interactive = yes

keep temp files = no

description = $description$
This will update an existing GNUmed version 22
database to the version 23 schema. It does not do
any harm to the data contained within.

The existing database will be cloned first. The copy is
then modified. The original database remains unchanged.
$description$


bundles = $bundles$
v22-v23-static
v22-v23-dynamic
$bundles$

#----------------------------------
[bundle v22-v23-static]
database alias = gnumed_v23
minimum postgresql version = 15.0
schema base directory = ../sql/v22-v23/static/
schema = $schema$
v23-gm-nonempty_text-domain.sql
v23-gm-obj_export_passphrase-static.sql
v23-dem-staff-static.sql
v23-dem-gender_label-static.sql
v23-clin-incoming_data-static.sql
v23-clin-export_item-static.sql
v23-clin-intake-static.sql
v23-clin-intake_regimen-static.sql
v23-ref-vaccine-static.sql
v23-ref-vacc_indication-static.sql
v23-ref-lnk_indic2vaccine-static.sql
$schema$

#----------------------------------
[bundle v22-v23-dynamic]
database alias = gnumed_v23
minimum postgresql version = 15.0
schema base directory = ../sql/v22-v23/dynamic/
schema = $schema$
v23-audit-add_table_for_audit.sql
v23-cfg-cfg_item.sql
v23-cfg-v_cfg_options.sql
v23-clin-incoming_data-dynamic.sql
v23-cfg-incoming_area_plugin.sql
v23-cfg-set_option.sql
v23-cfg-drop_old.sql
v23-gm-obj_export_passphrase-dynamic.sql
v23-clin-sanity_check_patient_from_encounter_vs_episode.sql
v23-dem-gender_label-dynamic.sql
v23-dem-identity-dynamic.sql
v23-dem-unique_named_identity.sql
v23-dem-person_views.sql
v23-dem-create_urb.sql
v23-dem-v_pat_addresses.sql
v23-dem-staff-dynamic.sql
v23-ref-lnk_dose2drug-dynamic.sql
v23-ref-vacc-drop_tables.sql
v23-ref-vaccine-dynamic.sql
v23-ref-vacc_indication-dynamic.sql
v23-ref-insert_vacc_indications.sql
v23-ref-lnk_indic2vaccine-dynamic.sql
v23-ref-convert_vaccines.sql
v23-ref-v_vaccines.sql
v23-ref-v_indications4vaccine.sql
v23-clin-v_test_results.sql
v23-clin-v_test_results_inbox.sql
v23-clin-v_test_results_journal.sql
v23-clin-v_vaccinations.sql
v23-clin-v_vaccinations4indication.sql
v23-clin-v_last_vaccination4indication.sql
v23-clin-v_vaccinations_journal.sql
v23-clin-allergy_state-dynamic.sql
v23-clin-v_pat_allergy_state.sql
v23-clin-v_pat_allergy_state_journal.sql
v23-clin-export_item-dynamic.sql
v23-clin-v_export_items.sql
v23-clin-lnk_substance2episode-dynamic.sql
v23-clin-map_enc_or_epi_to_patient.sql
v23-clin-intake-dynamic.sql
v23-clin-intake_regimen-dynamic.sql
v23-clin-check_intake_vs_regimen.sql
v23-clin-intake_regimen-transfer_data.sql
v23-clin-substance_intake-dynamic.sql
v23-ref-vacc-drop_tables.sql
v23-clin-v_intakes.sql
v23-clin-v_intake4narrative_search.sql
v23-clin-v_intake_regimen4narrative_search.sql
v23-clin-v_narrative4search.sql
v23-clin-v_intake_regimen.sql
v23-clin-v_intakes_with_regimens.sql
v23-clin-v_intake_journal.sql
v23-clin-v_procedures_journal.sql
v23-clin-v_emr_journal.sql
v23-ref-v_drug_components.sql
v23-ref-paperwork_templates.sql
v23-dem-v_message_inbox.sql
v23-gm-adjust_view_options.sql
v23-role-gm_staff-permissions.sql
v23-release_notes-dynamic.sql
$schema$

#----------------------------------
[bundle v23-fixups]
database alias = gnumed_v23
minimum postgresql version = 15.0
schema base directory = ../sql/v22-v23/fixups/
schema = $schema$
$schema$

#----------------------------------
[bundle v23-test_data]
database alias = gnumed_v23
minimum postgresql version = 15.0
schema base directory = ../sql/test-data/
schema = $schema$
$schema$

#----------------------------------
[database gnumed_v23]
name = gnumed_v23
drop target database = 1
target version = v23
reindex = 1
revalidate = 1
transfer users = 1
source database = gnumed_v22
source version = v22
audit disable = 0
schema base directory = ../sql/v22-v23/
schema = $schema$
$schema$

superuser schema = $superuser schema$
superuser/v23-reinstall-btree_gist.sql
superuser/v23-gm-revalidate_all_constraints.sql
superuser/v23-gm-collations_functions.sql
$superuser schema$

script base directory = ../sql/v22-v23/python/
data import scripts = $data import scripts$
v23-import-form-templates
v23-import-plot-scripts.py
$data import scripts$




# line 1: old database
# line 2: new database
upgrade plausibility checks = $upgrade plausibility checks$
identities::::select count(1) from dem.identity
	select count(1) from dem.identity			-- new
names::::select count(1) from dem.names
	select count(1) from dem.names	-- new
v_all_persons::::select count(1) from dem.v_persons
	select count(1) from dem.v_all_persons	-- new
v_active_persons::::select count(1) from dem.v_active_persons
	select count(1) from dem.v_active_persons	-- new
staff::::select count(1) from dem.staff								-- old
	select count(1) from dem.v_staff								-- new
v_staff::::select count(1) from dem.v_staff
	select count(1) from dem.v_staff	-- new
addresses::::select count(1) from dem.address
	select count(1) from dem.address	-- new
unique URBs with ZIP::::select count(1) from dem.v_uniq_zipped_urbs		-- old
	select count(1) from dem.v_uniq_zipped_urbs							-- new
patient addresses view::::select count(1) from dem.v_pat_addresses		-- old
	select count(1) from dem.v_pat_addresses							-- new
episodes::::select count(1) from clin.episode where not summary ilike '%[substance use]%'
	select count(1) from clin.episode where not summary ilike '%[substance use]%'
denormalized episodes::::select count(1) from clin.v_pat_episodes where not summary ilike '%[substance use]%'
	select count(1) from clin.v_pat_episodes where not summary ilike '%[substance use]%'
encounters::::select count(1) from clin.encounter
	select count(1) from clin.v_pat_encounters
issues::::select count(1) from clin.health_issue
	select count(1) from clin.health_issue
issues view::::select count(1) from clin.v_health_issues
	select count(1) from clin.v_health_issues
procedures::::select count(1) from clin.procedure
	select count(1) from clin.procedure
documents::::select count(1) from blobs.doc_med
	select count(1) from blobs.doc_med
document pages::::select count(1) from blobs.doc_obj
	select count(1) from blobs.doc_obj
allergies::::select count(1) from clin.allergy
	select count(1) from clin.allergy
communication channels::::select count(1) from dem.lnk_identity2comm
	select count(1) from dem.lnk_identity2comm
test results::::select count(1) from clin.test_result
	select count(1) from clin.test_result
unmatched test results::::select count(1) from clin.incoming_data_unmatched
	select count(1) from clin.incoming_data
unmatchable test results::::select count(1) from clin.incoming_data_unmatchable
	select count(1) from clin.incoming_data_unmatchable
denormalized test results::::select count(1) from clin.v_test_results
	select count(1) from clin.v_test_results
allergy states::::select count(1) from clin.allergy_state where has_allergy is distinct from null
	select count(1) from clin.allergy_state
waiting list entries::::select count(1) from clin.waiting_list
	select count(1) from clin.waiting_list
waiting list view entries::::select count(1) from clin.v_waiting_list
	select count(1) from clin.v_waiting_list
waiting list integrity::::select((select count(1) from clin.v_waiting_list) = (select count(1) from clin.waiting_list))
	select((select count(1) from clin.v_waiting_list) = (select count(1) from clin.waiting_list))
--messages::::select count(1) + 1 from dem.message_inbox
--	select count(1) from dem.message_inbox
test orgs::::select count(1) from clin.test_org
	select count(1) from clin.test_org
-- fails because vaccines have been turned into single-component brands:
--drug components::::select count(1) from ref.v_drug_components
--	select count(1) from ref.v_drug_components
substance view -- correctness::::select True
	select (select count(1) from ref.substance) = (select count(1) from ref.v_substances)
dose view -- correctness::::select True
	select (select count(1) from ref.dose) = (select count(1) from ref.v_substance_doses)
--substance intakes (tables)::::select count(1) from clin.substance_intake
--	select count(1) from clin.intake
--intakes (view, ongoing)::::select count(1) from clin.v_substance_intakes where discontinued IS NULL
--	select count(1) from clin.v_intakes_with_regimens where discontinued IS NULL
vaccinations (total count)::::select count(1) from clin.vaccination
	select count(1) from clin.vaccination
vaccinations-by-indication (total count)::::select count(1) from clin.v_pat_vaccs4indication
	select count(1) from clin.v_vaccinations4indication
vaccinations-by-indication (patient count)::::select count(1) from (select distinct on (pk_patient) pk_patient from clin.v_pat_vaccs4indication) as distinct_pats
	select count(1) from (select distinct on (pk_patient) pk_patient from clin.v_vaccinations4indication) as distinct_pats
vaccinations-by-indication (shots count)::::select count(1) from (select distinct on (pk_vaccination) pk_vaccination from clin.v_pat_vaccs4indication) as distinct_shots
	select count(1) from (select distinct on (pk_vaccination) pk_vaccination from clin.v_vaccinations4indication) as distinct_shots
--vaccinations-by-indication (indications count)::::select count(1) from clin.v_pat_vaccs4indication group by pk_indication
--	select count(1) from clin.v_vaccinations4indication group by atc_indication
vaccinations-by-indication (vaccine count)::::select count(1) from (select distinct on (pk_vaccine) pk_vaccine from clin.v_pat_vaccs4indication) as distinct_vaccines
	select count(1) from (select distinct on (pk_vaccine) pk_vaccine from clin.v_vaccinations4indication) as distinct_vaccines
vaccinations-by-indication (consistency)::::select count(1) from clin.vaccination
	select count(1) from (select distinct on (pk_vaccination) pk_vaccination from clin.v_vaccinations4indication) as distinct_shots
latest vaccinations (total count)::::select count(1) from clin.v_pat_last_vacc4indication where indication NOT ILIKE '%influenza%'
	select count(1) from clin.v_last_vaccination4indication where indication NOT ILIKE '%influenza%'
latest vaccinations (patient count)::::select count(1) from clin.v_pat_last_vacc4indication where indication NOT ILIKE '%influenza%' group by pk_patient
	select count(1) from clin.v_last_vaccination4indication where indication NOT ILIKE '%influenza%' group by pk_patient
latest vaccinations (shots count)::::select count(1) from clin.v_pat_last_vacc4indication group by pk_vaccination
	select count(1) from clin.v_last_vaccination4indication group by pk_vaccination
latest vaccinations (indications count)::::select count(1) from clin.v_pat_last_vacc4indication group by atc_indication
	select count(1) from clin.v_last_vaccination4indication group by atc_indication
latest vaccinations (vaccine count)::::select count(1) from clin.v_pat_last_vacc4indication group by pk_vaccine
	select count(1) from clin.v_last_vaccination4indication group by pk_vaccine
requests on results::::select count(1) from clin.test_result where fk_request is not NULL
	select count(1) from clin.test_result where fk_request is not NULL
clinical code links (total) - internal consistency::::select True
	select ((select count(1) from clin.lnk_code2item_root) = (select ((select count(1) from clin.lnk_code2procedure) + (select count(1) from clin.lnk_code2rfe) + (select count(1) from clin.lnk_code2aoe) + (select count(1) from clin.lnk_code2episode) + (select count(1) from clin.lnk_code2h_issue) + (select count(1) from clin.lnk_code2narrative))))
paperwork templates::::select count(1) - 1 from ref.paperwork_templates
	select count(1) from ref.paperwork_templates
automatic hints::::select count(1) from ref.auto_hint
	select count(1) from ref.auto_hint
-- do NOT try to check the number of suppressed hints because
-- even though we do know the number of *hints* that will have
-- changed we simply cannot know the number of suppressions that
-- will be lifted by those changes
--suppressed hints::::select count(1) from clin.suppressed_hint
--	select count(1) from clin.suppressed_hint
raw keyword expansions::::select count(1) from ref.keyword_expansion
	select count(1) from ref.keyword_expansion
mapped keyword expansions::::select count(1) from ref.v_keyword_expansions
	select count(1) from ref.v_keyword_expansions
organisations::::select count(1) from dem.org
	select count(1) from dem.org
organisational units::::select count(1) from dem.org_unit
	select count(1) from dem.org_unit
data sources::::select count(1) from ref.data_source
	select count(1) from ref.data_source
bills with invoices but without VAT status::::select 0
	select count(1) from bill.bill where (apply_vat is null) and (fk_doc is not null)
--clinical items::::select count(1) from clin.clin_root_item
--	select count(1) from clin.clin_root_item
export items::::select count(1) from clin.export_item
	select count(1) from clin.export_item
export items view::::select count(1) from clin.v_export_items
	select count(1) from clin.v_export_items
export items integrity::::select ((select count(1) from clin.export_item) = (select count(1) from clin.v_export_items))
	select ((select count(1) from clin.export_item) = (select count(1) from clin.v_export_items))
external care::::select count(1) from clin.external_care
	select count(1) from clin.external_care
external care view integrity::::select TRUE
	select ((select count(1) from clin.external_care) = (select count(1) from clin.v_external_care))
-- ------------------
-- commented out:
--ATC codes::::select count(1) from (select distinct on (code, term, fk_data_source) 1 from ref.atc) as uniq_atc
--	select count(1) from ref.atc
--ATC vs generic codes - internal consistency::::select 0
--	select code, term, fk_data_source from ref.atc except select code, term, fk_data_source from ref.generic_coding_system
--LOINC codes::::select count(1) from ref.loinc
--	select count(1) from ref.loinc
--LOINC vs generic codes - internal consistency::::select 0
--	select code, term, fk_data_source from ref.loinc except select code, term, fk_data_source from ref.generic_coding_system
--generic codes (total) - internal consistency::::select True
--	select ((select count(1) from ref.coding_system_root) = (select ((select count(1) from ref.atc) + (select count(1) from ref.loinc))))
-- ------------------
-- for debugging:
--skipped check::::select 'skipped in old'		-- old
--	select 'skipped in new'						-- new
--check definition error::select 1				-- old
--	select 1									-- new
--syntax error (old)::::selec 1 				-- old
--	select 1									-- new
--syntax error (new)::::select 1 				-- old
--	selec 1										-- new
--data mismatch error::::select 1 				-- old
--	select 2 -- new
$upgrade plausibility checks$

#----------------------------------
[cluster]
host name = 
port = 5432
maintenance database = template1
schema base directory = ../sql/v22-v23/
schema = $schema$
$schema$

#----------------------------------
# these really shouldn't be changed
#----------------------------------
[GnuMed defaults]

groups = $groups$
$groups$

#----------------------------------
