summaryrefslogtreecommitdiffstats
path: root/sql
diff options
context:
space:
mode:
authorshess@chromium.org <shess@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-04-06 19:36:49 +0000
committershess@chromium.org <shess@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-04-06 19:36:49 +0000
commit8e0c0128f4860cc1393eb458f9b389fce5e46a93 (patch)
tree3fecb5a2b13c74d9ec8540ae2de73b9de2ad57b4 /sql
parent2781409defa7484f927ffe302c70cebbf7967134 (diff)
downloadchromium_src-8e0c0128f4860cc1393eb458f9b389fce5e46a93.zip
chromium_src-8e0c0128f4860cc1393eb458f9b389fce5e46a93.tar.gz
chromium_src-8e0c0128f4860cc1393eb458f9b389fce5e46a93.tar.bz2
Implement sql::Connection::Raze() in terms of sqlite3_backup API.
Wraps up the notion of reseting a database in a way which respects SQLite locking constraints and doesn't require closing the database. A similar outcome could be managed using filesystem operations, which requires coordination between clients of the database to make sure that no corruption occurs due to incorrect handling of -journal files. Also, Windows pins files until the last handle closes, making that approach challenging in some cases. BUG=none TEST=none Review URL: https://chromiumcodereview.appspot.com/9768006 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@131167 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'sql')
-rw-r--r--sql/connection.cc79
-rw-r--r--sql/connection.h20
-rw-r--r--sql/connection_unittest.cc134
3 files changed, 232 insertions, 1 deletions
diff --git a/sql/connection.cc b/sql/connection.cc
index 58bfb11..b2274f1 100644
--- a/sql/connection.cc
+++ b/sql/connection.cc
@@ -168,6 +168,85 @@ 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;
+ }
+
+ // Get the page size from the current connection, then propagate it
+ // to the null database.
+ 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;
+
+ // The page size doesn't take effect until a database has pages, and
+ // at this point the null database has none. Changing the schema
+ // version will create the first page. This will not affect the
+ // schema version in the resulting database, as SQLite's backup
+ // implementation propagates the schema version from the original
+ // connection to the new version of the database, incremented by one
+ // so that other readers see the schema change and act accordingly.
+ 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;
+ }
+
+ // -1 backs up the entire database.
+ int rc = sqlite3_backup_step(backup, -1);
+ int pages = sqlite3_backup_pagecount(backup);
+ sqlite3_backup_finish(backup);
+
+ // The destination database was locked.
+ if (rc == SQLITE_BUSY) {
+ return false;
+ }
+
+ // The entire database should have been backed up.
+ if (rc != SQLITE_DONE) {
+ DLOG(FATAL) << "Unable to copy entire null database.";
+ return false;
+ }
+
+ // Exactly one page should have been backed up. If this breaks,
+ // check this function to make sure assumptions aren't being broken.
+ DCHECK_EQ(pages, 1);
+
+ 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);
diff --git a/sql/connection.h b/sql/connection.h
index 9b81804b..1e3414f 100644
--- a/sql/connection.h
+++ b/sql/connection.h
@@ -181,6 +181,26 @@ class SQL_EXPORT Connection {
// generally exist either.
void Preload();
+ // Raze the database to the ground. This approximates creating a
+ // fresh database from scratch, within the constraints of SQLite's
+ // locking protocol (locks and open handles can make doing this with
+ // filesystem operations problematic). Returns true if the database
+ // was razed.
+ //
+ // false is returned if the database is locked by some other
+ // process. RazeWithTimeout() may be used if appropriate.
+ //
+ // NOTE(shess): Raze() will DCHECK in the following situations:
+ // - database is not open.
+ // - the connection has a transaction open.
+ // - a SQLite issue occurs which is structural in nature (like the
+ // statements used are broken).
+ // Since Raze() is expected to be called in unexpected situations,
+ // these all return false, since it is unlikely that the caller
+ // could fix them.
+ bool Raze();
+ bool RazeWithTimout(base::TimeDelta timeout);
+
// Transactions --------------------------------------------------------------
// Transaction management. We maintain a virtual transaction stack to emulate
diff --git a/sql/connection_unittest.cc b/sql/connection_unittest.cc
index 58734ac..96cc25b 100644
--- a/sql/connection_unittest.cc
+++ b/sql/connection_unittest.cc
@@ -15,7 +15,7 @@ class SQLConnectionTest : public testing::Test {
void SetUp() {
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
- ASSERT_TRUE(db_.Open(temp_dir_.path().AppendASCII("SQLConnectionTest.db")));
+ ASSERT_TRUE(db_.Open(db_path()));
}
void TearDown() {
@@ -24,6 +24,10 @@ class SQLConnectionTest : public testing::Test {
sql::Connection& db() { return db_; }
+ FilePath db_path() {
+ return temp_dir_.path().AppendASCII("SQLConnectionTest.db");
+ }
+
private:
ScopedTempDir temp_dir_;
sql::Connection db_;
@@ -129,3 +133,131 @@ TEST_F(SQLConnectionTest, Rollback) {
EXPECT_FALSE(db().CommitTransaction());
EXPECT_TRUE(db().BeginTransaction());
}
+
+// Test that sql::Connection::Raze() results in a database without the
+// tables from the original database.
+TEST_F(SQLConnectionTest, Raze) {
+ const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)";
+ ASSERT_TRUE(db().Execute(kCreateSql));
+ ASSERT_TRUE(db().Execute("INSERT INTO foo (value) VALUES (12)"));
+
+ {
+ sql::Statement s(db().GetUniqueStatement("PRAGMA page_count"));
+ ASSERT_TRUE(s.Step());
+ EXPECT_EQ(2, s.ColumnInt(0));
+ }
+
+ {
+ sql::Statement s(db().GetUniqueStatement("SELECT * FROM sqlite_master"));
+ ASSERT_TRUE(s.Step());
+ EXPECT_EQ("table", s.ColumnString(0));
+ EXPECT_EQ("foo", s.ColumnString(1));
+ EXPECT_EQ("foo", s.ColumnString(2));
+ EXPECT_EQ(2, s.ColumnInt(3));
+ EXPECT_EQ(kCreateSql, s.ColumnString(4));
+ }
+
+ ASSERT_TRUE(db().Raze());
+
+ {
+ sql::Statement s(db().GetUniqueStatement("PRAGMA page_count"));
+ ASSERT_TRUE(s.Step());
+ EXPECT_EQ(1, s.ColumnInt(0));
+ }
+
+ {
+ sql::Statement s(db().GetUniqueStatement("SELECT * FROM sqlite_master"));
+ ASSERT_FALSE(s.Step());
+ }
+}
+
+// Test that Raze() maintains page_size.
+TEST_F(SQLConnectionTest, RazePageSize) {
+ const int kPageSize = 4096;
+
+ // Make sure that the default size isn't already |kPageSize|.
+ // Scoped to release statement before Close().
+ {
+ sql::Statement s(db().GetUniqueStatement("PRAGMA page_size"));
+ ASSERT_TRUE(s.Step());
+ ASSERT_NE(kPageSize, s.ColumnInt(0));
+ }
+
+ // Re-open the database to allow setting the page size.
+ db().Close();
+ db().set_page_size(kPageSize);
+ ASSERT_TRUE(db().Open(db_path()));
+
+ // page_size should match the indicated value.
+ sql::Statement s(db().GetUniqueStatement("PRAGMA page_size"));
+ ASSERT_TRUE(s.Step());
+ ASSERT_EQ(kPageSize, s.ColumnInt(0));
+
+ // After raze, page_size should still match the indicated value.
+ ASSERT_TRUE(db().Raze());
+ s.Reset();
+ ASSERT_TRUE(s.Step());
+ ASSERT_EQ(kPageSize, s.ColumnInt(0));
+}
+
+// Test that Raze() results are seen in other connections.
+TEST_F(SQLConnectionTest, RazeMultiple) {
+ const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)";
+ ASSERT_TRUE(db().Execute(kCreateSql));
+
+ sql::Connection other_db;
+ ASSERT_TRUE(other_db.Open(db_path()));
+
+ // Check that the second connection sees the table.
+ const char *kTablesQuery = "SELECT COUNT(*) FROM sqlite_master";
+ sql::Statement s(other_db.GetUniqueStatement(kTablesQuery));
+ ASSERT_TRUE(s.Step());
+ ASSERT_EQ(1, s.ColumnInt(0));
+ ASSERT_FALSE(s.Step()); // Releases the shared lock.
+
+ ASSERT_TRUE(db().Raze());
+
+ // The second connection sees the updated database.
+ s.Reset();
+ ASSERT_TRUE(s.Step());
+ ASSERT_EQ(0, s.ColumnInt(0));
+}
+
+TEST_F(SQLConnectionTest, RazeLocked) {
+ const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)";
+ ASSERT_TRUE(db().Execute(kCreateSql));
+
+ // Open a transaction and write some data in a second connection.
+ // This will acquire a PENDING or EXCLUSIVE transaction, which will
+ // cause the raze to fail.
+ sql::Connection other_db;
+ ASSERT_TRUE(other_db.Open(db_path()));
+ ASSERT_TRUE(other_db.BeginTransaction());
+ const char* kInsertSql = "INSERT INTO foo VALUES (1, 'data')";
+ ASSERT_TRUE(other_db.Execute(kInsertSql));
+
+ ASSERT_FALSE(db().Raze());
+
+ // Works after COMMIT.
+ ASSERT_TRUE(other_db.CommitTransaction());
+ ASSERT_TRUE(db().Raze());
+
+ // Re-create the database.
+ ASSERT_TRUE(db().Execute(kCreateSql));
+ ASSERT_TRUE(db().Execute(kInsertSql));
+
+ // An unfinished read transaction in the other connection also
+ // blocks raze.
+ const char *kQuery = "SELECT COUNT(*) FROM foo";
+ sql::Statement s(other_db.GetUniqueStatement(kQuery));
+ ASSERT_TRUE(s.Step());
+ ASSERT_FALSE(db().Raze());
+
+ // Complete the statement unlocks the database.
+ ASSERT_FALSE(s.Step());
+ ASSERT_TRUE(db().Raze());
+}
+
+// TODO(shess): Spin up a background thread to hold other_db, to more
+// closely match real life. That would also allow testing
+// RazeWithTimeout().