DB Backup Bugfix
This commit is contained in:
@@ -51,16 +51,28 @@ public class DatabaseManagementService {
|
||||
// Disable foreign key checks during restore (PostgreSQL syntax)
|
||||
sqlDump.append("SET session_replication_role = replica;\n\n");
|
||||
|
||||
// List of tables in dependency order (children first for deletion, parents first for insertion)
|
||||
List<String> tables = Arrays.asList(
|
||||
"story_tags", "author_urls", "collection_stories",
|
||||
"stories", "authors", "series", "tags", "collections"
|
||||
// List of tables in dependency order (parents first for insertion)
|
||||
List<String> insertTables = Arrays.asList(
|
||||
"authors", "series", "tags", "collections",
|
||||
"stories", "story_tags", "author_urls", "collection_stories"
|
||||
);
|
||||
|
||||
// TRUNCATE in reverse order (children first)
|
||||
List<String> truncateTables = Arrays.asList(
|
||||
"collection_stories", "author_urls", "story_tags",
|
||||
"stories", "collections", "tags", "series", "authors"
|
||||
);
|
||||
|
||||
// Generate TRUNCATE statements for each table (assuming tables already exist)
|
||||
for (String tableName : tables) {
|
||||
sqlDump.append("-- Table: ").append(tableName).append("\n");
|
||||
for (String tableName : truncateTables) {
|
||||
sqlDump.append("-- Truncate Table: ").append(tableName).append("\n");
|
||||
sqlDump.append("TRUNCATE TABLE \"").append(tableName).append("\" CASCADE;\n");
|
||||
}
|
||||
sqlDump.append("\n");
|
||||
|
||||
// Generate INSERT statements in dependency order
|
||||
for (String tableName : insertTables) {
|
||||
sqlDump.append("-- Data for Table: ").append(tableName).append("\n");
|
||||
|
||||
// Get table data
|
||||
try (PreparedStatement stmt = connection.prepareStatement("SELECT * FROM \"" + tableName + "\"");
|
||||
@@ -124,18 +136,32 @@ public class DatabaseManagementService {
|
||||
// Parse SQL statements properly (handle semicolons inside string literals)
|
||||
List<String> statements = parseStatements(sqlContent.toString());
|
||||
|
||||
int successCount = 0;
|
||||
for (String statement : statements) {
|
||||
String trimmedStatement = statement.trim();
|
||||
if (!trimmedStatement.isEmpty()) {
|
||||
System.err.println("DEBUG: Executing statement: " +
|
||||
(trimmedStatement.length() > 200 ? trimmedStatement.substring(0, 200) + "..." : trimmedStatement));
|
||||
try (PreparedStatement stmt = connection.prepareStatement(trimmedStatement)) {
|
||||
stmt.execute();
|
||||
stmt.executeUpdate();
|
||||
successCount++;
|
||||
} catch (SQLException e) {
|
||||
// Log detailed error information for failed statements
|
||||
System.err.println("ERROR: Failed to execute SQL statement #" + (successCount + 1));
|
||||
System.err.println("Error: " + e.getMessage());
|
||||
System.err.println("SQL State: " + e.getSQLState());
|
||||
System.err.println("Error Code: " + e.getErrorCode());
|
||||
|
||||
// Show the problematic statement (first 500 chars)
|
||||
String statementPreview = trimmedStatement.length() > 500 ?
|
||||
trimmedStatement.substring(0, 500) + "..." : trimmedStatement;
|
||||
System.err.println("Statement: " + statementPreview);
|
||||
|
||||
throw e; // Re-throw to trigger rollback
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
connection.commit();
|
||||
System.err.println("Restore completed successfully. Executed " + successCount + " SQL statements.");
|
||||
|
||||
// Reindex search after successful restore
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user