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 so that future calls will return errors until | |
161 // 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() could plausibly fix. | |
170 // This does not guarantee that RecoverDatabase() will successfully recover | |
171 // the database. Returning false means the error is not related to the | |
172 // contents of the database (such as SQLITE_BUSY or SQLITE_MISUSE), or is not | |
173 // verified to be recovered correctly (such as SQLITE_IOERR or SQLITE_FULL). | |
Mark P
2016/06/27 23:20:13
nit: recovered -> recoverable ?
Scott Hess - ex-Googler
2016/06/29 21:39:30
I'm just going to remove the entire "why false" ca
Mark P
2016/07/01 22:40:55
Good move.
| |
174 static bool ShouldRecover(int extended_error); | |
175 | |
154 private: | 176 private: |
155 explicit Recovery(Connection* connection); | 177 explicit Recovery(Connection* connection); |
156 | 178 |
157 // Setup the recovery database handle for Begin(). Returns false in | 179 // Setup the recovery database handle for Begin(). Returns false in |
158 // case anything failed. | 180 // case anything failed. |
159 bool Init(const base::FilePath& db_path) WARN_UNUSED_RESULT; | 181 bool Init(const base::FilePath& db_path) WARN_UNUSED_RESULT; |
160 | 182 |
161 // Copy the recovered database over the original database. | 183 // Copy the recovered database over the original database. |
162 bool Backup() WARN_UNUSED_RESULT; | 184 bool Backup() WARN_UNUSED_RESULT; |
163 | 185 |
164 // Close the recovery database, and poison the original handle. | 186 // Close the recovery database, and poison the original handle. |
165 // |raze| controls whether the original database is razed or just | 187 // |raze| controls whether the original database is razed or just |
166 // poisoned. | 188 // poisoned. |
167 enum Disposition { | 189 enum Disposition { |
168 RAZE_AND_POISON, | 190 RAZE_AND_POISON, |
169 POISON, | 191 POISON, |
170 }; | 192 }; |
171 void Shutdown(Disposition raze); | 193 void Shutdown(Disposition raze); |
172 | 194 |
173 Connection* db_; // Original database connection. | 195 Connection* db_; // Original database connection. |
174 Connection recover_db_; // Recovery connection. | 196 Connection recover_db_; // Recovery connection. |
175 | 197 |
176 DISALLOW_COPY_AND_ASSIGN(Recovery); | 198 DISALLOW_COPY_AND_ASSIGN(Recovery); |
177 }; | 199 }; |
178 | 200 |
179 } // namespace sql | 201 } // namespace sql |
180 | 202 |
181 #endif // SQL_RECOVERY_H_ | 203 #endif // SQL_RECOVERY_H_ |
OLD | NEW |