| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 #ifndef SQL_RECOVERY_H_ | 5 #ifndef SQL_RECOVERY_H_ |
| 6 #define SQL_RECOVERY_H_ | 6 #define SQL_RECOVERY_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <memory> | 10 #include <memory> |
| 11 | 11 |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "sql/connection.h" | 13 #include "sql/connection.h" |
| 14 | 14 |
| 15 namespace base { | 15 namespace base { |
| 16 class FilePath; | 16 class FilePath; |
| 17 } | 17 } |
| 18 | 18 |
| 19 namespace sql { | 19 namespace sql { |
| 20 | 20 |
| 21 // Recovery module for sql/. The basic idea is to create a fresh | 21 // Recovery module for sql/. The basic idea is to create a fresh database and |
| 22 // database and populate it with the recovered contents of the | 22 // populate it with the recovered contents of the original database. If |
| 23 // original database. If recovery is successful, the recovered | 23 // recovery is successful, the recovered database is backed up over the original |
| 24 // database is backed up over the original database. If recovery is | 24 // database. If recovery is not successful, the original database is razed. In |
| 25 // not successful, the original database is razed. In either case, | 25 // either case, the original handle is poisoned so that operations on the stack |
| 26 // the original handle is poisoned so that operations on the stack do | 26 // do not accidentally disrupt the restored data. |
| 27 // not accidentally disrupt the restored data. | 27 // |
| 28 // RecoverDatabaseOrRaze() automates this, including recoverying the schema of |
| 29 // from the suspect database. If a database requires special handling, such as |
| 30 // recovering between different schema, or tables requiring post-processing, |
| 31 // then the module can be used manually like: |
| 28 // | 32 // |
| 29 // { | 33 // { |
| 30 // std::unique_ptr<sql::Recovery> r = | 34 // std::unique_ptr<sql::Recovery> r = |
| 31 // sql::Recovery::Begin(orig_db, orig_db_path); | 35 // sql::Recovery::Begin(orig_db, orig_db_path); |
| 32 // if (r) { | 36 // if (r) { |
| 33 // // Create the schema to recover to. On failure, clear the | 37 // // Create the schema to recover to. On failure, clear the |
| 34 // // database. | 38 // // database. |
| 35 // if (!r.db()->Execute(kCreateSchemaSql)) { | 39 // if (!r.db()->Execute(kCreateSchemaSql)) { |
| 36 // sql::Recovery::Unrecoverable(std::move(r)); | 40 // sql::Recovery::Unrecoverable(std::move(r)); |
| 37 // return; | 41 // return; |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 144 // table as needed. | 148 // table as needed. |
| 145 bool SetupMeta(); | 149 bool SetupMeta(); |
| 146 | 150 |
| 147 // Fetch the version number from temp.recover_meta. Returns false | 151 // Fetch the version number from temp.recover_meta. Returns false |
| 148 // if the query fails, or if there is no version row. Otherwise | 152 // if the query fails, or if there is no version row. Otherwise |
| 149 // returns true, with the version in |*version_number|. | 153 // returns true, with the version in |*version_number|. |
| 150 // | 154 // |
| 151 // Only valid to call after successful SetupMeta(). | 155 // Only valid to call after successful SetupMeta(). |
| 152 bool GetMetaVersionNumber(int* version_number); | 156 bool GetMetaVersionNumber(int* version_number); |
| 153 | 157 |
| 158 // Attempt to recover the database by creating a new database with schema from |
| 159 // |db|, then copying over as much data as possible. After this call, the |
| 160 // |db| handle will be poisoned (though technically remaining open) so that |
| 161 // future calls will return errors until the handle is re-opened. |
| 162 // |
| 163 // If a corrupt database contains tables without unique indices, the resulting |
| 164 // table may contain duplication. If this is not acceptable, the client |
| 165 // should use the manual process as described in the example at the top of the |
| 166 // file, cleaning up data at the appropriate points. |
| 167 static void RecoverDatabase(Connection* db, const base::FilePath& db_path); |
| 168 |
| 169 // Returns true for SQLite errors which RecoverDatabase() can plausibly fix. |
| 170 // This does not guarantee that RecoverDatabase() will successfully recover |
| 171 // the database. |
| 172 static bool ShouldRecover(int extended_error); |
| 173 |
| 154 private: | 174 private: |
| 155 explicit Recovery(Connection* connection); | 175 explicit Recovery(Connection* connection); |
| 156 | 176 |
| 157 // Setup the recovery database handle for Begin(). Returns false in | 177 // Setup the recovery database handle for Begin(). Returns false in |
| 158 // case anything failed. | 178 // case anything failed. |
| 159 bool Init(const base::FilePath& db_path) WARN_UNUSED_RESULT; | 179 bool Init(const base::FilePath& db_path) WARN_UNUSED_RESULT; |
| 160 | 180 |
| 161 // Copy the recovered database over the original database. | 181 // Copy the recovered database over the original database. |
| 162 bool Backup() WARN_UNUSED_RESULT; | 182 bool Backup() WARN_UNUSED_RESULT; |
| 163 | 183 |
| 164 // Close the recovery database, and poison the original handle. | 184 // Close the recovery database, and poison the original handle. |
| 165 // |raze| controls whether the original database is razed or just | 185 // |raze| controls whether the original database is razed or just |
| 166 // poisoned. | 186 // poisoned. |
| 167 enum Disposition { | 187 enum Disposition { |
| 168 RAZE_AND_POISON, | 188 RAZE_AND_POISON, |
| 169 POISON, | 189 POISON, |
| 170 }; | 190 }; |
| 171 void Shutdown(Disposition raze); | 191 void Shutdown(Disposition raze); |
| 172 | 192 |
| 173 Connection* db_; // Original database connection. | 193 Connection* db_; // Original database connection. |
| 174 Connection recover_db_; // Recovery connection. | 194 Connection recover_db_; // Recovery connection. |
| 175 | 195 |
| 176 DISALLOW_COPY_AND_ASSIGN(Recovery); | 196 DISALLOW_COPY_AND_ASSIGN(Recovery); |
| 177 }; | 197 }; |
| 178 | 198 |
| 179 } // namespace sql | 199 } // namespace sql |
| 180 | 200 |
| 181 #endif // SQL_RECOVERY_H_ | 201 #endif // SQL_RECOVERY_H_ |
| OLD | NEW |