{"record":{"id":"7e7eeb0cf026f417","repo":"theonedev/onedev","slug":"onedev-is-unable-to-restore-old-database-please-d","errorCode":null,"errorMessage":"OneDev is unable to restore old database, please do it manually by first resetting it (delete and create), and then running below command:","messagePattern":"OneDev is unable to restore old database, please do it manually by first resetting it \\(delete and create\\), and then running below command:","errorType":"exception","errorClass":"ExplicitException","httpStatus":null,"severity":"critical","filePath":"server-core/src/main/java/io/onedev/server/commandhandler/Upgrade.java","lineNumber":507,"sourceCode":"\t\t\t\t\t\t\t\t\t\tlogger.info(\"Old database restored\");\n\t\t\t\t\t\t\t\t\t\tdbChanged = false;\n\t\t\t\t\t\t\t\t\t} catch (Exception e) {\n\t\t\t\t\t\t\t\t\t\tlogger.error(\"Error restoring old database\", e);\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\tStringBuilder errorMessage = new StringBuilder(String.format(\"!! Failed to upgrade %s\", upgradeDir.getAbsolutePath()));\n\t\t\t\t\t\t\t\tif (dbChanged) {\n\t\t\t\t\t\t\t\t\tif (inDocker)\n\t\t\t\t\t\t\t\t\t\terrorMessage.append(\"\\nOneDev is unable to restore old database, please do it manually by first resetting it (delete and create), and then exec into the container to run below command:\");\n\t\t\t\t\t\t\t\t\telse\n\t\t\t\t\t\t\t\t\t\terrorMessage.append(\"\\nOneDev is unable to restore old database, please do it manually by first resetting it (delete and create), and then running below command:\");\t\t\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t\tif (SystemUtils.IS_OS_WINDOWS) \n\t\t\t\t\t\t\t\t\t\terrorMessage.append(\"\\n\" + upgradeDir.getAbsolutePath() + File.separator + \"bin\" + File.separator + \"restore-db.bat \" + dbBackupFile.getAbsolutePath());\n\t\t\t\t\t\t\t\t\telse \n\t\t\t\t\t\t\t\t\t\terrorMessage.append(\"\\n\" + upgradeDir.getAbsolutePath() + File.separator + \"bin\" + File.separator + \"restore-db.sh \" + dbBackupFile.getAbsolutePath());\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tthrow new ExplicitException(errorMessage.toString());\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\tlogger.info(\"Successfully upgraded {}\", upgradeDir.getAbsolutePath());\n\t\t\t\t\t\t\t\tFileUtils.deleteDir(programBackup);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\treturn null;\n\t\t\t\t\t\t}\n\t\t\t\t\t};\n\t\t\t\t\t\n\t\t\t\t\tvar maintenanceFile = OneDev.getMaintenanceFile(upgradeDir);\n\t\t\t\t\tFileUtils.touchFile(maintenanceFile);\n\t\t\t\t\tMaintenanceProbeServer.start(upgradeDir);\n\t\t\t\t\ttry {\n\t\t\t\t\t\tvar hibernateConfig = new HibernateConfig(upgradeDir);\n\t\t\t\t\t\tif (!hibernateConfig.isHSQLDialect()) {\n\t\t\t\t\t\t\tvar classLoader = newSiteLibClassLoader(upgradeDir);\n\t\t\t\t\t\t\ttry (var conn = openConnection(hibernateConfig, classLoader)) {\n\t\t\t\t\t\t\t\tcallWithLock(conn, () -> callable.call());\n\t\t\t\t\t\t\t}","sourceCodeStart":489,"sourceCodeEnd":525,"githubUrl":"https://github.com/theonedev/onedev/blob/d44925c47c37992c828ea673a5f9620539bc3ff2/server-core/src/main/java/io/onedev/server/commandhandler/Upgrade.java#L489-L525","documentation":"OneDev throws this ExplicitException during a failed self-upgrade when it could not automatically restore the old database from the backup taken before the upgrade. It tells the admin to manually reset the database (drop and recreate) and run the generated restore-db script to re-import the backup.","triggerScenarios":"The 'call' method of the Upgrade command handler performs the upgrade, detects a failure during DB upgrade/restore, and the automatic restore path could not complete, so it appends this message and throws.","commonSituations":"Upgrade interrupted mid-way (power loss, killed process), DB plugin/driver mismatch, insufficient DB permissions for the OneDev user, or corrupted backup file leaving the DB in a half-upgraded state.","solutions":["Delete the current (half-upgraded) database and recreate an empty one with the same name/owner","Run the generated script: <upgradeDir>/bin/restore-db.bat (Windows) or restore-db.sh (Linux) with the db backup file path shown in the error","Restore the previous OneDev program files from the program backup directory if the upgrade also modified them","Check database connectivity/credentials before retrying the upgrade"],"exampleFix":"# manual restore after failed upgrade\ndropdb onedev && createdb onedev\n/path/to/onedev-upgrade/bin/restore-db.sh /path/to/site-1.0/db_backup.dump","handlingStrategy":"fallback","validationCode":"// before upgrade: verify DB reachable and backup exists\nif (!db reachable || !dbBackupFile.exists()) abortUpgrade();","typeGuard":null,"tryCatchPattern":"try { upgrade(); } catch (ExplicitException e) { if (e.getMessage().contains(\"unable to restore old database\")) { resetDatabase(); runScript(upgradeDir + \"/bin/restore-db.sh\", dbBackupFile); } }","preventionTips":["Always run upgrades against a tested, recent DB backup","Upgrade during a maintenance window with stable power/process supervision","Verify DB user has full DDL and data privileges","Keep the upgrade directory's restore scripts accessible"],"tags":["database","upgrade","restore"],"backgroundTag":"database-restore-failed","analyzedSha":"d44925c47c37992c828ea673a5f9620539bc3ff2","analyzedAt":"2026-09-06T07:18:27.995Z","contentChangedAt":"2026-09-06T07:18:27.995Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}