From ba3e59477f9b4809c5ef248f91128dd4d0448a03 Mon Sep 17 00:00:00 2001 From: North Date: Wed, 9 Jan 2013 23:47:27 +0200 Subject: [PATCH] added versioning for the cards database DB_VERSION must be incremented when database needs clearing --- .../mage/cards/repository/CardRepository.java | 14 +++++++++++ .../cards/repository/DatabaseVersion.java | 23 +++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 Mage/src/mage/cards/repository/DatabaseVersion.java diff --git a/Mage/src/mage/cards/repository/CardRepository.java b/Mage/src/mage/cards/repository/CardRepository.java index 6902be403b..1279b645a0 100644 --- a/Mage/src/mage/cards/repository/CardRepository.java +++ b/Mage/src/mage/cards/repository/CardRepository.java @@ -51,7 +51,10 @@ import mage.Constants.CardType; public enum CardRepository { instance; + private static final String JDBC_URL = "jdbc:sqlite:db/cards.db"; + private static final long DB_VERSION = 1; + private Random random = new Random(); private Dao cardDao; private Set classNames; @@ -63,6 +66,17 @@ public enum CardRepository { } try { ConnectionSource connectionSource = new JdbcConnectionSource(JDBC_URL); + TableUtils.createTableIfNotExists(connectionSource, DatabaseVersion.class); + Dao dbVersionDao = DaoManager.createDao(connectionSource, DatabaseVersion.class); + List dbVersions = dbVersionDao.queryForAll(); + if (dbVersions.isEmpty() || dbVersions.get(0).getVersion() != DB_VERSION) { + TableUtils.dropTable(connectionSource, CardInfo.class, true); + if (dbVersions.isEmpty()) { + DatabaseVersion dbVersion = new DatabaseVersion(); + dbVersion.setVersion(DB_VERSION); + dbVersionDao.create(dbVersion); + } + } TableUtils.createTableIfNotExists(connectionSource, CardInfo.class); cardDao = DaoManager.createDao(connectionSource, CardInfo.class); diff --git a/Mage/src/mage/cards/repository/DatabaseVersion.java b/Mage/src/mage/cards/repository/DatabaseVersion.java new file mode 100644 index 0000000000..7f1b3aaab0 --- /dev/null +++ b/Mage/src/mage/cards/repository/DatabaseVersion.java @@ -0,0 +1,23 @@ +package mage.cards.repository; + +import com.j256.ormlite.field.DatabaseField; +import com.j256.ormlite.table.DatabaseTable; + +/** + * + * @author North + */ +@DatabaseTable(tableName = "version") +public class DatabaseVersion { + + @DatabaseField + protected Long version; + + public Long getVersion() { + return version; + } + + public void setVersion(Long version) { + this.version = version; + } +}