Bug 386394 - Add a BackupDB method to mozIStorageConnection. r=mano

This commit is contained in:
sdwilsh@shawnwilsher.com 2007-07-03 13:54:36 -07:00
parent f131cd226d
commit f7a000c5ad
3 changed files with 103 additions and 2 deletions

View File

@ -51,7 +51,7 @@ interface nsIFile;
* creating prepared statements, executing SQL, and examining database
* errors.
*/
[scriptable, uuid(0b3d9b17-6c07-4c39-9ee6-314f9fbc56a6)]
[scriptable, uuid(43933f76-3376-4999-9096-6f1d545fe5f6)]
interface mozIStorageConnection : nsISupports {
/*
* Initialization and status
@ -201,6 +201,25 @@ interface mozIStorageConnection : nsISupports {
in long aNumArguments,
in mozIStorageFunction aFunction);
/*
* Utilities
*/
/**
* Copies the current database file to the specified parent directory with the
* specified file name. If the parent directory is not specified, it places
* the backup in the same directory as the current file. This function
* ensures that the file being created is unique.
*
* @param aFileName
* The name of the new file to create.
* @param [optional] aParentDirectory
* The directory you'd like the file to be placed.
* @return The nsIFile representing the backup file.
*/
nsIFile backupDB(in AString aFileName,
[optional] in nsIFile aParentDirectory);
/**
* This is used to preload the database cache. It loads pages from the
* start of the database file until the memory cache (specified by

View File

@ -497,6 +497,48 @@ mozStorageConnection::CreateFunction(const char *aFunctionName,
return mFunctions->AppendElement(aFunction, PR_FALSE);
}
/**
** Utilities
**/
NS_IMETHODIMP
mozStorageConnection::BackupDB(const nsAString &aFileName,
nsIFile *aParentDirectory,
nsIFile **backup)
{
NS_ASSERTION(mDatabaseFile, "No database file to backup!");
nsresult rv;
nsCOMPtr<nsIFile> parentDir = aParentDirectory;
if (!parentDir) {
// This argument is optional, and defaults to the same parent directory
// as the current file.
rv = mDatabaseFile->GetParent(getter_AddRefs(parentDir));
NS_ENSURE_SUCCESS(rv, rv);
}
nsCOMPtr<nsIFile> backupDB;
rv = parentDir->Clone(getter_AddRefs(backupDB));
NS_ENSURE_SUCCESS(rv, rv);
rv = backupDB->Append(aFileName);
NS_ENSURE_SUCCESS(rv, rv);
rv = backupDB->CreateUnique(nsIFile::NORMAL_FILE_TYPE, 0600);
NS_ENSURE_SUCCESS(rv, rv);
nsAutoString fileName;
rv = backupDB->GetLeafName(fileName);
NS_ENSURE_SUCCESS(rv, rv);
rv = backupDB->Remove(PR_FALSE);
NS_ENSURE_SUCCESS(rv, rv);
backupDB.swap(*backup);
return mDatabaseFile->CopyTo(parentDir, fileName);
}
/**
* Mozilla-specific sqlite function to preload the DB into the cache. See the
* IDL and sqlite3.h

View File

@ -37,6 +37,8 @@
// This file tests the functions of mozIStorageConnection
const BACKUP_FILE_NAME = "test_storage.sqlite.backup";
function test_connectionReady()
{
// there doesn't seem to be a way for the connection to not be ready
@ -171,6 +173,42 @@ function test_set_schemaVersion_negative()
do_check_eq(version, msc.schemaVersion);
}
function test_backup_not_new_filename()
{
var msc = getOpenedDatabase();
const fname = getTestDB().leafName;
var backup = msc.backupDB(fname);
do_check_neq(fname, backup.leafName);
backup.remove(false);
}
function test_backup_new_filename()
{
var msc = getOpenedDatabase();
var backup = msc.backupDB(BACKUP_FILE_NAME);
do_check_eq(BACKUP_FILE_NAME, backup.leafName);
backup.remove(false);
}
function test_backup_new_folder()
{
var msc = getOpenedDatabase();
var parentDir = getTestDB().parent;
parentDir.append("test_storage_temp");
parentDir.create(Ci.nsIFile.DIRECTORY_TYPE, 0600);
do_check_true(parentDir.exists());
var backup = msc.backupDB(BACKUP_FILE_NAME, parentDir);
do_check_eq(BACKUP_FILE_NAME, backup.leafName);
do_check_true(parentDir.equals(backup.parent));
parentDir.remove(true);
}
var tests = [test_connectionReady, test_databaseFile,
test_tableExists_not_created, test_indexExists_not_created,
test_createTable_not_created, test_indexExists_created,
@ -179,7 +217,9 @@ var tests = [test_connectionReady, test_databaseFile,
test_commitTransaction_no_transaction,
test_rollbackTransaction_no_transaction,
test_get_schemaVersion_not_set, test_set_schemaVersion,
test_set_schemaVersion_same, test_set_schemaVersion_negative];
test_set_schemaVersion_same, test_set_schemaVersion_negative,
test_backup_not_new_filename, test_backup_new_filename,
test_backup_new_folder];
function run_test()
{