Skip to content

Commit d075bc6

Browse files
Smart123sgames647
authored andcommitted
Handle table creations via MigrationManager
An entry to the migrations table must be added to keep track of the table's version. Fixes "premium" table creation on new databases.
1 parent e2dcc6a commit d075bc6

File tree

5 files changed

+48
-15
lines changed

5 files changed

+48
-15
lines changed

core/src/main/java/com/github/games647/fastlogin/core/shared/FastLoginCore.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,6 @@ public boolean setupDatabase() {
254254

255255
try {
256256
storage.createTables();
257-
storage.migrateTable();
258257
return true;
259258
} catch (Exception ex) {
260259
plugin.getLog().warn("Failed to setup database. Disabling plugin...", ex);

core/src/main/java/com/github/games647/fastlogin/core/storage/MigratableStorage.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
*/
2626
package com.github.games647.fastlogin.core.storage;
2727

28+
import java.sql.SQLException;
29+
2830
public interface MigratableStorage {
2931

3032
/**
@@ -34,4 +36,6 @@ public interface MigratableStorage {
3436

3537
String getTableName();
3638

39+
void createTable() throws SQLException;
40+
3741
}

core/src/main/java/com/github/games647/fastlogin/core/storage/MigrationManager.java

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ protected MigrationManager(FastLoginCore<?, ?, ?> core, SQLStorage storage) {
6969
this.storage = storage;
7070
}
7171

72-
protected void createTables() throws SQLException {
72+
private void createMigrationTable() throws SQLException {
7373
try (Connection con = storage.getDataSource().getConnection();
7474
Statement createStmt = con.createStatement()) {
7575

@@ -84,6 +84,29 @@ protected void createTables() throws SQLException {
8484
}
8585
}
8686

87+
/**
88+
* Initialize tables if they don't exist yet
89+
* @throws SQLException
90+
*/
91+
protected void createTables() throws SQLException {
92+
createMigrationTable();
93+
createTable(storage.getAuthStorage());
94+
}
95+
96+
/**
97+
* Create a new table in the database, and add an entry about it to MigrationManager
98+
* @param table the table to be created
99+
* @throws SQLException
100+
*/
101+
private void createTable(MigratableStorage table) throws SQLException {
102+
if (!tableExists(table)) {
103+
table.createTable();
104+
try (Connection con = storage.getDataSource().getConnection()) {
105+
insertMigration(con, table, table.getRequiredVersion());
106+
}
107+
}
108+
}
109+
87110
/**
88111
* Check the current version of a table stored in the connected database
89112
* @param table the table to check
@@ -106,7 +129,7 @@ protected int getCurrentTableVersion(MigratableStorage table) {
106129
}
107130

108131
// special case: table premium was created before the migration manager
109-
if (version == 0 && "premium".equals(table.getTableName()) && tableExists(table)) {
132+
if (version == 0 && "premium".equals(table.getTableName())) {
110133
version = 1;
111134
}
112135

@@ -119,18 +142,13 @@ protected void migrateTable(MigratableStorage table) {
119142
for (int i = initialVersion; i < table.getRequiredVersion(); i++) {
120143
core.getPlugin().getLog().info("Starting database migration of table {} to version {}",
121144
table.getTableName(), i + 1);
122-
try (Connection con = storage.getDataSource().getConnection();
123-
PreparedStatement saveStmt = con.prepareStatement(INSERT_MIGRATION)) {
145+
try (Connection con = storage.getDataSource().getConnection()) {
124146
for (String statement : getMigrationStatement(table, i)) {
125147
try (Statement migrateStmt = con.createStatement()) {
126148
migrateStmt.executeUpdate(statement);
127149
}
150+
insertMigration(con, table, i + 1);
128151
}
129-
130-
// add entry to migrations table
131-
saveStmt.setString(1, table.getTableName());
132-
saveStmt.setInt(2, i + 1);
133-
saveStmt.executeUpdate();
134152
} catch (SQLException sqlEx) {
135153
core.getPlugin().getLog().error("Failed to migrate table {} to version {}",
136154
table.getTableName(), i + 1, sqlEx);
@@ -141,6 +159,22 @@ protected void migrateTable(MigratableStorage table) {
141159
}
142160
}
143161

162+
/**
163+
* Add an entry to migrations table
164+
* @param con an open connection that was used to execute the migration statements
165+
* @param table the table that was migrated
166+
* @param newVersion the version the table was migrated to
167+
*/
168+
private void insertMigration(Connection con, MigratableStorage table, int newVersion) {
169+
try (PreparedStatement saveStmt = con.prepareStatement(INSERT_MIGRATION)) {
170+
saveStmt.setString(1, table.getTableName());
171+
saveStmt.setInt(2, newVersion);
172+
saveStmt.executeUpdate();
173+
} catch (SQLException e) {
174+
throw new RuntimeException(e);
175+
}
176+
}
177+
144178
/**
145179
* Get an SQL statement to migrate the table to the next version
146180
* @param table the database table to be migrated

core/src/main/java/com/github/games647/fastlogin/core/storage/SQLAuthStorage.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public SQLAuthStorage(FastLoginCore<?, ?, ?> core, SQLStorage storage) {
7676
this.storage = storage;
7777
}
7878

79-
protected void createTables() throws SQLException {
79+
public void createTable() throws SQLException {
8080
try (Connection con = storage.getDataSource().getConnection();
8181
Statement createStmt = con.createStatement()) {
8282

core/src/main/java/com/github/games647/fastlogin/core/storage/SQLStorage.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,6 @@ public SQLStorage(FastLoginCore<?, ?, ?> core, HikariConfig config) {
6262

6363
public void createTables() throws SQLException {
6464
migrationManager.createTables();
65-
authStorage.createTables();
66-
}
67-
68-
public void migrateTable() {
6965
migrationManager.migrateTable(authStorage);
7066
}
7167

0 commit comments

Comments
 (0)