blob: a829e77057f8f71083b42e81575ec4c169f40758 [file] [log] [blame]
[email protected]8d409412013-07-19 18:25:301// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef SQL_RECOVERY_H_
6#define SQL_RECOVERY_H_
7
avi0b519202015-12-21 07:25:198#include <stddef.h>
9
mostynbd82cd9952016-04-11 20:05:3410#include <memory>
11
tfarina720d4f32015-05-11 22:31:2612#include "base/macros.h"
[email protected]8d409412013-07-19 18:25:3013#include "sql/connection.h"
14
15namespace base {
16class FilePath;
17}
18
19namespace sql {
20
shessa402e752016-07-02 00:25:1121// Recovery module for sql/. The basic idea is to create a fresh database and
22// populate it with the recovered contents of the original database. If
23// recovery is successful, the recovered database is backed up over the original
24// database. If recovery is not successful, the original database is razed. In
25// either case, the original handle is poisoned so that operations on the stack
26// do 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:
[email protected]8d409412013-07-19 18:25:3032//
33// {
mostynbd82cd9952016-04-11 20:05:3434// std::unique_ptr<sql::Recovery> r =
[email protected]8d409412013-07-19 18:25:3035// sql::Recovery::Begin(orig_db, orig_db_path);
36// if (r) {
[email protected]ae4f1622013-12-08 06:49:1237// // Create the schema to recover to. On failure, clear the
38// // database.
39// if (!r.db()->Execute(kCreateSchemaSql)) {
dcheng7061e5f2016-03-04 01:21:4740// sql::Recovery::Unrecoverable(std::move(r));
[email protected]ae4f1622013-12-08 06:49:1241// return;
[email protected]8d409412013-07-19 18:25:3042// }
[email protected]ae4f1622013-12-08 06:49:1243//
44// // Recover data in "mytable".
45// size_t rows_recovered = 0;
46// if (!r.AutoRecoverTable("mytable", 0, &rows_recovered)) {
dcheng7061e5f2016-03-04 01:21:4747// sql::Recovery::Unrecoverable(std::move(r));
[email protected]ae4f1622013-12-08 06:49:1248// return;
49// }
50//
51// // Manually cleanup additional constraints.
52// if (!r.db()->Execute(kCleanupSql)) {
dcheng7061e5f2016-03-04 01:21:4753// sql::Recovery::Unrecoverable(std::move(r));
[email protected]ae4f1622013-12-08 06:49:1254// return;
55// }
56//
57// // Commit the recovered data to the original database file.
dcheng7061e5f2016-03-04 01:21:4758// sql::Recovery::Recovered(std::move(r));
[email protected]8d409412013-07-19 18:25:3059// }
60// }
61//
62// If Recovered() is not called, then RazeAndClose() is called on
63// orig_db.
64
65class SQL_EXPORT Recovery {
66 public:
67 ~Recovery();
68
[email protected]df5d95c42013-10-07 22:24:2269 // This module is intended to be used in concert with a virtual
70 // table module (see third_party/sqlite/src/src/recover.c). If the
71 // build defines USE_SYSTEM_SQLITE, this module will not be present.
72 // TODO(shess): I am still debating how to handle this - perhaps it
73 // will just imply Unrecoverable(). This is exposed to allow tests
74 // to adapt to the cases, please do not rely on it in production
75 // code.
76 static bool FullRecoverySupported();
77
[email protected]8d409412013-07-19 18:25:3078 // Begin the recovery process by opening a temporary database handle
79 // and attach the existing database to it at "corrupt". To prevent
80 // deadlock, all transactions on |connection| are rolled back.
81 //
82 // Returns NULL in case of failure, with no cleanup done on the
83 // original connection (except for breaking the transactions). The
84 // caller should Raze() or otherwise cleanup as appropriate.
85 //
86 // TODO(shess): Later versions of SQLite allow extracting the path
87 // from the connection.
88 // TODO(shess): Allow specifying the connection point?
mostynbd82cd9952016-04-11 20:05:3489 static std::unique_ptr<Recovery> Begin(Connection* connection,
90 const base::FilePath& db_path)
91 WARN_UNUSED_RESULT;
[email protected]8d409412013-07-19 18:25:3092
93 // Mark recovery completed by replicating the recovery database over
94 // the original database, then closing the recovery database. The
95 // original database handle is poisoned, causing future calls
96 // against it to fail.
97 //
98 // If Recovered() is not called, the destructor will call
99 // Unrecoverable().
100 //
[email protected]74cdede2013-09-25 05:39:57101 // TODO(shess): At this time, this function can fail while leaving
[email protected]8d409412013-07-19 18:25:30102 // the original database intact. Figure out which failure cases
103 // should go to RazeAndClose() instead.
mostynbd82cd9952016-04-11 20:05:34104 static bool Recovered(std::unique_ptr<Recovery> r) WARN_UNUSED_RESULT;
[email protected]8d409412013-07-19 18:25:30105
106 // Indicate that the database is unrecoverable. The original
107 // database is razed, and the handle poisoned.
mostynbd82cd9952016-04-11 20:05:34108 static void Unrecoverable(std::unique_ptr<Recovery> r);
[email protected]8d409412013-07-19 18:25:30109
[email protected]74cdede2013-09-25 05:39:57110 // When initially developing recovery code, sometimes the possible
111 // database states are not well-understood without further
112 // diagnostics. Abandon recovery but do not raze the original
113 // database.
114 // NOTE(shess): Only call this when adding recovery support. In the
115 // steady state, all databases should progress to recovered or razed.
mostynbd82cd9952016-04-11 20:05:34116 static void Rollback(std::unique_ptr<Recovery> r);
[email protected]74cdede2013-09-25 05:39:57117
[email protected]8d409412013-07-19 18:25:30118 // Handle to the temporary recovery database.
119 sql::Connection* db() { return &recover_db_; }
120
[email protected]a8848a72013-11-18 04:18:47121 // Attempt to recover the named table from the corrupt database into
122 // the recovery database using a temporary recover virtual table.
123 // The virtual table schema is derived from the named table's schema
shess806f4992016-02-04 21:12:09124 // in database [main]. Data is copied using INSERT OR IGNORE, so
125 // duplicates are dropped.
[email protected]a8848a72013-11-18 04:18:47126 //
shess6f68bd32016-02-04 19:29:44127 // If the source table has fewer columns than the target, the target
128 // DEFAULT value will be used for those columns.
[email protected]a8848a72013-11-18 04:18:47129 //
130 // Returns true if all operations succeeded, with the number of rows
131 // recovered in |*rows_recovered|.
132 //
133 // NOTE(shess): Due to a flaw in the recovery virtual table, at this
134 // time this code injects the DEFAULT value of the target table in
135 // locations where the recovery table returns NULL. This is not
136 // entirely correct, because it happens both when there is a short
137 // row (correct) but also where there is an actual NULL value
138 // (incorrect).
139 //
140 // TODO(shess): Flag for INSERT OR REPLACE vs IGNORE.
141 // TODO(shess): Handle extended table names.
shess6f68bd32016-02-04 19:29:44142 bool AutoRecoverTable(const char* table_name, size_t* rows_recovered);
[email protected]a8848a72013-11-18 04:18:47143
144 // Setup a recover virtual table at temp.recover_meta, reading from
145 // corrupt.meta. Returns true if created.
146 // TODO(shess): Perhaps integrate into Begin().
147 // TODO(shess): Add helpers to fetch additional items from the meta
148 // table as needed.
149 bool SetupMeta();
150
151 // Fetch the version number from temp.recover_meta. Returns false
152 // if the query fails, or if there is no version row. Otherwise
153 // returns true, with the version in |*version_number|.
154 //
155 // Only valid to call after successful SetupMeta().
156 bool GetMetaVersionNumber(int* version_number);
157
shessa402e752016-07-02 00:25:11158 // 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
[email protected]8d409412013-07-19 18:25:30174 private:
175 explicit Recovery(Connection* connection);
176
177 // Setup the recovery database handle for Begin(). Returns false in
178 // case anything failed.
179 bool Init(const base::FilePath& db_path) WARN_UNUSED_RESULT;
180
181 // Copy the recovered database over the original database.
182 bool Backup() WARN_UNUSED_RESULT;
183
184 // Close the recovery database, and poison the original handle.
185 // |raze| controls whether the original database is razed or just
186 // poisoned.
187 enum Disposition {
188 RAZE_AND_POISON,
189 POISON,
190 };
191 void Shutdown(Disposition raze);
192
193 Connection* db_; // Original database connection.
194 Connection recover_db_; // Recovery connection.
195
196 DISALLOW_COPY_AND_ASSIGN(Recovery);
197};
198
199} // namespace sql
200
201#endif // SQL_RECOVERY_H_