76 行
5.3 KiB
Java
76 行
5.3 KiB
Java
import java.io.*;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.sql.*;
|
|
import java.util.*;
|
|
|
|
/** Atomic fresh seed import. Refuses nonempty target tables and never replays committed seeds. */
|
|
class DamengSeed {
|
|
public static void main(String[] args) throws Exception {
|
|
String schema=System.getenv("DATAGOV_DM_SCHEMA");
|
|
if(schema==null||!schema.matches("AIXUN_DATAGOV_[A-Z0-9_]+"))throw new IllegalArgumentException("Dedicated schema required");
|
|
Properties p=new Properties();p.setProperty("user",System.getenv("DATAGOV_DM_USERNAME"));p.setProperty("password",System.getenv("DATAGOV_DM_PASSWORD"));
|
|
p.setProperty("connectTimeout","6000");p.setProperty("socketTimeout","30000");
|
|
var input=new BufferedReader(new InputStreamReader(System.in,StandardCharsets.UTF_8));
|
|
String digest=input.readLine();String[] tables=input.readLine().split(",");
|
|
boolean verify=args.length==1&&"--verify".equals(args[0]);
|
|
boolean configureAdmin=args.length==1&&"--configure-admin".equals(args[0]);
|
|
try(Connection c=DriverManager.getConnection("jdbc:dm://"+System.getenv("DATAGOV_DM_HOST")+":"+System.getenv("DATAGOV_DM_PORT"),p)) {
|
|
c.setSchema(schema);
|
|
if(!schema.equals(c.getSchema()))throw new IllegalStateException("Schema mismatch");
|
|
c.setAutoCommit(false);
|
|
boolean committing=false;
|
|
try {
|
|
String previous=null,state=null;
|
|
try(Statement s=c.createStatement();ResultSet r=s.executeQuery("SELECT sha256,state FROM dg_dm_install_step WHERE step_key='seed:baseline'")) {
|
|
if(r.next()){previous=r.getString(1);state=r.getString(2);}
|
|
}
|
|
if(previous!=null) {
|
|
if(!digest.equals(previous)||!"SUCCESS".equals(state))throw new IllegalStateException("Seed drift or incomplete state; reconcile first");
|
|
if(configureAdmin) {
|
|
String hash=System.getenv("DATAGOV_DM_ADMIN_HASH");
|
|
if(hash==null||!hash.matches("\\$2[aby]\\$12\\$[./A-Za-z0-9]{53}"))throw new IllegalArgumentException("BCrypt administrator hash required");
|
|
try(PreparedStatement s=c.prepareStatement("UPDATE sys_user SET password=?,status='0',nick_name='艾讯管理员',update_time=LOCALTIMESTAMP WHERE user_id=1761100000000000001 AND user_name='admin' AND del_flag='0'")) {
|
|
s.setString(1,hash);
|
|
if(s.executeUpdate()!=1)throw new IllegalStateException("Expected administrator identity is missing");
|
|
}
|
|
committing=true;c.commit();committing=false;System.out.println("ADMIN_CONFIGURED=true");return;
|
|
}
|
|
System.out.println("SEED_ALREADY_APPLIED=true");return;
|
|
}
|
|
if(verify||configureAdmin)throw new IllegalStateException("Seed is not installed");
|
|
for(String table:tables) {
|
|
if(!table.matches("[a-z][a-z0-9_]+"))throw new IllegalArgumentException("Invalid table");
|
|
try(Statement s=c.createStatement();ResultSet r=s.executeQuery("SELECT COUNT(*) FROM "+table)) {
|
|
if(!r.next()||r.getLong(1)!=0)throw new IllegalStateException("Nonempty seed target: "+table);
|
|
}
|
|
}
|
|
// The unique journal key serializes concurrent fresh importers; transaction rollback removes it on failure.
|
|
try(PreparedStatement s=c.prepareStatement("INSERT INTO dg_dm_install_step(step_key,sha256,state) VALUES('seed:baseline',?,'STARTED')")) {
|
|
s.setString(1,digest);s.executeUpdate();
|
|
}
|
|
int rows=0;
|
|
try(Statement s=c.createStatement()) {
|
|
String line;
|
|
while((line=input.readLine())!=null) {
|
|
s.addBatch(new String(Base64.getDecoder().decode(line),StandardCharsets.UTF_8));rows++;
|
|
if(rows%100==0){s.executeBatch();s.clearBatch();System.out.println("SEED_ROWS_STAGED="+rows+" COMMITTED=false");}
|
|
}
|
|
s.executeBatch();
|
|
}
|
|
try(Statement s=c.createStatement()) {s.executeUpdate("UPDATE dg_dm_install_step SET state='SUCCESS',updated_at=CURRENT_TIMESTAMP WHERE step_key='seed:baseline'");}
|
|
committing=true;c.commit();committing=false;
|
|
System.out.println("SEED_ROWS_COMMITTED="+rows+" ADMIN_ACCOUNT=DISABLED_UNTIL_CONFIGURED");
|
|
} catch(Exception error) {
|
|
if(committing) {
|
|
System.err.println("SEED_COMMIT_UNKNOWN: verify the durable journal before any retry");System.exit(2);
|
|
}
|
|
try { c.rollback(); }
|
|
catch(SQLException rollbackError) {System.err.println("SEED_ROLLBACK_UNKNOWN: reconcile the journal and rows before retry");System.exit(2);}
|
|
if(error instanceof SQLException sql)System.err.println("SEED_ROLLED_BACK SQLSTATE="+sql.getSQLState()+" CODE="+sql.getErrorCode());
|
|
else System.err.println(error.getMessage());
|
|
System.exit(2);
|
|
}
|
|
} catch(SQLException error) {System.err.println("SEED_CONNECTION_FAILURE CODE="+error.getErrorCode());System.exit(2);}
|
|
}
|
|
}
|