<?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="adel.ka">
    <comment>add solution table</comment>
    <sql>
      CREATE TABLE IF NOT EXISTS public.solution (
      id varchar(36) PRIMARY KEY,
      name varchar(255) NOT NULL,
      description varchar(1024),
      display_name varchar(255),
      active boolean NOT NULL DEFAULT true,
      created TIMESTAMP WITHOUT TIME ZONE NOT NULL,
      modified TIMESTAMP WITHOUT TIME ZONE,
      deleted TIMESTAMP WITHOUT TIME ZONE,
      version int NOT NULL
      ) TABLESPACE pg_default;

      COMMENT ON COLUMN public.solution.id IS 'Идентификатор группы ролей';
      COMMENT ON COLUMN public.solution.name IS 'Имя группы ролей';
      COMMENT ON COLUMN public.solution.description IS 'Описание группы ролей';
      COMMENT ON COLUMN public.solution.display_name IS 'Отображаемое имя группы ролей';
      COMMENT ON COLUMN public.solution.active IS 'Флаг активности';
      COMMENT ON COLUMN public.solution.created IS 'Дата создания';
      COMMENT ON COLUMN public.solution.modified IS 'Дата последнего изменения';
      COMMENT ON COLUMN public.solution.deleted IS 'Дата удаления';
      COMMENT ON COLUMN public.solution.version IS 'Версия записи';

      ALTER TABLE public.solution OWNER TO ervu_account_applications;
      GRANT ALL ON TABLE public.solution TO ervu_account_applications;
    </sql>
  </changeSet>

  <changeSet id="0002" author="adel.ka">
    <comment>add recruitment_solution table</comment>
    <sql>
      CREATE TABLE IF NOT EXISTS public.recruitment_solution (
      recruitment_id VARCHAR(36),
      solution_id VARCHAR(36),
      CONSTRAINT pk_recruitment_solution PRIMARY KEY (recruitment_id, solution_id)
      ) TABLESPACE pg_default;

      COMMENT ON COLUMN public.recruitment_solution.recruitment_id IS 'Идентификатор организации';
      COMMENT ON COLUMN public.recruitment_solution.solution_id IS 'Идентификатор группы ролей';

      ALTER TABLE IF EXISTS public.recruitment_solution
      OWNER TO ervu_account_applications;

      GRANT ALL ON TABLE public.recruitment_solution TO ervu_account_applications;
    </sql>
  </changeSet>

  <changeSet id="0003" author="adel.ka">
    <comment>add solution_role table</comment>
    <sql>
      CREATE TABLE IF NOT EXISTS public.solution_role (
      role_id VARCHAR(36),
      solution_id VARCHAR(36),
      CONSTRAINT pk_solution_role PRIMARY KEY (role_id, solution_id)
      ) TABLESPACE pg_default;

      COMMENT ON COLUMN public.solution_role.role_id IS 'Идентификатор роли';
      COMMENT ON COLUMN public.solution_role.solution_id IS 'Идентификатор группы ролей';

      ALTER TABLE IF EXISTS public.solution_role
      OWNER TO ervu_account_applications;

      GRANT ALL ON TABLE public.solution_role TO ervu_account_applications;
    </sql>
  </changeSet>

  <changeSet id="0004" author="adel.ka">
    <comment>add recruitment_ip table</comment>
    <sql>
      CREATE TABLE IF NOT EXISTS public.recruitment_ip (
      ip_address VARCHAR(36) NOT NULL,
      recruitment_id VARCHAR(36) NOT NULL,
      CONSTRAINT pk_recruitment_ip PRIMARY KEY (recruitment_id, ip_address)
      ) TABLESPACE pg_default;

      COMMENT ON COLUMN public.recruitment_ip.ip_address IS 'IP-адрес';
      COMMENT ON COLUMN public.recruitment_ip.recruitment_id IS 'Идентификатор организации';

      ALTER TABLE IF EXISTS public.recruitment_ip
      OWNER TO ervu_account_applications;

      GRANT ALL ON TABLE public.recruitment_ip TO ervu_account_applications;
    </sql>
  </changeSet>

  <changeSet id="0005" author="adel.ka">
    <comment>add ip_directory table</comment>
    <sql>
      CREATE TABLE IF NOT EXISTS public.ip_directory (
      ip_address VARCHAR PRIMARY KEY,
      created TIMESTAMP DEFAULT now()
      ) TABLESPACE pg_default;

      COMMENT ON COLUMN public.ip_directory.ip_address IS 'Уникальный IP-адрес';
      COMMENT ON COLUMN public.ip_directory.created IS 'Дата добавления IP-адреса в справочник';

      ALTER TABLE IF EXISTS public.ip_directory
      OWNER TO ervu_account_applications;

      GRANT ALL ON TABLE public.ip_directory TO ervu_account_applications;
    </sql>
  </changeSet>

  <changeSet id="0006" author="adel.ka">
    <comment>drop columns from recruitment</comment>
    <sql>
      ALTER TABLE public.recruitment
      ADD COLUMN IF NOT EXISTS name VARCHAR(255) NOT NULL DEFAULT '',
      DROP COLUMN IF EXISTS hidden,
      DROP COLUMN IF EXISTS dns,
      DROP COLUMN IF EXISTS email,
      DROP COLUMN IF EXISTS phone,
      DROP COLUMN IF EXISTS address,
      DROP COLUMN IF EXISTS postal_address,
      DROP COLUMN IF EXISTS nsi_department_id,
      DROP COLUMN IF EXISTS nsi_organization_id,
      DROP COLUMN IF EXISTS oktmo,
      DROP COLUMN IF EXISTS org_ogrn,
      DROP COLUMN IF EXISTS dep_ogrn,
      DROP COLUMN IF EXISTS epgu_id,
      DROP COLUMN IF EXISTS kpp,
      DROP COLUMN IF EXISTS inn,
      DROP COLUMN IF EXISTS okato,
      DROP COLUMN IF EXISTS division_type,
      DROP COLUMN IF EXISTS tns_department_id,
      DROP COLUMN IF EXISTS region_id;
    </sql>
  </changeSet>
  <changeSet id="0007" author="adel.ka">
    <comment>alter table user_application_role</comment>
    <sql>
      ALTER TABLE public.user_application_role
      DROP COLUMN IF EXISTS finished;
      ALTER TABLE public.user_application_role
      ADD COLUMN IF NOT EXISTS deleted TIMESTAMP WITHOUT TIME ZONE;
      COMMENT ON COLUMN public.user_application_role.deleted IS 'Дата и время окончания действия роли';
      ALTER TABLE public.user_application_role
      ADD COLUMN IF NOT EXISTS active boolean NOT NULL DEFAULT true;
      ALTER TABLE public.user_application_role
      ADD COLUMN IF NOT EXISTS version int NOT NULL DEFAULT 0;
    </sql>
  </changeSet>

</databaseChangeLog>