From 2617753615e47acb6600a7d551c4f6508f5af91f Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sun, 2 Aug 2015 16:53:24 +1200 Subject: Add database schema version --- debug.xml | 8 ++++++++ src/org/traccar/database/DataManager.java | 18 ++++++++++++++++++ src/org/traccar/model/Schema.java | 29 +++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 src/org/traccar/model/Schema.java diff --git a/debug.xml b/debug.xml index f33a350c4..8dea1ae59 100644 --- a/debug.xml +++ b/debug.xml @@ -45,6 +45,10 @@ traccar + + SELECT * FROM traccar; + + CREATE TABLE "user" ( id INT PRIMARY KEY AUTO_INCREMENT, @@ -79,6 +83,8 @@ 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), @@ -126,6 +132,8 @@ CREATE TABLE traccar ( version INT DEFAULT 0 NOT NULL); + + INSERT INTO traccar (version) VALUES (301); diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java index 4c12ecd77..3cfeadae4 100644 --- a/src/org/traccar/database/DataManager.java +++ b/src/org/traccar/database/DataManager.java @@ -38,6 +38,7 @@ 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; @@ -157,6 +158,23 @@ public class DataManager implements IdentityManager { } } if (exist) { + + String schemaVersionQuery = getQuery("database.selectSchemaVersion"); + if (schemaVersionQuery != null) { + + Schema schema = QueryBuilder.create(dataSource, schemaVersionQuery).executeQuerySingle(new Schema()); + + int version = 0; + if (schema != null) { + version = schema.getVersion(); + } + + if (version != 301) { + Log.error("Wrong database schema version (" + version + ")"); + throw new RuntimeException(); + } + } + return; } diff --git a/src/org/traccar/model/Schema.java b/src/org/traccar/model/Schema.java new file mode 100644 index 000000000..1878f0edb --- /dev/null +++ b/src/org/traccar/model/Schema.java @@ -0,0 +1,29 @@ +/* + * Copyright 2015 Anton Tananaev (anton.tananaev@gmail.com) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.traccar.model; + +public class Schema implements Factory { + + @Override + public Schema create() { + return new Schema(); + } + + private int version; + public int getVersion() { return version; } + public void setVersion(int version) { this.version = version; } + +} -- cgit v1.2.3