added versioning for the cards database

DB_VERSION must be incremented when database needs clearing
This commit is contained in:
North 2013-01-09 23:47:27 +02:00
parent a4011fa789
commit ba3e59477f
2 changed files with 37 additions and 0 deletions

View file

@ -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<CardInfo, Object> cardDao;
private Set<String> classNames;
@ -63,6 +66,17 @@ public enum CardRepository {
}
try {
ConnectionSource connectionSource = new JdbcConnectionSource(JDBC_URL);
TableUtils.createTableIfNotExists(connectionSource, DatabaseVersion.class);
Dao<DatabaseVersion, Object> dbVersionDao = DaoManager.createDao(connectionSource, DatabaseVersion.class);
List<DatabaseVersion> 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);

View file

@ -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;
}
}