-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from whimc/track_gamemode
Track gamemode
- Loading branch information
Showing
7 changed files
with
204 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/main/java/edu/whimc/positiontracker/sql/migration/SchemaManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package edu.whimc.positiontracker.sql.migration; | ||
|
||
import edu.whimc.positiontracker.PositionTracker; | ||
import edu.whimc.positiontracker.sql.MySQLConnection; | ||
import edu.whimc.positiontracker.sql.migration.schemas.Schema_1; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.sql.Connection; | ||
import java.sql.SQLException; | ||
|
||
public class SchemaManager { | ||
|
||
public static final String VERSION_FILE_NAME = ".schema_version"; | ||
|
||
private static final SchemaVersion BASE_SCHEMA = new Schema_1(); | ||
|
||
private final PositionTracker plugin; | ||
|
||
private final MySQLConnection connection; | ||
|
||
public SchemaManager(PositionTracker plugin, MySQLConnection connection) { | ||
this.plugin = plugin; | ||
this.connection = connection; | ||
} | ||
|
||
protected Connection getConnection() { | ||
try { | ||
return this.connection.getConnection(); | ||
} catch (SQLException e) { | ||
return null; | ||
} | ||
} | ||
|
||
protected File getVersionFile() { | ||
return new File(this.plugin.getDataFolder(), VERSION_FILE_NAME); | ||
} | ||
|
||
private int getCurrentVersion() { | ||
try { | ||
return Integer.parseInt(new String(Files.readAllBytes(getVersionFile().toPath()))); | ||
} catch (NumberFormatException | IOException exc) { | ||
return 0; | ||
} | ||
} | ||
|
||
public boolean initialize() { | ||
int curVersion = getCurrentVersion(); | ||
|
||
SchemaVersion schema = BASE_SCHEMA; | ||
while (schema != null) { | ||
if (schema.getVersion() > curVersion) { | ||
this.plugin.getLogger().info("Migrating to schema " + schema.getVersion() + "..."); | ||
if (!schema.migrate(this)) { | ||
return false; | ||
} | ||
} | ||
schema = schema.getNextSchema(); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
src/main/java/edu/whimc/positiontracker/sql/migration/SchemaVersion.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package edu.whimc.positiontracker.sql.migration; | ||
|
||
import com.google.common.io.Files; | ||
|
||
import java.io.IOException; | ||
import java.sql.Connection; | ||
import java.sql.SQLException; | ||
|
||
public abstract class SchemaVersion { | ||
|
||
private final int version; | ||
private final SchemaVersion nextSchema; | ||
|
||
protected SchemaVersion(int version, SchemaVersion next) { | ||
this.version = version; | ||
this.nextSchema = next; | ||
} | ||
|
||
public int getVersion() { | ||
return this.version; | ||
} | ||
|
||
public SchemaVersion getNextSchema() { | ||
return this.nextSchema; | ||
} | ||
|
||
protected abstract void migrateRoutine(Connection connection) throws SQLException; | ||
|
||
public final boolean migrate(SchemaManager manager) { | ||
// Migrate the database | ||
try { | ||
migrateRoutine(manager.getConnection()); | ||
} catch (SQLException exc) { | ||
exc.printStackTrace(); | ||
return false; | ||
} | ||
|
||
// Update the schema version | ||
try { | ||
Files.write(String.valueOf(this.version).getBytes(), manager.getVersionFile()); | ||
} catch (IOException exc) { | ||
exc.printStackTrace(); | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
} |
54 changes: 54 additions & 0 deletions
54
src/main/java/edu/whimc/positiontracker/sql/migration/schemas/Schema_1.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package edu.whimc.positiontracker.sql.migration.schemas; | ||
|
||
import edu.whimc.positiontracker.sql.migration.SchemaVersion; | ||
import java.sql.Connection; | ||
import java.sql.PreparedStatement; | ||
import java.sql.SQLException; | ||
|
||
public class Schema_1 extends SchemaVersion { | ||
|
||
/** Table that will store player positions. */ | ||
private static final String CREATE_POSITIONS_TABLE = | ||
"CREATE TABLE IF NOT EXISTS `whimc_player_positions` (" + | ||
" `rowid` BIGINT AUTO_INCREMENT NOT NULL," + | ||
" `x` INT NOT NULL," + | ||
" `y` INT NOT NULL," + | ||
" `z` INT NOT NULL," + | ||
" `world` VARCHAR(64) NOT NULL," + | ||
" `biome` VARCHAR(64) NOT NULL," + | ||
" `username` VARCHAR(16) NOT NULL," + | ||
" `uuid` VARCHAR(36) NOT NULL," + | ||
" `time` BIGINT NOT NULL," + | ||
" PRIMARY KEY (`rowid`));"; | ||
|
||
/** Table that will store region change events. */ | ||
private static final String CREATE_REGIONS_TABLE = | ||
"CREATE TABLE IF NOT EXISTS `whimc_player_region_events` (" + | ||
" `rowid` BIGINT AUTO_INCREMENT NOT NULL," + | ||
" `region` VARCHAR(64) NOT NULL," + | ||
" `trigger` VARCHAR(16) NOT NULL," + | ||
" `isEnter` BIT(1) NOT NULL," + | ||
" `x` INT NOT NULL," + | ||
" `y` INT NOT NULL," + | ||
" `z` INT NOT NULL," + | ||
" `world` VARCHAR(64) NOT NULL," + | ||
" `username` VARCHAR(16) NOT NULL," + | ||
" `uuid` VARCHAR(36) NOT NULL," + | ||
" `time` BIGINT NOT NULL," + | ||
" PRIMARY KEY (`rowid`));"; | ||
|
||
|
||
public Schema_1() { | ||
super(1, new Schema_2()); | ||
} | ||
|
||
@Override | ||
protected void migrateRoutine(Connection connection) throws SQLException { | ||
try (PreparedStatement createPosTable = connection.prepareStatement(CREATE_POSITIONS_TABLE)) { | ||
createPosTable.execute(); | ||
} | ||
try (PreparedStatement createRegionTable = connection.prepareStatement(CREATE_REGIONS_TABLE)) { | ||
createRegionTable.execute(); | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/edu/whimc/positiontracker/sql/migration/schemas/Schema_2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package edu.whimc.positiontracker.sql.migration.schemas; | ||
|
||
import edu.whimc.positiontracker.sql.migration.SchemaVersion; | ||
import java.sql.Connection; | ||
import java.sql.PreparedStatement; | ||
import java.sql.SQLException; | ||
|
||
public class Schema_2 extends SchemaVersion { | ||
|
||
private static final String ADD_GAMEMODE = | ||
"ALTER TABLE whimc_player_positions ADD COLUMN gamemode VARCHAR(16);"; | ||
|
||
public Schema_2() { | ||
super(2, null); | ||
} | ||
|
||
@Override | ||
protected void migrateRoutine(Connection connection) throws SQLException { | ||
try (PreparedStatement addGamemode = connection.prepareStatement(ADD_GAMEMODE)) { | ||
addGamemode.execute(); | ||
} | ||
} | ||
} |