Skip to content

Commit

Permalink
sql: Have MetaTable expose a "delete the table" method
Browse files Browse the repository at this point in the history
sql::MetaTable works by manipulating a caller-provided database to store
additional version information in a new metadata table. Some clients
using sql::MetaTable might start out with their databases unversioned
and switch to maintaining version metadata at a later time. In this
case, it can help to be able to test the no-metadata-maintained to
metadata-maintained upgrade.  However, once a database begins using a
metadata table, it might be difficult to emulate the state of the
database prior to using a metadata table. Clearing out the initialized
metadata table can achieve this effect, but the name of the metadata
table is an implementation detail, so it's not possible to just delete
the table directly from test code. To get around this issue, this CL
adds an sql::MetaTable::DeleteTableForTesting method to do just that.

As one example, the child CL crrev.com/c/2908133 uses this method to
verify its database correctly handles an upgrade from a state with no
version written to disk.

Bug: None
Change-Id: I5d440e11196638a04ad16beb4002ed5d3e25af19
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2920848
Commit-Queue: Victor Costan <pwnall@chromium.org>
Reviewed-by: Victor Costan <pwnall@chromium.org>
Cr-Commit-Position: refs/heads/master@{#894658}
  • Loading branch information
David Van Cleve authored and Chromium LUCI CQ committed Jun 22, 2021
1 parent 0c46622 commit f7b270f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
6 changes: 6 additions & 0 deletions sql/meta_table.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ bool MetaTable::DoesTableExist(sql::Database* db) {
return db->DoesTableExist("meta");
}

// static
bool MetaTable::DeleteTableForTesting(sql::Database* db) {
DCHECK(db);
return db->Execute("DROP TABLE IF EXISTS meta");
}

// static
bool MetaTable::GetMmapStatus(Database* db, int64_t* status) {
const char* kMmapStatusSql = "SELECT value FROM meta WHERE key = ?";
Expand Down
5 changes: 5 additions & 0 deletions sql/meta_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ class COMPONENT_EXPORT(SQL) MetaTable {
// Returns true if the 'meta' table exists.
static bool DoesTableExist(Database* db);

// Deletes the 'meta' table if it exists, returning false if an internal error
// occurred during the deletion and true otherwise (no matter whether the
// table existed).
static bool DeleteTableForTesting(Database* db);

// If the current version of the database is less than or equal to
// |deprecated_version|, raze the database. Must be called outside of a
// transaction.
Expand Down
8 changes: 8 additions & 0 deletions sql/meta_table_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ TEST_F(SQLMetaTableTest, DoesTableExist) {
EXPECT_TRUE(MetaTable::DoesTableExist(&db_));
}

TEST_F(SQLMetaTableTest, DeleteTableForTesting) {
MetaTable meta_table;
EXPECT_TRUE(meta_table.Init(&db_, 1, 1));

EXPECT_TRUE(MetaTable::DeleteTableForTesting(&db_));
EXPECT_FALSE(MetaTable::DoesTableExist(&db_));
}

TEST_F(SQLMetaTableTest, RazeIfDeprecated) {
const int kDeprecatedVersion = 1;
const int kVersion = 2;
Expand Down

0 comments on commit f7b270f

Please sign in to comment.