move remaining mysql class to mysql module
This commit is contained in:
@@ -10,14 +10,6 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>mysql</artifactId>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>jef</groupId>
|
||||
<artifactId>core</artifactId>
|
||||
<version>0.1</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
@@ -25,4 +17,17 @@
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>jef</groupId>
|
||||
<artifactId>core</artifactId>
|
||||
<version>${project.parent.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>jef</groupId>
|
||||
<artifactId>migration-creator</artifactId>
|
||||
<version>${project.parent.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -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() + "`"
|
||||
@@ -1,13 +1,16 @@
|
||||
package jef.mysql.migration;
|
||||
package jef.platform.mysql.migration;
|
||||
|
||||
import jef.Database;
|
||||
import jef.DatabaseOptions;
|
||||
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.mysql.MysqlDatabase;
|
||||
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;
|
||||
@@ -18,6 +21,10 @@ 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;
|
||||
@@ -42,7 +49,8 @@ public class MysqlMigrationTest {
|
||||
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 db = new MysqlDatabase(conn, dboptions);
|
||||
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();
|
||||
@@ -65,18 +73,18 @@ public class MysqlMigrationTest {
|
||||
|
||||
private void generateInitialMigration() {
|
||||
try {
|
||||
var javaHome = System.getProperty("java.home");
|
||||
var isWindows = System.getProperty("os.name").toLowerCase(Locale.ROOT).equals("win");
|
||||
var java = new File(javaHome, "bin/java" + (isWindows ? ".exe" : ""));
|
||||
var process = new ProcessBuilder()
|
||||
.command(java.getAbsolutePath(),
|
||||
"-cp", "target/classes", "jef.main.Main",
|
||||
"migration", "add", "--cp", "target/test-classes", "-c", "MysqlMigrationTest$Ctx", "-o", GENERATE_MIGRATIONS_FOLDER_SRC + "MysqlMigrationTest", "Initial")
|
||||
.inheritIO()
|
||||
.start();
|
||||
process.waitFor();
|
||||
var exitCode = process.exitValue();
|
||||
assertEquals(0, exitCode, "Initial migration generation failed");
|
||||
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) {
|
||||
@@ -91,7 +99,7 @@ public class MysqlMigrationTest {
|
||||
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",
|
||||
"-cp", "target/classes" + File.pathSeparator + "target/test-classes" + File.pathSeparator + "../core/target/classes",
|
||||
"-encoding", "UTF8",
|
||||
"-g", //debug symbols
|
||||
"-d", GENERATE_MIGRATIONS_FOLDER_TARGET //target
|
||||
@@ -118,6 +126,9 @@ public class MysqlMigrationTest {
|
||||
@Clazz(Employee.class)
|
||||
private DbSet<Employee> employees;
|
||||
|
||||
public Ctx() {
|
||||
}
|
||||
|
||||
public Ctx(Database database, DbContextOptions options) {
|
||||
super(database, options);
|
||||
}
|
||||
Reference in New Issue
Block a user