aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2015-12-18 16:32:46 +1300
committerAnton Tananaev <anton.tananaev@gmail.com>2015-12-18 16:32:46 +1300
commit5d974ea790397d0646f013e91c8d3b0829ffbd54 (patch)
tree7c4100ddd89d0bf0a4e20b9c165e6e66bed2bc09
parent2c1aee51f375885b3ee575425814ca8cbe6129ff (diff)
downloadtrackermap-server-5d974ea790397d0646f013e91c8d3b0829ffbd54.tar.gz
trackermap-server-5d974ea790397d0646f013e91c8d3b0829ffbd54.tar.bz2
trackermap-server-5d974ea790397d0646f013e91c8d3b0829ffbd54.zip
Finish Liquibase migration implementation
-rw-r--r--database/db.changelog-3.0.xml25
-rw-r--r--database/db.changelog-3.1.xml102
-rw-r--r--database/db.changelog-3.2.xml50
-rw-r--r--database/db.changelog-3.3.xml30
-rw-r--r--database/db.changelog-master.xml7
-rw-r--r--debug.xml122
-rw-r--r--src/org/traccar/database/DataManager.java64
7 files changed, 211 insertions, 189 deletions
diff --git a/database/db.changelog-3.0.xml b/database/db.changelog-3.0.xml
index bbc52dfeb..4f94494b4 100644
--- a/database/db.changelog-3.0.xml
+++ b/database/db.changelog-3.0.xml
@@ -1,9 +1,9 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<databaseChangeLog
- xmlns="http://www.liquibase.org/xml/ns/dbchangelog/1.9"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog/1.9
- http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-1.9.xsd">
+<?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-3.0">
@@ -44,6 +44,9 @@
<column name="distanceUnit" type="VARCHAR(1024)" defaultValue="km">
<constraints nullable="false" />
</column>
+ <column name="speedUnit" type="VARCHAR(1024)" defaultValue="kmh">
+ <constraints nullable="false" />
+ </column>
<column name="latitude" type="DOUBLE" defaultValueNumeric="0">
<constraints nullable="false" />
</column>
@@ -156,9 +159,6 @@
<addForeignKeyConstraint baseTableName="data" baseColumnNames="deviceId" constraintName="fk_data_deviceId" referencedTableName="device" referencedColumnNames="id" onDelete="CASCADE" />
- <addForeignKeyConstraint baseTableName="device" baseColumnNames="positionId" constraintName="fk_device_positionId" referencedTableName="position" referencedColumnNames="id" />
- <addForeignKeyConstraint baseTableName="device" baseColumnNames="dataId" constraintName="fk_device_dataId" referencedTableName="data" referencedColumnNames="id" />
-
<createTable tableName="server">
<column name="id" type="INT" autoIncrement="true">
<constraints primaryKey="true" />
@@ -183,5 +183,12 @@
</column>
</createTable>
+ <insert tableName="server">
+ <column name="registration" valueBoolean="true" />
+ <column name="latitude" valueNumeric="0" />
+ <column name="longitude" valueNumeric="0" />
+ <column name="zoom" valueNumeric="0" />
+ </insert>
+
</changeSet>
</databaseChangeLog>
diff --git a/database/db.changelog-3.1.xml b/database/db.changelog-3.1.xml
index b8c706806..589356863 100644
--- a/database/db.changelog-3.1.xml
+++ b/database/db.changelog-3.1.xml
@@ -1,17 +1,97 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<databaseChangeLog
- xmlns="http://www.liquibase.org/xml/ns/dbchangelog/1.9"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog/1.9
- http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-1.9.xsd">
+<?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-3.1">
- <createTable tableName="TablesAndTables">
- <column name="COLUMN1" type="TEXT">
- <constraints nullable="true" primaryKey="false" unique="false"/>
- </column>
- </createTable>
+ <preConditions onFail="MARK_RAN">
+ <not>
+ <columnExists tableName="traccar" columnName="version" />
+ </not>
+ </preConditions>
+
+ <modifyDataType tableName="user" columnName="name" newDataType="VARCHAR(128)" />
+ <modifyDataType tableName="user" columnName="email" newDataType="VARCHAR(128)" />
+ <modifyDataType tableName="user" columnName="hashedPassword" newDataType="VARCHAR(128)" />
+ <dropDefaultValue tableName="user" columnName="salt" />
+ <modifyDataType tableName="user" columnName="salt" newDataType="VARCHAR(128)" />
+ <addDefaultValue tableName="user" columnName="salt" defaultValue="" />
+ <dropDefaultValue tableName="user" columnName="map" />
+ <dropNotNullConstraint tableName="user" columnName="map" columnDataType="VARCHAR(128)" />
+ <modifyDataType tableName="user" columnName="map" newDataType="VARCHAR(128)" />
+ <dropDefaultValue tableName="user" columnName="language" />
+ <dropNotNullConstraint tableName="user" columnName="language" columnDataType="VARCHAR(128)" />
+ <modifyDataType tableName="user" columnName="language" newDataType="VARCHAR(128)" />
+ <dropDefaultValue tableName="user" columnName="distanceUnit" />
+ <dropNotNullConstraint tableName="user" columnName="distanceUnit" columnDataType="VARCHAR(128)" />
+ <modifyDataType tableName="user" columnName="distanceUnit" newDataType="VARCHAR(128)" />
+ <dropDefaultValue tableName="user" columnName="speedUnit" />
+ <dropNotNullConstraint tableName="user" columnName="speedUnit" columnDataType="VARCHAR(128)" />
+ <modifyDataType tableName="user" columnName="speedUnit" newDataType="VARCHAR(128)" />
+ <dropDefaultValue tableName="user" columnName="latitude" />
+ <modifyDataType tableName="user" columnName="latitude" newDataType="FLOAT" />
+ <addDefaultValue tableName="user" columnName="latitude" defaultValueNumeric="0" />
+ <dropDefaultValue tableName="user" columnName="longitude" />
+ <modifyDataType tableName="user" columnName="longitude" newDataType="FLOAT" />
+ <addDefaultValue tableName="user" columnName="longitude" defaultValueNumeric="0" />
+
+ <modifyDataType tableName="device" columnName="name" newDataType="VARCHAR(128)" />
+ <modifyDataType tableName="device" columnName="uniqueId" newDataType="VARCHAR(128)" />
+ <modifyDataType tableName="device" columnName="status" newDataType="VARCHAR(128)" />
+
+ <dropIndex tableName="user_device" indexName="user_device_user_id" />
+ <createIndex tableName="user_device" indexName="user_device_userId">
+ <column name="userId" />
+ </createIndex>
+
+ <modifyDataType tableName="position" columnName="protocol" newDataType="VARCHAR(128)" />
+ <modifyDataType tableName="position" columnName="latitude" newDataType="FLOAT" />
+ <modifyDataType tableName="position" columnName="longitude" newDataType="FLOAT" />
+ <modifyDataType tableName="position" columnName="altitude" newDataType="FLOAT" />
+ <modifyDataType tableName="position" columnName="speed" newDataType="FLOAT" />
+ <modifyDataType tableName="position" columnName="course" newDataType="FLOAT" />
+ <modifyDataType tableName="position" columnName="address" newDataType="VARCHAR(512)" />
+ <modifyDataType tableName="position" columnName="other" newDataType="VARCHAR(4096)" />
+
+ <createIndex tableName="position" indexName="position_deviceId_fixTime">
+ <column name="deviceId" />
+ <column name="fixTime" />
+ </createIndex>
+
+ <modifyDataType tableName="data" columnName="protocol" newDataType="VARCHAR(128)" />
+ <modifyDataType tableName="data" columnName="other" newDataType="VARCHAR(4096)" />
+
+ <addColumn tableName="server">
+ <column name="map" type="VARCHAR(128)" />
+ </addColumn>
+ <addColumn tableName="server">
+ <column name="language" type="VARCHAR(128)" />
+ </addColumn>
+ <addColumn tableName="server">
+ <column name="distanceUnit" type="VARCHAR(128)" />
+ </addColumn>
+ <addColumn tableName="server">
+ <column name="speedUnit" type="VARCHAR(128)" />
+ </addColumn>
+ <modifyDataType tableName="server" columnName="latitude" newDataType="FLOAT" />
+ <addDefaultValue tableName="server" columnName="latitude" defaultValueNumeric="0" />
+ <modifyDataType tableName="server" columnName="longitude" newDataType="FLOAT" />
+ <addDefaultValue tableName="server" columnName="longitude" defaultValueNumeric="0" />
+
+ <dropPrimaryKey tableName="traccar" />
+ <addColumn tableName="traccar">
+ <column name="version" type="INT" defaultValueNumeric="0">
+ <constraints nullable="false" />
+ </column>
+ </addColumn>
+ <dropColumn tableName="traccar" columnName="id" />
+
+ <insert tableName="traccar">
+ <column name="version" valueNumeric="301" />
+ </insert>
</changeSet>
</databaseChangeLog>
diff --git a/database/db.changelog-3.2.xml b/database/db.changelog-3.2.xml
index a072c0703..576713dc7 100644
--- a/database/db.changelog-3.2.xml
+++ b/database/db.changelog-3.2.xml
@@ -1,17 +1,45 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<databaseChangeLog
- xmlns="http://www.liquibase.org/xml/ns/dbchangelog/1.9"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog/1.9
- http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-1.9.xsd">
+<?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-3.2">
- <createTable tableName="TablesAndTables">
- <column name="COLUMN1" type="TEXT">
- <constraints nullable="true" primaryKey="false" unique="false"/>
- </column>
- </createTable>
+ <preConditions onFail="MARK_RAN">
+ <not>
+ <columnExists tableName="position" columnName="attributes" />
+ </not>
+ </preConditions>
+
+ <dropDefaultValue tableName="user" columnName="latitude" />
+ <modifyDataType tableName="user" columnName="latitude" newDataType="DOUBLE" />
+ <addDefaultValue tableName="user" columnName="latitude" defaultValueNumeric="0" />
+ <dropDefaultValue tableName="user" columnName="longitude" />
+ <modifyDataType tableName="user" columnName="longitude" newDataType="DOUBLE" />
+ <addDefaultValue tableName="user" columnName="longitude" defaultValueNumeric="0" />
+
+ <dropColumn tableName="device" columnName="dataId" />
+
+ <modifyDataType tableName="position" columnName="latitude" newDataType="DOUBLE" />
+ <modifyDataType tableName="position" columnName="longitude" newDataType="DOUBLE" />
+ <renameColumn tableName="position" oldColumnName="other" newColumnName="attributes" />
+
+ <dropTable tableName="data" />
+
+ <addColumn tableName="server">
+ <column name="bingKey" type="VARCHAR(128)" />
+ </addColumn>
+ <addColumn tableName="server">
+ <column name="mapUrl" type="VARCHAR(128)" />
+ </addColumn>
+ <modifyDataType tableName="server" columnName="latitude" newDataType="DOUBLE" />
+ <modifyDataType tableName="server" columnName="longitude" newDataType="DOUBLE" />
+
+ <update tableName="traccar">
+ <column name="version" valueNumeric="302" />
+ </update>
</changeSet>
</databaseChangeLog>
diff --git a/database/db.changelog-3.3.xml b/database/db.changelog-3.3.xml
new file mode 100644
index 000000000..cb9823ef8
--- /dev/null
+++ b/database/db.changelog-3.3.xml
@@ -0,0 +1,30 @@
+<?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-3.3">
+
+ <dropDefaultValue tableName="user" columnName="salt" />
+
+ <addDefaultValue tableName="server" columnName="registration" defaultValueBoolean="true" />
+
+ <addColumn tableName="server">
+ <column name="readonly" type="BOOLEAN" defaultValueBoolean="false">
+ <constraints nullable="false" />
+ </column>
+ </addColumn>
+
+ <dropTable tableName="traccar" />
+
+ <renameTable oldTableName="user" newTableName="users" />
+ <renameTable oldTableName="device" newTableName="devices" />
+ <renameTable oldTableName="position" newTableName="positions" />
+
+ <dropColumn tableName="user_device" columnName="read" />
+ <dropColumn tableName="user_device" columnName="write" />
+
+ </changeSet>
+</databaseChangeLog>
diff --git a/database/db.changelog-master.xml b/database/db.changelog-master.xml
index 151cb5d19..c0dd947b1 100644
--- a/database/db.changelog-master.xml
+++ b/database/db.changelog-master.xml
@@ -3,9 +3,10 @@
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.1.xsd">
+ http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.4.xsd">
<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" />-->
+ <include file="db.changelog-3.1.xml" relativeToChangelogFile="true" />
+ <include file="db.changelog-3.2.xml" relativeToChangelogFile="true" />
+ <include file="db.changelog-3.3.xml" relativeToChangelogFile="true" />
</databaseChangeLog>
diff --git a/debug.xml b/debug.xml
index 019c20d10..53126404a 100644
--- a/debug.xml
+++ b/debug.xml
@@ -49,98 +49,12 @@
<entry key='database.password'></entry>
<entry key='database.mock'>true</entry>
<entry key='database.xml'>false</entry>
-
<entry key='database.changelog'>./database/db.changelog-master.xml</entry>
- <entry key='database.checkTable'>traccar</entry>
-
- <entry key='database.selectSchemaVersion'>
- SELECT * FROM traccar;
- </entry>
-
- <entry key='database.createSchema'>
- CREATE TABLE "user" (
- id INT PRIMARY KEY AUTO_INCREMENT,
- name VARCHAR(128) NOT NULL,
- email VARCHAR(128) NOT NULL UNIQUE,
- hashedPassword VARCHAR(128) NOT NULL,
- salt VARCHAR(128) DEFAULT '' NOT NULL,
- readonly BIT DEFAULT 0 NOT NULL,
- admin BIT DEFAULT 0 NOT NULL,
- map VARCHAR(128),
- language VARCHAR(128),
- distanceUnit VARCHAR(128),
- speedUnit VARCHAR(128),
- latitude DOUBLE PRECISION DEFAULT 0 NOT NULL,
- longitude DOUBLE PRECISION DEFAULT 0 NOT NULL,
- zoom INT DEFAULT 0 NOT NULL);
-
- CREATE TABLE device (
- id INT PRIMARY KEY AUTO_INCREMENT,
- name VARCHAR(128) NOT NULL,
- uniqueId VARCHAR(128) NOT NULL UNIQUE,
- status VARCHAR(128),
- lastUpdate TIMESTAMP,
- positionId INT);
-
- CREATE TABLE user_device (
- userId INT NOT NULL,
- deviceId INT NOT NULL,
- "read" BIT DEFAULT 1 NOT NULL,
- "write" BIT DEFAULT 1 NOT NULL,
- FOREIGN KEY (userId) REFERENCES "user" (id) ON DELETE CASCADE,
- FOREIGN KEY (deviceId) REFERENCES device (id) ON DELETE CASCADE);
-
- CREATE INDEX user_device_userId ON user_device(userId);
-
- CREATE TABLE position (
- id INT PRIMARY KEY AUTO_INCREMENT,
- protocol VARCHAR(128),
- deviceId INT NOT NULL,
- serverTime TIMESTAMP NOT NULL,
- deviceTime TIMESTAMP NOT NULL,
- fixTime TIMESTAMP NOT NULL,
- valid BIT NOT NULL,
- latitude DOUBLE PRECISION NOT NULL,
- longitude DOUBLE PRECISION NOT NULL,
- altitude FLOAT NOT NULL,
- speed FLOAT NOT NULL,
- course FLOAT NOT NULL,
- address VARCHAR(512),
- attributes VARCHAR(4096) NOT NULL,
- FOREIGN KEY (deviceId) REFERENCES device (id) ON DELETE CASCADE);
-
- CREATE INDEX position_deviceId_fixTime ON position (deviceId, fixTime);
-
- CREATE TABLE server (
- id INT PRIMARY KEY AUTO_INCREMENT,
- registration BIT NOT NULL,
- readonly BIT NOT NULL,
- map VARCHAR(128),
- bingKey VARCHAR(128),
- mapUrl VARCHAR(128),
- language VARCHAR(128),
- distanceUnit VARCHAR(128),
- speedUnit VARCHAR(128),
- latitude DOUBLE PRECISION DEFAULT 0 NOT NULL,
- longitude DOUBLE PRECISION DEFAULT 0 NOT NULL,
- zoom INT DEFAULT 0 NOT NULL);
-
- CREATE TABLE traccar (
- version INT DEFAULT 0 NOT NULL);
-
- INSERT INTO traccar (version) VALUES (302);
- </entry>
-
<entry key='database.selectServers'>
SELECT * FROM server;
</entry>
- <entry key='database.insertServer'>
- INSERT INTO server (registration, readonly, latitude, longitude, zoom)
- VALUES (:registration, :readonly, :latitude, :longitude, :zoom);
- </entry>
-
<entry key='database.updateServer'>
UPDATE server SET
registration = :registration,
@@ -158,26 +72,26 @@
</entry>
<entry key='database.loginUser'>
- SELECT * FROM "user"
+ SELECT * FROM users
WHERE email = :email;
</entry>
<entry key='database.selectUser'>
- SELECT * FROM "user"
+ SELECT * FROM users
WHERE id = :id;
</entry>
<entry key='database.selectUsersAll'>
- SELECT * FROM "user";
+ SELECT * FROM users;
</entry>
<entry key='database.insertUser'>
- INSERT INTO "user" (name, email, hashedPassword, salt, admin)
+ INSERT INTO users (name, email, hashedPassword, salt, admin)
VALUES (:name, :email, :hashedPassword, :salt, :admin);
</entry>
<entry key='database.updateUser'>
- UPDATE "user" SET
+ UPDATE users SET
name = :name,
email = :email,
admin = :admin,
@@ -192,11 +106,11 @@
</entry>
<entry key='database.updateUserPassword'>
- UPDATE "user" SET hashedPassword = :hashedPassword, salt = :salt WHERE id = :id;
+ UPDATE users SET hashedPassword = :hashedPassword, salt = :salt WHERE id = :id;
</entry>
<entry key='database.deleteUser'>
- DELETE FROM "user" WHERE id = :id;
+ DELETE FROM users WHERE id = :id;
</entry>
<entry key='database.getPermissionsAll'>
@@ -204,27 +118,27 @@
</entry>
<entry key='database.selectDevicesAll'>
- SELECT * FROM device;
+ SELECT * FROM devices;
</entry>
<entry key='database.selectDevices'>
- SELECT * FROM device d INNER JOIN user_device ud ON d.id = ud.deviceId WHERE ud.userId = :userId;
+ SELECT * FROM devices d INNER JOIN user_device ud ON d.id = ud.deviceId WHERE ud.userId = :userId;
</entry>
<entry key='database.insertDevice'>
- INSERT INTO device (name, uniqueId) VALUES (:name, :uniqueId);
+ INSERT INTO devices (name, uniqueId) VALUES (:name, :uniqueId);
</entry>
<entry key='database.updateDevice'>
- UPDATE device SET name = :name, uniqueId = :uniqueId WHERE id = :id;
+ UPDATE devices SET name = :name, uniqueId = :uniqueId WHERE id = :id;
</entry>
<entry key='database.updateDeviceStatus'>
- UPDATE device SET status = :status, lastUpdate = :lastUpdate WHERE id = :id;
+ UPDATE devices SET status = :status, lastUpdate = :lastUpdate WHERE id = :id;
</entry>
<entry key='database.deleteDevice'>
- DELETE FROM device WHERE id = :id;
+ DELETE FROM devices WHERE id = :id;
</entry>
<entry key='database.linkDevice'>
@@ -236,20 +150,20 @@
</entry>
<entry key='database.selectPositions'>
- SELECT * FROM position WHERE deviceId = :deviceId AND fixTime BETWEEN :from AND :to ORDER BY fixTime;
+ SELECT * FROM positions WHERE deviceId = :deviceId AND fixTime BETWEEN :from AND :to ORDER BY fixTime;
</entry>
<entry key='database.insertPosition'>
- INSERT INTO position (deviceId, protocol, serverTime, deviceTime, fixTime, valid, latitude, longitude, altitude, speed, course, address, attributes)
- VALUES (:deviceId, :protocol, CURRENT_TIMESTAMP(), :time, :time, :valid, :latitude, :longitude, :altitude, :speed, :course, :address, :attributes);
+ INSERT INTO positions (deviceId, protocol, serverTime, deviceTime, fixTime, valid, latitude, longitude, altitude, speed, course, address, attributes)
+ VALUES (:deviceId, :protocol, :now, :time, :time, :valid, :latitude, :longitude, :altitude, :speed, :course, :address, :attributes);
</entry>
<entry key='database.selectLatestPositions'>
- SELECT * FROM position WHERE id IN (SELECT positionId FROM device);
+ SELECT * FROM positions WHERE id IN (SELECT positionId FROM devices);
</entry>
<entry key='database.updateLatestPosition'>
- UPDATE device SET positionId = :id WHERE id = :deviceId;
+ UPDATE devices SET positionId = :id WHERE id = :deviceId;
</entry>
<!-- PROTOCOL CONFIG -->
diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java
index f905b4ded..37cbac35d 100644
--- a/src/org/traccar/database/DataManager.java
+++ b/src/org/traccar/database/DataManager.java
@@ -19,12 +19,8 @@ import com.mchange.v2.c3p0.ComboPooledDataSource;
import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
-import java.nio.file.Path;
-import java.nio.file.Paths;
-import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
-import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Date;
@@ -34,10 +30,11 @@ import javax.naming.InitialContext;
import javax.sql.DataSource;
import liquibase.Contexts;
+import liquibase.LabelExpression;
import liquibase.Liquibase;
+import liquibase.changelog.ChangeSetStatus;
import liquibase.database.Database;
import liquibase.database.DatabaseFactory;
-import liquibase.exception.DatabaseException;
import liquibase.exception.LiquibaseException;
import liquibase.resource.FileSystemResourceAccessor;
import liquibase.resource.ResourceAccessor;
@@ -48,7 +45,6 @@ import org.traccar.model.Device;
import org.traccar.model.MiscFormatter;
import org.traccar.model.Permission;
import org.traccar.model.Position;
-import org.traccar.model.Schema;
import org.traccar.model.Server;
import org.traccar.model.User;
import org.traccar.web.AsyncServlet;
@@ -160,9 +156,9 @@ public class DataManager implements IdentityManager {
private void initDatabaseSchema() throws SQLException, LiquibaseException {
- if (config.getString("web.type", "new").equals("new") || config.getString("web.type", "new").equals("api")) {
+ if (config.hasKey("database.changelog")) {
- /*ResourceAccessor resourceAccessor = new FileSystemResourceAccessor();
+ ResourceAccessor resourceAccessor = new FileSystemResourceAccessor();
Database database = DatabaseFactory.getInstance().openDatabase(
config.getString("database.url"),
@@ -173,46 +169,17 @@ public class DataManager implements IdentityManager {
Liquibase liquibase = new Liquibase(
config.getString("database.changelog"), resourceAccessor, database);
- liquibase.update(new Contexts());*/
-
-
- boolean exist = false;
-
- try (Connection connection = dataSource.getConnection();
- ResultSet result = connection.getMetaData().getTables(connection.getCatalog(), null, null, null)) {
-
- String checkTable = config.getString("database.checkTable");
- while (result.next()) {
- if (result.getString("TABLE_NAME").equalsIgnoreCase(checkTable)) {
- exist = true;
- break;
- }
+ boolean first = true;
+ for (ChangeSetStatus status : liquibase.getChangeSetStatuses(null, new LabelExpression())) {
+ if (!status.getWillRun()) {
+ first = false;
+ break;
}
}
- if (exist) {
-
- String schemaVersionQuery = getQuery("database.selectSchemaVersion");
- if (schemaVersionQuery != null) {
-
- Schema schema = QueryBuilder.create(dataSource, schemaVersionQuery)
- .executeQuerySingle(Schema.class);
-
- int version = 0;
- if (schema != null) {
- version = schema.getVersion();
- }
-
- if (version != 302) {
- Log.error("Wrong database schema version (" + version + ")");
- throw new RuntimeException();
- }
- }
-
- } else {
-
- QueryBuilder.create(dataSource, getQuery("database.createSchema")).executeUpdate();
+ liquibase.update(new Contexts());
+ if (first) {
User admin = new User();
admin.setName("admin");
admin.setEmail("admin");
@@ -220,14 +187,7 @@ public class DataManager implements IdentityManager {
admin.setPassword("admin");
addUser(admin);
- Server server = new Server();
- server.setRegistration(true);
- QueryBuilder.create(dataSource, getQuery("database.insertServer"))
- .setObject(server)
- .executeUpdate();
-
mockData(admin.getId());
-
}
}
}
@@ -395,6 +355,7 @@ public class DataManager implements IdentityManager {
public void addPosition(Position position) throws SQLException {
position.setId(QueryBuilder.create(dataSource, getQuery("database.insertPosition"), true)
+ .setDate("now", new Date())
.setObject(position)
.setDate("time", position.getFixTime()) // tmp
.setLong("device_id", position.getDeviceId()) // tmp
@@ -406,6 +367,7 @@ public class DataManager implements IdentityManager {
public void updateLatestPosition(Position position) throws SQLException {
QueryBuilder.create(dataSource, getQuery("database.updateLatestPosition"))
+ .setDate("now", new Date())
.setObject(position)
.setDate("time", position.getFixTime()) // tmp
.setLong("device_id", position.getDeviceId()) // tmp