summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorKonstantin Käfer <mail@kkaefer.com>2018-06-07 15:59:18 +0200
committerKonstantin Käfer <mail@kkaefer.com>2018-06-12 17:41:16 +0200
commit0ecdbebb092f17499718cb6ec8376edac46cad33 (patch)
treed0530a9dc45bd1b042e13ed908cda482e56a3c99 /test
parent79394ee47d60ee7db6bb516a0e858adb2e9239d3 (diff)
downloadqtlocation-mapboxgl-0ecdbebb092f17499718cb6ec8376edac46cad33.tar.gz
[core] consolidate database opening workflow
Before this change, we've tried to open the database in read/write, but not create mode. In situations where the database didn't exist yet, this logged an error to the console, and we proceeded to opening it again in read/write/create mode, which actually created the file. The reason we did this is so that we could detect really old caching databases from January 2016 in case a developer upgraded from an older SDK (iOS v3.1 and earlier, Android v3.2 and earlier) that didn't have https://github.com/mapbox/mapbox-gl-native/pull/3715 yet. However, these error messages, while innocent, look scary in the console and some users suspect that it's a bug. This change opens the file directly in read/write/create mode, omitting the first failed attempt. To handle old cache databases, we're now deleting the `http_cache` table, which was the only table in those old databases, and create the new schema, rather than deleting the entire file and recreating the Database object. In most scenarios, this will lead to one fewer opening attempt, while the database migration will continue to work for the few users who upgrade all the way from a January 2016 SDK. Additionally, this fixes a mismatch between the Qt and non-Qt implementation: Qt doesn't support opening a file in read/write mode without the create flag. This means that we've seen a different control flow on Qt compared to the non-Qt implementation when opening a database.
Diffstat (limited to 'test')
-rw-r--r--test/storage/offline_database.test.cpp25
-rw-r--r--test/storage/sqlite.test.cpp6
2 files changed, 4 insertions, 27 deletions
diff --git a/test/storage/offline_database.test.cpp b/test/storage/offline_database.test.cpp
index e54cd6813b..36b71bc4b4 100644
--- a/test/storage/offline_database.test.cpp
+++ b/test/storage/offline_database.test.cpp
@@ -23,17 +23,6 @@ TEST(OfflineDatabase, TEST_REQUIRES_WRITE(Create)) {
OfflineDatabase db(filename);
EXPECT_FALSE(bool(db.get({ Resource::Unknown, "mapbox://test" })));
-#ifdef __QT__
- // Qt doesn't support opening a SQLite database without also creating it if it doesn't exist yet.
- // Therefore, we're currently using the code path that thinks that we opened an old database
- // (user_version = 0), deletes and recreates the database.
- EXPECT_EQ(1u, log.count({ EventSeverity::Warning, Event::Database, -1, "Removing existing incompatible offline database" }));
-#endif
-#ifndef __QT__
- // Only non-Qt platforms are setting a logger on the SQLite object.
- EXPECT_EQ(1u, log.count({ EventSeverity::Info, Event::Database, static_cast<int64_t>(mapbox::sqlite::ResultCode::CantOpen), "cannot open file" }, true));
- EXPECT_EQ(1u, log.count({ EventSeverity::Info, Event::Database, static_cast<int64_t>(mapbox::sqlite::ResultCode::CantOpen), "No such file or directory" }, true));
-#endif
EXPECT_EQ(0u, log.uncheckedCount());
}
@@ -42,13 +31,12 @@ TEST(OfflineDatabase, TEST_REQUIRES_WRITE(SchemaVersion)) {
util::deleteFile(filename);
{
- mapbox::sqlite::Database db = mapbox::sqlite::Database::open(filename, mapbox::sqlite::Create | mapbox::sqlite::ReadWrite);
+ mapbox::sqlite::Database db = mapbox::sqlite::Database::open(filename, mapbox::sqlite::ReadWriteCreate);
db.exec("PRAGMA user_version = 1");
}
OfflineDatabase db(filename);
- EXPECT_EQ(1u, log.count({ EventSeverity::Warning, Event::Database, -1, "Removing existing incompatible offline database" }));
EXPECT_EQ(0u, log.uncheckedCount());
}
@@ -339,17 +327,6 @@ TEST(OfflineDatabase, TEST_REQUIRES_WRITE(ConcurrentUse)) {
util::deleteFile(filename);
OfflineDatabase db1(filename);
-#ifdef __QT__
- // Qt doesn't support opening a SQLite database without also creating it if it doesn't exist yet.
- // Therefore, we're currently using the code path that thinks that we opened an old database
- // (user_version = 0), deletes and recreates the database.
- EXPECT_EQ(1u, log.count({ EventSeverity::Warning, Event::Database, -1, "Removing existing incompatible offline database" }));
-#endif
-#ifndef __QT__
- // Only non-Qt platforms are setting a logger on the SQLite object.
- EXPECT_EQ(1u, log.count({ EventSeverity::Info, Event::Database, static_cast<int64_t>(mapbox::sqlite::ResultCode::CantOpen), "cannot open file" }, true));
- EXPECT_EQ(1u, log.count({ EventSeverity::Info, Event::Database, static_cast<int64_t>(mapbox::sqlite::ResultCode::CantOpen), "No such file or directory" }, true));
-#endif
EXPECT_EQ(0u, log.uncheckedCount());
OfflineDatabase db2(filename);
diff --git a/test/storage/sqlite.test.cpp b/test/storage/sqlite.test.cpp
index a7c9d95e9b..22958c8bed 100644
--- a/test/storage/sqlite.test.cpp
+++ b/test/storage/sqlite.test.cpp
@@ -8,7 +8,7 @@ using namespace mbgl;
TEST(SQLite, Statement) {
using namespace mbgl;
- mapbox::sqlite::Database db = mapbox::sqlite::Database::open(":memory:", mapbox::sqlite::Create | mapbox::sqlite::ReadWrite);
+ mapbox::sqlite::Database db = mapbox::sqlite::Database::open(":memory:", mapbox::sqlite::ReadWriteCreate);
db.exec("CREATE TABLE test (id INTEGER);");
mapbox::sqlite::Statement stmt1{ db, "INSERT INTO test (id) VALUES (?1);" };
@@ -49,7 +49,7 @@ TEST(SQLite, TEST_REQUIRES_WRITE(TryOpen)) {
TEST(SQLite, CloseDatabaseWithPendingTransaction) {
auto db = std::make_unique<mapbox::sqlite::Database>(mapbox::sqlite::Database::open(
- ":memory:", mapbox::sqlite::ReadWrite | mapbox::sqlite::Create));
+ ":memory:", mapbox::sqlite::ReadWriteCreate));
mapbox::sqlite::Transaction transaction(*db);
transaction.commit();
}
@@ -58,7 +58,7 @@ TEST(SQLite, CloseMovedDatabaseWithPendingTransaction) {
// Verifies that we can correctly commit a transaction even if we move the Database object to
// another address.
auto db1 = mapbox::sqlite::Database::open(":memory:",
- mapbox::sqlite::ReadWrite | mapbox::sqlite::Create);
+ mapbox::sqlite::ReadWriteCreate);
std::unique_ptr<mapbox::sqlite::Database> db2;
mapbox::sqlite::Transaction transaction(db1);
db2 = std::make_unique<mapbox::sqlite::Database>(std::move(db1));