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
@ -281,6 +281,27 @@ public final class PostgresEamDbSettings {
|
|||||||
return true;
|
return true;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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.
|
* Initialize the database schema.
|
||||||
|
@ -109,6 +109,20 @@ public final class SqliteEamDbSettings {
|
|||||||
ModuleSettings.setConfigSetting("CentralRepository", "db.sqlite.bulkThreshold", Integer.toString(getBulkThreshold())); // NON-NLS
|
ModuleSettings.setConfigSetting("CentralRepository", "db.sqlite.bulkThreshold", Integer.toString(getBulkThreshold())); // NON-NLS
|
||||||
ModuleSettings.setConfigSetting("CentralRepository", "db.badTags", String.join(",", badTags)); // NON-NLS
|
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.
|
* Verify that the db directory path exists.
|
||||||
@ -148,6 +162,15 @@ public final class SqliteEamDbSettings {
|
|||||||
|
|
||||||
return true;
|
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
|
* Get the full connection URL as a String
|
||||||
|
@ -319,26 +319,32 @@ public class EamDbSettingsDialog extends JDialog {
|
|||||||
switch (selectedPlatform) {
|
switch (selectedPlatform) {
|
||||||
case POSTGRESQL:
|
case POSTGRESQL:
|
||||||
if (dbSettingsPostgres.verifyConnection()) {
|
if (dbSettingsPostgres.verifyConnection()) {
|
||||||
if (dbSettingsPostgres.verifyDatabaseExists()
|
if (dbSettingsPostgres.verifyDatabaseExists()) {
|
||||||
&& dbSettingsPostgres.verifyDatabaseSchema()) {
|
if( dbSettingsPostgres.verifyDatabaseSchema()) {
|
||||||
testingStatus = DatabaseTestResult.TESTEDOK;
|
testingStatus = DatabaseTestResult.TESTEDOK;
|
||||||
|
} else {
|
||||||
|
testingStatus = DatabaseTestResult.SCHEMA_INVALID;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
testingStatus = DatabaseTestResult.SCHEMA_INVALID;
|
testingStatus = DatabaseTestResult.DB_DOES_NOT_EXIST;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
testingStatus = DatabaseTestResult.CONNECTION_FAILED;
|
testingStatus = DatabaseTestResult.CONNECTION_FAILED;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case SQLITE:
|
case SQLITE:
|
||||||
if (dbSettingsSqlite.dbDirectoryExists()
|
if (dbSettingsSqlite.dbFileExists()){
|
||||||
&& dbSettingsSqlite.verifyConnection()) {
|
if(dbSettingsSqlite.verifyConnection()) {
|
||||||
if (dbSettingsSqlite.verifyDatabaseSchema()) {
|
if (dbSettingsSqlite.verifyDatabaseSchema()) {
|
||||||
testingStatus = DatabaseTestResult.TESTEDOK;
|
testingStatus = DatabaseTestResult.TESTEDOK;
|
||||||
|
} else {
|
||||||
|
testingStatus = DatabaseTestResult.SCHEMA_INVALID;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
testingStatus = DatabaseTestResult.SCHEMA_INVALID;
|
testingStatus = DatabaseTestResult.SCHEMA_INVALID;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
testingStatus = DatabaseTestResult.SCHEMA_INVALID;
|
testingStatus = DatabaseTestResult.DB_DOES_NOT_EXIST;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -362,6 +368,11 @@ public class EamDbSettingsDialog extends JDialog {
|
|||||||
&& dbSettingsPostgres.insertDefaultDatabaseContent();
|
&& dbSettingsPostgres.insertDefaultDatabaseContent();
|
||||||
}
|
}
|
||||||
if (!result) {
|
if (!result) {
|
||||||
|
// Remove the incomplete database
|
||||||
|
if(dbCreated){
|
||||||
|
dbSettingsPostgres.deleteDatabase();
|
||||||
|
}
|
||||||
|
|
||||||
JOptionPane.showMessageDialog(WindowManager.getDefault().getMainWindow(),
|
JOptionPane.showMessageDialog(WindowManager.getDefault().getMainWindow(),
|
||||||
Bundle.EamDbSettingsDialog_okButton_createPostgresDbError_message(),
|
Bundle.EamDbSettingsDialog_okButton_createPostgresDbError_message(),
|
||||||
Bundle.EamDbSettingsDialog_okButton_createDbError_title(),
|
Bundle.EamDbSettingsDialog_okButton_createDbError_title(),
|
||||||
@ -379,6 +390,10 @@ public class EamDbSettingsDialog extends JDialog {
|
|||||||
&& dbSettingsSqlite.insertDefaultDatabaseContent();
|
&& dbSettingsSqlite.insertDefaultDatabaseContent();
|
||||||
}
|
}
|
||||||
if (!result) {
|
if (!result) {
|
||||||
|
if(dbCreated){
|
||||||
|
dbSettingsSqlite.deleteDatabase();
|
||||||
|
}
|
||||||
|
|
||||||
JOptionPane.showMessageDialog(WindowManager.getDefault().getMainWindow(),
|
JOptionPane.showMessageDialog(WindowManager.getDefault().getMainWindow(),
|
||||||
Bundle.EamDbSettingsDialog_okButton_createSQLiteDbError_message(),
|
Bundle.EamDbSettingsDialog_okButton_createSQLiteDbError_message(),
|
||||||
Bundle.EamDbSettingsDialog_okButton_createDbError_title(),
|
Bundle.EamDbSettingsDialog_okButton_createDbError_title(),
|
||||||
@ -395,6 +410,8 @@ public class EamDbSettingsDialog extends JDialog {
|
|||||||
@Messages({"EamDbSettingsDialog.okButton.errorTitle.text=Restart Required.",
|
@Messages({"EamDbSettingsDialog.okButton.errorTitle.text=Restart Required.",
|
||||||
"EamDbSettingsDialog.okButton.errorMsg.text=Please restart Autopsy to begin using the new database platform.",
|
"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.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.title=Database Does Not Exist",
|
||||||
"EamDbSettingsDialog.okButton.createDbDialog.message=Database does not exist, would you like to create it?",
|
"EamDbSettingsDialog.okButton.createDbDialog.message=Database does not exist, would you like to create it?",
|
||||||
"EamDbSettingsDialog.okButton.databaseConnectionFailed.title=Database Connection Failed",
|
"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_message(),
|
||||||
Bundle.EamDbSettingsDialog_okButton_databaseConnectionFailed_title(),
|
Bundle.EamDbSettingsDialog_okButton_databaseConnectionFailed_title(),
|
||||||
JOptionPane.WARNING_MESSAGE);
|
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
|
//database doesn't exist do you want to create
|
||||||
if (JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(WindowManager.getDefault().getMainWindow(),
|
if (JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(WindowManager.getDefault().getMainWindow(),
|
||||||
Bundle.EamDbSettingsDialog_okButton_createDbDialog_message(),
|
Bundle.EamDbSettingsDialog_okButton_createDbDialog_message(),
|
||||||
@ -751,6 +774,7 @@ public class EamDbSettingsDialog extends JDialog {
|
|||||||
UNTESTED,
|
UNTESTED,
|
||||||
CONNECTION_FAILED,
|
CONNECTION_FAILED,
|
||||||
SCHEMA_INVALID,
|
SCHEMA_INVALID,
|
||||||
|
DB_DOES_NOT_EXIST,
|
||||||
TESTEDOK;
|
TESTEDOK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user