use of com.sk89q.worldguard.protection.managers.storage.StorageException in project WorldGuard by EngineHub.
the class SQLDriver method migrate.
/**
* Attempt to migrate the tables to the latest version.
*
* @throws StorageException thrown if migration fails
* @throws SQLException thrown on SQL error
*/
private void migrate() throws SQLException, StorageException {
Closer closer = Closer.create();
Connection conn = closer.register(getConnection());
try {
// Check some tables
boolean tablesExist;
boolean isRecent;
boolean isBeforeMigrations;
boolean hasMigrations;
try {
tablesExist = tryQuery(conn, "SELECT * FROM " + config.getTablePrefix() + "region_cuboid LIMIT 1");
isRecent = tryQuery(conn, "SELECT world_id FROM " + config.getTablePrefix() + "region_cuboid LIMIT 1");
isBeforeMigrations = !tryQuery(conn, "SELECT uuid FROM " + config.getTablePrefix() + "user LIMIT 1");
hasMigrations = tryQuery(conn, "SELECT * FROM " + config.getTablePrefix() + "migrations LIMIT 1");
} finally {
closer.closeQuietly();
}
// We don't bother with migrating really old tables
if (tablesExist && !isRecent) {
throw new StorageException("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<String, String> placeHolders = new HashMap<>();
placeHolders.put("tablePrefix", config.getTablePrefix());
Flyway flyway = new Flyway();
// checks and issue messages appropriately
if (!hasMigrations) {
flyway.setInitOnMigrate(true);
if (tablesExist) {
// Detect if this is before migrations
if (isBeforeMigrations) {
flyway.setInitVersion(MigrationVersion.fromVersion("1"));
}
log.log(Level.INFO, "The SQL 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"));
log.log(Level.INFO, "SQL region tables do not exist: creating...");
}
}
flyway.setClassLoader(getClass().getClassLoader());
flyway.setLocations("migrations/region/" + getMigrationFolderName());
flyway.setDataSource(config.getDsn(), config.getUsername(), config.getPassword());
flyway.setTable(config.getTablePrefix() + "migrations");
flyway.setPlaceholders(placeHolders);
flyway.setValidateOnMigrate(false);
flyway.migrate();
} catch (FlywayException e) {
throw new StorageException("Failed to migrate tables", e);
} finally {
closer.closeQuietly();
}
}
use of com.sk89q.worldguard.protection.managers.storage.StorageException in project WorldGuard by EngineHub.
the class SQLDriver method getAll.
@Override
public List<RegionDatabase> getAll() throws StorageException {
Closer closer = Closer.create();
try {
List<RegionDatabase> stores = new ArrayList<>();
Connection connection = closer.register(getConnection());
Statement stmt = connection.createStatement();
ResultSet rs = closer.register(stmt.executeQuery("SELECT name FROM " + config.getTablePrefix() + "world"));
while (rs.next()) {
stores.add(get(rs.getString(1)));
}
return stores;
} catch (SQLException e) {
throw new StorageException("Failed to fetch list of worlds", e);
} finally {
closer.closeQuietly();
}
}
use of com.sk89q.worldguard.protection.managers.storage.StorageException in project WorldGuard by EngineHub.
the class SQLRegionDatabase method saveAll.
@Override
public void saveAll(Set<ProtectedRegion> regions) throws StorageException {
checkNotNull(regions);
initialize();
Closer closer = Closer.create();
DataUpdater updater;
try {
try {
updater = new DataUpdater(this, closer.register(getConnection()));
} catch (SQLException e) {
throw new StorageException("Failed to get a connection to the database", e);
}
try {
updater.saveAll(regions);
} catch (SQLException e) {
throw new StorageException("Failed to save the region data to the database", e);
}
} finally {
closer.closeQuietly();
}
}
use of com.sk89q.worldguard.protection.managers.storage.StorageException in project WorldGuard by EngineHub.
the class SQLRegionDatabase method initialize.
/**
* Initialize the database if it hasn't been yet initialized.
*
* @throws StorageException thrown if initialization fails
*/
private synchronized void initialize() throws StorageException {
if (!initialized) {
driver.initialize();
try {
worldId = chooseWorldId(worldName);
} catch (SQLException e) {
throw new StorageException("Failed to choose the ID for this world", e);
}
initialized = true;
}
}
use of com.sk89q.worldguard.protection.managers.storage.StorageException in project TARDIS by eccentricdevotion.
the class TARDISWorldGuardUtils method addRechargerProtection.
/**
* Adds a WorldGuard protected region to recharger location. A 3x3 block area is protected.
*
* @param p the player to assign as the owner of the region
* @param name the name of the recharger
* @param one a start location of a cuboid region
* @param two an end location of a cuboid region
*/
public void addRechargerProtection(Player p, String name, Location one, Location two) {
RegionManager rm = wg.getRegionContainer().get(new BukkitWorld(one.getWorld()));
BlockVector3 b1;
BlockVector3 b2;
b1 = makeBlockVector(one);
b2 = makeBlockVector(two);
ProtectedCuboidRegion region = new ProtectedCuboidRegion("tardis_recharger_" + name, b1, b2);
DefaultDomain dd = new DefaultDomain();
dd.addPlayer(p.getUniqueId());
region.setOwners(dd);
HashMap<Flag<?>, Object> flags = new HashMap<>();
flags.put(Flags.TNT, State.DENY);
flags.put(Flags.CREEPER_EXPLOSION, State.DENY);
flags.put(Flags.FIRE_SPREAD, State.DENY);
flags.put(Flags.LAVA_FIRE, State.DENY);
flags.put(Flags.LAVA_FLOW, State.DENY);
flags.put(Flags.LIGHTER, State.DENY);
region.setFlags(flags);
rm.addRegion(region);
try {
rm.save();
} catch (StorageException e) {
plugin.getLogger().log(Level.INFO, "Could not create WorldGuard Protection for recharger! " + e.getMessage());
}
}
Aggregations