<?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>edit deregistration.solutions</comment>
		<sql>
			DROP VIEW IF EXISTS deregistration.view_solutions;
      
			ALTER TABLE deregistration.solutions DROP COLUMN IF EXISTS count_refused_signed;
			ALTER TABLE deregistration.solutions DROP COLUMN IF EXISTS count_refused_for_sign;
			ALTER TABLE deregistration.solutions DROP COLUMN IF EXISTS count_accepted_canceled;
			ALTER TABLE deregistration.solutions DROP COLUMN IF EXISTS count_refused_canceled;

			ALTER TABLE deregistration.solutions ADD COLUMN IF NOT EXISTS count_refused int8 DEFAULT 0 NOT NULL;

			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_solutions
			AS SELECT solutions.solutions_id,
				solutions.count_accepted_signed + solutions.count_accepted_for_sign + solutions.count_refused AS count_accepted_all,
				round(solutions.count_accepted_signed::numeric * 100::numeric / (solutions.count_accepted_signed + solutions.count_accepted_for_sign + solutions.count_refused)::numeric) AS percent_accepted_for_sign,
				round(solutions.count_accepted_for_sign::numeric * 100::numeric / (solutions.count_accepted_signed + solutions.count_accepted_for_sign + solutions.count_refused)::numeric) AS percent_accepted_signed,
				round(solutions.count_refused::numeric * 100::numeric / (solutions.count_accepted_signed + solutions.count_accepted_for_sign + solutions.count_refused)::numeric) AS percent_refused
			   FROM deregistration.solutions;
		</sql>
	</changeSet>
</databaseChangeLog>