mirror of
https://github.com/correl/mage.git
synced 2024-12-27 03:00:13 +00:00
added versioning for the cards database
DB_VERSION must be incremented when database needs clearing
This commit is contained in:
parent
a4011fa789
commit
ba3e59477f
2 changed files with 37 additions and 0 deletions
|
@ -51,7 +51,10 @@ import mage.Constants.CardType;
|
||||||
public enum CardRepository {
|
public enum CardRepository {
|
||||||
|
|
||||||
instance;
|
instance;
|
||||||
|
|
||||||
private static final String JDBC_URL = "jdbc:sqlite:db/cards.db";
|
private static final String JDBC_URL = "jdbc:sqlite:db/cards.db";
|
||||||
|
private static final long DB_VERSION = 1;
|
||||||
|
|
||||||
private Random random = new Random();
|
private Random random = new Random();
|
||||||
private Dao<CardInfo, Object> cardDao;
|
private Dao<CardInfo, Object> cardDao;
|
||||||
private Set<String> classNames;
|
private Set<String> classNames;
|
||||||
|
@ -63,6 +66,17 @@ public enum CardRepository {
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
ConnectionSource connectionSource = new JdbcConnectionSource(JDBC_URL);
|
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);
|
TableUtils.createTableIfNotExists(connectionSource, CardInfo.class);
|
||||||
cardDao = DaoManager.createDao(connectionSource, CardInfo.class);
|
cardDao = DaoManager.createDao(connectionSource, CardInfo.class);
|
||||||
|
|
23
Mage/src/mage/cards/repository/DatabaseVersion.java
Normal file
23
Mage/src/mage/cards/repository/DatabaseVersion.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue