<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
    xmlns="http://www.liquibase.org/xml/ns/dbchangelog/1.9"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog/1.9
                      http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-1.9.xsd">
					  

	<changeSet id="0001" author="saliakhov">
        <comment>Удаление колонок, добавление вью count-all</comment>
		<sql>
			DROP VIEW IF EXISTS init_registration_info.view_records_info_id_uk_id_ern;
			ALTER TABLE init_registration_info.records_info_id_uk_id_ern DROP COLUMN IF EXISTS count_all;
			ALTER TABLE init_registration_info.records_info_id_uk_id_ern DROP COLUMN IF EXISTS records_with_id_ern;
			
			CREATE OR REPLACE VIEW init_registration_info.view_records_info_id_uk_id_ern
			AS SELECT records_info_id_uk_id_ern.records_info_id_uk_id_ern_id,
				records_info_id_uk_id_ern.records_with_id_uk + records_info_id_uk_id_ern.records_without_id_uk AS count_all,
				round(records_info_id_uk_id_ern.records_with_id_uk::numeric * 100::numeric / (records_info_id_uk_id_ern.records_with_id_uk + records_info_id_uk_id_ern.records_without_id_uk)::numeric) AS percent_with_id_uk,
				round(records_info_id_uk_id_ern.records_without_id_uk::numeric * 100::numeric / (records_info_id_uk_id_ern.records_with_id_uk + records_info_id_uk_id_ern.records_without_id_uk)::numeric) AS percent_without_id_uk
				FROM init_registration_info.records_info_id_uk_id_ern;	
		</sql>
	</changeSet>

	<changeSet id="0002" author="saliakhov">
        <comment>Удаление вью, удаление-добавление колонок</comment>
		<sql>
			DROP VIEW IF EXISTS actualization.view_change_data_from_gir_vu_citizen;

			ALTER TABLE actualization.change_data_from_gir_vu_citizen DROP COLUMN IF EXISTS count_renaming;
			ALTER TABLE actualization.change_data_from_gir_vu_citizen DROP COLUMN IF EXISTS count_change_birth_date;
			ALTER TABLE actualization.change_data_from_gir_vu_citizen DROP COLUMN IF EXISTS count_change_birth_address;
			ALTER TABLE actualization.change_data_from_gir_vu_citizen DROP COLUMN IF EXISTS count_change_passport;
			ALTER TABLE actualization.change_data_from_gir_vu_citizen DROP COLUMN IF EXISTS count_reg_address;
			ALTER TABLE actualization.change_data_from_gir_vu_citizen DROP COLUMN IF EXISTS count_place_of_stay_with_reg;
			ALTER TABLE actualization.change_data_from_gir_vu_citizen DROP COLUMN IF EXISTS count_place_of_stay_without_reg;
			ALTER TABLE actualization.change_data_from_gir_vu_citizen DROP COLUMN IF EXISTS count_other_attributes;

			ALTER TABLE actualization.change_data_from_gir_vu_citizen ADD COLUMN IF NOT EXISTS count_unique_records int8 DEFAULT 0 NOT NULL; 
		</sql>
	</changeSet>
	
	<changeSet id="0003" author="saliakhov">
        <comment>Удаление вью, удаление колонки</comment>
		<sql>
			DROP VIEW IF EXISTS summonses_list_subsystem.view_personal_accounts;

			ALTER TABLE summonses_list_subsystem.personal_accounts DROP COLUMN IF EXISTS count_not_registered;
		</sql>
	</changeSet>
	
	<changeSet id="0004" author="saliakhov">
        <comment>Удаление вью, удаление-добавление колонок</comment>
		<sql>
			DROP VIEW IF EXISTS summonses_list_subsystem.view_summonses_direct_to_pers_acc;
			
			ALTER TABLE summonses_list_subsystem.summonses_direct_to_pers_acc DROP COLUMN IF EXISTS count_draft_board;
			ALTER TABLE summonses_list_subsystem.summonses_direct_to_pers_acc DROP COLUMN IF EXISTS count_place_of_military_service;
			ALTER TABLE summonses_list_subsystem.summonses_direct_to_pers_acc DROP COLUMN IF EXISTS count_med_inspection;
			ALTER TABLE summonses_list_subsystem.summonses_direct_to_pers_acc DROP COLUMN IF EXISTS count_doc_clarification;
				
			ALTER TABLE summonses_list_subsystem.summonses_direct_to_pers_acc ADD COLUMN IF NOT EXISTS count_draft_board int8 DEFAULT 0 NOT NULL;
			ALTER TABLE summonses_list_subsystem.summonses_direct_to_pers_acc ADD COLUMN IF NOT EXISTS count_recruitment_company int8 DEFAULT 0 NOT NULL;
			ALTER TABLE summonses_list_subsystem.summonses_direct_to_pers_acc ADD COLUMN IF NOT EXISTS count_military_registration int8 DEFAULT 0 NOT NULL;
			ALTER TABLE summonses_list_subsystem.summonses_direct_to_pers_acc ADD COLUMN IF NOT EXISTS count_mobilisation int8 DEFAULT 0 NOT NULL;
						
			CREATE OR REPLACE VIEW summonses_list_subsystem.view_summonses_direct_to_pers_acc
			AS SELECT summonses_direct_to_pers_acc.summonses_direct_to_pers_acc_id,
				summonses_direct_to_pers_acc.count_draft_board + summonses_direct_to_pers_acc.count_recruitment_company + summonses_direct_to_pers_acc.count_military_registration + summonses_direct_to_pers_acc.count_mobilisation AS count_all,
				round(summonses_direct_to_pers_acc.count_draft_board::numeric * 100::numeric / (summonses_direct_to_pers_acc.count_draft_board + summonses_direct_to_pers_acc.count_recruitment_company + summonses_direct_to_pers_acc.count_military_registration + summonses_direct_to_pers_acc.count_mobilisation)::numeric) AS percent_draft_board,
				round(summonses_direct_to_pers_acc.count_recruitment_company::numeric * 100::numeric / (summonses_direct_to_pers_acc.count_draft_board + summonses_direct_to_pers_acc.count_recruitment_company + summonses_direct_to_pers_acc.count_military_registration + summonses_direct_to_pers_acc.count_mobilisation)::numeric) AS percent_recruitment_company,
				round(summonses_direct_to_pers_acc.count_military_registration::numeric * 100::numeric / (summonses_direct_to_pers_acc.count_draft_board + summonses_direct_to_pers_acc.count_recruitment_company + summonses_direct_to_pers_acc.count_military_registration + summonses_direct_to_pers_acc.count_mobilisation)::numeric) AS percent_military_registration,
				round(summonses_direct_to_pers_acc.count_mobilisation::numeric * 100::numeric / (summonses_direct_to_pers_acc.count_draft_board + summonses_direct_to_pers_acc.count_recruitment_company + summonses_direct_to_pers_acc.count_military_registration + summonses_direct_to_pers_acc.count_mobilisation)::numeric) AS percent_mobilisation
			   FROM summonses_list_subsystem.summonses_direct_to_pers_acc;
		</sql>
	</changeSet>
	
	<changeSet id="0005" author="saliakhov">
        <comment>Удаление вью, удаление колонок</comment>
		<sql>
			DROP VIEW IF EXISTS deregistration.view_data_from_gir_vu;
			
			ALTER TABLE deregistration.data_from_gir_vu DROP COLUMN IF EXISTS count_formed_death_reason;
			ALTER TABLE deregistration.data_from_gir_vu DROP COLUMN IF EXISTS count_formed_age_limit;
			ALTER TABLE deregistration.data_from_gir_vu DROP COLUMN IF EXISTS count_for_sign_death_reason;
			ALTER TABLE deregistration.data_from_gir_vu DROP COLUMN IF EXISTS count_for_sign_age_limit;

			CREATE OR REPLACE VIEW deregistration.view_data_from_gir_vu
			AS SELECT data_from_gir_vu.data_from_gir_vu_id,
				data_from_gir_vu.count_arrived_death_reason + data_from_gir_vu.count_arrived_age_limit AS count_arrived_all,
				round(data_from_gir_vu.count_arrived_death_reason::numeric * 100::numeric / (data_from_gir_vu.count_arrived_death_reason + data_from_gir_vu.count_arrived_age_limit)::numeric) AS percent_arrived_death_reason,
				round(data_from_gir_vu.count_arrived_age_limit::numeric * 100::numeric / (data_from_gir_vu.count_arrived_death_reason + data_from_gir_vu.count_arrived_age_limit)::numeric) AS percent_arrived_age_limit
			   FROM deregistration.data_from_gir_vu;
		</sql>
	</changeSet>
	
	<changeSet id="0006" author="saliakhov">
        <comment>Удаление, добавление столбцов</comment>
		<sql>
			DROP VIEW IF EXISTS deregistration.view_data_from_gir_vu;
			
			ALTER TABLE deregistration.data_from_gir_vu DROP COLUMN IF EXISTS count_formed_death_reason;
			ALTER TABLE deregistration.data_from_gir_vu DROP COLUMN IF EXISTS count_formed_age_limit;
			ALTER TABLE deregistration.data_from_gir_vu DROP COLUMN IF EXISTS count_for_sign_death_reason;
			ALTER TABLE deregistration.data_from_gir_vu DROP COLUMN IF EXISTS count_for_sign_age_limit;
			
			ALTER TABLE deregistration.solutions DROP CONSTRAINT chk_info_source;

			ALTER TABLE deregistration.solutions ADD CONSTRAINT chk_info_source CHECK (info_source::text = ANY (ARRAY['PERSONAL_VISIT'::character varying::text,'GIR_VU'::character varying::text,'EPGU'::character varying::text,'MANUAL'::character varying::text]));
			
			COMMENT ON COLUMN deregistration.solutions.info_source IS 'информация об источнике - GIR_VU, EPGU, PERSONAL_VISIT, MANUAL';
			
			CREATE OR REPLACE VIEW deregistration.view_data_from_gir_vu
			AS SELECT data_from_gir_vu.data_from_gir_vu_id,
				data_from_gir_vu.count_arrived_death_reason + data_from_gir_vu.count_arrived_age_limit AS count_arrived_all,
				round(data_from_gir_vu.count_arrived_death_reason::numeric * 100::numeric / (data_from_gir_vu.count_arrived_death_reason + data_from_gir_vu.count_arrived_age_limit)::numeric) AS percent_arrived_death_reason,
				round(data_from_gir_vu.count_arrived_age_limit::numeric * 100::numeric / (data_from_gir_vu.count_arrived_death_reason + data_from_gir_vu.count_arrived_age_limit)::numeric) AS percent_arrived_age_limit
			   FROM deregistration.data_from_gir_vu;
		</sql>
	</changeSet>
	
	<changeSet id="0007" author="absatarov">
        <comment>Добавление колонки</comment>
		<sql>
			ALTER TABLE metrics.fz_53_appeals ADD COLUMN IF NOT EXISTS count_partially bigint NOT NULL DEFAULT 0;
		</sql>
	</changeSet>
	
	<changeSet id="0008" author="saliakhov">
        <comment>Добавление колонки</comment>
		<sql>
			DROP VIEW IF EXISTS metrics.view_fz_53_appeals;

			CREATE OR REPLACE VIEW metrics.view_fz_53_appeals
			AS SELECT fz_53_appeals.fz_53_appeals_id,
				fz_53_appeals.count_accepted + fz_53_appeals.count_refused + fz_53_appeals.count_partially AS count_all,
				round(fz_53_appeals.count_accepted::numeric * 100::numeric / (fz_53_appeals.count_accepted + fz_53_appeals.count_refused + fz_53_appeals.count_partially)::numeric) AS percent_accepted,
				round(fz_53_appeals.count_refused::numeric * 100::numeric / (fz_53_appeals.count_accepted + fz_53_appeals.count_refused + fz_53_appeals.count_partially)::numeric) AS percent_refused,
				round(fz_53_appeals.count_partially::numeric * 100::numeric / (fz_53_appeals.count_accepted + fz_53_appeals.count_refused + fz_53_appeals.count_partially)::numeric) AS percent_partially
			   FROM metrics.fz_53_appeals;
		</sql>
	</changeSet>
</databaseChangeLog>