mirror of
https://github.com/BlueMap-Minecraft/BlueMap.git
synced 2025-01-25 01:31:22 +01:00
Add script to update databases correctly for the new version format
This commit is contained in:
parent
b60984221c
commit
493f0c9bec
@ -35,6 +35,7 @@
|
||||
import org.apache.commons.pool2.ObjectPool;
|
||||
import org.apache.commons.pool2.impl.GenericObjectPool;
|
||||
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
|
||||
import org.intellij.lang.annotations.Language;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.io.*;
|
||||
@ -114,7 +115,6 @@ public OutputStream writeMapTile(String mapId, int lod, Vector2i tile) throws IO
|
||||
}
|
||||
|
||||
executeUpdate(connection,
|
||||
//language=SQL
|
||||
"REPLACE INTO `bluemap_map_tile` (`map`, `lod`, `x`, `z`, `compression`, `data`) " +
|
||||
"VALUES (?, ?, ?, ?, ?, ?)",
|
||||
mapFK,
|
||||
@ -138,7 +138,6 @@ public Optional<CompressedInputStream> readMapTile(String mapId, int lod, Vector
|
||||
try {
|
||||
byte[] data = recoveringConnection(connection -> {
|
||||
ResultSet result = executeQuery(connection,
|
||||
//language=SQL
|
||||
"SELECT t.`data` " +
|
||||
"FROM `bluemap_map_tile` t " +
|
||||
" INNER JOIN `bluemap_map` m " +
|
||||
@ -179,7 +178,6 @@ public Optional<TileInfo> readMapTileInfo(final String mapId, int lod, final Vec
|
||||
try {
|
||||
TileInfo tileInfo = recoveringConnection(connection -> {
|
||||
ResultSet result = executeQuery(connection,
|
||||
//language=SQL
|
||||
"SELECT t.`changed`, LENGTH(t.`data`) as 'size' " +
|
||||
"FROM `bluemap_map_tile` t " +
|
||||
" INNER JOIN `bluemap_map` m " +
|
||||
@ -240,7 +238,6 @@ public void deleteMapTile(String mapId, int lod, Vector2i tile) throws IOExcepti
|
||||
try {
|
||||
recoveringConnection(connection ->
|
||||
executeUpdate(connection,
|
||||
//language=SQL
|
||||
"DELETE t " +
|
||||
"FROM `bluemap_map_tile` t " +
|
||||
" INNER JOIN `bluemap_map` m " +
|
||||
@ -273,7 +270,6 @@ public OutputStream writeMeta(String mapId, String name) {
|
||||
}
|
||||
|
||||
executeUpdate(connection,
|
||||
//language=SQL
|
||||
"REPLACE INTO `bluemap_map_meta` (`map`, `key`, `value`) " +
|
||||
"VALUES (?, ?, ?)",
|
||||
mapFK,
|
||||
@ -292,7 +288,6 @@ public Optional<InputStream> readMeta(String mapId, String name) throws IOExcept
|
||||
try {
|
||||
byte[] data = recoveringConnection(connection -> {
|
||||
ResultSet result = executeQuery(connection,
|
||||
//language=SQL
|
||||
"SELECT t.`value` " +
|
||||
"FROM `bluemap_map_meta` t " +
|
||||
" INNER JOIN `bluemap_map` m " +
|
||||
@ -323,7 +318,6 @@ public Optional<MetaInfo> readMetaInfo(String mapId, String name) throws IOExcep
|
||||
try {
|
||||
MetaInfo tileInfo = recoveringConnection(connection -> {
|
||||
ResultSet result = executeQuery(connection,
|
||||
//language=SQL
|
||||
"SELECT LENGTH(t.`value`) as 'size' " +
|
||||
"FROM `bluemap_map_meta` t " +
|
||||
" INNER JOIN `bluemap_map` m " +
|
||||
@ -366,7 +360,6 @@ public void deleteMeta(String mapId, String name) throws IOException {
|
||||
try {
|
||||
recoveringConnection(connection ->
|
||||
executeUpdate(connection,
|
||||
//language=SQL
|
||||
"DELETE t " +
|
||||
"FROM `bluemap_map_meta` t " +
|
||||
" INNER JOIN `bluemap_map` m " +
|
||||
@ -386,7 +379,6 @@ public void purgeMap(String mapId) throws IOException {
|
||||
try {
|
||||
recoveringConnection(connection -> {
|
||||
executeUpdate(connection,
|
||||
//language=SQL
|
||||
"DELETE t " +
|
||||
"FROM `bluemap_map_tile` t " +
|
||||
" INNER JOIN `bluemap_map` m " +
|
||||
@ -396,7 +388,6 @@ public void purgeMap(String mapId) throws IOException {
|
||||
);
|
||||
|
||||
executeUpdate(connection,
|
||||
//language=SQL
|
||||
"DELETE t " +
|
||||
"FROM `bluemap_map_meta` t " +
|
||||
" INNER JOIN `bluemap_map` m " +
|
||||
@ -410,6 +401,7 @@ public void purgeMap(String mapId) throws IOException {
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("UnusedAssignment")
|
||||
public void initialize() throws IOException {
|
||||
try {
|
||||
|
||||
@ -423,7 +415,6 @@ public void initialize() throws IOException {
|
||||
")");
|
||||
|
||||
ResultSet result = executeQuery(connection,
|
||||
//language=SQL
|
||||
"SELECT `value` FROM `bluemap_storage_meta` " +
|
||||
"WHERE `key` = ?",
|
||||
"schema_version"
|
||||
@ -433,7 +424,6 @@ public void initialize() throws IOException {
|
||||
return result.getString("value");
|
||||
} else {
|
||||
executeUpdate(connection,
|
||||
//language=SQL
|
||||
"INSERT INTO `bluemap_storage_meta` (`key`, `value`) " +
|
||||
"VALUES (?, ?)",
|
||||
"schema_version", "0"
|
||||
@ -450,7 +440,7 @@ public void initialize() throws IOException {
|
||||
}
|
||||
|
||||
// validate schema version
|
||||
if (schemaVersion < 0 || schemaVersion > 2)
|
||||
if (schemaVersion < 0 || schemaVersion > 3)
|
||||
throw new IOException("Unknown schema-version: " + schemaVersion);
|
||||
|
||||
// update schema to current version
|
||||
@ -502,21 +492,59 @@ public void initialize() throws IOException {
|
||||
);
|
||||
|
||||
executeUpdate(connection,
|
||||
//language=SQL
|
||||
"UPDATE `bluemap_storage_meta` " +
|
||||
"SET `value` = ? " +
|
||||
"WHERE `key` = ?",
|
||||
"2", "schema_version"
|
||||
"3", "schema_version"
|
||||
);
|
||||
}, 2);
|
||||
|
||||
schemaVersion = 2;
|
||||
schemaVersion = 3;
|
||||
}
|
||||
|
||||
if (schemaVersion == 1)
|
||||
throw new IOException("Outdated database schema: " + schemaVersion +
|
||||
" (Cannot automatically update, reset your database and reload bluemap to fix this)");
|
||||
|
||||
if (schemaVersion == 2) {
|
||||
Logger.global.logInfo("Updating database schema: Renaming bluemap_map_meta keys to new format...");
|
||||
recoveringConnection(connection -> {
|
||||
|
||||
// delete potential files that are already in the new format to avoid constraint-issues
|
||||
connection.createStatement().executeUpdate(
|
||||
"DELETE FROM `bluemap_storage_meta`" +
|
||||
"WHERE `key` IN('settings.json', 'textures.json', '.rstate')"
|
||||
);
|
||||
|
||||
// rename files
|
||||
connection.createStatement().executeUpdate(
|
||||
"UPDATE `bluemap_storage_meta`" +
|
||||
"SET `key` = 'settings.json'" +
|
||||
"WHERE `key` = 'settings'"
|
||||
);
|
||||
connection.createStatement().executeUpdate(
|
||||
"UPDATE `bluemap_storage_meta`" +
|
||||
"SET `key` = 'textures.json'" +
|
||||
"WHERE `key` = 'textures'"
|
||||
);
|
||||
connection.createStatement().executeUpdate(
|
||||
"UPDATE `bluemap_storage_meta`" +
|
||||
"SET `key` = '.rstate'" +
|
||||
"WHERE `key` = 'render_state'"
|
||||
);
|
||||
|
||||
// update schemaVersion
|
||||
executeUpdate(connection,
|
||||
"UPDATE `bluemap_storage_meta` " +
|
||||
"SET `value` = ? " +
|
||||
"WHERE `key` = ?",
|
||||
"3", "schema_version"
|
||||
);
|
||||
}, 2);
|
||||
|
||||
schemaVersion = 3;
|
||||
}
|
||||
|
||||
} catch (SQLException ex) {
|
||||
throw new IOException(ex);
|
||||
}
|
||||
@ -539,7 +567,7 @@ public void close() throws IOException {
|
||||
}
|
||||
}
|
||||
|
||||
private ResultSet executeQuery(Connection connection, String sql, Object... parameters) throws SQLException {
|
||||
private ResultSet executeQuery(Connection connection, @Language("sql") String sql, Object... parameters) throws SQLException {
|
||||
// we only use this prepared statement once, but the DB-Driver caches those and reuses them
|
||||
PreparedStatement statement = connection.prepareStatement(sql);
|
||||
for (int i = 0; i < parameters.length; i++) {
|
||||
@ -549,7 +577,7 @@ private ResultSet executeQuery(Connection connection, String sql, Object... para
|
||||
}
|
||||
|
||||
@SuppressWarnings("UnusedReturnValue")
|
||||
private int executeUpdate(Connection connection, String sql, Object... parameters) throws SQLException {
|
||||
private int executeUpdate(Connection connection, @Language("sql") String sql, Object... parameters) throws SQLException {
|
||||
// we only use this prepared statement once, but the DB-Driver caches those and reuses them
|
||||
PreparedStatement statement = connection.prepareStatement(sql);
|
||||
for (int i = 0; i < parameters.length; i++) {
|
||||
|
Loading…
Reference in New Issue
Block a user