| 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 static_assert(kDeprecatedVersionNumber < kCurrentVersionNumber, |
| 94 "Deprecation version must be less than current"); |
| 95 sql::MetaTable::RazeIfDeprecated(&db_, kDeprecatedVersionNumber); |
| 96 |
| 97 // Scope initialization in a transaction so we can't be partially |
| 98 // initialized. |
| 91 sql::Transaction transaction(&db_); | 99 sql::Transaction transaction(&db_); |
| 92 if (!transaction.Begin()) | 100 if (!transaction.Begin()) |
| 93 return sql::INIT_FAILURE; | 101 return sql::INIT_FAILURE; |
| 94 | 102 |
| 95 // Version check. | 103 // Version check. |
| 96 if (!meta_table_.Init(&db_, kCurrentVersionNumber, kCompatibleVersionNumber)) | 104 if (!meta_table_.Init(&db_, kCurrentVersionNumber, kCompatibleVersionNumber)) |
| 97 return sql::INIT_FAILURE; | 105 return sql::INIT_FAILURE; |
| 98 if (meta_table_.GetCompatibleVersionNumber() > kCurrentVersionNumber) { | 106 if (meta_table_.GetCompatibleVersionNumber() > kCurrentVersionNumber) { |
| 99 LOG(WARNING) << "Web database is too new."; | 107 LOG(WARNING) << "Web database is too new."; |
| 100 return sql::INIT_TOO_NEW; | 108 return sql::INIT_TOO_NEW; |
| (...skipping 27 matching lines...) Expand all Loading... |
| 128 | 136 |
| 129 sql::InitStatus WebDatabase::MigrateOldVersionsAsNeeded() { | 137 sql::InitStatus WebDatabase::MigrateOldVersionsAsNeeded() { |
| 130 // Some malware used to lower the version number, causing migration to | 138 // 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 | 139 // fail. Ensure the version number is at least as high as the compatible |
| 132 // version number. | 140 // version number. |
| 133 int current_version = std::max(meta_table_.GetVersionNumber(), | 141 int current_version = std::max(meta_table_.GetVersionNumber(), |
| 134 meta_table_.GetCompatibleVersionNumber()); | 142 meta_table_.GetCompatibleVersionNumber()); |
| 135 if (current_version > meta_table_.GetVersionNumber()) | 143 if (current_version > meta_table_.GetVersionNumber()) |
| 136 ChangeVersion(&meta_table_, current_version, false); | 144 ChangeVersion(&meta_table_, current_version, false); |
| 137 | 145 |
| 138 if (current_version < 20) { | 146 DCHECK_GT(current_version, kDeprecatedVersionNumber); |
| 139 // Versions 1 - 19 are unhandled. Version numbers greater than | |
| 140 // kCurrentVersionNumber should have already been weeded out by the caller. | |
| 141 // | |
| 142 // When the version is too old, we return failure error code. The schema | |
| 143 // 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 | |
| 150 << " is too old to handle."; | |
| 151 NOTREACHED(); | |
| 152 return sql::INIT_FAILURE; | |
| 153 } | |
| 154 | 147 |
| 155 for (int next_version = current_version + 1; | 148 for (int next_version = current_version + 1; |
| 156 next_version <= kCurrentVersionNumber; | 149 next_version <= kCurrentVersionNumber; |
| 157 ++next_version) { | 150 ++next_version) { |
| 158 | 151 |
| 159 // Do any database-wide migrations. | 152 // Do any database-wide migrations. |
| 160 bool update_compatible_version = false; | 153 bool update_compatible_version = false; |
| 161 if (!MigrateToVersion(next_version, &update_compatible_version)) | 154 if (!MigrateToVersion(next_version, &update_compatible_version)) |
| 162 return FailedMigrationTo(next_version); | 155 return FailedMigrationTo(next_version); |
| 163 | 156 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 192 | 185 |
| 193 bool WebDatabase::MigrateToVersion58DropWebAppsAndIntents() { | 186 bool WebDatabase::MigrateToVersion58DropWebAppsAndIntents() { |
| 194 sql::Transaction transaction(&db_); | 187 sql::Transaction transaction(&db_); |
| 195 return transaction.Begin() && | 188 return transaction.Begin() && |
| 196 db_.Execute("DROP TABLE IF EXISTS web_apps") && | 189 db_.Execute("DROP TABLE IF EXISTS web_apps") && |
| 197 db_.Execute("DROP TABLE IF EXISTS web_app_icons") && | 190 db_.Execute("DROP TABLE IF EXISTS web_app_icons") && |
| 198 db_.Execute("DROP TABLE IF EXISTS web_intents") && | 191 db_.Execute("DROP TABLE IF EXISTS web_intents") && |
| 199 db_.Execute("DROP TABLE IF EXISTS web_intents_defaults") && | 192 db_.Execute("DROP TABLE IF EXISTS web_intents_defaults") && |
| 200 transaction.Commit(); | 193 transaction.Commit(); |
| 201 } | 194 } |
| OLD | NEW |