create migrations table via built-in migration

This commit is contained in:
wea_ondara
2022-11-26 06:06:51 +01:00
parent 913df99732
commit 2910b2c282
8 changed files with 95 additions and 32 deletions

View File

@@ -1,12 +1,8 @@
package jef.platform.mysql.migration;
import jef.platform.base.DatabaseOptions;
import jef.model.migration.Migration;
import jef.model.migration.operation.AddFieldOperation;
import jef.model.migration.operation.AddTableOperation;
import jef.model.migration.operation.AddUniqueKeyOperation;
import jef.model.migration.operation.MigrationOperation;
import jef.platform.SqlPlatform;
import jef.platform.base.DatabaseOptions;
import jef.platform.base.migration.MigrationApplier;
import java.sql.Connection;
@@ -21,7 +17,7 @@ public class MysqlMigrationApplier extends MigrationApplier {
@Override
protected void insertMigrationLog(Migration m) throws SQLException {
try (var stmt = connection.prepareStatement("INSERT INTO `__jef_migration_log` (`name`, `version`) VALUES (?, ?)")) {//TODO configurable log table name
try (var stmt = connection.prepareStatement("INSERT INTO `" + options.getMigrationsTableName() + "` (`name`, `version`) VALUES (?, ?)")) {//TODO configurable log table name
stmt.setString(1, m.getClass().getSimpleName());
stmt.setString(2, "0.1"); //TODO insert actual library version
stmt.executeUpdate();
@@ -33,7 +29,7 @@ public class MysqlMigrationApplier extends MigrationApplier {
try (var stmt = connection.prepareStatement("SHOW TABLES");
var res = stmt.executeQuery()) {
while (res.next()) {
if (res.getString(1).equals("__jef_migration_log")) {
if (res.getString(1).equals(options.getMigrationsTableName())) {
return true;
}
}
@@ -41,23 +37,6 @@ public class MysqlMigrationApplier extends MigrationApplier {
return false;
}
@Override
protected void createMigrationsTable() throws SQLException {
var table = "__jef_migration_log";
var ops = List.of(
new AddTableOperation.Builder(table, List.of(
new AddFieldOperation.Builder(table, "name").notNull(true).sqlType("VARCHAR(255)"),
new AddFieldOperation.Builder(table, "version").notNull(true).sqlType("VARCHAR(255)")
)),
new AddUniqueKeyOperation.Builder("U_" + table + "_name", table, List.of("name"))
);
for (MigrationOperation.Builder<? extends MigrationOperation> e : ops) {
try (var stmt = sqlPlatform.getTranslator().translate(connection, e.build())) {
stmt.executeUpdate();
}
}
}
@Override
protected List<String> getAppliedMigrations() throws SQLException {
var ret = new ArrayList<String>();
@@ -65,11 +44,11 @@ public class MysqlMigrationApplier extends MigrationApplier {
return ret;
}
try (var stmt = connection.prepareStatement("SELECT `name` FROM `__jef_migration_log`");
try (var stmt = connection.prepareStatement("SELECT `migration` FROM `" + options.getMigrationsTableName() + "`");
var res = stmt.executeQuery()) {
while (res.next()) {
if (res.getString(1).equals("__jef_migration_log")) {
ret.add(res.getString("name"));
if (!res.getString(1).equals(options.getMigrationsTableName())) {
ret.add(res.getString("migration"));
}
}
}

View File

@@ -46,7 +46,7 @@ public class MysqlMigrationTest {
generateInitialMigration();
compileInitialMigration();
Class.forName("com.mysql.cj.jdbc.Driver").getDeclaredConstructor().newInstance();
var dboptions = new DatabaseOptions("jdbc:mysql://localhost/test", "test", "password", getClass().getSimpleName());
var dboptions = new DatabaseOptions("jdbc:mysql://localhost/test", "test", "password", getClass().getSimpleName(), null);
var ctxoptions = new DbContextOptions(dboptions);
var conn = DriverManager.getConnection(dboptions.getUrl(), dboptions.getUser(), dboptions.getPassword());
var sqlPlatform = new MysqlPlatform();