Add the migration to fix column types in the postgresql.

This commit is contained in:
Anatoliy Sablin
2020-05-06 19:39:33 +03:00
parent 4b5eecd7e7
commit 791361c10d

View File

@@ -23,6 +23,9 @@ package io.kamax.mxisd.storage.ormlite;
import com.j256.ormlite.dao.CloseableWrappedIterable; import com.j256.ormlite.dao.CloseableWrappedIterable;
import com.j256.ormlite.dao.Dao; import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.dao.DaoManager; import com.j256.ormlite.dao.DaoManager;
import com.j256.ormlite.db.DatabaseType;
import com.j256.ormlite.db.PostgresDatabaseType;
import com.j256.ormlite.db.SqliteDatabaseType;
import com.j256.ormlite.jdbc.JdbcConnectionSource; import com.j256.ormlite.jdbc.JdbcConnectionSource;
import com.j256.ormlite.stmt.QueryBuilder; import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.support.ConnectionSource; import com.j256.ormlite.support.ConnectionSource;
@@ -82,6 +85,7 @@ public class OrmLiteSqlStorage implements IStorage {
public static class Migrations { public static class Migrations {
public static final String FIX_ACCEPTED_DAO = "2019_12_09__2254__fix_accepted_dao"; public static final String FIX_ACCEPTED_DAO = "2019_12_09__2254__fix_accepted_dao";
public static final String FIX_HASH_DAO_UNIQUE_INDEX = "2020_03_22__1153__fix_hash_dao_unique_index"; public static final String FIX_HASH_DAO_UNIQUE_INDEX = "2020_03_22__1153__fix_hash_dao_unique_index";
public static final String CHANGE_TYPE_TO_TEXT_INVITE = "2020_04_21__2338__change_type_table_invites";
} }
private Dao<ThreePidInviteIO, String> invDao; private Dao<ThreePidInviteIO, String> invDao;
@@ -109,7 +113,19 @@ public class OrmLiteSqlStorage implements IStorage {
} }
withCatcher(() -> { withCatcher(() -> {
ConnectionSource connPool = new JdbcConnectionSource("jdbc:" + backend + ":" + database, username, password); DatabaseType databaseType;
switch (backend) {
case postgresql:
databaseType = new PostgresDatabaseType();
break;
case sqlite:
databaseType = new SqliteDatabaseType();
break;
default:
throw new ConfigurationException("storage.backend");
}
ConnectionSource connPool = new JdbcConnectionSource("jdbc:" + backend + ":" + database, username, password, databaseType);
changelogDao = createDaoAndTable(connPool, ChangelogDao.class); changelogDao = createDaoAndTable(connPool, ChangelogDao.class);
invDao = createDaoAndTable(connPool, ThreePidInviteIO.class); invDao = createDaoAndTable(connPool, ThreePidInviteIO.class);
expInvDao = createDaoAndTable(connPool, HistoricalThreePidInviteIO.class); expInvDao = createDaoAndTable(connPool, HistoricalThreePidInviteIO.class);
@@ -134,6 +150,11 @@ public class OrmLiteSqlStorage implements IStorage {
changelogDao changelogDao
.create(new ChangelogDao(Migrations.FIX_HASH_DAO_UNIQUE_INDEX, new Date(), "Add the id and migrate the unique index.")); .create(new ChangelogDao(Migrations.FIX_HASH_DAO_UNIQUE_INDEX, new Date(), "Add the id and migrate the unique index."));
} }
ChangelogDao fixInviteTableColumnType = changelogDao.queryForId(Migrations.CHANGE_TYPE_TO_TEXT_INVITE);
if (fixInviteTableColumnType == null) {
fixInviteTableColumnType(connPol);
changelogDao.create(new ChangelogDao(Migrations.CHANGE_TYPE_TO_TEXT_INVITE, new Date(), "Modify column type to text."));
}
} }
private void fixAcceptedDao(ConnectionSource connPool) throws SQLException { private void fixAcceptedDao(ConnectionSource connPool) throws SQLException {
@@ -148,6 +169,19 @@ public class OrmLiteSqlStorage implements IStorage {
TableUtils.createTableIfNotExists(connPool, HashDao.class); TableUtils.createTableIfNotExists(connPool, HashDao.class);
} }
private void fixInviteTableColumnType(ConnectionSource connPool) throws SQLException {
LOGGER.info("Migration: {}", Migrations.CHANGE_TYPE_TO_TEXT_INVITE);
if (StorageConfig.BackendEnum.postgresql == backend) {
invDao.executeRawNoArgs("alter table invite_3pid alter column \"roomId\" type text");
invDao.executeRawNoArgs("alter table invite_3pid alter column id type text");
invDao.executeRawNoArgs("alter table invite_3pid alter column token type text");
invDao.executeRawNoArgs("alter table invite_3pid alter column sender type text");
invDao.executeRawNoArgs("alter table invite_3pid alter column medium type text");
invDao.executeRawNoArgs("alter table invite_3pid alter column address type text");
invDao.executeRawNoArgs("alter table invite_3pid alter column properties type text");
}
}
private <V, K> Dao<V, K> createDaoAndTable(ConnectionSource connPool, Class<V> c) throws SQLException { private <V, K> Dao<V, K> createDaoAndTable(ConnectionSource connPool, Class<V> c) throws SQLException {
return createDaoAndTable(connPool, c, false); return createDaoAndTable(connPool, c, false);
} }