<?xml version="1.0" encoding="UTF-8"?>
<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-3.4.xsd"
  logicalFilePath="changelog-6.13.0">

  <changeSet author="author" id="changelog-6.13.0">

    <addColumn tableName="tc_devices">
      <column name="motionlatitude" type="DOUBLE" defaultValueNumeric="0" />
      <column name="motionlongitude" type="DOUBLE" defaultValueNumeric="0" />
    </addColumn>

    <createTable tableName="tc_device_device">
      <column name="deviceid" type="INT">
        <constraints nullable="false" />
      </column>
      <column name="linkeddeviceid" type="INT">
        <constraints nullable="false" />
      </column>
    </createTable>

    <addForeignKeyConstraint baseTableName="tc_device_device" baseColumnNames="deviceid" constraintName="fk_device_device_deviceid" onDelete="CASCADE" referencedColumnNames="id" referencedTableName="tc_devices" />

  </changeSet>

  <changeSet author="author" id="changelog-6.13.0-fk-linkeddeviceid" dbms="!mssql">
    <preConditions onFail="MARK_RAN">
      <not>
        <foreignKeyConstraintExists foreignKeyName="fk_device_device_linkeddeviceid" />
      </not>
    </preConditions>
    <addForeignKeyConstraint baseTableName="tc_device_device" baseColumnNames="linkeddeviceid" constraintName="fk_device_device_linkeddeviceid" onDelete="CASCADE" referencedColumnNames="id" referencedTableName="tc_devices" />
  </changeSet>

  <changeSet author="author" id="changelog-6.13.0-fk-linkeddeviceid-mssql" dbms="mssql">
    <preConditions onFail="MARK_RAN">
      <not>
        <foreignKeyConstraintExists foreignKeyName="fk_device_device_linkeddeviceid" />
      </not>
    </preConditions>
    <addForeignKeyConstraint baseTableName="tc_device_device" baseColumnNames="linkeddeviceid" constraintName="fk_device_device_linkeddeviceid" referencedColumnNames="id" referencedTableName="tc_devices" />
  </changeSet>

</databaseChangeLog>
