diff --git a/contrib/region_storage_update_20110325.sql b/contrib/region_manual_update_20110325.sql similarity index 95% rename from contrib/region_storage_update_20110325.sql rename to contrib/region_manual_update_20110325.sql index bca8beca..765fe82b 100644 --- a/contrib/region_storage_update_20110325.sql +++ b/contrib/region_manual_update_20110325.sql @@ -1,3 +1,11 @@ +-- +-- This file only needs to be run if you are using a very old version +-- of the region database (before 2011/03/25). +-- +-- Otherwise, WG knows how to update your tables automatically, as well +-- as set them up initially. +-- + SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0; SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0; SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL'; diff --git a/pom.xml b/pom.xml index 9e30a902..45877df9 100644 --- a/pom.xml +++ b/pom.xml @@ -176,6 +176,14 @@ jar + + org.flywaydb + flyway-core + 3.0 + compile + jar + + junit junit @@ -230,6 +238,11 @@ blacklist.txt + + migrations/ + false + ${basedir}/src/main/resources/migrations/ + @@ -361,6 +374,7 @@ net.sf.opencsv:opencsv org.khelekore:prtree com.jolbox:bonecp + org.flywaydb:flyway-core diff --git a/src/main/build/import-control.xml b/src/main/build/import-control.xml index ba3066d0..907fc35d 100644 --- a/src/main/build/import-control.xml +++ b/src/main/build/import-control.xml @@ -35,4 +35,6 @@ + + \ No newline at end of file diff --git a/src/main/java/com/sk89q/worldguard/internal/protection/database/mysql/AbstractJob.java b/src/main/java/com/sk89q/worldguard/internal/protection/database/mysql/AbstractJob.java index 44e6e3ec..6987970d 100644 --- a/src/main/java/com/sk89q/worldguard/internal/protection/database/mysql/AbstractJob.java +++ b/src/main/java/com/sk89q/worldguard/internal/protection/database/mysql/AbstractJob.java @@ -47,6 +47,14 @@ protected AbstractJob(MySQLDatabaseImpl database, Connection conn) { this.logger = database.getLogger(); } + static void closeQuietly(Connection connection) { + if (connection != null) { + try { + connection.close(); + } catch (SQLException ignored) {} + } + } + static void closeQuietly(ResultSet rs) { if (rs != null) { try { diff --git a/src/main/java/com/sk89q/worldguard/internal/protection/database/mysql/MySQLDatabaseImpl.java b/src/main/java/com/sk89q/worldguard/internal/protection/database/mysql/MySQLDatabaseImpl.java index 5b4e09ad..58f3e0ec 100644 --- a/src/main/java/com/sk89q/worldguard/internal/protection/database/mysql/MySQLDatabaseImpl.java +++ b/src/main/java/com/sk89q/worldguard/internal/protection/database/mysql/MySQLDatabaseImpl.java @@ -23,9 +23,12 @@ import com.jolbox.bonecp.BoneCPConfig; import com.sk89q.worldguard.bukkit.ConfigurationManager; import com.sk89q.worldguard.protection.databases.AbstractAsynchronousDatabase; -import com.sk89q.worldguard.protection.databases.InvalidTableFormatException; import com.sk89q.worldguard.protection.databases.ProtectionDatabaseException; import com.sk89q.worldguard.protection.regions.ProtectedRegion; +import com.sk89q.worldguard.util.io.Closer; +import org.flywaydb.core.Flyway; +import org.flywaydb.core.api.FlywayException; +import org.flywaydb.core.api.MigrationVersion; import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.DumperOptions.FlowStyle; import org.yaml.snakeyaml.Yaml; @@ -39,6 +42,7 @@ import java.sql.Statement; import java.util.HashMap; import java.util.Map; +import java.util.logging.Level; import java.util.logging.Logger; import static com.google.common.base.Preconditions.checkNotNull; @@ -76,6 +80,14 @@ public MySQLDatabaseImpl(ConfigurationManager config, String worldName, Logger l throw new ProtectionDatabaseException("Failed to connect to the database", e); } + try { + migrate(); + } catch (FlywayException e) { + throw new ProtectionDatabaseException("Failed to migrate tables", e); + } catch (SQLException e) { + throw new ProtectionDatabaseException("Failed to migrate tables", e); + } + try { worldId = chooseWorldId(worldName); } catch (SQLException e) { @@ -83,6 +95,92 @@ public MySQLDatabaseImpl(ConfigurationManager config, String worldName, Logger l } } + private boolean tryQuery(Connection conn, String sql) throws SQLException { + Closer closer = Closer.create(); + try { + Statement statement = closer.register(conn.createStatement()); + statement.executeQuery(sql); + return true; + } catch (SQLException ex) { + return false; + } finally { + closer.closeQuietly(); + } + } + + /** + * Migrate the tables to the latest version. + * + * @throws SQLException thrown if a connection can't be opened + * @throws ProtectionDatabaseException thrown on other error + */ + private void migrate() throws SQLException, ProtectionDatabaseException { + Closer closer = Closer.create(); + Connection conn = closer.register(getConnection()); + + // Check some tables + boolean tablesExist; + boolean isRecent; + boolean hasMigrations; + + try { + tablesExist = tryQuery(conn, "SELECT * FROM `" + config.sqlTablePrefix + "region_cuboid` LIMIT 1"); + isRecent = tryQuery(conn, "SELECT `world_id` FROM `" + config.sqlTablePrefix + "region_cuboid` LIMIT 1"); + hasMigrations = tryQuery(conn, "SELECT * FROM `" + config.sqlTablePrefix + "migrations` LIMIT 1"); + } finally { + closer.closeQuietly(); + } + + // We don't bother with migrating really old tables + if (tablesExist && !isRecent) { + throw new ProtectionDatabaseException( + "Sorry, your tables are too old for the region SQL auto-migration system. " + + "Please run region_manual_update_20110325.sql on your database, which comes " + + "with WorldGuard or can be found in http://github.com/sk89q/worldguard"); + } + + // Our placeholders + Map placeHolders = new HashMap(); + placeHolders.put("tablePrefix", config.sqlTablePrefix); + + BoneCPConfig boneConfig = connectionPool.getConfig(); + + Flyway flyway = new Flyway(); + + // The MySQL support predates the usage of Flyway, so let's do some + // checks and issue messages appropriately + if (!hasMigrations) { + flyway.setInitOnMigrate(true); + + if (tablesExist) { + logger.log(Level.INFO, "The MySQL region tables exist but the migrations table seems to not exist yet. Creating the migrations table..."); + } else { + // By default, if Flyway sees any tables at all in the schema, it + // will assume that we are up to date, so we have to manually + // check ourselves and then ask Flyway to start from the beginning + // if our test table doesn't exist + flyway.setInitVersion(MigrationVersion.fromVersion("0")); + + logger.log(Level.INFO, "MySQL region tables do not exist: creating..."); + } + } + + flyway.setClassLoader(getClass().getClassLoader()); + flyway.setLocations("migrations/region/mysql"); + flyway.setDataSource(boneConfig.getJdbcUrl(), boneConfig.getUser(), boneConfig.getPassword()); + flyway.setTable(config.sqlTablePrefix + "migrations"); + flyway.setPlaceholders(placeHolders); + flyway.migrate(); + } + + /** + * Get the ID for this world from the database or pick a new one if + * an entry does not exist yet. + * + * @param worldName the world name + * @return a world ID + * @throws SQLException on a database access error + */ private int chooseWorldId(String worldName) throws SQLException { Connection conn = getConnection(); PreparedStatement worldStmt = null; @@ -90,17 +188,6 @@ private int chooseWorldId(String worldName) throws SQLException { PreparedStatement insertWorldStatement = null; try { - PreparedStatement verTest = null; - try { - // Test if the database is up to date, if not throw a critical error - verTest = conn.prepareStatement("SELECT `world_id` FROM `" + config.sqlTablePrefix + "region_cuboid` LIMIT 0, 1;"); - verTest.execute(); - } catch (SQLException ex) { - throw new InvalidTableFormatException("region_storage_update_20110325.sql"); - } finally { - AbstractJob.closeQuietly(verTest); - } - worldStmt = conn.prepareStatement( "SELECT `id` FROM `" + config.sqlTablePrefix + "world` WHERE `name` = ? LIMIT 0, 1" ); diff --git a/src/main/java/com/sk89q/worldguard/util/io/Closer.java b/src/main/java/com/sk89q/worldguard/util/io/Closer.java new file mode 100644 index 00000000..43985131 --- /dev/null +++ b/src/main/java/com/sk89q/worldguard/util/io/Closer.java @@ -0,0 +1,308 @@ +/* + * WorldGuard, a suite of tools for Minecraft + * Copyright (C) sk89q + * Copyright (C) WorldGuard team and contributors + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +package com.sk89q.worldguard.util.io; + +import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Throwables; + +import java.io.Closeable; +import java.io.IOException; +import java.lang.reflect.Method; +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.ArrayDeque; +import java.util.Deque; +import java.util.logging.Level; +import java.util.logging.Logger; + +import static com.google.common.base.Preconditions.checkNotNull; + +public final class Closer implements Closeable { + + private static final Logger logger = Logger.getLogger(Closer.class.getCanonicalName()); + + /** + * The suppressor implementation to use for the current Java version. + */ + private static final Suppressor SUPPRESSOR = SuppressingSuppressor.isAvailable() + ? SuppressingSuppressor.INSTANCE + : LoggingSuppressor.INSTANCE; + + /** + * Creates a new {@link Closer}. + */ + public static Closer create() { + return new Closer(SUPPRESSOR); + } + + @VisibleForTesting + final Suppressor suppressor; + + // only need space for 2 elements in most cases, so try to use the smallest array possible + private final Deque stack = new ArrayDeque(4); + private Throwable thrown; + + @VisibleForTesting + Closer(Suppressor suppressor) { + this.suppressor = checkNotNull(suppressor); // checkNotNull to satisfy null tests + } + + /** + * Registers the given {@code closeable} to be closed when this {@code Closer} is + * {@linkplain #close closed}. + * + * @return the given {@code closeable} + */ + // close. this word no longer has any meaning to me. + public C register(C closeable) { + stack.push(closeable); + return closeable; + } + + /** + * Registers the given {@code connection} to be closed when this + * {@code Closer} is {@linkplain #close closed}. + * + * @return the given {@code connection} + */ + public C register(final C connection) { + register(new Closeable() { + @Override + public void close() throws IOException { + try { + connection.close(); + } catch (SQLException e) { + throw new IOException("Failed to close", e); + } + } + }); + return connection; + } + + /** + * Registers the given {@code statement} to be closed when this + * {@code Closer} is {@linkplain #close closed}. + * + * @return the given {@code statement} + */ + public C register(final C statement) { + register(new Closeable() { + @Override + public void close() throws IOException { + try { + statement.close(); + } catch (SQLException e) { + throw new IOException("Failed to close", e); + } + } + }); + return statement; + } + + /** + * Registers the given {@code resultSet} to be closed when this + * {@code Closer} is {@linkplain #close closed}. + * + * @return the given {@code resultSet} + */ + public C register(final C resultSet) { + register(new Closeable() { + @Override + public void close() throws IOException { + try { + resultSet.close(); + } catch (SQLException e) { + throw new IOException("Failed to close", e); + } + } + }); + return resultSet; + } + + /** + * Stores the given throwable and rethrows it. It will be rethrown as is if it is an + * {@code IOException}, {@code RuntimeException} or {@code Error}. Otherwise, it will be rethrown + * wrapped in a {@code RuntimeException}. Note: Be sure to declare all of the checked + * exception types your try block can throw when calling an overload of this method so as to avoid + * losing the original exception type. + * + *

This method always throws, and as such should be called as + * {@code throw closer.rethrow(e);} to ensure the compiler knows that it will throw. + * + * @return this method does not return; it always throws + * @throws IOException when the given throwable is an IOException + */ + public RuntimeException rethrow(Throwable e) throws IOException { + thrown = e; + Throwables.propagateIfPossible(e, IOException.class); + throw Throwables.propagate(e); + } + + /** + * Stores the given throwable and rethrows it. It will be rethrown as is if it is an + * {@code IOException}, {@code RuntimeException}, {@code Error} or a checked exception of the + * given type. Otherwise, it will be rethrown wrapped in a {@code RuntimeException}. Note: + * Be sure to declare all of the checked exception types your try block can throw when calling an + * overload of this method so as to avoid losing the original exception type. + * + *

This method always throws, and as such should be called as + * {@code throw closer.rethrow(e, ...);} to ensure the compiler knows that it will throw. + * + * @return this method does not return; it always throws + * @throws IOException when the given throwable is an IOException + * @throws X when the given throwable is of the declared type X + */ + public RuntimeException rethrow(Throwable e, + Class declaredType) throws IOException, X { + thrown = e; + Throwables.propagateIfPossible(e, IOException.class); + Throwables.propagateIfPossible(e, declaredType); + throw Throwables.propagate(e); + } + + /** + * Stores the given throwable and rethrows it. It will be rethrown as is if it is an + * {@code IOException}, {@code RuntimeException}, {@code Error} or a checked exception of either + * of the given types. Otherwise, it will be rethrown wrapped in a {@code RuntimeException}. + * Note: Be sure to declare all of the checked exception types your try block can throw + * when calling an overload of this method so as to avoid losing the original exception type. + * + *

This method always throws, and as such should be called as + * {@code throw closer.rethrow(e, ...);} to ensure the compiler knows that it will throw. + * + * @return this method does not return; it always throws + * @throws IOException when the given throwable is an IOException + * @throws X1 when the given throwable is of the declared type X1 + * @throws X2 when the given throwable is of the declared type X2 + */ + public RuntimeException rethrow( + Throwable e, Class declaredType1, Class declaredType2) throws IOException, X1, X2 { + thrown = e; + Throwables.propagateIfPossible(e, IOException.class); + Throwables.propagateIfPossible(e, declaredType1, declaredType2); + throw Throwables.propagate(e); + } + + /** + * Closes all {@code Closeable} instances that have been added to this {@code Closer}. If an + * exception was thrown in the try block and passed to one of the {@code exceptionThrown} methods, + * any exceptions thrown when attempting to close a closeable will be suppressed. Otherwise, the + * first exception to be thrown from an attempt to close a closeable will be thrown and any + * additional exceptions that are thrown after that will be suppressed. + */ + @Override + public void close() throws IOException { + Throwable throwable = thrown; + + // close closeables in LIFO order + while (!stack.isEmpty()) { + Closeable closeable = stack.pop(); + try { + closeable.close(); + } catch (Throwable e) { + if (throwable == null) { + throwable = e; + } else { + suppressor.suppress(closeable, throwable, e); + } + } + } + + if (thrown == null && throwable != null) { + Throwables.propagateIfPossible(throwable, IOException.class); + throw new AssertionError(throwable); // not possible + } + } + + /** + * Close quietly. + */ + public void closeQuietly() { + try { + close(); + } catch (IOException ignored) { + } + } + + /** + * Suppression strategy interface. + */ + @VisibleForTesting interface Suppressor { + /** + * Suppresses the given exception ({@code suppressed}) which was thrown when attempting to close + * the given closeable. {@code thrown} is the exception that is actually being thrown from the + * method. Implementations of this method should not throw under any circumstances. + */ + void suppress(Closeable closeable, Throwable thrown, Throwable suppressed); + } + + /** + * Suppresses exceptions by logging them. + */ + @VisibleForTesting static final class LoggingSuppressor implements Suppressor { + + static final LoggingSuppressor INSTANCE = new LoggingSuppressor(); + + @Override + public void suppress(Closeable closeable, Throwable thrown, Throwable suppressed) { + // log to the same place as Closeables + logger.log(Level.WARNING, "Suppressing exception thrown when closing " + closeable, suppressed); + } + } + + /** + * Suppresses exceptions by adding them to the exception that will be thrown using JDK7's + * addSuppressed(Throwable) mechanism. + */ + @VisibleForTesting static final class SuppressingSuppressor implements Suppressor { + + static final SuppressingSuppressor INSTANCE = new SuppressingSuppressor(); + + static boolean isAvailable() { + return addSuppressed != null; + } + + static final Method addSuppressed = getAddSuppressed(); + + private static Method getAddSuppressed() { + try { + return Throwable.class.getMethod("addSuppressed", Throwable.class); + } catch (Throwable e) { + return null; + } + } + + @Override + public void suppress(Closeable closeable, Throwable thrown, Throwable suppressed) { + // ensure no exceptions from addSuppressed + if (thrown == suppressed) { + return; + } + try { + addSuppressed.invoke(thrown, suppressed); + } catch (Throwable e) { + // if, somehow, IllegalAccessException or another exception is thrown, fall back to logging + LoggingSuppressor.INSTANCE.suppress(closeable, thrown, suppressed); + } + } + } +} diff --git a/contrib/region_storage.sql b/src/main/resources/migrations/region/mysql/V1__Initial.sql similarity index 79% rename from contrib/region_storage.sql rename to src/main/resources/migrations/region/mysql/V1__Initial.sql index 34d9e80d..985b173f 100644 --- a/contrib/region_storage.sql +++ b/src/main/resources/migrations/region/mysql/V1__Initial.sql @@ -5,7 +5,7 @@ SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL'; -- ----------------------------------------------------- -- Table `group` -- ----------------------------------------------------- -CREATE TABLE IF NOT EXISTS `group` ( +CREATE TABLE IF NOT EXISTS `${tablePrefix}group` ( `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT , `name` VARCHAR(64) CHARACTER SET 'utf8' COLLATE 'utf8_bin' NOT NULL , PRIMARY KEY (`id`) , @@ -18,7 +18,7 @@ COLLATE = utf8_bin; -- ----------------------------------------------------- -- Table `world` -- ----------------------------------------------------- -CREATE TABLE IF NOT EXISTS `world` ( +CREATE TABLE IF NOT EXISTS `${tablePrefix}world` ( `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT , `name` VARCHAR(128) CHARACTER SET 'utf8' COLLATE 'utf8_bin' NOT NULL , PRIMARY KEY (`id`) , @@ -31,7 +31,7 @@ COLLATE = utf8_bin; -- ----------------------------------------------------- -- Table `region` -- ----------------------------------------------------- -CREATE TABLE IF NOT EXISTS `region` ( +CREATE TABLE IF NOT EXISTS `${tablePrefix}region` ( `id` VARCHAR(128) CHARACTER SET 'utf8' COLLATE 'utf8_bin' NOT NULL , `world_id` INT(10) UNSIGNED NOT NULL , `type` ENUM('cuboid','poly2d','global') CHARACTER SET 'utf8' COLLATE 'utf8_bin' NOT NULL , @@ -40,14 +40,14 @@ CREATE TABLE IF NOT EXISTS `region` ( PRIMARY KEY (`id`, `world_id`) , INDEX `fk_region_world` (`world_id` ASC) , INDEX `parent` (`parent` ASC) , - CONSTRAINT `fk_region_world1` + CONSTRAINT `fk_${tablePrefix}region_world1` FOREIGN KEY (`world_id` ) - REFERENCES `world` (`id` ) + REFERENCES `${tablePrefix}world` (`id` ) ON DELETE CASCADE ON UPDATE CASCADE, - CONSTRAINT `parent` + CONSTRAINT `${tablePrefix}parent` FOREIGN KEY (`parent` ) - REFERENCES `region` (`id` ) + REFERENCES `${tablePrefix}region` (`id` ) ON DELETE SET NULL ON UPDATE CASCADE) ENGINE = InnoDB @@ -58,7 +58,7 @@ COLLATE = utf8_bin; -- ----------------------------------------------------- -- Table `region_cuboid` -- ----------------------------------------------------- -CREATE TABLE IF NOT EXISTS `region_cuboid` ( +CREATE TABLE IF NOT EXISTS `${tablePrefix}region_cuboid` ( `region_id` VARCHAR(128) CHARACTER SET 'utf8' COLLATE 'utf8_bin' NOT NULL , `world_id` INT(10) UNSIGNED NOT NULL , `min_x` BIGINT(20) NOT NULL , @@ -69,9 +69,9 @@ CREATE TABLE IF NOT EXISTS `region_cuboid` ( `max_z` BIGINT(20) NOT NULL , PRIMARY KEY (`region_id`, `world_id`) , INDEX `fk_region_cuboid_region` (`region_id` ASC) , - CONSTRAINT `fk_region_cuboid_region` + CONSTRAINT `fk_${tablePrefix}region_cuboid_region` FOREIGN KEY (`region_id` , `world_id` ) - REFERENCES `region` (`id` , `world_id` ) + REFERENCES `${tablePrefix}region` (`id` , `world_id` ) ON DELETE CASCADE ON UPDATE CASCADE) ENGINE = InnoDB @@ -82,7 +82,7 @@ COLLATE = utf8_bin; -- ----------------------------------------------------- -- Table `region_flag` -- ----------------------------------------------------- -CREATE TABLE IF NOT EXISTS `region_flag` ( +CREATE TABLE IF NOT EXISTS `${tablePrefix}region_flag` ( `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT , `region_id` VARCHAR(128) CHARACTER SET 'utf8' COLLATE 'utf8_bin' NOT NULL , `world_id` INT(10) UNSIGNED NOT NULL , @@ -90,9 +90,9 @@ CREATE TABLE IF NOT EXISTS `region_flag` ( `value` VARCHAR(256) CHARACTER SET 'utf8' COLLATE 'utf8_bin' NOT NULL , PRIMARY KEY (`id`) , INDEX `fk_flags_region` (`region_id` ASC, `world_id` ASC) , - CONSTRAINT `fk_flags_region` + CONSTRAINT `fk_${tablePrefix}flags_region` FOREIGN KEY (`region_id` , `world_id` ) - REFERENCES `region` (`id` , `world_id` ) + REFERENCES `${tablePrefix}region` (`id` , `world_id` ) ON DELETE CASCADE ON UPDATE CASCADE) ENGINE = InnoDB @@ -103,7 +103,7 @@ COLLATE = utf8_bin; -- ----------------------------------------------------- -- Table `region_groups` -- ----------------------------------------------------- -CREATE TABLE IF NOT EXISTS `region_groups` ( +CREATE TABLE IF NOT EXISTS `${tablePrefix}region_groups` ( `region_id` VARCHAR(128) CHARACTER SET 'utf8' COLLATE 'utf8_bin' NOT NULL , `world_id` INT(10) UNSIGNED NOT NULL , `group_id` INT(10) UNSIGNED NOT NULL , @@ -111,14 +111,14 @@ CREATE TABLE IF NOT EXISTS `region_groups` ( PRIMARY KEY (`region_id`, `world_id`, `group_id`) , INDEX `fk_region_groups_region` (`region_id` ASC) , INDEX `fk_region_groups_group` (`group_id` ASC) , - CONSTRAINT `fk_region_groups_group` + CONSTRAINT `fk_${tablePrefix}region_groups_group` FOREIGN KEY (`group_id` ) - REFERENCES `group` (`id` ) + REFERENCES `${tablePrefix}group` (`id` ) ON DELETE CASCADE ON UPDATE CASCADE, - CONSTRAINT `fk_region_groups_region` + CONSTRAINT `fk_${tablePrefix}region_groups_region` FOREIGN KEY (`region_id` , `world_id` ) - REFERENCES `region` (`id` , `world_id` ) + REFERENCES `${tablePrefix}region` (`id` , `world_id` ) ON DELETE CASCADE ON UPDATE CASCADE) ENGINE = InnoDB @@ -129,7 +129,7 @@ COLLATE = utf8_bin; -- ----------------------------------------------------- -- Table `user` -- ----------------------------------------------------- -CREATE TABLE IF NOT EXISTS `user` ( +CREATE TABLE IF NOT EXISTS `${tablePrefix}user` ( `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT , `name` VARCHAR(64) CHARACTER SET 'utf8' COLLATE 'utf8_bin' NOT NULL , PRIMARY KEY (`id`) , @@ -142,7 +142,7 @@ COLLATE = utf8_bin; -- ----------------------------------------------------- -- Table `region_players` -- ----------------------------------------------------- -CREATE TABLE IF NOT EXISTS `region_players` ( +CREATE TABLE IF NOT EXISTS `${tablePrefix}region_players` ( `region_id` VARCHAR(128) CHARACTER SET 'utf8' COLLATE 'utf8_bin' NOT NULL , `world_id` INT(10) UNSIGNED NOT NULL , `user_id` INT(10) UNSIGNED NOT NULL , @@ -150,14 +150,14 @@ CREATE TABLE IF NOT EXISTS `region_players` ( PRIMARY KEY (`region_id`, `world_id`, `user_id`) , INDEX `fk_region_players_region` (`region_id` ASC) , INDEX `fk_region_users_user` (`user_id` ASC) , - CONSTRAINT `fk_region_users_region` + CONSTRAINT `fk_${tablePrefix}region_users_region` FOREIGN KEY (`region_id` , `world_id` ) - REFERENCES `region` (`id` , `world_id` ) + REFERENCES `${tablePrefix}region` (`id` , `world_id` ) ON DELETE CASCADE ON UPDATE CASCADE, - CONSTRAINT `fk_region_users_user` + CONSTRAINT `fk_${tablePrefix}region_users_user` FOREIGN KEY (`user_id` ) - REFERENCES `user` (`id` ) + REFERENCES `${tablePrefix}user` (`id` ) ON DELETE CASCADE ON UPDATE CASCADE) ENGINE = InnoDB @@ -168,16 +168,16 @@ COLLATE = utf8_bin; -- ----------------------------------------------------- -- Table `region_poly2d` -- ----------------------------------------------------- -CREATE TABLE IF NOT EXISTS `region_poly2d` ( +CREATE TABLE IF NOT EXISTS `${tablePrefix}region_poly2d` ( `region_id` VARCHAR(128) CHARACTER SET 'utf8' COLLATE 'utf8_bin' NOT NULL , `world_id` INT(10) UNSIGNED NOT NULL , `min_y` INT(11) NOT NULL , `max_y` INT(11) NOT NULL , PRIMARY KEY (`region_id`, `world_id`) , INDEX `fk_region_poly2d_region` (`region_id` ASC) , - CONSTRAINT `fk_region_poly2d_region` + CONSTRAINT `fk_${tablePrefix}region_poly2d_region` FOREIGN KEY (`region_id` , `world_id` ) - REFERENCES `region` (`id` , `world_id` ) + REFERENCES `${tablePrefix}region` (`id` , `world_id` ) ON DELETE CASCADE ON UPDATE CASCADE) ENGINE = InnoDB @@ -188,7 +188,7 @@ COLLATE = utf8_bin; -- ----------------------------------------------------- -- Table `region_poly2d_point` -- ----------------------------------------------------- -CREATE TABLE IF NOT EXISTS `region_poly2d_point` ( +CREATE TABLE IF NOT EXISTS `${tablePrefix}region_poly2d_point` ( `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT , `region_id` VARCHAR(128) CHARACTER SET 'utf8' COLLATE 'utf8_bin' NOT NULL , `world_id` INT(10) UNSIGNED NOT NULL , @@ -196,9 +196,9 @@ CREATE TABLE IF NOT EXISTS `region_poly2d_point` ( `z` BIGINT(20) NOT NULL , PRIMARY KEY (`id`) , INDEX `fk_region_poly2d_point_region_poly2d` (`region_id` ASC, `world_id` ASC) , - CONSTRAINT `fk_region_poly2d_point_region_poly2d` + CONSTRAINT `fk_${tablePrefix}region_poly2d_point_region_poly2d` FOREIGN KEY (`region_id` , `world_id` ) - REFERENCES `region_poly2d` (`region_id` , `world_id` ) + REFERENCES `${tablePrefix}region_poly2d` (`region_id` , `world_id` ) ON DELETE CASCADE ON UPDATE CASCADE) ENGINE = InnoDB