<?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-3.10">

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

    <preConditions onFail="MARK_RAN">
      <not>
        <tableExists tableName="tc_servers" />
      </not>
    </preConditions>

    <createTable tableName="calendars">
      <column name="id" type="INT" autoIncrement="true">
        <constraints primaryKey="true" />
      </column>
      <column name="name" type="VARCHAR(128)">
        <constraints nullable="false" />
      </column>
      <column name="data" type="BLOB">
        <constraints nullable="false" />
      </column>
      <column name="attributes" type="VARCHAR(4000)">
        <constraints nullable="false" />
      </column>
    </createTable>

    <createTable tableName="user_calendar">
      <column name="userid" type="INT">
        <constraints nullable="false" />
      </column>
      <column name="calendarid" type="INT">
        <constraints nullable="false" />
      </column>
    </createTable>

    <addForeignKeyConstraint baseTableName="user_calendar" baseColumnNames="userid" constraintName="fk_user_calendar_userid" referencedTableName="users" referencedColumnNames="id" onDelete="CASCADE" />
    <addForeignKeyConstraint baseTableName="user_calendar" baseColumnNames="calendarid" constraintName="fk_user_calendar_geofenceid" referencedTableName="calendars" referencedColumnNames="id" onDelete="CASCADE" />

    <addColumn tableName="geofences">
      <column name="calendarid" type="INT" />
    </addColumn>

    <addForeignKeyConstraint baseColumnNames="calendarid" baseTableName="geofences" constraintName="fk_geofence_calendar_calendarid" onDelete="SET NULL" onUpdate="RESTRICT" referencedColumnNames="id" referencedTableName="calendars" />

    <addColumn tableName="positions">
      <column name="accuracy" type="DOUBLE" defaultValueNumeric="0">
        <constraints nullable="false" />
      </column>
      <column name="network" type="VARCHAR(4000)" />
    </addColumn>

    <addColumn tableName="users">
      <column name="userlimit" type="INT" defaultValueNumeric="0" />
    </addColumn>

    <createTable tableName="user_user">
      <column name="userid" type="INT">
        <constraints nullable="false" />
      </column>
      <column name="manageduserid" type="INT">
        <constraints nullable="false" />
      </column>
    </createTable>

    <addForeignKeyConstraint baseTableName="user_user" baseColumnNames="userid" constraintName="fk_user_user_userid" referencedTableName="users" referencedColumnNames="id" onDelete="CASCADE" />

    <update tableName="users">
      <column name="devicelimit" valueNumeric="-1" />
      <where>devicelimit = 0</where>
    </update>
    <dropDefaultValue tableName="users" columnName="devicelimit" />
    <addDefaultValue tableName="users" columnName="devicelimit" defaultValueNumeric="-1" />

    <addColumn tableName="users">
      <column name="devicereadonly" type="BOOLEAN" defaultValueBoolean="false" />
    </addColumn>

  </changeSet>

  <changeSet author="author" id="changelog-3.10-notmssql">

    <preConditions onFail="MARK_RAN">
      <not>
        <tableExists tableName="tc_servers" />
      </not>
      <not>
        <dbms type="mssql" />
      </not>
    </preConditions>

    <addForeignKeyConstraint baseTableName="user_user" baseColumnNames="manageduserid" constraintName="fk_user_user_manageduserid" referencedTableName="users" referencedColumnNames="id" onDelete="CASCADE" />

  </changeSet>

  <changeSet author="author" id="changelog-3.10-mssql">

    <preConditions onFail="MARK_RAN">
      <not>
        <tableExists tableName="tc_servers" />
      </not>
      <dbms type="mssql" />
    </preConditions>

    <sql>
      CREATE TRIGGER tg_users_delete
      ON users FOR DELETE
      AS BEGIN
        DELETE FROM user_user WHERE manageduserid IN (SELECT deleted.id FROM deleted)
      END
    </sql>

  </changeSet>

  <changeSet author="author" id="changelog-3.7-mssql">

    <preConditions onFail="MARK_RAN">
      <not>
        <tableExists tableName="tc_servers" />
      </not>
      <dbms type="mssql" />
    </preConditions>

    <sql>
      CREATE TRIGGER tg_groups_delete
      ON groups FOR DELETE
      AS BEGIN
        UPDATE groups SET groupid = NULL WHERE groupid IN (SELECT deleted.id FROM deleted)
      END
    </sql>

  </changeSet>

</databaseChangeLog>
