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

Side by Side Diff: components/offline_pages/offline_page_metadata_store_sql.cc

Issue 2019333008: [Offline Pages] Fix crash due to table inconsistency. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: comments from jianli@ Created 4 years, 6 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 unified diff | Download patch
« no previous file with comments | « components/offline_pages/offline_page_metadata_store_impl_unittest.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/offline_pages/offline_page_metadata_store_sql.h" 5 #include "components/offline_pages/offline_page_metadata_store_sql.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/files/file_path.h" 8 #include "base/files/file_path.h"
9 #include "base/files/file_util.h" 9 #include "base/files/file_util.h"
10 #include "base/location.h" 10 #include "base/location.h"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/metrics/histogram_macros.h" 12 #include "base/metrics/histogram_macros.h"
13 #include "base/sequenced_task_runner.h" 13 #include "base/sequenced_task_runner.h"
14 #include "base/strings/utf_string_conversions.h" 14 #include "base/strings/utf_string_conversions.h"
15 #include "base/threading/thread_task_runner_handle.h" 15 #include "base/threading/thread_task_runner_handle.h"
16 #include "components/offline_pages/offline_page_item.h" 16 #include "components/offline_pages/offline_page_item.h"
17 #include "sql/connection.h" 17 #include "sql/connection.h"
18 #include "sql/statement.h" 18 #include "sql/statement.h"
19 #include "sql/transaction.h" 19 #include "sql/transaction.h"
20 20
21 namespace offline_pages { 21 namespace offline_pages {
22 22
23 namespace { 23 namespace {
24 24
25 // This is a macro instead of a const so that 25 // This is a macro instead of a const so that
26 // it can be used inline in other SQL statements below. 26 // it can be used inline in other SQL statements below.
27 #define OFFLINE_PAGES_TABLE_NAME "offlinepages_v1" 27 #define OFFLINE_PAGES_TABLE_NAME "offlinepages_v1"
28 28
29 // New columns should be added at the end of the list in order to avoid
30 // complicated table upgrade.
29 const char kOfflinePagesColumns[] = 31 const char kOfflinePagesColumns[] =
30 "(offline_id INTEGER PRIMARY KEY NOT NULL," 32 "(offline_id INTEGER PRIMARY KEY NOT NULL,"
31 " creation_time INTEGER NOT NULL," 33 " creation_time INTEGER NOT NULL,"
32 " file_size INTEGER NOT NULL," 34 " file_size INTEGER NOT NULL,"
33 " version INTEGER NOT NULL," 35 " version INTEGER NOT NULL,"
34 " last_access_time INTEGER NOT NULL," 36 " last_access_time INTEGER NOT NULL,"
35 " access_count INTEGER NOT NULL," 37 " access_count INTEGER NOT NULL,"
36 " status INTEGER NOT NULL DEFAULT 0," 38 " status INTEGER NOT NULL DEFAULT 0,"
37 // A note on this field: It will be NULL for now and is reserved for 39 // A note on this field: It will be NULL for now and is reserved for
38 // later use. We will treat NULL as "Unknown" in any subsequent queries 40 // later use. We will treat NULL as "Unknown" in any subsequent queries
39 // for user_initiated values. 41 // for user_initiated values.
40 " user_initiated INTEGER," // this is actually a boolean 42 " user_initiated INTEGER," // this is actually a boolean
41 " expiration_time INTEGER NOT NULL," 43 " expiration_time INTEGER NOT NULL DEFAULT 0,"
42 " client_namespace VARCHAR NOT NULL," 44 " client_namespace VARCHAR NOT NULL,"
43 " client_id VARCHAR NOT NULL," 45 " client_id VARCHAR NOT NULL,"
44 " online_url VARCHAR NOT NULL," 46 " online_url VARCHAR NOT NULL,"
45 " offline_url VARCHAR NOT NULL DEFAULT ''," 47 " offline_url VARCHAR NOT NULL DEFAULT '',"
46 " file_path VARCHAR NOT NULL" 48 " file_path VARCHAR NOT NULL"
47 ")"; 49 ")";
48 50
49 // This is cloned from //content/browser/appcache/appcache_database.cc 51 // This is cloned from //content/browser/appcache/appcache_database.cc
50 struct TableInfo { 52 struct TableInfo {
51 const char* table_name; 53 const char* table_name;
(...skipping 22 matching lines...) Expand all
74 OP_FILE_PATH, 76 OP_FILE_PATH,
75 }; 77 };
76 78
77 bool CreateTable(sql::Connection* db, const TableInfo& table_info) { 79 bool CreateTable(sql::Connection* db, const TableInfo& table_info) {
78 std::string sql("CREATE TABLE "); 80 std::string sql("CREATE TABLE ");
79 sql += table_info.table_name; 81 sql += table_info.table_name;
80 sql += table_info.columns; 82 sql += table_info.columns;
81 return db->Execute(sql.c_str()); 83 return db->Execute(sql.c_str());
82 } 84 }
83 85
86 bool RefreshColumns(sql::Connection* db) {
87 if (!db->Execute("ALTER TABLE " OFFLINE_PAGES_TABLE_NAME
88 " RENAME TO temp_" OFFLINE_PAGES_TABLE_NAME)) {
89 return false;
90 }
91 if (!CreateTable(db, kOfflinePagesTable))
92 return false;
93 if (!db->Execute(
94 "INSERT INTO " OFFLINE_PAGES_TABLE_NAME
95 " (offline_id, creation_time, file_size, version, last_access_time, "
96 "access_count, status, user_initiated, client_namespace, client_id, "
97 "online_url, offline_url, file_path) "
98 "SELECT offline_id, creation_time, file_size, version, "
99 "last_access_time, "
100 "access_count, status, user_initiated, client_namespace, client_id, "
101 "online_url, offline_url, file_path "
102 "FROM temp_" OFFLINE_PAGES_TABLE_NAME)) {
103 return false;
104 }
105 if (!db->Execute("DROP TABLE IF EXISTS temp_" OFFLINE_PAGES_TABLE_NAME))
106 return false;
107
108 return true;
109 }
110
84 bool CreateSchema(sql::Connection* db) { 111 bool CreateSchema(sql::Connection* db) {
85 // If you create a transaction but don't Commit() it is automatically 112 // If you create a transaction but don't Commit() it is automatically
86 // rolled back by its destructor when it falls out of scope. 113 // rolled back by its destructor when it falls out of scope.
87 sql::Transaction transaction(db); 114 sql::Transaction transaction(db);
88 if (!transaction.Begin()) 115 if (!transaction.Begin())
89 return false; 116 return false;
90 if (db->DoesTableExist(kOfflinePagesTable.table_name))
91 return true;
92 117
93 if (!CreateTable(db, kOfflinePagesTable)) 118 if (!db->DoesTableExist(kOfflinePagesTable.table_name)) {
94 return false; 119 if (!CreateTable(db, kOfflinePagesTable))
120 return false;
121 }
122
123 if (!db->DoesColumnExist(kOfflinePagesTable.table_name, "expiration_time")) {
124 if (!RefreshColumns(db))
125 return false;
126 }
95 127
96 // TODO(bburns): Add indices here. 128 // TODO(bburns): Add indices here.
97 return transaction.Commit(); 129 return transaction.Commit();
98 } 130 }
99 131
100 bool DeleteByOfflineId(sql::Connection* db, int64_t offline_id) { 132 bool DeleteByOfflineId(sql::Connection* db, int64_t offline_id) {
101 static const char kSql[] = 133 static const char kSql[] =
102 "DELETE FROM " OFFLINE_PAGES_TABLE_NAME " WHERE offline_id=?"; 134 "DELETE FROM " OFFLINE_PAGES_TABLE_NAME " WHERE offline_id=?";
103 sql::Statement statement(db->GetCachedStatement(SQL_FROM_HERE, kSql)); 135 sql::Statement statement(db->GetCachedStatement(SQL_FROM_HERE, kSql));
104 statement.BindInt64(0, offline_id); 136 statement.BindInt64(0, offline_id);
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after
329 } 361 }
330 362
331 void OfflinePageMetadataStoreSQL::Reset(const ResetCallback& callback) { 363 void OfflinePageMetadataStoreSQL::Reset(const ResetCallback& callback) {
332 background_task_runner_->PostTask( 364 background_task_runner_->PostTask(
333 FROM_HERE, 365 FROM_HERE,
334 base::Bind(&OfflinePageMetadataStoreSQL::ResetSync, base::Passed(&db_), 366 base::Bind(&OfflinePageMetadataStoreSQL::ResetSync, base::Passed(&db_),
335 base::ThreadTaskRunnerHandle::Get(), callback)); 367 base::ThreadTaskRunnerHandle::Get(), callback));
336 } 368 }
337 369
338 } // namespace offline_pages 370 } // namespace offline_pages
OLDNEW
« no previous file with comments | « components/offline_pages/offline_page_metadata_store_impl_unittest.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698