OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/webdata/common/web_database.h" | 5 #include "components/webdata/common/web_database.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
10 #include "sql/statement.h" | 10 #include "sql/statement.h" |
11 #include "sql/transaction.h" | 11 #include "sql/transaction.h" |
12 | 12 |
13 // Current version number. Note: when changing the current version number, | 13 // Current version number. Note: when changing the current version number, |
14 // corresponding changes must happen in the unit tests, and new migration test | 14 // corresponding changes must happen in the unit tests, and new migration test |
15 // added. See |WebDatabaseMigrationTest::kCurrentTestedVersionNumber|. | 15 // added. See |WebDatabaseMigrationTest::kCurrentTestedVersionNumber|. |
16 // static | 16 // static |
17 const int WebDatabase::kCurrentVersionNumber = 62; | 17 const int WebDatabase::kCurrentVersionNumber = 62; |
18 | 18 |
19 const int WebDatabase::kDeprecatedVersionNumber = 51; | |
20 | |
19 namespace { | 21 namespace { |
20 | 22 |
21 const int kCompatibleVersionNumber = 61; | 23 const int kCompatibleVersionNumber = 61; |
22 | 24 |
23 // Change the version number and possibly the compatibility version of | 25 // Change the version number and possibly the compatibility version of |
24 // |meta_table_|. | 26 // |meta_table_|. |
25 void ChangeVersion(sql::MetaTable* meta_table, | 27 void ChangeVersion(sql::MetaTable* meta_table, |
26 int version_num, | 28 int version_num, |
27 bool update_compatible_version_num) { | 29 bool update_compatible_version_num) { |
28 meta_table->SetVersionNumber(version_num); | 30 meta_table->SetVersionNumber(version_num); |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
80 // infrequent. So we go with a small cache size. | 82 // infrequent. So we go with a small cache size. |
81 db_.set_cache_size(32); | 83 db_.set_cache_size(32); |
82 | 84 |
83 // Run the database in exclusive mode. Nobody else should be accessing the | 85 // Run the database in exclusive mode. Nobody else should be accessing the |
84 // database while we're running, and this will give somewhat improved perf. | 86 // database while we're running, and this will give somewhat improved perf. |
85 db_.set_exclusive_locking(); | 87 db_.set_exclusive_locking(); |
86 | 88 |
87 if (!db_.Open(db_name)) | 89 if (!db_.Open(db_name)) |
88 return sql::INIT_FAILURE; | 90 return sql::INIT_FAILURE; |
89 | 91 |
90 // Initialize various tables | 92 // Clobber really old databases. |
93 DCHECK_LT(kDeprecatedVersionNumber, kCurrentVersionNumber); | |
Peter Kasting
2015/02/26 22:33:19
I don't know that this DCHECK is really needed.
Evan Stade
2015/02/26 22:37:19
This is copied from TopSitesDatabase. I am ambival
Scott Hess - ex-Googler
2015/02/27 06:49:38
The rationale was that if someone accidentally got
Peter Kasting
2015/02/27 09:15:25
I didn't even think of that. Yes, please use a st
Evan Stade
2015/02/28 00:13:41
Done.
| |
94 sql::MetaTable::RazeIfDeprecated(&db_, kDeprecatedVersionNumber); | |
95 | |
96 // Scope initialization in a transaction so we can't be partially | |
97 // initialized. | |
91 sql::Transaction transaction(&db_); | 98 sql::Transaction transaction(&db_); |
92 if (!transaction.Begin()) | 99 if (!transaction.Begin()) |
93 return sql::INIT_FAILURE; | 100 return sql::INIT_FAILURE; |
94 | 101 |
95 // Version check. | 102 // Version check. |
96 if (!meta_table_.Init(&db_, kCurrentVersionNumber, kCompatibleVersionNumber)) | 103 if (!meta_table_.Init(&db_, kCurrentVersionNumber, kCompatibleVersionNumber)) |
97 return sql::INIT_FAILURE; | 104 return sql::INIT_FAILURE; |
98 if (meta_table_.GetCompatibleVersionNumber() > kCurrentVersionNumber) { | 105 if (meta_table_.GetCompatibleVersionNumber() > kCurrentVersionNumber) { |
99 LOG(WARNING) << "Web database is too new."; | 106 LOG(WARNING) << "Web database is too new."; |
100 return sql::INIT_TOO_NEW; | 107 return sql::INIT_TOO_NEW; |
(...skipping 27 matching lines...) Expand all Loading... | |
128 | 135 |
129 sql::InitStatus WebDatabase::MigrateOldVersionsAsNeeded() { | 136 sql::InitStatus WebDatabase::MigrateOldVersionsAsNeeded() { |
130 // Some malware used to lower the version number, causing migration to | 137 // Some malware used to lower the version number, causing migration to |
131 // fail. Ensure the version number is at least as high as the compatible | 138 // fail. Ensure the version number is at least as high as the compatible |
132 // version number. | 139 // version number. |
133 int current_version = std::max(meta_table_.GetVersionNumber(), | 140 int current_version = std::max(meta_table_.GetVersionNumber(), |
134 meta_table_.GetCompatibleVersionNumber()); | 141 meta_table_.GetCompatibleVersionNumber()); |
135 if (current_version > meta_table_.GetVersionNumber()) | 142 if (current_version > meta_table_.GetVersionNumber()) |
136 ChangeVersion(&meta_table_, current_version, false); | 143 ChangeVersion(&meta_table_, current_version, false); |
137 | 144 |
138 if (current_version < 20) { | 145 if (current_version < kDeprecatedVersionNumber) { |
Scott Hess - ex-Googler
2015/02/27 06:49:38
This case should not be possible anymore, right?
Peter Kasting
2015/02/27 09:15:25
I think not; even if the user manually copies old
Evan Stade
2015/02/28 00:13:41
Done.
| |
139 // Versions 1 - 19 are unhandled. Version numbers greater than | 146 // Deprecated versions are unhandled. Version numbers greater than |
140 // kCurrentVersionNumber should have already been weeded out by the caller. | 147 // kCurrentVersionNumber should have already been weeded out by the caller. |
141 // | 148 // |
142 // When the version is too old, we return failure error code. The schema | 149 // When the version is too old, we return failure error code. The schema |
143 // is too out of date to migrate. | 150 // is too out of date to migrate. |
144 // | |
145 // There should not be a released product that makes a database too old to | |
146 // migrate. If we do encounter such a legacy database, we will need a | |
147 // better solution to handle it (i.e., pop up a dialog to tell the user, | |
148 // erase all their prefs and start over, etc.). | |
149 LOG(WARNING) << "Web database version " << current_version | 151 LOG(WARNING) << "Web database version " << current_version |
150 << " is too old to handle."; | 152 << " is too old to handle."; |
151 NOTREACHED(); | |
152 return sql::INIT_FAILURE; | 153 return sql::INIT_FAILURE; |
153 } | 154 } |
154 | 155 |
155 for (int next_version = current_version + 1; | 156 for (int next_version = current_version + 1; |
156 next_version <= kCurrentVersionNumber; | 157 next_version <= kCurrentVersionNumber; |
157 ++next_version) { | 158 ++next_version) { |
158 | 159 |
159 // Do any database-wide migrations. | 160 // Do any database-wide migrations. |
160 bool update_compatible_version = false; | 161 bool update_compatible_version = false; |
161 if (!MigrateToVersion(next_version, &update_compatible_version)) | 162 if (!MigrateToVersion(next_version, &update_compatible_version)) |
(...skipping 30 matching lines...) Expand all Loading... | |
192 | 193 |
193 bool WebDatabase::MigrateToVersion58DropWebAppsAndIntents() { | 194 bool WebDatabase::MigrateToVersion58DropWebAppsAndIntents() { |
194 sql::Transaction transaction(&db_); | 195 sql::Transaction transaction(&db_); |
195 return transaction.Begin() && | 196 return transaction.Begin() && |
196 db_.Execute("DROP TABLE IF EXISTS web_apps") && | 197 db_.Execute("DROP TABLE IF EXISTS web_apps") && |
197 db_.Execute("DROP TABLE IF EXISTS web_app_icons") && | 198 db_.Execute("DROP TABLE IF EXISTS web_app_icons") && |
198 db_.Execute("DROP TABLE IF EXISTS web_intents") && | 199 db_.Execute("DROP TABLE IF EXISTS web_intents") && |
199 db_.Execute("DROP TABLE IF EXISTS web_intents_defaults") && | 200 db_.Execute("DROP TABLE IF EXISTS web_intents_defaults") && |
200 transaction.Commit(); | 201 transaction.Commit(); |
201 } | 202 } |
OLD | NEW |