Added cards database cleanup on new builds/releases run (client/server sides);

This commit is contained in:
Oleg Agafonov 2018-12-21 23:11:43 +04:00
parent 20d5bfc3c9
commit 20a3b0b777
6 changed files with 83 additions and 15 deletions

View file

@ -1,5 +1,7 @@
package mage.utils;
import mage.util.JarVersion;
import java.io.Serializable;
/**

View file

@ -10,9 +10,6 @@ import com.j256.ormlite.stmt.Where;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.support.DatabaseConnection;
import com.j256.ormlite.table.TableUtils;
import java.io.File;
import java.sql.SQLException;
import java.util.*;
import mage.cards.CardSetInfo;
import mage.constants.CardType;
import mage.constants.SetType;
@ -20,6 +17,10 @@ import mage.constants.SuperType;
import mage.util.RandomUtil;
import org.apache.log4j.Logger;
import java.io.File;
import java.sql.SQLException;
import java.util.*;
/**
* @author North
*/
@ -43,9 +44,10 @@ public enum CardRepository {
}
try {
ConnectionSource connectionSource = new JdbcConnectionSource(JDBC_URL);
boolean obsolete = RepositoryUtil.isDatabaseObsolete(connectionSource, VERSION_ENTITY_NAME, CARD_DB_VERSION);
if (obsolete) {
boolean isObsolete = RepositoryUtil.isDatabaseObsolete(connectionSource, VERSION_ENTITY_NAME, CARD_DB_VERSION);
boolean isNewBuild = RepositoryUtil.isNewBuildRun(connectionSource, VERSION_ENTITY_NAME, CardRepository.class); // recreate db on new build
if (isObsolete || isNewBuild) {
TableUtils.dropTable(connectionSource, CardInfo.class, true);
}
@ -483,9 +485,7 @@ public enum CardRepository {
DatabaseConnection conn = cardDao.getConnectionSource().getReadWriteConnection();
conn.executeStatement("shutdown compact", 0);
}
} catch (SQLException ex) {
}
}

View file

@ -0,0 +1,33 @@
package mage.cards.repository;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;
/**
* @author JayDi85
*/
@DatabaseTable(tableName = "build")
public class DatabaseBuild {
@DatabaseField
protected String entity;
@DatabaseField(columnName = "last_build")
protected String lastBuild;
public String getEntity() {
return entity;
}
public void setEntity(String entity) {
this.entity = entity;
}
public String getLastBuild() {
return lastBuild;
}
public void setLastBuild(String lastBuild) {
this.lastBuild = lastBuild;
}
}

View file

@ -8,16 +8,16 @@ import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.SelectArg;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;
import org.apache.log4j.Logger;
import java.io.File;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import org.apache.log4j.Logger;
/**
*
* @author North
*/
public enum ExpansionRepository {
@ -42,9 +42,10 @@ public enum ExpansionRepository {
}
try {
ConnectionSource connectionSource = new JdbcConnectionSource(JDBC_URL);
boolean obsolete = RepositoryUtil.isDatabaseObsolete(connectionSource, VERSION_ENTITY_NAME, EXPANSION_DB_VERSION);
if (obsolete) {
boolean isObsolete = RepositoryUtil.isDatabaseObsolete(connectionSource, VERSION_ENTITY_NAME, EXPANSION_DB_VERSION);
boolean isNewBuild = RepositoryUtil.isNewBuildRun(connectionSource, VERSION_ENTITY_NAME, ExpansionRepository.class); // recreate db on new build
if (isObsolete || isNewBuild) {
TableUtils.dropTable(connectionSource, ExpansionInfo.class, true);
}
@ -93,9 +94,9 @@ public enum ExpansionRepository {
// only with boosters and cards
GenericRawResults<ExpansionInfo> setsList = expansionDao.queryRaw(
"select * from expansion e "
+ " where e.boosters = 1 "
+ " and exists(select (1) from card c where c.setcode = e.code) "
+ " order by e.releasedate desc",
+ " where e.boosters = 1 "
+ " and exists(select (1) from card c where c.setcode = e.code) "
+ " order by e.releasedate desc",
expansionDao.getRawRowMapper());
List<ExpansionInfo> resList = new ArrayList<>();

View file

@ -7,6 +7,8 @@ import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.SelectArg;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;
import mage.util.JarVersion;
import java.sql.SQLException;
import java.util.List;
@ -33,6 +35,29 @@ public final class RepositoryUtil {
return dbVersions.isEmpty();
}
public static boolean isNewBuildRun(ConnectionSource connectionSource, String entityName, Class clazz) throws SQLException {
// build time checks only for releases, not runtime (e.g. IDE debug)
String currentBuild = JarVersion.getBuildTime(clazz);
if (!JarVersion.isBuildTimeOk(currentBuild)) {
return false;
}
TableUtils.createTableIfNotExists(connectionSource, DatabaseBuild.class);
Dao<DatabaseBuild, Object> dbBuildDao = DaoManager.createDao(connectionSource, DatabaseBuild.class);
QueryBuilder<DatabaseBuild, Object> queryBuilder = dbBuildDao.queryBuilder();
queryBuilder.where().eq("entity", new SelectArg(entityName)).and().eq("last_build", currentBuild);
List<DatabaseBuild> dbBuilds = dbBuildDao.query(queryBuilder.prepare());
if (dbBuilds.isEmpty()) {
DatabaseBuild dbBuild = new DatabaseBuild();
dbBuild.setEntity(entityName);
dbBuild.setLastBuild(currentBuild);
dbBuildDao.create(dbBuild);
}
return dbBuilds.isEmpty();
}
public static void updateVersion(ConnectionSource connectionSource, String entityName, long version) throws SQLException {
TableUtils.createTableIfNotExists(connectionSource, DatabaseVersion.class);
Dao<DatabaseVersion, Object> dbVersionDao = DaoManager.createDao(connectionSource, DatabaseVersion.class);

View file

@ -1,4 +1,4 @@
package mage.utils;
package mage.util;
import org.apache.log4j.Logger;
@ -47,4 +47,11 @@ public class JarVersion {
return JAR_BUILD_TIME_ERROR;
}
}
public static boolean isBuildTimeOk(String buildTime) {
return buildTime != null
&& !buildTime.isEmpty()
&& !buildTime.equals(JAR_BUILD_TIME_ERROR)
&& !buildTime.equals(JAR_BUILD_TIME_FROM_CLASSES);
}
}