aboutsummaryrefslogtreecommitdiff
path: root/database/db.changelog-normalize.xml
blob: 7621846ca006db2c50e52a4156c59f047dbd50ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?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">

  <changeSet author="author" id="changelog-normalize-user">

    <preConditions onFail="MARK_RAN">
      <tableExists tableName="traccar" />
      <not>
        <columnExists tableName="user" columnName="id"/>
      </not>
    </preConditions>

    <renameTable oldTableName="quotedUser" newTableName="user" />

    <modifySql>
      <replace replace="quotedUser" with="&quot;user&quot;" />
    </modifySql>

  </changeSet>

  <changeSet author="author" id="changelog-normalize-user-device">

    <preConditions onFail="MARK_RAN">
      <tableExists tableName="traccar" />
      <not>
        <columnExists tableName="user_device" columnName="read"/>
      </not>
    </preConditions>

    <renameColumn tableName="user_device" oldColumnName="quotedRead" newColumnName="read" columnDataType="BOOLEAN" />
    <renameColumn tableName="user_device" oldColumnName="quotedWrite" newColumnName="write" columnDataType="BOOLEAN" />

    <modifySql>
      <replace replace="quotedRead" with="&quot;read&quot;" />
      <replace replace="quotedWrite" with="&quot;write&quot;" />
    </modifySql>

  </changeSet>

  <changeSet author="author" id="changelog-normalize-fk-h2">

    <preConditions onFail="MARK_RAN">
      <tableExists tableName="traccar" />
      <dbms type="h2" />
    </preConditions>

    <sql>CREATE ALIAS IF NOT EXISTS EXECUTE AS $$ void executeSql(Connection conn, String sql) throws SQLException { try { conn.createStatement().executeUpdate(sql); } catch (Exception e) {} } $$;</sql>

    <sql>CALL EXECUTE('ALTER TABLE POSITION DROP CONSTRAINT ' || (SELECT DISTINCT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.CONSTRAINTS WHERE CONSTRAINT_TYPE = 'REFERENTIAL' AND TABLE_NAME = 'POSITION' AND COLUMN_LIST = 'DEVICEID'));</sql>
    <sql>CALL EXECUTE('ALTER TABLE DEVICE DROP CONSTRAINT ' || (SELECT DISTINCT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.CONSTRAINTS WHERE CONSTRAINT_TYPE = 'REFERENTIAL' AND TABLE_NAME = 'DEVICE' AND COLUMN_LIST = 'POSITIONID'));</sql>
    <sql>CALL EXECUTE('ALTER TABLE DEVICE DROP CONSTRAINT ' || (SELECT DISTINCT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.CONSTRAINTS WHERE CONSTRAINT_TYPE = 'REFERENTIAL' AND TABLE_NAME = 'DEVICE' AND COLUMN_LIST = 'DATAID'));</sql>
    <sql>CALL EXECUTE('ALTER TABLE USER_DEVICE DROP CONSTRAINT ' || (SELECT DISTINCT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.CONSTRAINTS WHERE CONSTRAINT_TYPE = 'REFERENTIAL' AND TABLE_NAME = 'USER_DEVICE' AND COLUMN_LIST = 'USERID'));</sql>
    <sql>CALL EXECUTE('ALTER TABLE USER_DEVICE DROP CONSTRAINT ' || (SELECT DISTINCT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.CONSTRAINTS WHERE CONSTRAINT_TYPE = 'REFERENTIAL' AND TABLE_NAME = 'USER_DEVICE' AND COLUMN_LIST = 'DEVICEID'));</sql>
    <sql>CALL EXECUTE('ALTER TABLE DATA DROP CONSTRAINT ' || (SELECT DISTINCT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.CONSTRAINTS WHERE CONSTRAINT_TYPE = 'REFERENTIAL' AND TABLE_NAME = 'DATA' AND COLUMN_LIST = 'DEVICEID'));</sql>

  </changeSet>

  <changeSet author="author" id="changelog-normalize-fk-other">

    <preConditions onFail="MARK_RAN">
      <tableExists tableName="traccar" />
      <not>
        <dbms type="h2" />
      </not>
    </preConditions>

    <dropAllForeignKeyConstraints baseTableName="position" />
    <dropAllForeignKeyConstraints baseTableName="device" />
    <dropAllForeignKeyConstraints baseTableName="user_device" />

  </changeSet>

  <changeSet author="author" id="changelog-normalize-fk">

    <preConditions onFail="MARK_RAN">
      <tableExists tableName="traccar" />
    </preConditions>

    <addForeignKeyConstraint baseTableName="user_device" baseColumnNames="userId" constraintName="fk_user_device_userId" referencedTableName="user" referencedColumnNames="id" onDelete="CASCADE" />
    <addForeignKeyConstraint baseTableName="user_device" baseColumnNames="deviceId" constraintName="fk_user_device_deviceId" referencedTableName="device" referencedColumnNames="id" onDelete="CASCADE" />

    <addForeignKeyConstraint baseTableName="position" baseColumnNames="deviceId" constraintName="fk_position_deviceId" referencedTableName="device" referencedColumnNames="id" onDelete="CASCADE" />

  </changeSet>

  <changeSet author="author" id="changelog-normalize-fk-data-other">

    <preConditions onFail="MARK_RAN">
      <tableExists tableName="traccar" />
      <tableExists tableName="data" />
      <not>
        <dbms type="h2" />
      </not>
    </preConditions>

    <dropAllForeignKeyConstraints baseTableName="data" />

  </changeSet>

  <changeSet author="author" id="changelog-normalize-fk-data">

    <preConditions onFail="MARK_RAN">
      <tableExists tableName="traccar" />
      <tableExists tableName="data" />
    </preConditions>

    <addForeignKeyConstraint baseTableName="data" baseColumnNames="deviceId" constraintName="fk_data_deviceId" referencedTableName="device" referencedColumnNames="id" onDelete="CASCADE" />

  </changeSet>
</databaseChangeLog>