1
0
mirror of https://github.com/Zrips/Jobs.git synced 2024-12-31 21:37:57 +01:00

Creating clean data tables on first run

Dropping support for really old data bases
This commit is contained in:
Zrips 2016-04-21 18:10:27 +03:00
parent 258362144d
commit 454b9f5c35
3 changed files with 93 additions and 224 deletions

View File

@ -43,6 +43,7 @@ import com.gamingmesh.jobs.container.LogAmounts;
import com.gamingmesh.jobs.container.PlayerInfo; import com.gamingmesh.jobs.container.PlayerInfo;
import com.gamingmesh.jobs.container.PlayerPoints; import com.gamingmesh.jobs.container.PlayerPoints;
import com.gamingmesh.jobs.container.TopList; import com.gamingmesh.jobs.container.TopList;
import com.gamingmesh.jobs.stuff.Debug;
import com.gamingmesh.jobs.stuff.Loging; import com.gamingmesh.jobs.stuff.Loging;
import com.gamingmesh.jobs.stuff.TimeManage; import com.gamingmesh.jobs.stuff.TimeManage;
@ -76,18 +77,22 @@ public abstract class JobsDAO {
} }
try { try {
Debug.D("V"+version);
if (version <= 1) if (version <= 1)
checkUpdate1(); checkUpdate();
else if (version <= 2) else {
checkUpdate2(); if (version <= 2)
checkUpdate2();
checkUpdate4(); checkUpdate4();
checkUpdate5(); checkUpdate5();
checkUpdate6(); checkUpdate6();
checkUpdate7(); checkUpdate7();
// creating explore database // creating explore database
checkUpdate8(); checkUpdate8();
checkUpdate9(); checkUpdate9();
}
version = 9; version = 9;
updateSchemaVersion(version); updateSchemaVersion(version);
@ -97,7 +102,7 @@ public abstract class JobsDAO {
protected abstract void setupConfig() throws SQLException; protected abstract void setupConfig() throws SQLException;
protected abstract void checkUpdate1() throws SQLException; protected abstract void checkUpdate() throws SQLException;
protected abstract void checkUpdate2() throws SQLException; protected abstract void checkUpdate2() throws SQLException;

View File

@ -21,15 +21,12 @@ package com.gamingmesh.jobs.dao;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.UUID;
import java.util.Map.Entry; import java.util.Map.Entry;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import com.gamingmesh.jobs.Jobs; import com.gamingmesh.jobs.Jobs;
import com.gamingmesh.jobs.container.PlayerInfo; import com.gamingmesh.jobs.container.PlayerInfo;
import com.gamingmesh.jobs.stuff.ChatColor;
import com.gamingmesh.jobs.stuff.UUIDUtil; import com.gamingmesh.jobs.stuff.UUIDUtil;
public class JobsDAOMySQL extends JobsDAO { public class JobsDAOMySQL extends JobsDAO {
@ -98,123 +95,18 @@ public class JobsDAOMySQL extends JobsDAO {
} }
@Override @Override
protected synchronized void checkUpdate1() throws SQLException { protected synchronized void checkUpdate() throws SQLException {
JobsConnection conn = getConnection(); JobsConnection conn = getConnection();
if (conn == null) { if (conn == null) {
Jobs.getPluginLogger().severe("Could not run database updates! Could not connect to MySQL!"); Jobs.getPluginLogger().severe("Could not run database updates! Could not connect to MySQL!");
return; return;
} }
PreparedStatement prest = null; createDefaultJobsBase();
int rows = 0; createDefaultLogBase();
try { createDefaultArchiveBase();
// Check for jobs table createDefaultPointsBase();
prest = conn.prepareStatement("SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = ? AND table_name = ?;"); createDefaultExploreBase();
prest.setString(1, database); createDefaultUsersBase();
prest.setString(2, getPrefix() + "jobs");
ResultSet res = prest.executeQuery();
if (res.next()) {
rows = res.getInt(1);
}
} finally {
if (prest != null) {
try {
prest.close();
} catch (SQLException e) {
}
}
}
PreparedStatement pst1 = null;
PreparedStatement pst2 = null;
try {
if (rows == 0) {
executeSQL("CREATE TABLE `" + getPrefix()
+ "jobs` (`id` int NOT NULL AUTO_INCREMENT PRIMARY KEY, `player_uuid` binary(16) NOT NULL, `job` varchar(20), `experience` int, `level` int);");
} else {
Jobs.getPluginLogger().info("Converting existing usernames to Mojang UUIDs. This could take a long time!");
try {
// Check for jobs table id column
// This is extra check to be sure there is no column by this name already
int idrows = 0;
prest = conn.prepareStatement("SELECT COUNT(*) FROM information_schema.columns WHERE table_schema = ? AND table_name = ? AND column_name = ?;");
prest.setString(1, database);
prest.setString(2, getPrefix() + "jobs");
prest.setString(3, "id");
ResultSet res = prest.executeQuery();
if (res.next()) {
idrows = res.getInt(1);
}
if (idrows == 0)
executeSQL("ALTER TABLE `" + getPrefix() + "jobs` ADD COLUMN `id` int NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST;");
} finally {
}
try {
// Check for jobs table id column
// This is extra check to be sure there is no column by this name already
int uuidrows = 0;
prest = conn.prepareStatement("SELECT COUNT(*) FROM information_schema.columns WHERE table_schema = ? AND table_name = ? AND column_name = ?;");
prest.setString(1, database);
prest.setString(2, getPrefix() + "jobs");
prest.setString(3, "player_uuid");
ResultSet res = prest.executeQuery();
if (res.next()) {
uuidrows = res.getInt(1);
}
if (uuidrows == 0)
executeSQL("ALTER TABLE `" + getPrefix() + "jobs` ADD COLUMN `player_uuid` binary(16) DEFAULT NULL AFTER `id`;");
} finally {
}
pst1 = conn.prepareStatement("SELECT DISTINCT `username` FROM `" + getPrefix() + "jobs` WHERE `player_uuid` IS NULL;");
ResultSet rs = pst1.executeQuery();
ArrayList<String> usernames = new ArrayList<String>();
while (rs.next()) {
usernames.add(rs.getString(1));
}
pst2 = conn.prepareStatement("UPDATE `" + getPrefix() + "jobs` SET `player_uuid` = ? WHERE `username` = ?;");
int i = 0;
int y = 0;
for (String names : usernames) {
i++;
y++;
if (i >= 10) {
Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.DARK_GREEN + "" + y + " of " + usernames.size());
i = 0;
}
Entry<String, PlayerInfo> info = Jobs.getPlayerManager().getPlayerInfoByName(names);
if (info == null)
continue;
pst2.setBytes(1, UUIDUtil.toBytes(UUID.fromString(info.getKey())));
pst2.setString(2, names);
pst2.execute();
}
Jobs.getPluginLogger().info("Mojang UUID conversion complete!");
}
} finally {
if (pst1 != null) {
try {
pst1.close();
} catch (SQLException e) {
}
}
if (pst2 != null) {
try {
pst2.close();
} catch (SQLException e) {
}
}
}
checkUpdate2();
} }
@Override @Override
@ -250,7 +142,6 @@ public class JobsDAOMySQL extends JobsDAO {
executeSQL("ALTER TABLE `" + getPrefix() + "jobs` ADD COLUMN `username` varchar(20);"); executeSQL("ALTER TABLE `" + getPrefix() + "jobs` ADD COLUMN `username` varchar(20);");
} finally { } finally {
} }
checkUpdate4();
} }
@Override @Override
@ -582,8 +473,7 @@ public class JobsDAOMySQL extends JobsDAO {
try { try {
if (rows == 0) if (rows == 0)
executeSQL("CREATE TABLE `" + getPrefix() createDefaultExploreBase();
+ "explore` (`id` int NOT NULL AUTO_INCREMENT PRIMARY KEY, `worldname` varchar(64), `chunkX` int, `chunkZ` int, `playerName` varchar(32));");
} catch (Exception e) { } catch (Exception e) {
} finally { } finally {
} }
@ -792,14 +682,30 @@ public class JobsDAOMySQL extends JobsDAO {
} catch (Exception e) { } catch (Exception e) {
} }
// Create new points table // Create new points table
try { createDefaultPointsBase();
executeSQL("CREATE TABLE `" + getPrefix()
+ "points` (`id` int NOT NULL AUTO_INCREMENT PRIMARY KEY, `userid` int, `totalpoints` double, `currentpoints` double);");
} catch (Exception e) {
}
} }
} }
private boolean createDefaultExploreBase() {
try {
executeSQL("CREATE TABLE `" + getPrefix()
+ "explore` (`id` int NOT NULL AUTO_INCREMENT PRIMARY KEY, `worldname` varchar(64), `chunkX` int, `chunkZ` int, `playerName` varchar(32));");
} catch (SQLException e) {
return false;
}
return true;
}
private boolean createDefaultPointsBase() {
try {
executeSQL("CREATE TABLE `" + getPrefix()
+ "points` (`id` int NOT NULL AUTO_INCREMENT PRIMARY KEY, `userid` int, `totalpoints` double, `currentpoints` double);");
} catch (SQLException e) {
return false;
}
return true;
}
private boolean createDefaultLogBase() { private boolean createDefaultLogBase() {
try { try {
executeSQL("CREATE TABLE `" + getPrefix() executeSQL("CREATE TABLE `" + getPrefix()
@ -830,6 +736,16 @@ public class JobsDAOMySQL extends JobsDAO {
return true; return true;
} }
private boolean createDefaultUsersBase() {
try {
executeSQL("CREATE TABLE `" + getPrefix()
+ "users` (`id` int NOT NULL AUTO_INCREMENT PRIMARY KEY, `player_uuid` varchar(36) NOT NULL, `username` varchar(20));");
} catch (SQLException e) {
return false;
}
return true;
}
private boolean dropDataBase(String name) { private boolean dropDataBase(String name) {
try { try {
executeSQL("DROP TABLE IF EXISTS `" + getPrefix() + name + "`;"); executeSQL("DROP TABLE IF EXISTS `" + getPrefix() + name + "`;");

View File

@ -22,15 +22,11 @@ import java.io.File;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.UUID;
import java.util.Map.Entry; import java.util.Map.Entry;
import org.bukkit.Bukkit;
import com.gamingmesh.jobs.Jobs; import com.gamingmesh.jobs.Jobs;
import com.gamingmesh.jobs.container.PlayerInfo; import com.gamingmesh.jobs.container.PlayerInfo;
import com.gamingmesh.jobs.stuff.ChatColor;
import com.gamingmesh.jobs.stuff.UUIDUtil; import com.gamingmesh.jobs.stuff.UUIDUtil;
public class JobsDAOSQLite extends JobsDAO { public class JobsDAOSQLite extends JobsDAO {
@ -99,93 +95,18 @@ public class JobsDAOSQLite extends JobsDAO {
} }
@Override @Override
protected synchronized void checkUpdate1() throws SQLException { protected synchronized void checkUpdate() throws SQLException {
JobsConnection conn = getConnection(); JobsConnection conn = getConnection();
if (conn == null) { if (conn == null) {
Jobs.getPluginLogger().severe("Could not run database updates! Could not connect to MySQL!"); Jobs.getPluginLogger().severe("Could not run database updates! Could not connect to MySQL!");
return; return;
} }
PreparedStatement prest = null; createDefaultJobsBase();
int rows = 0; createDefaultLogBase();
try { createDefaultArchiveBase();
// Check for jobs table createDefaultPointsBase();
prest = conn.prepareStatement("SELECT COUNT(*) FROM sqlite_master WHERE name = ?;"); createDefaultExploreBase();
prest.setString(1, getPrefix() + "jobs"); createDefaultUsersBase();
ResultSet res = prest.executeQuery();
if (res.next()) {
rows = res.getInt(1);
}
} finally {
if (prest != null) {
try {
prest.close();
} catch (SQLException e) {
}
}
}
PreparedStatement pst1 = null;
PreparedStatement pst2 = null;
try {
if (rows > 0) {
Jobs.getPluginLogger().info("Converting existing usernames to Mojang UUIDs. This could take a long time!!!");
executeSQL("ALTER TABLE `" + getPrefix() + "jobs` RENAME TO `" + getPrefix() + "jobs_old`;");
executeSQL("ALTER TABLE `" + getPrefix() + "jobs_old` ADD COLUMN `player_uuid` binary(16) DEFAULT NULL;");
}
executeSQL("CREATE TABLE `" + getPrefix()
+ "jobs` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `player_uuid` binary(16) NOT NULL, `job` varchar(20), `experience` int, `level` int);");
if (rows > 0) {
pst1 = conn.prepareStatement("SELECT DISTINCT `username` FROM `" + getPrefix() + "jobs_old` WHERE `player_uuid` IS NULL;");
ResultSet rs = pst1.executeQuery();
ArrayList<String> usernames = new ArrayList<String>();
while (rs.next()) {
usernames.add(rs.getString(1));
}
pst2 = conn.prepareStatement("UPDATE `" + getPrefix() + "jobs_old` SET `player_uuid` = ? WHERE `username` = ?;");
int i = 0;
int y = 0;
for (String names : usernames) {
i++;
y++;
if (i >= 50) {
Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.DARK_GREEN + "" + y + " of " + usernames.size());
i = 0;
}
Entry<String, PlayerInfo> info = Jobs.getPlayerManager().getPlayerInfoByName(names);
if (info == null)
continue;
pst2.setBytes(1, UUIDUtil.toBytes(UUID.fromString(info.getKey())));
pst2.setString(2, names);
pst2.execute();
}
executeSQL("INSERT INTO `" + getPrefix() + "jobs` (`player_uuid`, `job`, `experience`, `level`) SELECT `player_uuid`, `job`, `experience`, `level` FROM `"
+ getPrefix() + "jobs_old`;");
}
} finally {
if (pst1 != null) {
try {
pst1.close();
} catch (SQLException e) {
}
}
if (pst2 != null) {
try {
pst2.close();
} catch (SQLException e) {
}
}
}
if (rows > 0) {
executeSQL("DROP TABLE `" + getPrefix() + "jobs_old`;");
Jobs.getPluginLogger().info("Mojang UUID conversion complete!");
}
checkUpdate2();
} }
@Override @Override
@ -221,7 +142,6 @@ public class JobsDAOSQLite extends JobsDAO {
} finally { } finally {
} }
checkUpdate4();
} }
@Override @Override
@ -859,14 +779,42 @@ public class JobsDAOSQLite extends JobsDAO {
executeSQL("ALTER TABLE `" + getPrefix() + "log_temp` RENAME TO `" + getPrefix() + "log`;"); executeSQL("ALTER TABLE `" + getPrefix() + "log_temp` RENAME TO `" + getPrefix() + "log`;");
// Create new points table // Create new points table
executeSQL("CREATE TABLE `" + getPrefix() createDefaultPointsBase();
+ "points` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `userid` int, `totalpoints` double, `currentpoints` double);");
} catch (Exception e) { } catch (Exception e) {
} }
} }
} }
private boolean createDefaultExploreBase() {
try {
executeSQL("CREATE TABLE `" + getPrefix()
+ "explore` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `worldname` varchar(64), `chunkX` int, `chunkZ` int, `playerName` varchar(32));");
} catch (SQLException e) {
return false;
}
return true;
}
private boolean createDefaultPointsBase() {
try {
executeSQL("CREATE TABLE `" + getPrefix()
+ "points` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `userid` int, `totalpoints` double, `currentpoints` double);");
} catch (SQLException e) {
return false;
}
return true;
}
private boolean createDefaultUsersBase() {
try {
executeSQL("CREATE TABLE `" + getPrefix()
+ "users` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `player_uuid` varchar(36) NOT NULL, `username` varchar(20));");
} catch (SQLException e) {
return false;
}
return true;
}
private boolean createDefaultLogBase() { private boolean createDefaultLogBase() {
try { try {
executeSQL("CREATE TABLE `" + getPrefix() executeSQL("CREATE TABLE `" + getPrefix()