sql: Have MetaTable expose a "delete the table" method
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}
This commit is contained in:

committed by
Chromium LUCI CQ

parent
0c466221c8
commit
f7b270fdbb
@ -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 = ?";
|
||||
|
@ -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.
|
||||
|
@ -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;
|
||||
|
Reference in New Issue
Block a user