mirror of
https://github.com/overcuriousity/autopsy-flatpak.git
synced 2025-07-13 00:16:16 +00:00
Merge pull request #3031 from APriestman/2860_deleteDatabase
Delete a newly created central repo database if initialization fails
This commit is contained in:
commit
48e8827398
@ -282,6 +282,27 @@ public final class PostgresEamDbSettings {
|
||||
|
||||
}
|
||||
|
||||
public boolean deleteDatabase() {
|
||||
Connection conn = getEphemeralConnection(true);
|
||||
if (null == conn) {
|
||||
return false;
|
||||
}
|
||||
|
||||
String sql = "DROP DATABASE %s"; // NON-NLS
|
||||
try {
|
||||
Statement stmt;
|
||||
stmt = conn.createStatement();
|
||||
stmt.execute(String.format(sql, getDbName()));
|
||||
} catch (SQLException ex) {
|
||||
LOGGER.log(Level.SEVERE, "Failed to execute drop database statement.", ex); // NON-NLS
|
||||
return false;
|
||||
} finally {
|
||||
EamDbUtil.closeConnection(conn);
|
||||
}
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the database schema.
|
||||
*
|
||||
|
@ -110,6 +110,20 @@ public final class SqliteEamDbSettings {
|
||||
ModuleSettings.setConfigSetting("CentralRepository", "db.badTags", String.join(",", badTags)); // NON-NLS
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that the db file exists.
|
||||
*
|
||||
* @return true if exists, else false
|
||||
*/
|
||||
public boolean dbFileExists() {
|
||||
File dbFile = new File(getFileNameWithPath());
|
||||
if(! dbFile.exists()){
|
||||
return false;
|
||||
}
|
||||
// It's unlikely, but make sure the file isn't actually a directory
|
||||
return ( ! dbFile.isDirectory());
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that the db directory path exists.
|
||||
*
|
||||
@ -149,6 +163,15 @@ public final class SqliteEamDbSettings {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the database
|
||||
* @return
|
||||
*/
|
||||
public boolean deleteDatabase() {
|
||||
File dbFile = new File(this.getFileNameWithPath());
|
||||
return dbFile.delete();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the full connection URL as a String
|
||||
*
|
||||
|
@ -319,19 +319,22 @@ public class EamDbSettingsDialog extends JDialog {
|
||||
switch (selectedPlatform) {
|
||||
case POSTGRESQL:
|
||||
if (dbSettingsPostgres.verifyConnection()) {
|
||||
if (dbSettingsPostgres.verifyDatabaseExists()
|
||||
&& dbSettingsPostgres.verifyDatabaseSchema()) {
|
||||
if (dbSettingsPostgres.verifyDatabaseExists()) {
|
||||
if( dbSettingsPostgres.verifyDatabaseSchema()) {
|
||||
testingStatus = DatabaseTestResult.TESTEDOK;
|
||||
} else {
|
||||
testingStatus = DatabaseTestResult.SCHEMA_INVALID;
|
||||
}
|
||||
} else {
|
||||
testingStatus = DatabaseTestResult.DB_DOES_NOT_EXIST;
|
||||
}
|
||||
} else {
|
||||
testingStatus = DatabaseTestResult.CONNECTION_FAILED;
|
||||
}
|
||||
break;
|
||||
case SQLITE:
|
||||
if (dbSettingsSqlite.dbDirectoryExists()
|
||||
&& dbSettingsSqlite.verifyConnection()) {
|
||||
if (dbSettingsSqlite.dbFileExists()){
|
||||
if(dbSettingsSqlite.verifyConnection()) {
|
||||
if (dbSettingsSqlite.verifyDatabaseSchema()) {
|
||||
testingStatus = DatabaseTestResult.TESTEDOK;
|
||||
} else {
|
||||
@ -340,6 +343,9 @@ public class EamDbSettingsDialog extends JDialog {
|
||||
} else {
|
||||
testingStatus = DatabaseTestResult.SCHEMA_INVALID;
|
||||
}
|
||||
} else {
|
||||
testingStatus = DatabaseTestResult.DB_DOES_NOT_EXIST;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
@ -362,6 +368,11 @@ public class EamDbSettingsDialog extends JDialog {
|
||||
&& dbSettingsPostgres.insertDefaultDatabaseContent();
|
||||
}
|
||||
if (!result) {
|
||||
// Remove the incomplete database
|
||||
if(dbCreated){
|
||||
dbSettingsPostgres.deleteDatabase();
|
||||
}
|
||||
|
||||
JOptionPane.showMessageDialog(WindowManager.getDefault().getMainWindow(),
|
||||
Bundle.EamDbSettingsDialog_okButton_createPostgresDbError_message(),
|
||||
Bundle.EamDbSettingsDialog_okButton_createDbError_title(),
|
||||
@ -379,6 +390,10 @@ public class EamDbSettingsDialog extends JDialog {
|
||||
&& dbSettingsSqlite.insertDefaultDatabaseContent();
|
||||
}
|
||||
if (!result) {
|
||||
if(dbCreated){
|
||||
dbSettingsSqlite.deleteDatabase();
|
||||
}
|
||||
|
||||
JOptionPane.showMessageDialog(WindowManager.getDefault().getMainWindow(),
|
||||
Bundle.EamDbSettingsDialog_okButton_createSQLiteDbError_message(),
|
||||
Bundle.EamDbSettingsDialog_okButton_createDbError_title(),
|
||||
@ -395,6 +410,8 @@ public class EamDbSettingsDialog extends JDialog {
|
||||
@Messages({"EamDbSettingsDialog.okButton.errorTitle.text=Restart Required.",
|
||||
"EamDbSettingsDialog.okButton.errorMsg.text=Please restart Autopsy to begin using the new database platform.",
|
||||
"EamDbSettingsDialog.okButton.connectionErrorMsg.text=Failed to connect to Central Repository database.",
|
||||
"EamDbSettingsDialog.okButton.corruptDatabaseExists.title=Error Loading Database",
|
||||
"EamDbSettingsDialog.okButton.corruptDatabaseExists.message=Database exists but is not the right format. Manually delete it or choose a different path (if applicable).",
|
||||
"EamDbSettingsDialog.okButton.createDbDialog.title=Database Does Not Exist",
|
||||
"EamDbSettingsDialog.okButton.createDbDialog.message=Database does not exist, would you like to create it?",
|
||||
"EamDbSettingsDialog.okButton.databaseConnectionFailed.title=Database Connection Failed",
|
||||
@ -407,7 +424,13 @@ public class EamDbSettingsDialog extends JDialog {
|
||||
Bundle.EamDbSettingsDialog_okButton_databaseConnectionFailed_message(),
|
||||
Bundle.EamDbSettingsDialog_okButton_databaseConnectionFailed_title(),
|
||||
JOptionPane.WARNING_MESSAGE);
|
||||
} else if (testingStatus == DatabaseTestResult.SCHEMA_INVALID) {
|
||||
} else if (testingStatus == DatabaseTestResult.SCHEMA_INVALID){
|
||||
// There's an existing database or file, but it's not in our format.
|
||||
JOptionPane.showMessageDialog(WindowManager.getDefault().getMainWindow(),
|
||||
Bundle.EamDbSettingsDialog_okButton_corruptDatabaseExists_message(),
|
||||
Bundle.EamDbSettingsDialog_okButton_corruptDatabaseExists_title(),
|
||||
JOptionPane.WARNING_MESSAGE);
|
||||
} else if (testingStatus == DatabaseTestResult.DB_DOES_NOT_EXIST) {
|
||||
//database doesn't exist do you want to create
|
||||
if (JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(WindowManager.getDefault().getMainWindow(),
|
||||
Bundle.EamDbSettingsDialog_okButton_createDbDialog_message(),
|
||||
@ -751,6 +774,7 @@ public class EamDbSettingsDialog extends JDialog {
|
||||
UNTESTED,
|
||||
CONNECTION_FAILED,
|
||||
SCHEMA_INVALID,
|
||||
DB_DOES_NOT_EXIST,
|
||||
TESTEDOK;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user