Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(193)

Unified Diff: components/offline_pages/offline_page_metadata_store_sql.cc

Issue 2336813002: [Offline pages] OPMStoreSQL: Upgrade to M55, Upgrade refactoring, conversion refactoring (Closed)
Patch Set: Removing the version field Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: components/offline_pages/offline_page_metadata_store_sql.cc
diff --git a/components/offline_pages/offline_page_metadata_store_sql.cc b/components/offline_pages/offline_page_metadata_store_sql.cc
index b15d72214e7396ebae626e6d65388c69c6c308f3..c8f6bec773aeb8c4425c3e611035194a72313bc4 100644
--- a/components/offline_pages/offline_page_metadata_store_sql.cc
+++ b/components/offline_pages/offline_page_metadata_store_sql.cc
@@ -33,29 +33,19 @@ bool CreateOfflinePagesTable(sql::Connection* db) {
"(offline_id INTEGER PRIMARY KEY NOT NULL,"
" creation_time INTEGER NOT NULL,"
" file_size INTEGER NOT NULL,"
- " version INTEGER NOT NULL,"
" last_access_time INTEGER NOT NULL,"
" access_count INTEGER NOT NULL,"
- // TODO(fgorski): Remove. Never used.
- " status INTEGER NOT NULL DEFAULT 0,"
- // A note on this field: It will be NULL for now and is
- // reserved for later use. We will treat NULL as
- // "Unknown" in any subsequent queries for user_initiated
- // values.
- " user_initiated INTEGER," // this is actually a boolean
" expiration_time INTEGER NOT NULL DEFAULT 0,"
" client_namespace VARCHAR NOT NULL,"
" client_id VARCHAR NOT NULL,"
" online_url VARCHAR NOT NULL,"
- // TODO(fgorski): Remove. Never used.
- " offline_url VARCHAR NOT NULL DEFAULT '',"
" file_path VARCHAR NOT NULL,"
" title VARCHAR NOT NULL DEFAULT ''"
")";
return db->Execute(kSql);
}
-bool RefreshColumnsFrom52To54(sql::Connection* db) {
+bool UpgradeFrom52(sql::Connection* db) {
if (!db->Execute("ALTER TABLE " OFFLINE_PAGES_TABLE_NAME
" RENAME TO temp_" OFFLINE_PAGES_TABLE_NAME)) {
return false;
@@ -64,13 +54,13 @@ bool RefreshColumnsFrom52To54(sql::Connection* db) {
return false;
if (!db->Execute(
"INSERT INTO " OFFLINE_PAGES_TABLE_NAME
- " (offline_id, creation_time, file_size, version, last_access_time, "
- "access_count, status, user_initiated, client_namespace, client_id, "
- "online_url, offline_url, file_path) "
- "SELECT offline_id, creation_time, file_size, version, "
- "last_access_time, "
- "access_count, status, user_initiated, client_namespace, client_id, "
- "online_url, offline_url, file_path "
+ " (offline_id, creation_time, file_size, last_access_time, "
+ "access_count, client_namespace, client_id, "
+ "online_url, file_path) "
+ "SELECT "
+ "offline_id, creation_time, file_size, last_access_time, "
+ "access_count, client_namespace, client_id, "
+ "online_url, file_path "
"FROM temp_" OFFLINE_PAGES_TABLE_NAME)) {
return false;
}
@@ -80,7 +70,7 @@ bool RefreshColumnsFrom52To54(sql::Connection* db) {
return true;
}
-bool RefreshColumnsFrom53To54(sql::Connection* db) {
+bool UpgradeFrom53(sql::Connection* db) {
if (!db->Execute("ALTER TABLE " OFFLINE_PAGES_TABLE_NAME
" RENAME TO temp_" OFFLINE_PAGES_TABLE_NAME)) {
return false;
@@ -89,13 +79,38 @@ bool RefreshColumnsFrom53To54(sql::Connection* db) {
return false;
if (!db->Execute(
"INSERT INTO " OFFLINE_PAGES_TABLE_NAME
- " (offline_id, creation_time, file_size, version, last_access_time, "
- "access_count, status, user_initiated, client_namespace, client_id, "
- "online_url, offline_url, file_path, expiration_time) "
- "SELECT offline_id, creation_time, file_size, version, "
- "last_access_time, "
- "access_count, status, user_initiated, client_namespace, client_id, "
- "online_url, offline_url, file_path, expiration_time "
+ " (offline_id, creation_time, file_size, last_access_time, "
+ "access_count, expiration_time, client_namespace, client_id, "
+ "online_url, file_path) "
+ "SELECT "
+ "offline_id, creation_time, file_size, last_access_time, "
+ "access_count, expiration_time, client_namespace, client_id, "
+ "online_url, file_path "
+ "FROM temp_" OFFLINE_PAGES_TABLE_NAME)) {
+ return false;
+ }
+ if (!db->Execute("DROP TABLE IF EXISTS temp_" OFFLINE_PAGES_TABLE_NAME))
+ return false;
+
+ return true;
+}
+
+bool UpgradeFrom54(sql::Connection* db) {
+ if (!db->Execute("ALTER TABLE " OFFLINE_PAGES_TABLE_NAME
+ " RENAME TO temp_" OFFLINE_PAGES_TABLE_NAME)) {
+ return false;
+ }
+ if (!CreateOfflinePagesTable(db))
+ return false;
+ if (!db->Execute(
dewittj 2016/09/13 18:10:07 Now that there are 3 copies of this function, it s
fgorski 2016/09/13 21:40:21 Done.
+ "INSERT INTO " OFFLINE_PAGES_TABLE_NAME
+ " (offline_id, creation_time, file_size, last_access_time, "
+ "access_count, expiration_time, client_namespace, client_id, "
+ "online_url, file_path, title) "
+ "SELECT "
+ "offline_id, creation_time, file_size, last_access_time, "
+ "access_count, expiration_time, client_namespace, client_id, "
+ "online_url, file_path, title "
"FROM temp_" OFFLINE_PAGES_TABLE_NAME)) {
return false;
}
@@ -117,11 +132,17 @@ bool CreateSchema(sql::Connection* db) {
return false;
}
+ // Last Schema update was M55. When making a new Schema update, please update
+ // all methods below to appropriately upgrade from their respective milestones
+ // and add appropriate method to cover changes from M55.
dewittj 2016/09/13 18:10:08 I would like to see somewhere a log of the changes
romax 2016/09/13 18:32:14 I wish these comments can be with those upgrade fu
fgorski 2016/09/13 21:40:21 Done.
if (!db->DoesColumnExist(OFFLINE_PAGES_TABLE_NAME, "expiration_time")) {
- if (!RefreshColumnsFrom52To54(db))
+ if (!UpgradeFrom52(db))
return false;
} else if (!db->DoesColumnExist(OFFLINE_PAGES_TABLE_NAME, "title")) {
- if (!RefreshColumnsFrom53To54(db))
+ if (!UpgradeFrom53(db))
+ return false;
+ } else if (db->DoesColumnExist(OFFLINE_PAGES_TABLE_NAME, "offline_url")) {
+ if (!UpgradeFrom54(db))
return false;
}
@@ -137,6 +158,26 @@ bool DeleteByOfflineId(sql::Connection* db, int64_t offline_id) {
return statement.Run();
}
+base::FilePath GetPathFromString(const std::string& path_string) {
dewittj 2016/09/13 18:10:07 Maybe name this GetPathFromUTF8String (and similar
fgorski 2016/09/13 21:40:21 Done.
+#if defined(OS_POSIX)
+ return base::FilePath(path_string);
+#elif defined(OS_WIN)
+ return base::FilePath(base::UTF8ToWide(path_string));
+#else
+#error Unknown OS
+#endif
+}
+
+std::string GetStringFromPath(const base::FilePath& path) {
+#if defined(OS_POSIX)
+ return path.value();
+#elif defined(OS_WIN)
+ return base::WideToUTF8(path.value());
+#else
+#error Unknown OS
+#endif
+}
+
// Create an offline page item from a SQL result. Expects complete rows with
// all columns present.
OfflinePageItem MakeOfflinePageItem(sql::Statement* statement) {
@@ -144,25 +185,21 @@ OfflinePageItem MakeOfflinePageItem(sql::Statement* statement) {
base::Time creation_time =
base::Time::FromInternalValue(statement->ColumnInt64(1));
int64_t file_size = statement->ColumnInt64(2);
- ClientId client_id(statement->ColumnString(9),
- statement->ColumnString(10));
- GURL url(statement->ColumnString(11));
-#if defined(OS_POSIX)
- base::FilePath path(statement->ColumnString(13));
-#elif defined(OS_WIN)
- base::FilePath path(base::UTF8ToWide(statement->ColumnString(13)));
-#else
-#error Unknown OS
-#endif
+ base::Time last_access_time =
+ base::Time::FromInternalValue(statement->ColumnInt64(3));
+ int access_count = statement->ColumnInt(4);
+ base::Time expiration_time =
+ base::Time::FromInternalValue(statement->ColumnInt64(5));
+ ClientId client_id(statement->ColumnString(6), statement->ColumnString(7));
+ GURL url(statement->ColumnString(8));
+ base::FilePath path(GetPathFromString(statement->ColumnString(9)));
+ base::string16 title = statement->ColumnString16(10);
OfflinePageItem item(url, id, client_id, path, file_size, creation_time);
- item.version = statement->ColumnInt(3);
- item.last_access_time = base::Time::FromInternalValue(
- statement->ColumnInt64(4));
- item.access_count = statement->ColumnInt(5);
- item.expiration_time =
- base::Time::FromInternalValue(statement->ColumnInt64(8));
- item.title = statement->ColumnString16(14);
+ item.last_access_time = last_access_time;
+ item.access_count = access_count;
+ item.expiration_time = expiration_time;
+ item.title = title;
return item;
}
@@ -170,31 +207,23 @@ bool InsertOrReplace(sql::Connection* db, const OfflinePageItem& item) {
const char kSql[] =
"INSERT OR REPLACE INTO " OFFLINE_PAGES_TABLE_NAME
" (offline_id, online_url, client_namespace, client_id, file_path, "
- "file_size, creation_time, last_access_time, version, access_count, "
+ "file_size, creation_time, last_access_time, access_count, "
"expiration_time, title)"
" VALUES "
- " (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
+ " (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
sql::Statement statement(db->GetCachedStatement(SQL_FROM_HERE, kSql));
statement.BindInt64(0, item.offline_id);
statement.BindString(1, item.url.spec());
statement.BindString(2, item.client_id.name_space);
statement.BindString(3, item.client_id.id);
-#if defined(OS_POSIX)
- std::string path_string = item.file_path.value();
-#elif defined(OS_WIN)
- std::string path_string = base::WideToUTF8(item.file_path.value());
-#else
-#error Unknown OS
-#endif
- statement.BindString(4, path_string);
+ statement.BindString(4, GetStringFromPath(item.file_path));
statement.BindInt64(5, item.file_size);
statement.BindInt64(6, item.creation_time.ToInternalValue());
statement.BindInt64(7, item.last_access_time.ToInternalValue());
- statement.BindInt(8, item.version);
- statement.BindInt(9, item.access_count);
- statement.BindInt64(10, item.expiration_time.ToInternalValue());
- statement.BindString16(11, item.title);
+ statement.BindInt(8, item.access_count);
+ statement.BindInt64(9, item.expiration_time.ToInternalValue());
+ statement.BindString16(10, item.title);
return statement.Run();
}

Powered by Google App Engine
This is Rietveld 408576698