新增:Linux Docker 部署与自动回收、备份及容量监测

This commit is contained in:
2026-11-10 14:53:07 +08:00
parent 3962521a5c
commit dc5b9b76ec
45 changed files with 5591 additions and 35 deletions
@@ -39,7 +39,7 @@ class AnalysisExecutionProtocolStoreMySqlConcurrencyTest {
assertTrue(baseUrl.contains("/?"), "test server URL must have an empty database path and query parameters");
admin = new JdbcTemplate(new DriverManagerDataSource(baseUrl, user, password));
database = "quality_protocol_test_" + UUID.randomUUID().toString().replace("-", "");
admin.execute("CREATE DATABASE `" + database + "` CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci");
cn.yunsheng.quality.testsupport.OwnedTestDatabase.create(admin.getDataSource(),database);
created = true;
dataSource = new DriverManagerDataSource(baseUrl.replaceFirst("/\\?", "/" + database + "?"), user, password);
jdbc = new JdbcTemplate(dataSource);
@@ -52,7 +52,7 @@ class AnalysisExecutionProtocolStoreMySqlConcurrencyTest {
@AfterEach void cleanup() {
if (created && admin != null && database != null && database.matches("quality_protocol_test_[0-9a-f]{32}")) {
admin.execute("DROP DATABASE IF EXISTS `" + database + "`");
cn.yunsheng.quality.testsupport.OwnedTestDatabase.drop(admin.getDataSource(),database);
}
}
@@ -41,7 +41,7 @@ class AnalysisProtocolServiceMySqlTest {
String user=System.getenv("QUALITY_PROTOCOL_TEST_MYSQL_USER"),password=System.getenv("QUALITY_PROTOCOL_TEST_MYSQL_PASSWORD");
admin=new JdbcTemplate(new DriverManagerDataSource(url,user,password));
database="quality_protocol_test_"+UUID.randomUUID().toString().replace("-","");
admin.execute("CREATE DATABASE `"+database+"` CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci");created=true;
cn.yunsheng.quality.testsupport.OwnedTestDatabase.create(admin.getDataSource(),database);created=true;
var ds=new DriverManagerDataSource(url.replaceFirst("/\\?","/"+database+"?"),user,password);
jdbc=new JdbcTemplate(ds);tx=new TransactionTemplate(new DataSourceTransactionManager(ds));
jdbc.execute("CREATE TABLE analysis_session(id CHAR(36) PRIMARY KEY,user_id CHAR(36),release_id CHAR(36)) ENGINE=InnoDB");
@@ -67,7 +67,7 @@ class AnalysisProtocolServiceMySqlTest {
jdbc.update("INSERT INTO analysis_session VALUES(?,?,?)",session,actor.getId().toString(),release);
tx.executeWithoutResult(s->service.initialize(session,Map.of("releaseId",release,"year",2026)));
}
@AfterEach void cleanup(){if(created&&database.matches("quality_protocol_test_[0-9a-f]{32}"))admin.execute("DROP DATABASE `"+database+"`");}
@AfterEach void cleanup(){if(created&&database.matches("quality_protocol_test_[0-9a-f]{32}"))cn.yunsheng.quality.testsupport.OwnedTestDatabase.drop(admin.getDataSource(),database);}
@Test void clarificationTrendClearAndCancellationUseOneAuthority() {
String first=newTurn("今年18×15×4综合合格率变化趋势");var a=claim(first,false);
@@ -48,7 +48,7 @@ class AnalysisProtocolTurnBindingMySqlTest {
String password = System.getenv("QUALITY_PROTOCOL_TEST_MYSQL_PASSWORD");
admin = new JdbcTemplate(new DriverManagerDataSource(url, dbUser, password));
database = "quality_protocol_test_" + UUID.randomUUID().toString().replace("-", "");
admin.execute("CREATE DATABASE `" + database + "` CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci");
cn.yunsheng.quality.testsupport.OwnedTestDatabase.create(admin.getDataSource(),database);
created = true;
var dataSource = new DriverManagerDataSource(url.replaceFirst("/\\?", "/" + database + "?"), dbUser, password);
jdbc = new JdbcTemplate(dataSource);
@@ -73,7 +73,7 @@ class AnalysisProtocolTurnBindingMySqlTest {
@AfterEach void cleanup() {
if (created && database != null && database.matches("quality_protocol_test_[0-9a-f]{32}"))
admin.execute("DROP DATABASE IF EXISTS `" + database + "`");
cn.yunsheng.quality.testsupport.OwnedTestDatabase.drop(admin.getDataSource(),database);
}
private AnalysisExecutionProtocolStore.Execution start(String turnId, String userId, String key) {
@@ -36,7 +36,7 @@ class FineDefectRollupsMySqlTest {
String user = System.getenv("QUALITY_SOURCE_COLUMN_TEST_MYSQL_USER"), password = System.getenv("QUALITY_SOURCE_COLUMN_TEST_MYSQL_PASSWORD");
admin = new JdbcTemplate(new DriverManagerDataSource(url, user, password));
database = "quality_fine_rollup_" + UUID.randomUUID().toString().replace("-", "");
admin.execute("CREATE DATABASE " + database + " CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci");
cn.yunsheng.quality.testsupport.OwnedTestDatabase.create(admin.getDataSource(),database);
jdbc = spy(new JdbcTemplate(new DriverManagerDataSource(url.replaceFirst("/\\?", "/" + database + "?"), user, password)));
jdbc.execute("CREATE TABLE transform_build(id CHAR(36) PRIMARY KEY,source_revision_id CHAR(36))");
jdbc.execute("CREATE TABLE source_revision(id CHAR(36) PRIMARY KEY)");
@@ -161,7 +161,7 @@ class FineDefectRollupsMySqlTest {
}
@AfterEach void cleanup() {
if (admin != null && database != null && database.matches("quality_fine_rollup_[a-f0-9]{32}")) admin.execute("DROP DATABASE " + database);
if (admin != null && database != null && database.matches("quality_fine_rollup_[a-f0-9]{32}")) cn.yunsheng.quality.testsupport.OwnedTestDatabase.drop(admin.getDataSource(),database);
}
@Test
@@ -26,7 +26,7 @@ class SalesOrderTypesMySqlTest {
assertTrue(url.startsWith("jdbc:mysql://127.0.0.1:")&&url.contains("/?"));
String user=System.getenv("QUALITY_SOURCE_COLUMN_TEST_MYSQL_USER"),password=System.getenv("QUALITY_SOURCE_COLUMN_TEST_MYSQL_PASSWORD");
admin=new JdbcTemplate(new DriverManagerDataSource(url,user,password));database="quality_sales_type_test_"+UUID.randomUUID().toString().replace("-","");
admin.execute("CREATE DATABASE "+database+" CHARACTER SET utf8mb4");
cn.yunsheng.quality.testsupport.OwnedTestDatabase.create(admin.getDataSource(),database);
var source=new DriverManagerDataSource(url.replaceFirst("/\\?","/"+database+"?"),user,password);jdbc=new JdbcTemplate(source);
try(var connection=source.getConnection()){ScriptUtils.executeSqlScript(connection,new ClassPathResource("db/migration/V39__sales_order_types.sql"));}
jdbc.execute("CREATE TABLE query_filter_field(field_key VARCHAR(64),label VARCHAR(80),black_column VARCHAR(160),bright_column VARCHAR(160),enabled BOOLEAN,allow_empty BOOLEAN,sort_order INT)");
@@ -41,7 +41,7 @@ class SalesOrderTypesMySqlTest {
jdbc.update("INSERT INTO fact_black(id,build_id,source_revision_id,department,group_name,raw_values) VALUES(1,'black-build','black-rev','部门A','组A',JSON_OBJECT('销售订单号','0001')),(2,'black-build','black-rev','部门A','组A',JSON_OBJECT('销售订单号','absent')),(3,'black-build','black-rev','部门A','组A',JSON_OBJECT('销售订单号',NULL))");
jdbc.update("INSERT INTO fact_bright(id,build_id,source_revision_id,department,group_name,raw_values) VALUES(4,'bright-build','bright-rev','部门A','组A',JSON_OBJECT('销售订单','0001'))");
}
@AfterEach void cleanup(){RequestContextHolder.resetRequestAttributes();if(database!=null)admin.execute("DROP DATABASE "+database);}
@AfterEach void cleanup(){RequestContextHolder.resetRequestAttributes();if(database!=null)cn.yunsheng.quality.testsupport.OwnedTestDatabase.drop(admin.getDataSource(),database);}
Map<String,Object> load(String key,String type,String mode) throws Exception {
var service=new SalesOrderTypeService(jdbc);var file=SalesOrderTypeServiceTest.workbook(new String[]{key,type});var preview=service.preview(file,mode);
return service.importFile(file,mode,(String)preview.get("expectedVersion"),(String)preview.get("sha256"),actor);
@@ -44,7 +44,7 @@ class SourceColumnQueriesMySqlTest {
String password = System.getenv("QUALITY_SOURCE_COLUMN_TEST_MYSQL_PASSWORD");
admin = new JdbcTemplate(new DriverManagerDataSource(url, user, password));
database = "quality_source_column_test_" + UUID.randomUUID().toString().replace("-", "");
admin.execute("CREATE DATABASE " + database + " CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci");
cn.yunsheng.quality.testsupport.OwnedTestDatabase.create(admin.getDataSource(),database);
created = true;
var dataSource = new DriverManagerDataSource(url.replaceFirst("/\\?", "/" + database + "?"), user, password);
jdbc = new JdbcTemplate(dataSource);
@@ -57,7 +57,7 @@ class SourceColumnQueriesMySqlTest {
@AfterEach void cleanup() {
if (created && database != null && database.matches("quality_source_column_test_[0-9a-f]{32}"))
admin.execute("DROP DATABASE " + database);
cn.yunsheng.quality.testsupport.OwnedTestDatabase.drop(admin.getDataSource(),database);
}
@Test void catalogIsScopedAuthorizedAndSupportsThirdAndSingleStageColumns() {
@@ -18,7 +18,7 @@ class ClassificationPreviewMySqlTest {
@BeforeEach void create(){
String url=System.getenv("QUALITY_SOURCE_COLUMN_TEST_MYSQL_SERVER_URL");assertTrue(url.startsWith("jdbc:mysql://127.0.0.1:")&&url.contains("/?"));
String user=System.getenv("QUALITY_SOURCE_COLUMN_TEST_MYSQL_USER"),password=System.getenv("QUALITY_SOURCE_COLUMN_TEST_MYSQL_PASSWORD");
admin=new JdbcTemplate(new DriverManagerDataSource(url,user,password));database="quality_classification_preview_"+UUID.randomUUID().toString().replace("-","");admin.execute("CREATE DATABASE "+database+" CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci");
admin=new JdbcTemplate(new DriverManagerDataSource(url,user,password));database="quality_classification_preview_"+UUID.randomUUID().toString().replace("-","");cn.yunsheng.quality.testsupport.OwnedTestDatabase.create(admin.getDataSource(),database);
jdbc=new JdbcTemplate(new DriverManagerDataSource(url.replaceFirst("/\\?","/"+database+"?"),user,password));
jdbc.execute("CREATE TABLE current_release(dataset_id VARCHAR(20),release_id VARCHAR(20))");jdbc.update("INSERT INTO current_release VALUES('quality','r')");
jdbc.execute("CREATE TABLE release_member(release_id VARCHAR(20),transform_build_id VARCHAR(20))");jdbc.update("INSERT INTO release_member VALUES('r','b'),('r','l')");
@@ -31,7 +31,7 @@ class ClassificationPreviewMySqlTest {
seed("bright","l","A","R","小写区域","2026-04","0001","x",200);
service=new ClassificationPreviewService(new GovernanceSupport(jdbc,JsonMapper.builder().build()),mock(CandidateAcceptanceService.class));
}
@AfterEach void cleanup(){if(database!=null&&database.matches("quality_classification_preview_[a-f0-9]{32}"))admin.execute("DROP DATABASE "+database);}
@AfterEach void cleanup(){if(database!=null&&database.matches("quality_classification_preview_[a-f0-9]{32}"))cn.yunsheng.quality.testsupport.OwnedTestDatabase.drop(admin.getDataSource(),database);}
private void seed(String stage,String build,String department,String product,String group,String month,String code,String region,int weight){
Map<String,Object> components=new HashMap<>();components.put("sourcePdtCode",code);components.put("sourceSalesRegion",region);
jdbc.update("INSERT INTO fact_"+stage+" VALUES(?,?,?,?,?,?,CAST(? AS JSON))",build,department,product,group,month,weight,JsonMapper.builder().build().writeValueAsString(components));
@@ -19,7 +19,7 @@ class ClassificationSourceMySqlTest {
@BeforeEach void create(){
String url=System.getenv("QUALITY_SOURCE_COLUMN_TEST_MYSQL_SERVER_URL");assertTrue(url.startsWith("jdbc:mysql://127.0.0.1:")&&url.contains("/?"));
String user=System.getenv("QUALITY_SOURCE_COLUMN_TEST_MYSQL_USER"),password=System.getenv("QUALITY_SOURCE_COLUMN_TEST_MYSQL_PASSWORD");
admin=new JdbcTemplate(new DriverManagerDataSource(url,user,password));database="quality_classification_source_"+UUID.randomUUID().toString().replace("-","");admin.execute("CREATE DATABASE "+database+" CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci");
admin=new JdbcTemplate(new DriverManagerDataSource(url,user,password));database="quality_classification_source_"+UUID.randomUUID().toString().replace("-","");cn.yunsheng.quality.testsupport.OwnedTestDatabase.create(admin.getDataSource(),database);
jdbc=new JdbcTemplate(new DriverManagerDataSource(url.replaceFirst("/\\?","/"+database+"?"),user,password));
jdbc.execute("CREATE TABLE current_release(dataset_id VARCHAR(20),release_id VARCHAR(20))");jdbc.update("INSERT INTO current_release VALUES('quality','r')");
jdbc.execute("CREATE TABLE data_release(id VARCHAR(20),status VARCHAR(20))");jdbc.update("INSERT INTO data_release VALUES('r','PUBLISHED'),('new','PUBLISHED')");
@@ -32,7 +32,7 @@ class ClassificationSourceMySqlTest {
access=mock(CandidateAcceptanceService.class);fields=mock(GovernanceAccessService.class);when(fields.allowedFields(actor)).thenReturn(Set.of("客户"));
service=new ClassificationSourceService(new GovernanceSupport(jdbc,JsonMapper.builder().build()),access,fields);
}
@AfterEach void cleanup(){if(database!=null&&database.matches("quality_classification_source_[a-f0-9]{32}"))admin.execute("DROP DATABASE "+database);}
@AfterEach void cleanup(){if(database!=null&&database.matches("quality_classification_source_[a-f0-9]{32}"))cn.yunsheng.quality.testsupport.OwnedTestDatabase.drop(admin.getDataSource(),database);}
@Test void exactSourceRowsPaginateFifteenAndStayPinnedWithoutPreviewOrTarget(){
var first=service.list(new ClassificationSourceService.Request("black",conditions,1,15,null),actor);
assertEquals(18,first.get("total"));var rows=(List<?>)first.get("rows");assertEquals(15,rows.size());var row=(Map<?,?>)rows.getFirst();assertEquals("原始数据.xlsx",row.get("sourceFile"));assertEquals(1,row.get("rowNumber"));assertEquals(Map.of("客户","原始客户"),row.get("raw"));
@@ -21,7 +21,7 @@ class UnclassifiedMappingRebuildMySqlTest {
Map<String,Object> pending(){return Map.of("id","classification-test","name","区域补分类","enabled",true,"onlyUnclassified",true,"priority",-9900,"stage","all","target","group","value","G","conditions",List.of(Map.of("field","@department","operator","EQUALS","value","A"),Map.of("field","销售片区","operator","EQUALS","value","X")));}
@BeforeEach void setup()throws Exception{
String url=System.getenv("QUALITY_SOURCE_COLUMN_TEST_MYSQL_SERVER_URL"),user=System.getenv("QUALITY_SOURCE_COLUMN_TEST_MYSQL_USER"),password=System.getenv("QUALITY_SOURCE_COLUMN_TEST_MYSQL_PASSWORD");assertTrue(url.startsWith("jdbc:mysql://127.0.0.1:")&&url.contains("/?"));
admin=new JdbcTemplate(new DriverManagerDataSource(url,user,password));database="quality_unclassified_delta_"+UUID.randomUUID().toString().replace("-","");admin.execute("CREATE DATABASE "+database+" CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci");
admin=new JdbcTemplate(new DriverManagerDataSource(url,user,password));database="quality_unclassified_delta_"+UUID.randomUUID().toString().replace("-","");cn.yunsheng.quality.testsupport.OwnedTestDatabase.create(admin.getDataSource(),database);
dataSource=new DriverManagerDataSource(url.replaceFirst("/\\?","/"+database+"?"),user,password);jdbc=new JdbcTemplate(dataSource);
jdbc.execute("CREATE TABLE transform_build(id CHAR(36) PRIMARY KEY,source_revision_id VARCHAR(100),build_key CHAR(64),parser_version VARCHAR(100),rule_version VARCHAR(100),mapping_version VARCHAR(100),status VARCHAR(32),row_count BIGINT,rejected_count BIGINT,component_summary JSON,error_summary JSON,started_at TIMESTAMP(6) NULL,completed_at TIMESTAMP(6) NULL,parser_config JSON,parser_config_hash VARCHAR(64))");
jdbc.execute("CREATE TABLE data_release(id CHAR(36) PRIMARY KEY,name VARCHAR(190),status VARCHAR(32),parent_release_id CHAR(36),expected_current_release_id CHAR(36),rule_version VARCHAR(100),mapping_version VARCHAR(100),target_version_id VARCHAR(100),complete BOOLEAN,validation JSON,created_by CHAR(36))");
@@ -47,7 +47,7 @@ class UnclassifiedMappingRebuildMySqlTest {
jdbc.update("INSERT INTO defect_component(build_id,stage,sheet_name,physical_row_no,defect_name,weight,source_field) VALUES(?,?,'S',?,'料废',?,'原字段')",build,stage,row,scrap);
}
UnclassifiedMappingRebuild.Candidate run(MetricsRollupService metrics){return new TransactionTemplate(new DataSourceTransactionManager(dataSource)).execute(status->new UnclassifiedMappingRebuild(jdbc,json,worker,metrics).create(UUID.randomUUID().toString(),parent,"new-map","rule",Map.of("matchRules",List.of(pending())),List.of(pending()),actor));}
@AfterEach void cleanup(){if(admin!=null&&database!=null&&database.matches("quality_unclassified_delta_[a-f0-9]{32}"))admin.execute("DROP DATABASE "+database);}
@AfterEach void cleanup(){if(admin!=null&&database!=null&&database.matches("quality_unclassified_delta_[a-f0-9]{32}"))cn.yunsheng.quality.testsupport.OwnedTestDatabase.drop(admin.getDataSource(),database);}
@Test void onlyEmptyRowsAreMatchedAndOldFactsWeightsAndExistingClassificationsRemainUnchanged(){
var candidate=run(rollups);assertEquals(3,candidate.scannedRows());assertEquals(2,candidate.classifiedRows());
assertEquals(2,jdbc.queryForObject("SELECT COUNT(*) FROM release_member m JOIN fact_black f ON f.build_id=m.transform_build_id WHERE m.release_id=? AND f.group_name IS NULL",Integer.class,parent));
@@ -30,7 +30,7 @@ class SalesOrderMetricRollupsMySqlTest {
String user=System.getenv("QUALITY_SOURCE_COLUMN_TEST_MYSQL_USER"),password=System.getenv("QUALITY_SOURCE_COLUMN_TEST_MYSQL_PASSWORD");
admin=new JdbcTemplate(new DriverManagerDataSource(url,user,password));
database="quality_sales_metric_test_"+UUID.randomUUID().toString().replace("-","");
admin.execute("CREATE DATABASE "+database+" CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci");
cn.yunsheng.quality.testsupport.OwnedTestDatabase.create(admin.getDataSource(),database);
var source=new DriverManagerDataSource(url.replaceFirst("/\\?","/"+database+"?"),user,password);jdbc=new JdbcTemplate(source);
jdbc.execute("CREATE TABLE transform_build(id CHAR(36) PRIMARY KEY)");
jdbc.execute("CREATE TABLE data_release(id CHAR(36),name VARCHAR(100),status VARCHAR(32),rule_version VARCHAR(100))");
@@ -53,7 +53,7 @@ class SalesOrderMetricRollupsMySqlTest {
when(governance.allowedFields(actor)).thenReturn(Set.of("*"));when(actor.getDepartments()).thenReturn(List.of("*"));when(actor.getGroups()).thenReturn(List.of());
metrics=new MetricsService(jdbc,governance);rollups=new SalesOrderMetricRollups(jdbc);
}
@AfterEach void cleanup(){if(database!=null)admin.execute("DROP DATABASE "+database);}
@AfterEach void cleanup(){if(database!=null)cn.yunsheng.quality.testsupport.OwnedTestDatabase.drop(admin.getDataSource(),database);}
Object normalized(Object value){return json.readValue(json.writeValueAsString(value),Object.class);}
MetricsService.MetricsQuery query(String view, boolean blanks, Map<String,List<String>> filters){return new MetricsService.MetricsQuery(2026,null,null,null,null,null,null,view,blanks,release,"month",Map.of(),filters);}
@@ -74,10 +74,11 @@ class SourceUploadAppendMySqlTest {
String user=System.getenv("QUALITY_MULTIFILE_TEST_MYSQL_USER"),password=System.getenv("QUALITY_MULTIFILE_TEST_MYSQL_PASSWORD");
admin=new JdbcTemplate(new DriverManagerDataSource(serverUrl,user,password));
database="quality_verify_multifile_"+UUID.randomUUID().toString().replace("-","");
admin.execute("CREATE DATABASE "+database+" CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci");
cn.yunsheng.quality.testsupport.OwnedTestDatabase.createEmpty(admin.getDataSource(),database);
dataSource=new DriverManagerDataSource(serverUrl.replaceFirst("/\\?","/"+database+"?"),user,password);
Flyway.configure().dataSource(dataSource).locations("classpath:db/migration")
.placeholders(Map.of("legacyTimestampOffsetSeconds","0")).load().migrate();
cn.yunsheng.quality.testsupport.OwnedTestDatabase.seal(admin.getDataSource(),database);
jdbc=new JdbcTemplate(dataSource);tx=new TransactionTemplate(new DataSourceTransactionManager(dataSource));
archiveRoot=tempDir.resolve("archive");Files.createDirectories(archiveRoot);
adminId=UUID.randomUUID();
@@ -101,7 +102,7 @@ class SourceUploadAppendMySqlTest {
}
@AfterEach void cleanup(){
if(admin!=null&&database!=null&&database.matches("quality_verify_multifile_[a-f0-9]{32}"))admin.execute("DROP DATABASE "+database);
if(admin!=null&&database!=null&&database.matches("quality_verify_multifile_[a-f0-9]{32}"))cn.yunsheng.quality.testsupport.OwnedTestDatabase.drop(admin.getDataSource(),database);
}
@Test void mainAndTwoAppendFilesImportPublishReplaceWithdrawAndKeepHistory() throws Exception {
@@ -0,0 +1,141 @@
package cn.yunsheng.quality.testsupport;
import java.nio.file.*;
import java.nio.file.attribute.PosixFilePermissions;
import java.sql.*;
import java.time.Instant;
import java.util.UUID;
import javax.sql.DataSource;
/** Durable ownership for test schemas. Production classes never use this helper. */
public final class OwnedTestDatabase {
private static final String MARKER = "__quality_test_owner";
private OwnedTestDatabase() {}
private static Path root() throws Exception {
for (Path p = Path.of("").toAbsolutePath().normalize(); p != null; p = p.getParent()) {
if (Files.isRegularFile(p.resolve(".runtime/local-config.json"))) return p.toRealPath();
}
throw new IllegalStateException("Project test configuration not found");
}
private static void valid(String name) {
if (name == null || name.length() > 64 || !name.matches("quality_(?:classification_source|classification_preview|sales_type_test|unclassified_delta|sales_metric_test|source_column_test|protocol_test|fine_rollup|verify_multifile)_[a-f0-9]{32}"))
throw new IllegalArgumentException("Not a disposable test database");
}
private static void protect(Path root, String database) throws Exception {
var match = java.util.regex.Pattern.compile("\"mysqlDatabase\"\\s*:\\s*\"([^\"]+)\"")
.matcher(Files.readString(root.resolve(".runtime/local-config.json")));
if (!match.find() || database.equals(match.group(1)))
throw new IllegalArgumentException("Active database is protected");
}
private static String quote(String text) {
StringBuilder s = new StringBuilder("\"");
for (char c : text.toCharArray()) {
if (c == '"' || c == '\\') s.append('\\').append(c);
else if (c < 32) s.append(String.format("\\u%04x", (int)c));
else s.append(c);
}
return s.append('"').toString();
}
private static void write(Path path, String value) throws Exception {
Path temp = path.resolveSibling(path.getFileName() + "." + UUID.randomUUID() + ".tmp");
Files.writeString(temp, value, StandardOpenOption.CREATE_NEW);
if (Files.getFileStore(temp).supportsFileAttributeView("posix"))
Files.setPosixFilePermissions(temp, PosixFilePermissions.fromString("rw-------"));
try (var stream = java.nio.channels.FileChannel.open(temp, StandardOpenOption.WRITE)) { stream.force(true); }
Files.move(temp, path, StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING);
}
private static Path record(Path root, String database) throws Exception {
Path base = root.resolve(".runtime/test-resources");
if (Files.isSymbolicLink(root.resolve(".runtime")) || Files.isSymbolicLink(base))
throw new IllegalStateException("Unsafe test registry");
Files.createDirectories(base);
if (Files.getFileStore(base).supportsFileAttributeView("posix"))
Files.setPosixFilePermissions(base, PosixFilePermissions.fromString("rwx------"));
return base.resolve(database + ".json");
}
public static void create(DataSource source, String database) {
create(source, database, false);
}
public static void createEmpty(DataSource source, String database) {
create(source, database, true);
}
private static void create(DataSource source, String database, boolean empty) {
valid(database);
try {
Path root = root(), path = record(root, database);
protect(root, database);
String token = UUID.randomUUID().toString().replace("-", "");
double now = Instant.now().toEpochMilli() / 1000.0;
double started = ProcessHandle.current().info().startInstant().orElseThrow().toEpochMilli() / 1000.0;
try (Connection c = source.getConnection(); Statement s = c.createStatement()) {
String server;
try (ResultSet rs = s.executeQuery("SELECT @@server_uuid")) { rs.next(); server = rs.getString(1); }
if (!server.matches("[a-fA-F0-9-]{36}")) throw new IllegalStateException("Server identity unavailable");
String prefix = "{\"kind\":\"quality-owned-test-db\",\"formatVersion\":1,\"root\":" + quote(root.toString()) +
",\"database\":" + quote(database) + ",\"token\":" + quote(token) + ",\"serverUuid\":" + quote(server) +
",\"ownerPid\":" + ProcessHandle.current().pid() + ",\"ownerCreated\":" + started +
",\"createdAt\":" + now + ",\"expiresAt\":" + (now + 86400) + ",\"children\":[],\"users\":[]";
Files.createFile(path); write(path, prefix + ",\"created\":false}");
try { s.execute("CREATE DATABASE `" + database + "` CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci"); }
catch (SQLException error) { Files.deleteIfExists(path); throw error; }
write(path, prefix + ",\"created\":true}");
if (!empty) {
s.execute("CREATE TABLE `" + database + "`.`" + MARKER + "` (token CHAR(32) PRIMARY KEY)");
s.execute("INSERT INTO `" + database + "`.`" + MARKER + "` VALUES ('" + token + "')");
write(path, prefix + ",\"created\":true,\"ready\":true}");
}
}
} catch (Exception error) { throw new IllegalStateException("Owned test database creation failed", error); }
}
public static void seal(DataSource source, String database) {
valid(database);
try {
Path root = root(); protect(root, database); Path path = record(root, database);
String value = Files.readString(path);
var match = java.util.regex.Pattern.compile("\"token\"\\s*:\\s*\"([a-f0-9]{32})\"").matcher(value);
if (!match.find()) throw new IllegalStateException("Test token unavailable");
try (Connection c = source.getConnection(); Statement s = c.createStatement()) {
s.execute("CREATE TABLE `" + database + "`.`" + MARKER + "` (token CHAR(32) PRIMARY KEY)");
s.execute("INSERT INTO `" + database + "`.`" + MARKER + "` VALUES ('" + match.group(1) + "')");
}
write(path, value.substring(0, value.length()-1) + ",\"ready\":true}");
} catch (Exception error) { throw new IllegalStateException("Test schema registration failed", error); }
}
public static void drop(DataSource source, String database) {
valid(database);
try {
Path root = root(); protect(root, database);
Path path = record(root, database);
if (Files.isSymbolicLink(path)) throw new IllegalStateException("Unsafe test owner record");
String record = Files.readString(path);
var tokenMatch = java.util.regex.Pattern.compile("\"token\"\\s*:\\s*\"([a-f0-9]{32})\"").matcher(record);
var serverMatch = java.util.regex.Pattern.compile("\"serverUuid\"\\s*:\\s*\"([a-fA-F0-9-]{36})\"").matcher(record);
if (!tokenMatch.find() || !serverMatch.find()) throw new IllegalStateException("Test identity unavailable");
try (Connection c = source.getConnection(); Statement s = c.createStatement()) {
try (ResultSet rs = s.executeQuery("SELECT @@server_uuid")) {
rs.next(); if (!serverMatch.group(1).equals(rs.getString(1))) throw new IllegalStateException("Test server changed");
}
if (record.contains("\"ready\":true")) {
try (ResultSet rs = s.executeQuery("SELECT token FROM `" + database + "`.`" + MARKER + "`")) {
if (!rs.next() || !tokenMatch.group(1).equals(rs.getString(1))) throw new IllegalStateException("Test schema identity changed");
}
} else if (!record.contains("\"ownerPid\":" + ProcessHandle.current().pid() + ",")) {
throw new IllegalStateException("Unsealed test owner not confirmed");
}
s.execute("DROP DATABASE `" + database + "`");
Files.delete(path);
}
} catch (Exception error) { throw new IllegalStateException("Owned test database cleanup deferred", error); }
}
}