move remaining mysql class to mysql module

This commit is contained in:
wea_ondara
2022-11-23 17:15:53 +01:00
parent 99b49083ec
commit 802288e6ab
7 changed files with 98 additions and 40 deletions

View File

@@ -1,157 +0,0 @@
package jef.mysql.migration;
import jef.Database;
import jef.DatabaseOptions;
import jef.DbSet;
import jef.model.DbContext;
import jef.model.DbContextOptions;
import jef.model.annotations.Clazz;
import jef.model.annotations.ForeignKey;
import jef.mysql.MysqlDatabase;
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.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 db = new MysqlDatabase(conn, dboptions);
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 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");
} 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",
"-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(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;
}
}