<databaseChangeLog
    xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
                      http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.9.xsd">

  <changeSet id="0001" author="AlexSave">
    <preConditions onFail="MARK_RAN">
      <not>
        <tableExists tableName="deferment_reason" schemaName="ervu_dashboard"/>
      </not>
    </preConditions>
    <createTable tableName="deferment_reason" schemaName="ervu_dashboard" remarks="Справчоник причин для отсрочки/освобождения от службы">
      <column name="deferment_reason_id" type="BIGSERIAL">
        <constraints primaryKey="true" nullable="false"/>
      </column>
      <column name="code" type="varchar(15)" remarks="Cправочный код причины">
        <constraints nullable="false"/>
      </column>
      <column name="value" type="varchar(255)" remarks="Причина отсрочки/освобождения">
        <constraints nullable="false"/>
      </column>
      <column name="actual" type="boolean" defaultValueBoolean="true" remarks="Признак актуальности справочного значения">
        <constraints nullable="false"/>
      </column>
    </createTable>
    <addUniqueConstraint tableName="deferment_reason"
                         columnNames="code"
                         constraintName="deferment_reason_code_unique_key"
                         schemaName="ervu_dashboard"/>
    <rollback>
      <dropTable tableName="deferment_reason" schemaName="ervu_dashboard"/>
    </rollback>
  </changeSet>

  <changeSet id="0002" author="AlexSave">
    <createTable tableName="citizen_deferment_right" schemaName="ervu_dashboard">
      <column name="citizen_deferment_right_id" type="BIGSERIAL">
        <constraints primaryKey="true" nullable="false"/>
      </column>
      <column name="recruit_id" type="uuid">
        <constraints nullable="false"/>
      </column>
      <column name="start_date" type="timestamp" remarks="Дата начала действия права на отсрочку">
        <constraints nullable="false"/>
      </column>
      <column name="end_date" type="timestamp" remarks="Дата окончания действия права на отсрочку"/>
      <column name="code" type="varchar(255)" remarks="Справочный код причины отсрочки">
        <constraints nullable="false"
                     referencedTableName="deferment_reason"
                     referencedColumnNames="code"
                     foreignKeyName="citizen_deferment_right_deferment_reason_code_fk"/>
      </column>
      <column name="actual" type="boolean" defaultValueBoolean="false" remarks="Признак актуальности права на отсрочку">
        <constraints nullable="false"/>
      </column>
    </createTable>
    <rollback>
      <dropTable tableName="citizen_deferment_right" schemaName="ervu_dashboard"/>
    </rollback>
  </changeSet>

  <changeSet id="0003" author="AlexSave">
    <createTable tableName="citizen_deferment" schemaName="ervu_dashboard" remarks="">
      <column name="citizen_deferment_id" type="BIGSERIAL">
        <constraints primaryKey="true" nullable="false"/>
      </column>
      <column name="recruit_id" type="uuid">
        <constraints nullable="false"/>
      </column>
      <column name="decision_number" type="varchar" remarks="Номер решения"/>
      <column name="type" type="text">
        <constraints nullable="false"/>
      </column>
      <column name="start_date" type="timestamp" remarks="Дата начала действия отсрочки/освобождения">
        <constraints nullable="false"/>
      </column>
      <column name="end_date" type="timestamp" remarks="Дата окончания действия отсрочки/освобождения"/>
      <column name="reason_code" type="varchar" remarks="Справочный код причины отсрочки/освобождения">
        <constraints nullable="false"
                     referencedTableName="deferment_reason"
                     referencedColumnNames="code"
                     foreignKeyName="citizen_deferment_deferment_reason_code_fk"/>
      </column>
      <column name="active" type="boolean" defaultValueBoolean="false" remarks="Признак активности отсрочки/освобождения">
        <constraints nullable="false"/>
      </column>
    </createTable>

    <rollback>
      <dropTable tableName="citizen_deferment" schemaName="ervu_dashboard"/>
    </rollback>
  </changeSet>

  <changeSet id="0004" author="AlexSave">
    <addColumn tableName="citizen" schemaName="ervu_dashboard">
      <column name="has_deferment" type="boolean" defaultValueBoolean="false" remarks="Признак наличия активной отсрочки">
        <constraints nullable="false"/>
      </column>
      <column name="has_deferment_right" type="boolean" defaultValueBoolean="false" remarks="Признак наличия прав на отсрочку">
        <constraints nullable="false"/>
      </column>
      <column name="has_exemption" type="boolean" defaultValueBoolean="false" remarks="Признак наличия активного освобождения от службы">
        <constraints nullable="false"/>
      </column>
    </addColumn>
    <rollback>
      <dropColumn tableName="citizen" columnName="has_deferment" schemaName="ervu_dashboard"/>
      <dropColumn tableName="citizen" columnName="has_deferment_right" schemaName="ervu_dashboard"/>
      <dropColumn tableName="citizen" columnName="has_exemption" schemaName="ervu_dashboard"/>
    </rollback>
  </changeSet>

</databaseChangeLog>
