Files
2026-09-11 17:31:34 +08:00

99 lines
5.0 KiB
Java

import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
/**
* One-off migration: simplifies s_power keys to bare usage permission codes.
* menu/module permission points drop the action suffix:
* module.s_i18n_type.read/.create/.update/.delete -> module.s_i18n_type
* b_action of the merged rows becomes 'access'; b_name is taken from s_module.b_name when available.
* Idempotent: old-style rows (b_type='module' with CRUD action) are merged only once.
*/
public final class SimplifyPowerKeys {
public static void main(String[] args) throws Exception {
Path apiRoot = Path.of("").toAbsolutePath().normalize();
Path configPath = apiRoot.resolve("config/dbconfigs/g3hd.properties").normalize();
Properties properties = new Properties();
try (InputStream input = Files.newInputStream(configPath)) {
properties.load(input);
}
try (Connection connection = DriverManager.getConnection(
properties.getProperty("url"),
properties.getProperty("username"),
properties.getProperty("password"))) {
if (!"FMS".equalsIgnoreCase(connection.getCatalog())) {
throw new IllegalStateException("Refusing to alter database: " + connection.getCatalog());
}
connection.setAutoCommit(false);
try {
List<String[]> oldRows = new ArrayList<>();
String findSql = "SELECT DISTINCT b_object_type, b_object_id FROM dbo.s_power "
+ "WHERE b_type = 'module' AND b_action <> 'access'";
try (Statement statement = connection.createStatement();
ResultSet rows = statement.executeQuery(findSql)) {
while (rows.next()) {
oldRows.add(new String[]{rows.getString(1), rows.getString(2)});
}
}
try (PreparedStatement delete = connection.prepareStatement(
"DELETE FROM dbo.s_power WHERE b_type = 'module' AND b_object_type = ? AND b_object_id = ?");
PreparedStatement insert = connection.prepareStatement(
"INSERT INTO dbo.s_power (b_id, b_name, b_i18n, b_type, b_object_type, b_object_id, b_action, b_canuse, b_xh) "
+ "VALUES (?, ?, NULL, 'module', ?, ?, 'access', 1, 0)")) {
for (String[] objectKey : oldRows) {
String objectType = objectKey[0];
String objectId = objectKey[1];
delete.setString(1, objectType);
delete.setString(2, objectId);
int removed = delete.executeUpdate();
String name = lookupModuleName(connection, objectId);
insert.setString(1, "module." + objectId);
insert.setString(2, name);
insert.setString(3, objectType);
insert.setString(4, objectId);
insert.executeUpdate();
System.out.println("MERGED|" + objectType + ":" + objectId
+ "|removed=" + removed + "|new=module." + objectId + "|name=" + name);
}
}
connection.commit();
System.out.println("COMMITTED|merged_objects=" + oldRows.size());
} catch (Exception exception) {
connection.rollback();
throw exception;
}
try (Statement statement = connection.createStatement();
ResultSet rows = statement.executeQuery(
"SELECT b_id, b_name, b_type, b_object_type, b_object_id, b_action, b_canuse "
+ "FROM dbo.s_power ORDER BY b_type, b_id")) {
while (rows.next()) {
System.out.println("ROW|" + rows.getString(1) + "|" + rows.getString(2)
+ "|type=" + rows.getString(3) + "|object=" + rows.getString(4) + ":" + rows.getString(5)
+ "|action=" + rows.getString(6) + "|canuse=" + rows.getInt(7));
}
}
}
}
private static String lookupModuleName(Connection connection, String moduleId) throws Exception {
try (PreparedStatement statement = connection.prepareStatement(
"SELECT b_name FROM dbo.s_module WHERE b_id = ?")) {
statement.setString(1, moduleId);
try (ResultSet rows = statement.executeQuery()) {
if (rows.next() && rows.getString(1) != null && !rows.getString(1).isBlank()) {
return rows.getString(1);
}
}
}
return moduleId;
}
}