DB Backup Bugfix
This commit is contained in:
@@ -51,16 +51,28 @@ public class DatabaseManagementService {
|
|||||||
// Disable foreign key checks during restore (PostgreSQL syntax)
|
// Disable foreign key checks during restore (PostgreSQL syntax)
|
||||||
sqlDump.append("SET session_replication_role = replica;\n\n");
|
sqlDump.append("SET session_replication_role = replica;\n\n");
|
||||||
|
|
||||||
// List of tables in dependency order (children first for deletion, parents first for insertion)
|
// List of tables in dependency order (parents first for insertion)
|
||||||
List<String> tables = Arrays.asList(
|
List<String> insertTables = Arrays.asList(
|
||||||
"story_tags", "author_urls", "collection_stories",
|
"authors", "series", "tags", "collections",
|
||||||
"stories", "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)
|
// Generate TRUNCATE statements for each table (assuming tables already exist)
|
||||||
for (String tableName : tables) {
|
for (String tableName : truncateTables) {
|
||||||
sqlDump.append("-- Table: ").append(tableName).append("\n");
|
sqlDump.append("-- Truncate Table: ").append(tableName).append("\n");
|
||||||
sqlDump.append("TRUNCATE TABLE \"").append(tableName).append("\" CASCADE;\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
|
// Get table data
|
||||||
try (PreparedStatement stmt = connection.prepareStatement("SELECT * FROM \"" + tableName + "\"");
|
try (PreparedStatement stmt = connection.prepareStatement("SELECT * FROM \"" + tableName + "\"");
|
||||||
@@ -124,18 +136,32 @@ public class DatabaseManagementService {
|
|||||||
// Parse SQL statements properly (handle semicolons inside string literals)
|
// Parse SQL statements properly (handle semicolons inside string literals)
|
||||||
List<String> statements = parseStatements(sqlContent.toString());
|
List<String> statements = parseStatements(sqlContent.toString());
|
||||||
|
|
||||||
|
int successCount = 0;
|
||||||
for (String statement : statements) {
|
for (String statement : statements) {
|
||||||
String trimmedStatement = statement.trim();
|
String trimmedStatement = statement.trim();
|
||||||
if (!trimmedStatement.isEmpty()) {
|
if (!trimmedStatement.isEmpty()) {
|
||||||
System.err.println("DEBUG: Executing statement: " +
|
|
||||||
(trimmedStatement.length() > 200 ? trimmedStatement.substring(0, 200) + "..." : trimmedStatement));
|
|
||||||
try (PreparedStatement stmt = connection.prepareStatement(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();
|
connection.commit();
|
||||||
|
System.err.println("Restore completed successfully. Executed " + successCount + " SQL statements.");
|
||||||
|
|
||||||
// Reindex search after successful restore
|
// Reindex search after successful restore
|
||||||
try {
|
try {
|
||||||
|
|||||||
Reference in New Issue
Block a user