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
3 // found in the LICENSE file.
5 #ifndef SQL_RECOVERY_H_
6 #define SQL_RECOVERY_H_
8 #include "base/macros.h"
9 #include "sql/connection.h"
17 // Recovery module for sql/. The basic idea is to create a fresh
18 // database and populate it with the recovered contents of the
19 // original database. If recovery is successful, the recovered
20 // database is backed up over the original database. If recovery is
21 // not successful, the original database is razed. In either case,
22 // the original handle is poisoned so that operations on the stack do
23 // not accidentally disrupt the restored data.
26 // scoped_ptr<sql::Recovery> r =
27 // sql::Recovery::Begin(orig_db, orig_db_path);
29 // // Create the schema to recover to. On failure, clear the
31 // if (!r.db()->Execute(kCreateSchemaSql)) {
32 // sql::Recovery::Unrecoverable(r.Pass());
36 // // Recover data in "mytable".
37 // size_t rows_recovered = 0;
38 // if (!r.AutoRecoverTable("mytable", 0, &rows_recovered)) {
39 // sql::Recovery::Unrecoverable(r.Pass());
43 // // Manually cleanup additional constraints.
44 // if (!r.db()->Execute(kCleanupSql)) {
45 // sql::Recovery::Unrecoverable(r.Pass());
49 // // Commit the recovered data to the original database file.
50 // sql::Recovery::Recovered(r.Pass());
54 // If Recovered() is not called, then RazeAndClose() is called on
57 class SQL_EXPORT Recovery
{
61 // This module is intended to be used in concert with a virtual
62 // table module (see third_party/sqlite/src/src/recover.c). If the
63 // build defines USE_SYSTEM_SQLITE, this module will not be present.
64 // TODO(shess): I am still debating how to handle this - perhaps it
65 // will just imply Unrecoverable(). This is exposed to allow tests
66 // to adapt to the cases, please do not rely on it in production
68 static bool FullRecoverySupported();
70 // Begin the recovery process by opening a temporary database handle
71 // and attach the existing database to it at "corrupt". To prevent
72 // deadlock, all transactions on |connection| are rolled back.
74 // Returns NULL in case of failure, with no cleanup done on the
75 // original connection (except for breaking the transactions). The
76 // caller should Raze() or otherwise cleanup as appropriate.
78 // TODO(shess): Later versions of SQLite allow extracting the path
79 // from the connection.
80 // TODO(shess): Allow specifying the connection point?
81 static scoped_ptr
<Recovery
> Begin(
82 Connection
* connection
,
83 const base::FilePath
& db_path
) WARN_UNUSED_RESULT
;
85 // Mark recovery completed by replicating the recovery database over
86 // the original database, then closing the recovery database. The
87 // original database handle is poisoned, causing future calls
88 // against it to fail.
90 // If Recovered() is not called, the destructor will call
93 // TODO(shess): At this time, this function can fail while leaving
94 // the original database intact. Figure out which failure cases
95 // should go to RazeAndClose() instead.
96 static bool Recovered(scoped_ptr
<Recovery
> r
) WARN_UNUSED_RESULT
;
98 // Indicate that the database is unrecoverable. The original
99 // database is razed, and the handle poisoned.
100 static void Unrecoverable(scoped_ptr
<Recovery
> r
);
102 // When initially developing recovery code, sometimes the possible
103 // database states are not well-understood without further
104 // diagnostics. Abandon recovery but do not raze the original
106 // NOTE(shess): Only call this when adding recovery support. In the
107 // steady state, all databases should progress to recovered or razed.
108 static void Rollback(scoped_ptr
<Recovery
> r
);
110 // Handle to the temporary recovery database.
111 sql::Connection
* db() { return &recover_db_
; }
113 // Attempt to recover the named table from the corrupt database into
114 // the recovery database using a temporary recover virtual table.
115 // The virtual table schema is derived from the named table's schema
116 // in database [main]. Data is copied using INSERT OR REPLACE, so
117 // duplicates overwrite each other.
119 // |extend_columns| allows recovering tables which have excess
120 // columns relative to the target schema. The recover virtual table
121 // treats more data than specified as a sign of corruption.
123 // Returns true if all operations succeeded, with the number of rows
124 // recovered in |*rows_recovered|.
126 // NOTE(shess): Due to a flaw in the recovery virtual table, at this
127 // time this code injects the DEFAULT value of the target table in
128 // locations where the recovery table returns NULL. This is not
129 // entirely correct, because it happens both when there is a short
130 // row (correct) but also where there is an actual NULL value
133 // TODO(shess): Flag for INSERT OR REPLACE vs IGNORE.
134 // TODO(shess): Handle extended table names.
135 bool AutoRecoverTable(const char* table_name
,
136 size_t extend_columns
,
137 size_t* rows_recovered
);
139 // Setup a recover virtual table at temp.recover_meta, reading from
140 // corrupt.meta. Returns true if created.
141 // TODO(shess): Perhaps integrate into Begin().
142 // TODO(shess): Add helpers to fetch additional items from the meta
146 // Fetch the version number from temp.recover_meta. Returns false
147 // if the query fails, or if there is no version row. Otherwise
148 // returns true, with the version in |*version_number|.
150 // Only valid to call after successful SetupMeta().
151 bool GetMetaVersionNumber(int* version_number
);
154 explicit Recovery(Connection
* connection
);
156 // Setup the recovery database handle for Begin(). Returns false in
157 // case anything failed.
158 bool Init(const base::FilePath
& db_path
) WARN_UNUSED_RESULT
;
160 // Copy the recovered database over the original database.
161 bool Backup() WARN_UNUSED_RESULT
;
163 // Close the recovery database, and poison the original handle.
164 // |raze| controls whether the original database is razed or just
170 void Shutdown(Disposition raze
);
172 Connection
* db_
; // Original database connection.
173 Connection recover_db_
; // Recovery connection.
175 DISALLOW_COPY_AND_ASSIGN(Recovery
);
180 #endif // SQL_RECOVERY_H_