Skip to content

Commit

Permalink
Workaround for postgresql.
Browse files Browse the repository at this point in the history
  • Loading branch information
Anatoly Sablin committed Jan 28, 2020
1 parent 7555fff commit 72977d6
Showing 1 changed file with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public static class Migrations {
private Dao<AcceptedDao, Long> acceptedDao;
private Dao<HashDao, String> hashDao;
private Dao<ChangelogDao, String> changelogDao;
private StorageConfig.BackendEnum backend;

public OrmLiteSqlStorage(StorageConfig.BackendEnum backend, String path) {
this(backend, path, null, null);
Expand All @@ -100,6 +101,7 @@ public OrmLiteSqlStorage(StorageConfig.BackendEnum backend, String database, Str
if (backend == null) {
throw new ConfigurationException("storage.backend");
}
this.backend = backend;

if (StringUtils.isBlank(database)) {
throw new ConfigurationException("Storage destination cannot be empty");
Expand All @@ -113,7 +115,7 @@ public OrmLiteSqlStorage(StorageConfig.BackendEnum backend, String database, Str
sessionDao = createDaoAndTable(connPool, ThreePidSessionDao.class);
asTxnDao = createDaoAndTable(connPool, ASTransactionDao.class);
accountDao = createDaoAndTable(connPool, AccountDao.class);
acceptedDao = createDaoAndTable(connPool, AcceptedDao.class);
acceptedDao = createDaoAndTable(connPool, AcceptedDao.class, true);
hashDao = createDaoAndTable(connPool, HashDao.class);
runMigration(connPool);
});
Expand All @@ -134,9 +136,27 @@ private void fixAcceptedDao(ConnectionSource connPool) throws SQLException {
}

private <V, K> Dao<V, K> createDaoAndTable(ConnectionSource connPool, Class<V> c) throws SQLException {
return createDaoAndTable(connPool, c, false);
}

/**
* Workaround for https://github.com/j256/ormlite-core/issues/20.
*/
private <V, K> Dao<V, K> createDaoAndTable(ConnectionSource connPool, Class<V> c, boolean workaround) throws SQLException {
LOGGER.info("Create the dao: {}", c.getSimpleName());
Dao<V, K> dao = DaoManager.createDao(connPool, c);
TableUtils.createTableIfNotExists(connPool, c);
if (workaround && StorageConfig.BackendEnum.postgresql.equals(backend)) {
LOGGER.info("Workaround for postgresql on dao: {}", c.getSimpleName());
try {
dao.countOf();
LOGGER.info("Table exists, do nothing");
} catch (SQLException e) {
LOGGER.info("Table doesn't exist, create");
TableUtils.createTableIfNotExists(connPool, c);
}
} else {
TableUtils.createTableIfNotExists(connPool, c);
}
return dao;
}

Expand Down

0 comments on commit 72977d6

Please sign in to comment.