aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2015-12-19 09:05:07 +1300
committerAnton Tananaev <anton.tananaev@gmail.com>2015-12-19 09:05:07 +1300
commit74c1e947ba979ebce33efffa5b6389354282881a (patch)
tree71c9570d52f500ac693f3745f4e2c84313758454
parent1f8ce82459bce2bd7bfe0726c3ec31010b58ab33 (diff)
downloadtraccar-server-74c1e947ba979ebce33efffa5b6389354282881a.tar.gz
traccar-server-74c1e947ba979ebce33efffa5b6389354282881a.tar.bz2
traccar-server-74c1e947ba979ebce33efffa5b6389354282881a.zip
Normalize database foreign key names
-rw-r--r--database/db.changelog-3.2.xml16
-rw-r--r--database/db.changelog-3.3.xml18
-rw-r--r--database/db.changelog-master.xml1
-rw-r--r--database/db.changelog-normalize.xml112
4 files changed, 113 insertions, 34 deletions
diff --git a/database/db.changelog-3.2.xml b/database/db.changelog-3.2.xml
index 8c7a0cc8a..bcf20c860 100644
--- a/database/db.changelog-3.2.xml
+++ b/database/db.changelog-3.2.xml
@@ -5,22 +5,6 @@
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-3.2-normalize">
-
- <preConditions onFail="MARK_RAN">
- <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-3.2">
<preConditions onFail="MARK_RAN">
diff --git a/database/db.changelog-3.3.xml b/database/db.changelog-3.3.xml
index 4c5c216db..cb9823ef8 100644
--- a/database/db.changelog-3.3.xml
+++ b/database/db.changelog-3.3.xml
@@ -5,24 +5,6 @@
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-3.3-normalize">
-
- <preConditions onFail="MARK_RAN">
- <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-3.3">
<dropDefaultValue tableName="user" columnName="salt" />
diff --git a/database/db.changelog-master.xml b/database/db.changelog-master.xml
index c0dd947b1..06fc7a695 100644
--- a/database/db.changelog-master.xml
+++ b/database/db.changelog-master.xml
@@ -5,6 +5,7 @@
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.4.xsd">
+ <include file="db.changelog-normalize.xml" relativeToChangelogFile="true" />
<include file="db.changelog-3.0.xml" relativeToChangelogFile="true" />
<include file="db.changelog-3.1.xml" relativeToChangelogFile="true" />
<include file="db.changelog-3.2.xml" relativeToChangelogFile="true" />
diff --git a/database/db.changelog-normalize.xml b/database/db.changelog-normalize.xml
new file mode 100644
index 000000000..21a2c45d7
--- /dev/null
+++ b/database/db.changelog-normalize.xml
@@ -0,0 +1,112 @@
+<?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">
+ <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">
+ <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 UNIQUE_INDEX_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 UNIQUE_INDEX_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 UNIQUE_INDEX_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 UNIQUE_INDEX_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 UNIQUE_INDEX_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 UNIQUE_INDEX_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>