Add Flyway, run db migration in local PostgreSQL db
First step in my plan to switch away from MySQL.
This commit is contained in:
@@ -12,6 +12,15 @@ public class ServerConfig {
|
||||
public String DB_PASS;
|
||||
public int INIT_CONNECTION_POOL_TIMEOUT;
|
||||
|
||||
// PostgreSQL database configuration
|
||||
public String PG_DB_NAME;
|
||||
public String PG_DB_HOST;
|
||||
public String PG_DB_SCHEMA;
|
||||
public String PG_DB_ADMIN_USERNAME;
|
||||
public String PG_DB_ADMIN_PASSWORD;
|
||||
public String PG_DB_USERNAME;
|
||||
public String PG_DB_PASSWORD;
|
||||
|
||||
//Login Configuration
|
||||
public int WORLDS;
|
||||
public int WLDLIST_SIZE;
|
||||
|
||||
30
src/main/java/database/PgDatabaseConfig.java
Normal file
30
src/main/java/database/PgDatabaseConfig.java
Normal file
@@ -0,0 +1,30 @@
|
||||
package database;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
public record PgDatabaseConfig(
|
||||
String databaseName, String host, String schema,
|
||||
String adminUsername, String adminPassword,
|
||||
String username, String password,
|
||||
Duration poolInitTimeout
|
||||
) {
|
||||
public PgDatabaseConfig {
|
||||
verifyNotBlank(databaseName);
|
||||
verifyNotBlank(host);
|
||||
verifyNotBlank(schema);
|
||||
verifyNotBlank(adminUsername);
|
||||
verifyNotBlank(adminPassword);
|
||||
verifyNotBlank(username);
|
||||
verifyNotBlank(password);
|
||||
}
|
||||
|
||||
private void verifyNotBlank(String value) {
|
||||
if (value == null || value.isBlank()) {
|
||||
throw new IllegalArgumentException("Missing or blank value in PG database config");
|
||||
}
|
||||
}
|
||||
|
||||
public String getJdbcUrl() {
|
||||
return "jdbc:postgresql://%s:5432/%s".formatted(host, databaseName);
|
||||
}
|
||||
}
|
||||
22
src/main/java/database/migration/FlywayRunner.java
Normal file
22
src/main/java/database/migration/FlywayRunner.java
Normal file
@@ -0,0 +1,22 @@
|
||||
package database.migration;
|
||||
|
||||
import database.PgDatabaseConfig;
|
||||
import org.flywaydb.core.Flyway;
|
||||
import org.flywaydb.core.api.FlywayException;
|
||||
|
||||
public class FlywayRunner {
|
||||
private final PgDatabaseConfig dbConfig;
|
||||
|
||||
public FlywayRunner(PgDatabaseConfig dbConfig) {
|
||||
this.dbConfig = dbConfig;
|
||||
}
|
||||
|
||||
public void migrate() throws FlywayException {
|
||||
Flyway flyway = Flyway.configure()
|
||||
.dataSource(dbConfig.getJdbcUrl(), dbConfig.adminUsername(), dbConfig.adminPassword())
|
||||
.schemas(dbConfig.schema())
|
||||
.createSchemas(true)
|
||||
.load();
|
||||
flyway.migrate();
|
||||
}
|
||||
}
|
||||
@@ -31,11 +31,14 @@ import client.inventory.ItemFactory;
|
||||
import client.inventory.manipulator.CashIdGenerator;
|
||||
import client.newyear.NewYearCardRecord;
|
||||
import client.processor.npc.FredrickProcessor;
|
||||
import config.ServerConfig;
|
||||
import config.YamlConfig;
|
||||
import constants.game.GameConstants;
|
||||
import constants.inventory.ItemConstants;
|
||||
import constants.net.OpcodeConstants;
|
||||
import constants.net.ServerConstants;
|
||||
import database.PgDatabaseConfig;
|
||||
import database.migration.FlywayRunner;
|
||||
import database.note.NoteDao;
|
||||
import net.ChannelDependencies;
|
||||
import net.PacketProcessor;
|
||||
@@ -840,6 +843,8 @@ public class Server {
|
||||
Runtime.getRuntime().addShutdownHook(new Thread(shutdown(false)));
|
||||
}
|
||||
|
||||
runDatabaseMigration();
|
||||
|
||||
if (!DatabaseConnection.initializeConnectionPool()) {
|
||||
throw new IllegalStateException("Failed to initiate a connection to the database");
|
||||
}
|
||||
@@ -922,6 +927,22 @@ public class Server {
|
||||
}
|
||||
}
|
||||
|
||||
private void runDatabaseMigration() {
|
||||
PgDatabaseConfig pgDbConfig = readPgDbConfig();
|
||||
FlywayRunner flywayRunner = new FlywayRunner(pgDbConfig);
|
||||
flywayRunner.migrate();
|
||||
}
|
||||
|
||||
private PgDatabaseConfig readPgDbConfig() {
|
||||
final ServerConfig serverConfig = YamlConfig.config.server;
|
||||
return new PgDatabaseConfig(
|
||||
serverConfig.PG_DB_NAME, serverConfig.PG_DB_HOST, serverConfig.PG_DB_SCHEMA,
|
||||
serverConfig.PG_DB_ADMIN_USERNAME, serverConfig.PG_DB_ADMIN_PASSWORD,
|
||||
serverConfig.PG_DB_USERNAME, serverConfig.PG_DB_PASSWORD,
|
||||
Duration.ofSeconds(serverConfig.INIT_CONNECTION_POOL_TIMEOUT)
|
||||
);
|
||||
}
|
||||
|
||||
private ChannelDependencies registerChannelDependencies() {
|
||||
NoteService noteService = new NoteService(new NoteDao());
|
||||
FredrickProcessor fredrickProcessor = new FredrickProcessor(noteService);
|
||||
|
||||
Reference in New Issue
Block a user