<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="Alex Save">
    <preConditions onFail="MARK_RAN">
      <not>
        <columnExists schemaName="ervu_dashboard" tableName="citizen" columnName="educated"/>
      </not>
      <not>
        <columnExists schemaName="ervu_dashboard" tableName="citizen" columnName="has_education_diploma"/>
      </not>
      <not>
        <columnExists schemaName="ervu_dashboard" tableName="citizen" columnName="has_foreign_education_diploma"/>
      </not>
      <not>
        <columnExists schemaName="ervu_dashboard" tableName="citizen" columnName="has_military_department_education"/>
      </not>
      <not>
        <columnExists schemaName="ervu_dashboard" tableName="citizen" columnName="has_military_training_center_education"/>
      </not>
      <not>
        <columnExists schemaName="ervu_dashboard" tableName="citizen" columnName="has_actual_academic_leave"/>
      </not>
      <not>
        <columnExists schemaName="ervu_dashboard" tableName="citizen_education" columnName="military_department_education_start_date"/>
      </not>
      <not>
        <columnExists schemaName="ervu_dashboard" tableName="citizen_education" columnName="military_department_education_end_date"/>
      </not>
    </preConditions>

    <!-- Citizen table columns -->
    <addColumn schemaName="ervu_dashboard" tableName="citizen">
      <column name="educated" type="boolean" defaultValueBoolean="false">
        <constraints nullable="false"/>
      </column>
    </addColumn>

    <addColumn schemaName="ervu_dashboard" tableName="citizen">
      <column name="has_education_diploma" type="boolean" defaultValueBoolean="false">
        <constraints nullable="false"/>
      </column>
    </addColumn>

    <addColumn schemaName="ervu_dashboard" tableName="citizen">
      <column name="has_foreign_education_diploma" type="boolean" defaultValueBoolean="false">
        <constraints nullable="false"/>
      </column>
    </addColumn>

    <addColumn schemaName="ervu_dashboard" tableName="citizen">
      <column name="has_military_department_education" type="boolean" defaultValueBoolean="false">
        <constraints nullable="false"/>
      </column>
    </addColumn>

    <addColumn schemaName="ervu_dashboard" tableName="citizen">
      <column name="has_military_training_center_education" type="boolean" defaultValueBoolean="false">
        <constraints nullable="false"/>
      </column>
    </addColumn>

    <addColumn schemaName="ervu_dashboard" tableName="citizen">
      <column name="has_actual_academic_leave" type="boolean" defaultValueBoolean="false">
        <constraints nullable="false"/>
      </column>
    </addColumn>

    <!-- Citizen_education table columns -->
    <addColumn schemaName="ervu_dashboard" tableName="citizen_education">
      <column name="military_department_education_start_date" type="date"/>
    </addColumn>

    <addColumn schemaName="ervu_dashboard" tableName="citizen_education">
      <column name="military_department_education_end_date" type="date"/>
    </addColumn>

    <rollback>
      <dropColumn schemaName="ervu_dashboard" tableName="citizen" columnName="educated"/>
      <dropColumn schemaName="ervu_dashboard" tableName="citizen" columnName="has_education_diploma"/>
      <dropColumn schemaName="ervu_dashboard" tableName="citizen" columnName="has_foreign_education_diploma"/>
      <dropColumn schemaName="ervu_dashboard" tableName="citizen" columnName="has_military_department_education"/>
      <dropColumn schemaName="ervu_dashboard" tableName="citizen" columnName="has_military_training_center_education"/>
      <dropColumn schemaName="ervu_dashboard" tableName="citizen" columnName="has_actual_academic_leave"/>
      <dropColumn schemaName="ervu_dashboard" tableName="citizen_education" columnName="military_department_education_start_date"/>
      <dropColumn schemaName="ervu_dashboard" tableName="citizen_education" columnName="military_department_education_end_date"/>
    </rollback>
  </changeSet>

  <changeSet id="0002" author="Alex Save">
    <preConditions onFail="MARK_RAN">
      <foreignKeyConstraintExists
          schemaName="ervu_dashboard"
          foreignKeyName="citizen_education_education_leave_reason_code_fk"/>
      <foreignKeyConstraintExists
          schemaName="ervu_dashboard"
          foreignKeyName="citizen_education_education_level_code_fk"/>
      <foreignKeyConstraintExists
          schemaName="ervu_dashboard"
          foreignKeyName="citizen_education_education_form_code_fk"/>
      <foreignKeyConstraintExists
          schemaName="ervu_dashboard"
          foreignKeyName="citizen_education_education_institution_type_code_fk"/>
      <foreignKeyConstraintExists
          schemaName="ervu_dashboard"
          foreignKeyName="citizen_education_education_status_type_code_fk"/>
      <foreignKeyConstraintExists
          schemaName="ervu_dashboard"
          foreignKeyName="citizen_education_education_minobr_speciality_code_fk"/>
      <foreignKeyConstraintExists
          schemaName="ervu_dashboard"
          foreignKeyName="citizen_education_education_minprosv_speciality_code_fk"/>
    </preConditions>

    <dropForeignKeyConstraint
        baseTableSchemaName="ervu_dashboard"
        baseTableName="citizen_education"
        constraintName="citizen_education_education_leave_reason_code_fk"/>

    <dropForeignKeyConstraint
        baseTableSchemaName="ervu_dashboard"
        baseTableName="citizen_education"
        constraintName="citizen_education_education_level_code_fk"/>

    <dropForeignKeyConstraint
        baseTableSchemaName="ervu_dashboard"
        baseTableName="citizen_education"
        constraintName="citizen_education_education_form_code_fk"/>

    <dropForeignKeyConstraint
        baseTableSchemaName="ervu_dashboard"
        baseTableName="citizen_education"
        constraintName="citizen_education_education_institution_type_code_fk"/>

    <dropForeignKeyConstraint
        baseTableSchemaName="ervu_dashboard"
        baseTableName="citizen_education"
        constraintName="citizen_education_education_status_type_code_fk"/>

    <dropForeignKeyConstraint
        baseTableSchemaName="ervu_dashboard"
        baseTableName="citizen_education"
        constraintName="citizen_education_education_minobr_speciality_code_fk"/>

    <dropForeignKeyConstraint
        baseTableSchemaName="ervu_dashboard"
        baseTableName="citizen_education"
        constraintName="citizen_education_education_minprosv_speciality_code_fk"/>

    <rollback>
      <addForeignKeyConstraint
          baseTableSchemaName="ervu_dashboard"
          baseTableName="citizen_education"
          baseColumnNames="education_leave_reason_code"
          constraintName="citizen_education_education_leave_reason_code_fk"
          referencedTableName="education_leave_reason"
          referencedColumnNames="code"/>

      <addForeignKeyConstraint
          baseTableSchemaName="ervu_dashboard"
          baseTableName="citizen_education"
          baseColumnNames="education_level_code"
          constraintName="citizen_education_education_level_code_fk"
          referencedTableName="education_level"
          referencedColumnNames="code"/>

      <addForeignKeyConstraint
          baseTableSchemaName="ervu_dashboard"
          baseTableName="citizen_education"
          baseColumnNames="education_form_code"
          constraintName="citizen_education_education_form_code_fk"
          referencedTableName="education_form"
          referencedColumnNames="code"/>

      <addForeignKeyConstraint
          baseTableSchemaName="ervu_dashboard"
          baseTableName="citizen_education"
          baseColumnNames="education_institution_type_code"
          constraintName="citizen_education_education_institution_type_code_fk"
          referencedTableName="education_institution_type"
          referencedColumnNames="code"/>

      <addForeignKeyConstraint
          baseTableSchemaName="ervu_dashboard"
          baseTableName="citizen_education"
          baseColumnNames="education_status_type_code"
          constraintName="citizen_education_education_status_type_code_fk"
          referencedTableName="education_status_type"
          referencedColumnNames="code"/>

      <addForeignKeyConstraint
          baseTableSchemaName="ervu_dashboard"
          baseTableName="citizen_education"
          baseColumnNames="education_minobr_speciality_code"
          constraintName="citizen_education_education_minobr_speciality_code_fk"
          referencedTableName="education_minobr_speciality"
          referencedColumnNames="code"/>

      <addForeignKeyConstraint
          baseTableSchemaName="ervu_dashboard"
          baseTableName="citizen_education"
          baseColumnNames="education_minprosv_speciality_code"
          constraintName="citizen_education_education_minprosv_speciality_code_fk"
          referencedTableName="education_minprosv_speciality"
          referencedColumnNames="code"/>
    </rollback>
  </changeSet>
</databaseChangeLog>