move remaining mysql class to mysql module
This commit is contained in:
@@ -1,19 +1,20 @@
|
||||
package jef.mysql;
|
||||
package jef.platform.mysql;
|
||||
|
||||
import jef.Database;
|
||||
import jef.DatabaseOptions;
|
||||
import jef.MigrationException;
|
||||
import jef.mysql.migration.MysqlMigrationApplier;
|
||||
import jef.platform.base.Database;
|
||||
import jef.platform.base.DatabaseOptions;
|
||||
|
||||
import java.sql.Connection;
|
||||
|
||||
public class MysqlDatabase extends Database {
|
||||
public MysqlDatabase(Connection connection, DatabaseOptions options) {
|
||||
private final MysqlPlatform platform;
|
||||
public MysqlDatabase(Connection connection, DatabaseOptions options, MysqlPlatform platform) {
|
||||
super(connection, options);
|
||||
this.platform = platform;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void migrate() throws MigrationException {
|
||||
new MysqlMigrationApplier(connection, options).migrate();
|
||||
platform.getMigrationApplier(connection, options).migrate();
|
||||
}
|
||||
}
|
||||
27
mysql/src/main/java/jef/platform/mysql/MysqlPlatform.java
Normal file
27
mysql/src/main/java/jef/platform/mysql/MysqlPlatform.java
Normal file
@@ -0,0 +1,27 @@
|
||||
package jef.platform.mysql;
|
||||
|
||||
import jef.platform.SqlPlatform;
|
||||
import jef.platform.base.DatabaseOptions;
|
||||
import jef.platform.base.SqlTypeMapper;
|
||||
import jef.platform.base.migration.MigrationApplier;
|
||||
import jef.platform.mysql.migration.MysqlMigrationApplier;
|
||||
import jef.platform.mysql.migration.MysqlMigrationOperationTranslator;
|
||||
|
||||
import java.sql.Connection;
|
||||
|
||||
public class MysqlPlatform implements SqlPlatform {
|
||||
@Override
|
||||
public MysqlMigrationOperationTranslator getTranslator() {
|
||||
return new MysqlMigrationOperationTranslator();
|
||||
}
|
||||
|
||||
@Override
|
||||
public SqlTypeMapper getTypeMapper() {
|
||||
return new MysqlTypeMapper();
|
||||
}
|
||||
|
||||
@Override
|
||||
public MigrationApplier getMigrationApplier(Connection connection, DatabaseOptions options) {
|
||||
return new MysqlMigrationApplier(connection, options, this);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package jef.platform.mysql;
|
||||
|
||||
import jef.platform.base.SqlTypeMapper;
|
||||
|
||||
public class MysqlTypeMapper extends SqlTypeMapper {
|
||||
|
||||
}
|
||||
@@ -1,11 +1,13 @@
|
||||
package jef.mysql.migration;
|
||||
package jef.platform.mysql.migration;
|
||||
|
||||
import jef.DatabaseOptions;
|
||||
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.migration.MigrationApplier;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
@@ -13,8 +15,8 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class MysqlMigrationApplier extends MigrationApplier {
|
||||
public MysqlMigrationApplier(Connection connection, DatabaseOptions options) {
|
||||
super(connection, options);
|
||||
public MysqlMigrationApplier(Connection connection, DatabaseOptions options, SqlPlatform sqlPlatform) {
|
||||
super(connection, options, sqlPlatform);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -50,7 +52,7 @@ public class MysqlMigrationApplier extends MigrationApplier {
|
||||
new AddUniqueKeyOperation.Builder("U_" + table + "_name", table, List.of("name"))
|
||||
);
|
||||
for (MigrationOperation.Builder<? extends MigrationOperation> e : ops) {
|
||||
try (var stmt = MysqlMigrationOperationTranslator.translate(connection, e.build())) {
|
||||
try (var stmt = sqlPlatform.getTranslator().translate(connection, e.build())) {
|
||||
stmt.executeUpdate();
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package jef.mysql.migration;
|
||||
package jef.platform.mysql.migration;
|
||||
|
||||
import jef.model.migration.operation.AddFieldOperation;
|
||||
import jef.model.migration.operation.AddForeignKeyOperation;
|
||||
@@ -14,6 +14,7 @@ import jef.model.migration.operation.MigrationOperation;
|
||||
import jef.model.migration.operation.RenameFieldOperation;
|
||||
import jef.model.migration.operation.RenameTableOperation;
|
||||
import jef.model.migration.operation.UpdateFieldOperation;
|
||||
import jef.platform.base.MigrationOperationTranslator;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
@@ -24,7 +25,7 @@ import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class MysqlMigrationOperationTranslator {
|
||||
public class MysqlMigrationOperationTranslator implements MigrationOperationTranslator {
|
||||
private static final Map<Class<? extends MigrationOperation>, Mapper<MigrationOperation>> translators;
|
||||
|
||||
static {
|
||||
@@ -45,10 +46,14 @@ public class MysqlMigrationOperationTranslator {
|
||||
translators = Collections.unmodifiableMap(map);
|
||||
}
|
||||
|
||||
public static PreparedStatement translate(Connection connection, MigrationOperation operation) throws SQLException {
|
||||
public static PreparedStatement translateS(Connection connection, MigrationOperation operation) throws SQLException {
|
||||
return Optional.ofNullable(translators.get(operation.getClass())).orElseThrow().apply(connection, operation);
|
||||
}
|
||||
|
||||
public PreparedStatement translate(Connection connection, MigrationOperation operation) throws SQLException {
|
||||
return translateS(connection, operation);
|
||||
}
|
||||
|
||||
private static PreparedStatement translateAddFieldOperation(Connection connection, MigrationOperation operation) throws SQLException {
|
||||
AddFieldOperation op = (AddFieldOperation) operation;
|
||||
return connection.prepareStatement("ALTER TABLE `" + op.getTable() + "`"
|
||||
@@ -0,0 +1,168 @@
|
||||
package jef.platform.mysql.migration;
|
||||
|
||||
import jef.DbSet;
|
||||
import jef.model.DbContext;
|
||||
import jef.model.DbContextOptions;
|
||||
import jef.model.ModelBuilder;
|
||||
import jef.model.annotations.Clazz;
|
||||
import jef.model.annotations.ForeignKey;
|
||||
import jef.model.migration.creator.MigrationCreator;
|
||||
import jef.platform.base.Database;
|
||||
import jef.platform.base.DatabaseOptions;
|
||||
import jef.platform.mysql.MysqlDatabase;
|
||||
import jef.platform.mysql.MysqlPlatform;
|
||||
import jef.serializable.SerializableObject;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.OpenOption;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
import java.sql.DriverManager;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class MysqlMigrationTest {
|
||||
private static final String GENERATE_MIGRATIONS_FOLDER = "target/test-generated-migrations/";
|
||||
private static final String GENERATE_MIGRATIONS_FOLDER_SRC = GENERATE_MIGRATIONS_FOLDER + "src/";
|
||||
private static final String GENERATE_MIGRATIONS_FOLDER_TARGET = GENERATE_MIGRATIONS_FOLDER + "target/";
|
||||
|
||||
@Test
|
||||
public void test() throws Exception {
|
||||
clearMigrationFolders();
|
||||
generateInitialMigration();
|
||||
compileInitialMigration();
|
||||
Class.forName("com.mysql.cj.jdbc.Driver").getDeclaredConstructor().newInstance();
|
||||
var dboptions = new DatabaseOptions("jdbc:mysql://localhost/test", "test", "password", getClass().getSimpleName());
|
||||
var ctxoptions = new DbContextOptions(dboptions);
|
||||
var conn = DriverManager.getConnection(dboptions.getUrl(), dboptions.getUser(), dboptions.getPassword());
|
||||
var sqlPlatform = new MysqlPlatform();
|
||||
var db = new MysqlDatabase(conn, dboptions, sqlPlatform);
|
||||
var ctx = new Ctx(db, ctxoptions);
|
||||
ctx.getDatabase().migrate();
|
||||
var result = ctx.getCompanies().filter(e -> e.getName().equals("foobar")).toString();
|
||||
System.out.println(result);
|
||||
}
|
||||
|
||||
private void clearMigrationFolders() {
|
||||
delRecursive(new File(GENERATE_MIGRATIONS_FOLDER_SRC + "MysqlMigrationTest"));
|
||||
delRecursive(new File(GENERATE_MIGRATIONS_FOLDER_TARGET + "MysqlMigrationTest"));
|
||||
}
|
||||
|
||||
private void delRecursive(File f) {
|
||||
if (f.isDirectory()) {
|
||||
Arrays.stream(Objects.requireNonNull(f.listFiles()))
|
||||
.filter(e -> !List.of(".", "..").contains(e.getName()))
|
||||
.forEach(this::delRecursive);
|
||||
}
|
||||
f.delete();
|
||||
}
|
||||
|
||||
private void generateInitialMigration() {
|
||||
try {
|
||||
var sqlPlatform = new MysqlPlatform();
|
||||
var from = new ModelBuilder();
|
||||
var to = ModelBuilder.from(Ctx.class);
|
||||
var result = new MigrationCreator(sqlPlatform, from, to, "Initial", "MysqlMigrationTest", null).createMigration();
|
||||
|
||||
var dir = Path.of(GENERATE_MIGRATIONS_FOLDER_SRC, "MysqlMigrationTest").toFile();
|
||||
dir.mkdirs();
|
||||
|
||||
var fileOptions = new OpenOption[]{StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE};
|
||||
Files.writeString(Path.of(dir.toString(), result.getMigrationClassName() + ".java"), result.getMigration(), fileOptions);
|
||||
Files.writeString(Path.of(dir.toString(), result.getMigrationSnapshotClassName() + ".java"), result.getMigrationSnapshot(), fileOptions);
|
||||
Files.writeString(Path.of(dir.toString(), result.getCurrentSnapshotClassName() + ".java"), result.getCurrentSnapshot(), fileOptions);
|
||||
} catch (AssertionError | RuntimeException e) {
|
||||
throw e;
|
||||
} catch (Throwable t) {
|
||||
throw new RuntimeException("Error while compiling generated class", t);
|
||||
}
|
||||
}
|
||||
|
||||
private void compileInitialMigration() {
|
||||
try {
|
||||
var javaHome = System.getProperty("java.home");
|
||||
var isWindows = System.getProperty("os.name").toLowerCase(Locale.ROOT).equals("win");
|
||||
var javac = new File(javaHome, "bin/javac" + (isWindows ? ".exe" : ""));
|
||||
var params = new ArrayList<>(List.of(
|
||||
javac.getAbsolutePath(),
|
||||
"-cp", "target/classes" + File.pathSeparator + "target/test-classes" + File.pathSeparator + "../core/target/classes",
|
||||
"-encoding", "UTF8",
|
||||
"-g", //debug symbols
|
||||
"-d", GENERATE_MIGRATIONS_FOLDER_TARGET //target
|
||||
));
|
||||
Arrays.stream(new File(GENERATE_MIGRATIONS_FOLDER_SRC + "MysqlMigrationTest").listFiles(File::isFile)).map(File::getPath).forEach(params::add);
|
||||
var process = new ProcessBuilder()
|
||||
.command(params.toArray(String[]::new))
|
||||
.inheritIO()
|
||||
.start();
|
||||
process.waitFor(10, TimeUnit.SECONDS);
|
||||
var exitCode = process.exitValue();
|
||||
assertEquals(0, exitCode, "Initial migration compilation failed");
|
||||
} catch (AssertionError | RuntimeException e) {
|
||||
throw e;
|
||||
} catch (Throwable t) {
|
||||
throw new RuntimeException("Error while compiling generated class", t);
|
||||
}
|
||||
}
|
||||
|
||||
@Getter
|
||||
public static class Ctx extends DbContext {
|
||||
@Clazz(Company.class)
|
||||
private DbSet<Company> companies;
|
||||
@Clazz(Employee.class)
|
||||
private DbSet<Employee> employees;
|
||||
|
||||
public Ctx() {
|
||||
}
|
||||
|
||||
public Ctx(Database database, DbContextOptions options) {
|
||||
super(database, options);
|
||||
}
|
||||
}
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@ToString
|
||||
@EqualsAndHashCode(callSuper = false)
|
||||
public static class Company extends SerializableObject {
|
||||
private int id;
|
||||
private String name;
|
||||
|
||||
private Employee ceo;
|
||||
}
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@ToString
|
||||
@EqualsAndHashCode(callSuper = false)
|
||||
public static class Employee extends SerializableObject {
|
||||
private int id;
|
||||
private String name;
|
||||
|
||||
private Employee boss;
|
||||
@ForeignKey(getterOrField = "boss")
|
||||
private int bossId;
|
||||
|
||||
private Company company;
|
||||
@ForeignKey(getterOrField = "company")
|
||||
private int companyId;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user