- 移除所有 ${VAR:default} 格式的环境变量占位符
- 将默认值直接写入配置文件中
- 删除对 .codex-tmp 秘密文件的依赖
- 修改测试代码使用新的配置加载方式
- 更新 Docker 部署不再使用 env_file
- 应用启动参数直接在 Dockerfile 中定义
- 添加配置自检测试确保无外部依赖
52 lines
5.9 KiB
Java
52 lines
5.9 KiB
Java
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.nio.file.*;
|
|
import java.sql.*;
|
|
import java.util.*;
|
|
import java.util.regex.*;
|
|
|
|
/** Explicit, one-time full copy into the named EMPTY production database only. */
|
|
public class InitializeEmptyProduction {
|
|
static final String TARGET="cmvr-project-prod", SOURCE="cmvr-project-dev";
|
|
public static void main(String[] args)throws Exception{
|
|
if(args.length!=2||!"--initialize-empty-cmvr-project-prod".equals(args[0]))throw new IllegalArgumentException("Explicit empty-production initialization required");
|
|
Path directory=Path.of(args[1]).toAbsolutePath().normalize(),allowed=Path.of(".codex-tmp/database-backups").toAbsolutePath().normalize();
|
|
if(!directory.startsWith(allowed))throw new IllegalArgumentException("Backup directory outside workspace backup root");
|
|
var json=new ObjectMapper();var manifest=json.readTree(directory.resolve("manifest.json").toFile());
|
|
if(!SOURCE.equals(manifest.path("database").asText())||!manifest.path("complete").asBoolean()||!manifest.path("readOnly").asBoolean())throw new IllegalArgumentException("Incomplete source backup");
|
|
for(String kind:List.of("views","triggers","routines","events"))if(!manifest.path(kind).isEmpty())throw new IllegalStateException("Manual review required for stored programs: "+kind);
|
|
Path sql=directory.resolve(SOURCE+".sql");byte[] bytes=Files.readAllBytes(sql);
|
|
if(!BackupReplayTarget.hash(bytes).equals(manifest.path("sha256").asText()))throw new IllegalStateException("Backup checksum mismatch");
|
|
String content=new String(bytes,StandardCharsets.UTF_8);if(!content.endsWith("-- BACKUP COMPLETE\n"))throw new IllegalStateException("Backup not finalized");
|
|
Map<String,String> config=new HashMap<>();boolean master=false;
|
|
for(String line:Files.readAllLines(Path.of("ruoyi-admin/src/main/resources/application-druid.yml"))){if(line.trim().equals("master:")){master=true;continue;}if(line.trim().equals("slave:"))break;if(master&&line.indexOf(':')>0){int colon=line.indexOf(':');config.put(line.substring(0,colon).trim(),DatabaseTool.value(line.substring(colon+1).trim()));}}
|
|
String configured=config.get("url");if(configured==null||!configured.matches("^jdbc:mysql://192\\.168\\.28\\.10:3306/cmvr-project-dev(?:\\?.*)?$"))throw new IllegalStateException("Unexpected datasource");
|
|
var properties=new Properties();properties.setProperty("user",config.get("username"));properties.setProperty("password",config.get("password"));properties.setProperty("socketTimeout","60000");properties.setProperty("connectTimeout","5000");properties.setProperty("tinyInt1isBit","false");
|
|
Set<String> names=new HashSet<>();manifest.path("tables").fieldNames().forEachRemaining(names::add);
|
|
var statements=new ArrayList<String>();var tablePattern=Pattern.compile("(?is)^(?:CREATE TABLE|INSERT INTO) `([A-Za-z0-9_]+)`.*");
|
|
for(String statement:DatabaseTool.split(content)){
|
|
if(statement.equals("USE `cmvr-project-dev`")||statement.matches("(?is)^DROP TABLE IF EXISTS `[A-Za-z0-9_]+`$")||statement.startsWith("SET "))continue;
|
|
var match=tablePattern.matcher(statement);if(!match.matches()||!names.contains(match.group(1)))throw new IllegalStateException("Unexpected backup statement");
|
|
if(Pattern.compile("`[^`]+`\\s*\\.\\s*`[^`]+`").matcher(statement).find())throw new IllegalStateException("Qualified database reference forbidden");
|
|
statements.add(statement);
|
|
}
|
|
Path output=Path.of(".codex-tmp/deploy-production");Files.createDirectories(output);
|
|
try(var c=DriverManager.getConnection(configured.replace("/"+SOURCE,"/"+TARGET),properties)){
|
|
if(!TARGET.equals(BackupReplayTarget.scalar(c,"select database()")))throw new IllegalStateException("Target mismatch");
|
|
try(var s=c.prepareStatement("select count(*) from information_schema.tables where table_schema=?")){s.setString(1,TARGET);try(var r=s.executeQuery()){r.next();if(r.getLong(1)!=0)throw new IllegalStateException("Production is not empty; refusing overwrite");}}
|
|
Files.writeString(output.resolve("production-before-initialize.json"),json.writerWithDefaultPrettyPrinter().writeValueAsString(Map.of("database",TARGET,"tables",0,"sourceBackup",directory.toString(),"sourceSha256",manifest.path("sha256").asText())),StandardOpenOption.CREATE_NEW);
|
|
try(var s=c.createStatement()){
|
|
s.execute("SET NAMES utf8mb4");s.execute("SET FOREIGN_KEY_CHECKS=0");
|
|
try{int count=0;for(String statement:statements){s.execute(statement);if(++count%1000==0)System.out.println("Production initialization statements="+count);}}finally{s.execute("SET FOREIGN_KEY_CHECKS=1");}
|
|
}
|
|
var changed=new ArrayList<String>();long rows=0;
|
|
for(String name:new TreeSet<>(names)){var definition=manifest.path("tables").path(name);long actual=Long.parseLong(BackupReplayTarget.scalar(c,"select count(*) from "+BackupReplayTarget.id(name)));rows+=actual;if(actual!=definition.path("rows").asLong()||!ReadOnlyReplayAudit.fingerprint(c,name,definition).equals(definition.path("rowFingerprint").asText()))changed.add(name);}
|
|
var result=Map.of("database",TARGET,"source",SOURCE,"tables",names.size(),"rows",rows,"fingerprintDifferences",changed,"sourceBackup",directory.toString(),"sourceUnmodified",true);
|
|
Files.writeString(output.resolve("production-initialization-result.json"),json.writerWithDefaultPrettyPrinter().writeValueAsString(result));
|
|
if(!changed.isEmpty())throw new IllegalStateException("Production copy verification failed: "+changed);
|
|
System.out.println("Production initialized and fingerprint verified: tables="+names.size()+", rows="+rows);
|
|
}
|
|
System.out.println("Backend settings are declared in application-prod.yml; no environment file or replacement token is generated.");
|
|
}
|
|
}
|