Index: sql/connection.cc |
diff --git a/sql/connection.cc b/sql/connection.cc |
index 378e01472aee83efdc70b547d9dac432e4706184..f72ed1b075de976eadf46265e177baef634182d8 100644 |
--- a/sql/connection.cc |
+++ b/sql/connection.cc |
@@ -168,6 +168,78 @@ void Connection::Preload() { |
#endif |
} |
+// Create an in-memory database with the existing database's page |
+// size, then backup that database over the existing database. |
+bool Connection::Raze() { |
+ if (!db_) { |
+ DLOG(FATAL) << "Cannot raze null db"; |
+ return false; |
+ } |
+ |
+ if (transaction_nesting_ > 0) { |
+ DLOG(FATAL) << "Cannot raze within a transaction"; |
+ return false; |
+ } |
+ |
+ sql::Connection null_db; |
+ if (!null_db.OpenInMemory()) { |
+ DLOG(FATAL) << "Unable to open in-memory database."; |
+ return false; |
+ } |
+ |
+ // Propagate the page size to the new database. |
Greg Billock
2012/03/31 01:23:50
Maybe say that this gets the page_size from the cu
Scott Hess - ex-Googler
2012/04/04 01:28:25
OK.
|
+ Statement s(GetUniqueStatement("PRAGMA page_size")); |
+ if (!s.Step()) |
+ return false; |
+ |
+ const std::string sql = StringPrintf("PRAGMA page_size=%d", s.ColumnInt(0)); |
+ if (!null_db.Execute(sql.c_str())) |
+ return false; |
+ |
+ // At this point the null database has no pages. Force it to have |
+ // one. The backup API tracks the original schema from the |
Greg Billock
2012/03/31 01:23:50
so the new DB after the backup will have schema_ve
Scott Hess - ex-Googler
2012/04/04 01:28:25
The backup API restores the backup destination's s
|
+ // destination database and writes it back (incremented once) after |
+ // the backup. |
+ if (!null_db.Execute("PRAGMA schema_version = 1")) |
+ return false; |
+ |
+ sqlite3_backup* backup = sqlite3_backup_init(db_, "main", |
+ null_db.db_, "main"); |
+ if (!backup) { |
+ DLOG(FATAL) << "Unable to start sqlite3_backup()."; |
+ return false; |
+ } |
+ |
+ ScopedBusyTimeout busy_timeout(db_); |
+ busy_timeout.SetTimeout(base::TimeDelta::FromSeconds(1)); |
Greg Billock
2012/03/31 01:23:50
Just rely on callers to use RazeWithTimeout here?
Scott Hess - ex-Googler
2012/04/04 01:28:25
That is a really good suggestion. I must have bee
|
+ |
+ int rc = sqlite3_backup_step(backup, 1); |
Greg Billock
2012/03/31 01:23:50
doc says pass -1 to copy all remaining pages. Woul
Scott Hess - ex-Googler
2012/04/04 01:28:25
Partly because I must have missed the -1 setting.
Scott Hess - ex-Googler
2012/04/04 19:26:54
I modified this code to use sqlite3_backup_pagecou
|
+ sqlite3_backup_finish(backup); |
+ |
+ // The destination database was locked. |
+ if (rc == SQLITE_BUSY) { |
Greg Billock
2012/03/31 01:23:50
Is there a way to force it open? Presumably someth
Scott Hess - ex-Googler
2012/04/04 01:28:25
Unfortunately, there's no way to force it open wit
|
+ return false; |
+ } |
+ |
+ // There should only be a single page to copy. |
Greg Billock
2012/03/31 01:23:50
Using -1 would mean you don't need this, I think.
|
+ if (rc != SQLITE_DONE) { |
+ DLOG(FATAL) << "Unable to copy entire null database."; |
+ return false; |
+ } |
+ |
+ return true; |
+} |
+bool Connection::RazeWithTimout(base::TimeDelta timeout) { |
+ if (!db_) { |
+ DLOG(FATAL) << "Cannot raze null db"; |
+ return false; |
+ } |
+ |
+ ScopedBusyTimeout busy_timeout(db_); |
+ busy_timeout.SetTimeout(timeout); |
+ return Raze(); |
+} |
+ |
bool Connection::BeginTransaction() { |
if (needs_rollback_) { |
DCHECK_GT(transaction_nesting_, 0); |