summaryrefslogtreecommitdiffstats
path: root/sql/connection_unittest.cc
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/connection_unittest.cc
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/connection_unittest.cc')
-rw-r--r--sql/connection_unittest.cc134
1 files changed, 133 insertions, 1 deletions
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().