Unit test for GPU command buffer wrapping condition.
[chromium-blink-merge.git] / sql / recovery_unittest.cc
blobce3884bbc08013e3936a6784d606369c72400bce
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 #include <string>
7 #include "base/bind.h"
8 #include "base/files/file_path.h"
9 #include "base/files/scoped_temp_dir.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "sql/connection.h"
12 #include "sql/meta_table.h"
13 #include "sql/recovery.h"
14 #include "sql/statement.h"
15 #include "sql/test/scoped_error_ignorer.h"
16 #include "sql/test/test_helpers.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18 #include "third_party/sqlite/sqlite3.h"
20 namespace {
22 // Execute |sql|, and stringify the results with |column_sep| between
23 // columns and |row_sep| between rows.
24 // TODO(shess): Promote this to a central testing helper.
25 std::string ExecuteWithResults(sql::Connection* db,
26 const char* sql,
27 const char* column_sep,
28 const char* row_sep) {
29 sql::Statement s(db->GetUniqueStatement(sql));
30 std::string ret;
31 while (s.Step()) {
32 if (!ret.empty())
33 ret += row_sep;
34 for (int i = 0; i < s.ColumnCount(); ++i) {
35 if (i > 0)
36 ret += column_sep;
37 if (s.ColumnType(i) == sql::COLUMN_TYPE_NULL) {
38 ret += "<null>";
39 } else if (s.ColumnType(i) == sql::COLUMN_TYPE_BLOB) {
40 ret += "<x'";
41 ret += base::HexEncode(s.ColumnBlob(i), s.ColumnByteLength(i));
42 ret += "'>";
43 } else {
44 ret += s.ColumnString(i);
48 return ret;
51 // Dump consistent human-readable representation of the database
52 // schema. For tables or indices, this will contain the sql command
53 // to create the table or index. For certain automatic SQLite
54 // structures with no sql, the name is used.
55 std::string GetSchema(sql::Connection* db) {
56 const char kSql[] =
57 "SELECT COALESCE(sql, name) FROM sqlite_master ORDER BY 1";
58 return ExecuteWithResults(db, kSql, "|", "\n");
61 class SQLRecoveryTest : public testing::Test {
62 public:
63 SQLRecoveryTest() {}
65 virtual void SetUp() {
66 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
67 ASSERT_TRUE(db_.Open(db_path()));
70 virtual void TearDown() {
71 db_.Close();
74 sql::Connection& db() { return db_; }
76 base::FilePath db_path() {
77 return temp_dir_.path().AppendASCII("SQLRecoveryTest.db");
80 bool Reopen() {
81 db_.Close();
82 return db_.Open(db_path());
85 private:
86 base::ScopedTempDir temp_dir_;
87 sql::Connection db_;
90 TEST_F(SQLRecoveryTest, RecoverBasic) {
91 const char kCreateSql[] = "CREATE TABLE x (t TEXT)";
92 const char kInsertSql[] = "INSERT INTO x VALUES ('This is a test')";
93 ASSERT_TRUE(db().Execute(kCreateSql));
94 ASSERT_TRUE(db().Execute(kInsertSql));
95 ASSERT_EQ("CREATE TABLE x (t TEXT)", GetSchema(&db()));
97 // If the Recovery handle goes out of scope without being
98 // Recovered(), the database is razed.
100 scoped_ptr<sql::Recovery> recovery = sql::Recovery::Begin(&db(), db_path());
101 ASSERT_TRUE(recovery.get());
103 EXPECT_FALSE(db().is_open());
104 ASSERT_TRUE(Reopen());
105 EXPECT_TRUE(db().is_open());
106 ASSERT_EQ("", GetSchema(&db()));
108 // Recreate the database.
109 ASSERT_TRUE(db().Execute(kCreateSql));
110 ASSERT_TRUE(db().Execute(kInsertSql));
111 ASSERT_EQ("CREATE TABLE x (t TEXT)", GetSchema(&db()));
113 // Unrecoverable() also razes.
115 scoped_ptr<sql::Recovery> recovery = sql::Recovery::Begin(&db(), db_path());
116 ASSERT_TRUE(recovery.get());
117 sql::Recovery::Unrecoverable(recovery.Pass());
119 // TODO(shess): Test that calls to recover.db() start failing.
121 EXPECT_FALSE(db().is_open());
122 ASSERT_TRUE(Reopen());
123 EXPECT_TRUE(db().is_open());
124 ASSERT_EQ("", GetSchema(&db()));
126 // Recreate the database.
127 ASSERT_TRUE(db().Execute(kCreateSql));
128 ASSERT_TRUE(db().Execute(kInsertSql));
129 ASSERT_EQ("CREATE TABLE x (t TEXT)", GetSchema(&db()));
131 // Recovered() replaces the original with the "recovered" version.
133 scoped_ptr<sql::Recovery> recovery = sql::Recovery::Begin(&db(), db_path());
134 ASSERT_TRUE(recovery.get());
136 // Create the new version of the table.
137 ASSERT_TRUE(recovery->db()->Execute(kCreateSql));
139 // Insert different data to distinguish from original database.
140 const char kAltInsertSql[] = "INSERT INTO x VALUES ('That was a test')";
141 ASSERT_TRUE(recovery->db()->Execute(kAltInsertSql));
143 // Successfully recovered.
144 ASSERT_TRUE(sql::Recovery::Recovered(recovery.Pass()));
146 EXPECT_FALSE(db().is_open());
147 ASSERT_TRUE(Reopen());
148 EXPECT_TRUE(db().is_open());
149 ASSERT_EQ("CREATE TABLE x (t TEXT)", GetSchema(&db()));
151 const char* kXSql = "SELECT * FROM x ORDER BY 1";
152 ASSERT_EQ("That was a test",
153 ExecuteWithResults(&db(), kXSql, "|", "\n"));
156 // The recovery virtual table is only supported for Chromium's SQLite.
157 #if !defined(USE_SYSTEM_SQLITE)
159 // Run recovery through its paces on a valid database.
160 TEST_F(SQLRecoveryTest, VirtualTable) {
161 const char kCreateSql[] = "CREATE TABLE x (t TEXT)";
162 ASSERT_TRUE(db().Execute(kCreateSql));
163 ASSERT_TRUE(db().Execute("INSERT INTO x VALUES ('This is a test')"));
164 ASSERT_TRUE(db().Execute("INSERT INTO x VALUES ('That was a test')"));
166 // Successfully recover the database.
168 scoped_ptr<sql::Recovery> recovery = sql::Recovery::Begin(&db(), db_path());
170 // Tables to recover original DB, now at [corrupt].
171 const char kRecoveryCreateSql[] =
172 "CREATE VIRTUAL TABLE temp.recover_x using recover("
173 " corrupt.x,"
174 " t TEXT STRICT"
175 ")";
176 ASSERT_TRUE(recovery->db()->Execute(kRecoveryCreateSql));
178 // Re-create the original schema.
179 ASSERT_TRUE(recovery->db()->Execute(kCreateSql));
181 // Copy the data from the recovery tables to the new database.
182 const char kRecoveryCopySql[] =
183 "INSERT INTO x SELECT t FROM recover_x";
184 ASSERT_TRUE(recovery->db()->Execute(kRecoveryCopySql));
186 // Successfully recovered.
187 ASSERT_TRUE(sql::Recovery::Recovered(recovery.Pass()));
190 // Since the database was not corrupt, the entire schema and all
191 // data should be recovered.
192 ASSERT_TRUE(Reopen());
193 ASSERT_EQ("CREATE TABLE x (t TEXT)", GetSchema(&db()));
195 const char* kXSql = "SELECT * FROM x ORDER BY 1";
196 ASSERT_EQ("That was a test\nThis is a test",
197 ExecuteWithResults(&db(), kXSql, "|", "\n"));
200 void RecoveryCallback(sql::Connection* db, const base::FilePath& db_path,
201 int* record_error, int error, sql::Statement* stmt) {
202 *record_error = error;
204 // Clear the error callback to prevent reentrancy.
205 db->reset_error_callback();
207 scoped_ptr<sql::Recovery> recovery = sql::Recovery::Begin(db, db_path);
208 ASSERT_TRUE(recovery.get());
210 const char kRecoveryCreateSql[] =
211 "CREATE VIRTUAL TABLE temp.recover_x using recover("
212 " corrupt.x,"
213 " id INTEGER STRICT,"
214 " v INTEGER STRICT"
215 ")";
216 const char kCreateTable[] = "CREATE TABLE x (id INTEGER, v INTEGER)";
217 const char kCreateIndex[] = "CREATE UNIQUE INDEX x_id ON x (id)";
219 // Replicate data over.
220 const char kRecoveryCopySql[] =
221 "INSERT OR REPLACE INTO x SELECT id, v FROM recover_x";
223 ASSERT_TRUE(recovery->db()->Execute(kRecoveryCreateSql));
224 ASSERT_TRUE(recovery->db()->Execute(kCreateTable));
225 ASSERT_TRUE(recovery->db()->Execute(kCreateIndex));
226 ASSERT_TRUE(recovery->db()->Execute(kRecoveryCopySql));
228 ASSERT_TRUE(sql::Recovery::Recovered(recovery.Pass()));
231 // Build a database, corrupt it by making an index reference to
232 // deleted row, then recover when a query selects that row.
233 TEST_F(SQLRecoveryTest, RecoverCorruptIndex) {
234 const char kCreateTable[] = "CREATE TABLE x (id INTEGER, v INTEGER)";
235 const char kCreateIndex[] = "CREATE UNIQUE INDEX x_id ON x (id)";
236 ASSERT_TRUE(db().Execute(kCreateTable));
237 ASSERT_TRUE(db().Execute(kCreateIndex));
239 // Insert a bit of data.
241 ASSERT_TRUE(db().BeginTransaction());
243 const char kInsertSql[] = "INSERT INTO x (id, v) VALUES (?, ?)";
244 sql::Statement s(db().GetUniqueStatement(kInsertSql));
245 for (int i = 0; i < 10; ++i) {
246 s.Reset(true);
247 s.BindInt(0, i);
248 s.BindInt(1, i);
249 EXPECT_FALSE(s.Step());
250 EXPECT_TRUE(s.Succeeded());
253 ASSERT_TRUE(db().CommitTransaction());
255 db().Close();
257 // Delete a row from the table, while leaving the index entry which
258 // references it.
259 const char kDeleteSql[] = "DELETE FROM x WHERE id = 0";
260 ASSERT_TRUE(sql::test::CorruptTableOrIndex(db_path(), "x_id", kDeleteSql));
262 ASSERT_TRUE(Reopen());
264 int error = SQLITE_OK;
265 db().set_error_callback(base::Bind(&RecoveryCallback,
266 &db(), db_path(), &error));
268 // This works before the callback is called.
269 const char kTrivialSql[] = "SELECT COUNT(*) FROM sqlite_master";
270 EXPECT_TRUE(db().IsSQLValid(kTrivialSql));
272 // TODO(shess): Could this be delete? Anything which fails should work.
273 const char kSelectSql[] = "SELECT v FROM x WHERE id = 0";
274 ASSERT_FALSE(db().Execute(kSelectSql));
275 EXPECT_EQ(SQLITE_CORRUPT, error);
277 // Database handle has been poisoned.
278 EXPECT_FALSE(db().IsSQLValid(kTrivialSql));
280 ASSERT_TRUE(Reopen());
282 // The recovered table should reflect the deletion.
283 const char kSelectAllSql[] = "SELECT v FROM x ORDER BY id";
284 EXPECT_EQ("1,2,3,4,5,6,7,8,9",
285 ExecuteWithResults(&db(), kSelectAllSql, "|", ","));
287 // The failing statement should now succeed, with no results.
288 EXPECT_EQ("", ExecuteWithResults(&db(), kSelectSql, "|", ","));
291 // Build a database, corrupt it by making a table contain a row not
292 // referenced by the index, then recover the database.
293 TEST_F(SQLRecoveryTest, RecoverCorruptTable) {
294 const char kCreateTable[] = "CREATE TABLE x (id INTEGER, v INTEGER)";
295 const char kCreateIndex[] = "CREATE UNIQUE INDEX x_id ON x (id)";
296 ASSERT_TRUE(db().Execute(kCreateTable));
297 ASSERT_TRUE(db().Execute(kCreateIndex));
299 // Insert a bit of data.
301 ASSERT_TRUE(db().BeginTransaction());
303 const char kInsertSql[] = "INSERT INTO x (id, v) VALUES (?, ?)";
304 sql::Statement s(db().GetUniqueStatement(kInsertSql));
305 for (int i = 0; i < 10; ++i) {
306 s.Reset(true);
307 s.BindInt(0, i);
308 s.BindInt(1, i);
309 EXPECT_FALSE(s.Step());
310 EXPECT_TRUE(s.Succeeded());
313 ASSERT_TRUE(db().CommitTransaction());
315 db().Close();
317 // Delete a row from the index while leaving a table entry.
318 const char kDeleteSql[] = "DELETE FROM x WHERE id = 0";
319 ASSERT_TRUE(sql::test::CorruptTableOrIndex(db_path(), "x", kDeleteSql));
321 // TODO(shess): Figure out a query which causes SQLite to notice
322 // this organically. Meanwhile, just handle it manually.
324 ASSERT_TRUE(Reopen());
326 // Index shows one less than originally inserted.
327 const char kCountSql[] = "SELECT COUNT (*) FROM x";
328 EXPECT_EQ("9", ExecuteWithResults(&db(), kCountSql, "|", ","));
330 // A full table scan shows all of the original data.
331 const char kDistinctSql[] = "SELECT DISTINCT COUNT (id) FROM x";
332 EXPECT_EQ("10", ExecuteWithResults(&db(), kDistinctSql, "|", ","));
334 // Insert id 0 again. Since it is not in the index, the insert
335 // succeeds, but results in a duplicate value in the table.
336 const char kInsertSql[] = "INSERT INTO x (id, v) VALUES (0, 100)";
337 ASSERT_TRUE(db().Execute(kInsertSql));
339 // Duplication is visible.
340 EXPECT_EQ("10", ExecuteWithResults(&db(), kCountSql, "|", ","));
341 EXPECT_EQ("11", ExecuteWithResults(&db(), kDistinctSql, "|", ","));
343 // This works before the callback is called.
344 const char kTrivialSql[] = "SELECT COUNT(*) FROM sqlite_master";
345 EXPECT_TRUE(db().IsSQLValid(kTrivialSql));
347 // Call the recovery callback manually.
348 int error = SQLITE_OK;
349 RecoveryCallback(&db(), db_path(), &error, SQLITE_CORRUPT, NULL);
350 EXPECT_EQ(SQLITE_CORRUPT, error);
352 // Database handle has been poisoned.
353 EXPECT_FALSE(db().IsSQLValid(kTrivialSql));
355 ASSERT_TRUE(Reopen());
357 // The recovered table has consistency between the index and the table.
358 EXPECT_EQ("10", ExecuteWithResults(&db(), kCountSql, "|", ","));
359 EXPECT_EQ("10", ExecuteWithResults(&db(), kDistinctSql, "|", ","));
361 // The expected value was retained.
362 const char kSelectSql[] = "SELECT v FROM x WHERE id = 0";
363 EXPECT_EQ("100", ExecuteWithResults(&db(), kSelectSql, "|", ","));
366 TEST_F(SQLRecoveryTest, Meta) {
367 const int kVersion = 3;
368 const int kCompatibleVersion = 2;
371 sql::MetaTable meta;
372 EXPECT_TRUE(meta.Init(&db(), kVersion, kCompatibleVersion));
373 EXPECT_EQ(kVersion, meta.GetVersionNumber());
376 // Test expected case where everything works.
378 scoped_ptr<sql::Recovery> recovery = sql::Recovery::Begin(&db(), db_path());
379 EXPECT_TRUE(recovery->SetupMeta());
380 int version = 0;
381 EXPECT_TRUE(recovery->GetMetaVersionNumber(&version));
382 EXPECT_EQ(kVersion, version);
384 sql::Recovery::Rollback(recovery.Pass());
386 ASSERT_TRUE(Reopen()); // Handle was poisoned.
388 // Test version row missing.
389 EXPECT_TRUE(db().Execute("DELETE FROM meta WHERE key = 'version'"));
391 scoped_ptr<sql::Recovery> recovery = sql::Recovery::Begin(&db(), db_path());
392 EXPECT_TRUE(recovery->SetupMeta());
393 int version = 0;
394 EXPECT_FALSE(recovery->GetMetaVersionNumber(&version));
395 EXPECT_EQ(0, version);
397 sql::Recovery::Rollback(recovery.Pass());
399 ASSERT_TRUE(Reopen()); // Handle was poisoned.
401 // Test meta table missing.
402 EXPECT_TRUE(db().Execute("DROP TABLE meta"));
404 sql::ScopedErrorIgnorer ignore_errors;
405 ignore_errors.IgnoreError(SQLITE_CORRUPT); // From virtual table.
406 scoped_ptr<sql::Recovery> recovery = sql::Recovery::Begin(&db(), db_path());
407 EXPECT_FALSE(recovery->SetupMeta());
408 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors());
412 // Baseline AutoRecoverTable() test.
413 TEST_F(SQLRecoveryTest, AutoRecoverTable) {
414 // BIGINT and VARCHAR to test type affinity.
415 const char kCreateSql[] = "CREATE TABLE x (id BIGINT, t TEXT, v VARCHAR)";
416 ASSERT_TRUE(db().Execute(kCreateSql));
417 ASSERT_TRUE(db().Execute("INSERT INTO x VALUES (11, 'This is', 'a test')"));
418 ASSERT_TRUE(db().Execute("INSERT INTO x VALUES (5, 'That was', 'a test')"));
420 // Save aside a copy of the original schema and data.
421 const std::string orig_schema(GetSchema(&db()));
422 const char kXSql[] = "SELECT * FROM x ORDER BY 1";
423 const std::string orig_data(ExecuteWithResults(&db(), kXSql, "|", "\n"));
425 // Create a lame-duck table which will not be propagated by recovery to
426 // detect that the recovery code actually ran.
427 ASSERT_TRUE(db().Execute("CREATE TABLE y (c TEXT)"));
428 ASSERT_NE(orig_schema, GetSchema(&db()));
431 scoped_ptr<sql::Recovery> recovery = sql::Recovery::Begin(&db(), db_path());
432 ASSERT_TRUE(recovery->db()->Execute(kCreateSql));
434 // Save a copy of the temp db's schema before recovering the table.
435 const char kTempSchemaSql[] = "SELECT name, sql FROM sqlite_temp_master";
436 const std::string temp_schema(
437 ExecuteWithResults(recovery->db(), kTempSchemaSql, "|", "\n"));
439 size_t rows = 0;
440 EXPECT_TRUE(recovery->AutoRecoverTable("x", 0, &rows));
441 EXPECT_EQ(2u, rows);
443 // Test that any additional temp tables were cleaned up.
444 EXPECT_EQ(temp_schema,
445 ExecuteWithResults(recovery->db(), kTempSchemaSql, "|", "\n"));
447 ASSERT_TRUE(sql::Recovery::Recovered(recovery.Pass()));
450 // Since the database was not corrupt, the entire schema and all
451 // data should be recovered.
452 ASSERT_TRUE(Reopen());
453 ASSERT_EQ(orig_schema, GetSchema(&db()));
454 ASSERT_EQ(orig_data, ExecuteWithResults(&db(), kXSql, "|", "\n"));
456 // Recovery fails if the target table doesn't exist.
458 scoped_ptr<sql::Recovery> recovery = sql::Recovery::Begin(&db(), db_path());
459 ASSERT_TRUE(recovery->db()->Execute(kCreateSql));
461 // TODO(shess): Should this failure implicitly lead to Raze()?
462 size_t rows = 0;
463 EXPECT_FALSE(recovery->AutoRecoverTable("y", 0, &rows));
465 sql::Recovery::Unrecoverable(recovery.Pass());
469 // Test that default values correctly replace nulls. The recovery
470 // virtual table reads directly from the database, so DEFAULT is not
471 // interpretted at that level.
472 TEST_F(SQLRecoveryTest, AutoRecoverTableWithDefault) {
473 ASSERT_TRUE(db().Execute("CREATE TABLE x (id INTEGER)"));
474 ASSERT_TRUE(db().Execute("INSERT INTO x VALUES (5)"));
475 ASSERT_TRUE(db().Execute("INSERT INTO x VALUES (15)"));
477 // ALTER effectively leaves the new columns NULL in the first two
478 // rows. The row with 17 will get the default injected at insert
479 // time, while the row with 42 will get the actual value provided.
480 // Embedded "'" to make sure default-handling continues to be quoted
481 // correctly.
482 ASSERT_TRUE(db().Execute("ALTER TABLE x ADD COLUMN t TEXT DEFAULT 'a''a'"));
483 ASSERT_TRUE(db().Execute("ALTER TABLE x ADD COLUMN b BLOB DEFAULT x'AA55'"));
484 ASSERT_TRUE(db().Execute("ALTER TABLE x ADD COLUMN i INT DEFAULT 93"));
485 ASSERT_TRUE(db().Execute("INSERT INTO x (id) VALUES (17)"));
486 ASSERT_TRUE(db().Execute("INSERT INTO x VALUES (42, 'b', x'1234', 12)"));
488 // Save aside a copy of the original schema and data.
489 const std::string orig_schema(GetSchema(&db()));
490 const char kXSql[] = "SELECT * FROM x ORDER BY 1";
491 const std::string orig_data(ExecuteWithResults(&db(), kXSql, "|", "\n"));
493 // Create a lame-duck table which will not be propagated by recovery to
494 // detect that the recovery code actually ran.
495 ASSERT_TRUE(db().Execute("CREATE TABLE y (c TEXT)"));
496 ASSERT_NE(orig_schema, GetSchema(&db()));
498 // Mechanically adjust the stored schema and data to allow detecting
499 // where the default value is coming from. The target table is just
500 // like the original with the default for [t] changed, to signal
501 // defaults coming from the recovery system. The two %5 rows should
502 // get the target-table default for [t], while the others should get
503 // the source-table default.
504 std::string final_schema(orig_schema);
505 std::string final_data(orig_data);
506 size_t pos;
507 while ((pos = final_schema.find("'a''a'")) != std::string::npos) {
508 final_schema.replace(pos, 6, "'c''c'");
510 while ((pos = final_data.find("5|a'a")) != std::string::npos) {
511 final_data.replace(pos, 5, "5|c'c");
515 scoped_ptr<sql::Recovery> recovery = sql::Recovery::Begin(&db(), db_path());
516 // Different default to detect which table provides the default.
517 ASSERT_TRUE(recovery->db()->Execute(final_schema.c_str()));
519 size_t rows = 0;
520 EXPECT_TRUE(recovery->AutoRecoverTable("x", 0, &rows));
521 EXPECT_EQ(4u, rows);
523 ASSERT_TRUE(sql::Recovery::Recovered(recovery.Pass()));
526 // Since the database was not corrupt, the entire schema and all
527 // data should be recovered.
528 ASSERT_TRUE(Reopen());
529 ASSERT_EQ(final_schema, GetSchema(&db()));
530 ASSERT_EQ(final_data, ExecuteWithResults(&db(), kXSql, "|", "\n"));
533 // Test that rows with NULL in a NOT NULL column are filtered
534 // correctly. In the wild, this would probably happen due to
535 // corruption, but here it is simulated by recovering a table which
536 // allowed nulls into a table which does not.
537 TEST_F(SQLRecoveryTest, AutoRecoverTableNullFilter) {
538 const char kOrigSchema[] = "CREATE TABLE x (id INTEGER, t TEXT)";
539 const char kFinalSchema[] = "CREATE TABLE x (id INTEGER, t TEXT NOT NULL)";
541 ASSERT_TRUE(db().Execute(kOrigSchema));
542 ASSERT_TRUE(db().Execute("INSERT INTO x VALUES (5, null)"));
543 ASSERT_TRUE(db().Execute("INSERT INTO x VALUES (15, 'this is a test')"));
545 // Create a lame-duck table which will not be propagated by recovery to
546 // detect that the recovery code actually ran.
547 ASSERT_EQ(kOrigSchema, GetSchema(&db()));
548 ASSERT_TRUE(db().Execute("CREATE TABLE y (c TEXT)"));
549 ASSERT_NE(kOrigSchema, GetSchema(&db()));
552 scoped_ptr<sql::Recovery> recovery = sql::Recovery::Begin(&db(), db_path());
553 ASSERT_TRUE(recovery->db()->Execute(kFinalSchema));
555 size_t rows = 0;
556 EXPECT_TRUE(recovery->AutoRecoverTable("x", 0, &rows));
557 EXPECT_EQ(1u, rows);
559 ASSERT_TRUE(sql::Recovery::Recovered(recovery.Pass()));
562 // The schema should be the same, but only one row of data should
563 // have been recovered.
564 ASSERT_TRUE(Reopen());
565 ASSERT_EQ(kFinalSchema, GetSchema(&db()));
566 const char kXSql[] = "SELECT * FROM x ORDER BY 1";
567 ASSERT_EQ("15|this is a test", ExecuteWithResults(&db(), kXSql, "|", "\n"));
570 // Test AutoRecoverTable with a ROWID alias.
571 TEST_F(SQLRecoveryTest, AutoRecoverTableWithRowid) {
572 // The rowid alias is almost always the first column, intentionally
573 // put it later.
574 const char kCreateSql[] =
575 "CREATE TABLE x (t TEXT, id INTEGER PRIMARY KEY NOT NULL)";
576 ASSERT_TRUE(db().Execute(kCreateSql));
577 ASSERT_TRUE(db().Execute("INSERT INTO x VALUES ('This is a test', null)"));
578 ASSERT_TRUE(db().Execute("INSERT INTO x VALUES ('That was a test', null)"));
580 // Save aside a copy of the original schema and data.
581 const std::string orig_schema(GetSchema(&db()));
582 const char kXSql[] = "SELECT * FROM x ORDER BY 1";
583 const std::string orig_data(ExecuteWithResults(&db(), kXSql, "|", "\n"));
585 // Create a lame-duck table which will not be propagated by recovery to
586 // detect that the recovery code actually ran.
587 ASSERT_TRUE(db().Execute("CREATE TABLE y (c TEXT)"));
588 ASSERT_NE(orig_schema, GetSchema(&db()));
591 scoped_ptr<sql::Recovery> recovery = sql::Recovery::Begin(&db(), db_path());
592 ASSERT_TRUE(recovery->db()->Execute(kCreateSql));
594 size_t rows = 0;
595 EXPECT_TRUE(recovery->AutoRecoverTable("x", 0, &rows));
596 EXPECT_EQ(2u, rows);
598 ASSERT_TRUE(sql::Recovery::Recovered(recovery.Pass()));
601 // Since the database was not corrupt, the entire schema and all
602 // data should be recovered.
603 ASSERT_TRUE(Reopen());
604 ASSERT_EQ(orig_schema, GetSchema(&db()));
605 ASSERT_EQ(orig_data, ExecuteWithResults(&db(), kXSql, "|", "\n"));
608 // Test that a compound primary key doesn't fire the ROWID code.
609 TEST_F(SQLRecoveryTest, AutoRecoverTableWithCompoundKey) {
610 const char kCreateSql[] =
611 "CREATE TABLE x ("
612 "id INTEGER NOT NULL,"
613 "id2 TEXT NOT NULL,"
614 "t TEXT,"
615 "PRIMARY KEY (id, id2)"
616 ")";
617 ASSERT_TRUE(db().Execute(kCreateSql));
619 // NOTE(shess): Do not accidentally use [id] 1, 2, 3, as those will
620 // be the ROWID values.
621 ASSERT_TRUE(db().Execute("INSERT INTO x VALUES (1, 'a', 'This is a test')"));
622 ASSERT_TRUE(db().Execute("INSERT INTO x VALUES (1, 'b', 'That was a test')"));
623 ASSERT_TRUE(db().Execute("INSERT INTO x VALUES (2, 'a', 'Another test')"));
625 // Save aside a copy of the original schema and data.
626 const std::string orig_schema(GetSchema(&db()));
627 const char kXSql[] = "SELECT * FROM x ORDER BY 1";
628 const std::string orig_data(ExecuteWithResults(&db(), kXSql, "|", "\n"));
630 // Create a lame-duck table which will not be propagated by recovery to
631 // detect that the recovery code actually ran.
632 ASSERT_TRUE(db().Execute("CREATE TABLE y (c TEXT)"));
633 ASSERT_NE(orig_schema, GetSchema(&db()));
636 scoped_ptr<sql::Recovery> recovery = sql::Recovery::Begin(&db(), db_path());
637 ASSERT_TRUE(recovery->db()->Execute(kCreateSql));
639 size_t rows = 0;
640 EXPECT_TRUE(recovery->AutoRecoverTable("x", 0, &rows));
641 EXPECT_EQ(3u, rows);
643 ASSERT_TRUE(sql::Recovery::Recovered(recovery.Pass()));
646 // Since the database was not corrupt, the entire schema and all
647 // data should be recovered.
648 ASSERT_TRUE(Reopen());
649 ASSERT_EQ(orig_schema, GetSchema(&db()));
650 ASSERT_EQ(orig_data, ExecuteWithResults(&db(), kXSql, "|", "\n"));
653 // Test |extend_columns| support.
654 TEST_F(SQLRecoveryTest, AutoRecoverTableExtendColumns) {
655 const char kCreateSql[] = "CREATE TABLE x (id INTEGER PRIMARY KEY, t0 TEXT)";
656 ASSERT_TRUE(db().Execute(kCreateSql));
657 ASSERT_TRUE(db().Execute("INSERT INTO x VALUES (1, 'This is')"));
658 ASSERT_TRUE(db().Execute("INSERT INTO x VALUES (2, 'That was')"));
660 // Save aside a copy of the original schema and data.
661 const std::string orig_schema(GetSchema(&db()));
662 const char kXSql[] = "SELECT * FROM x ORDER BY 1";
663 const std::string orig_data(ExecuteWithResults(&db(), kXSql, "|", "\n"));
665 // Modify the table to add a column, and add data to that column.
666 ASSERT_TRUE(db().Execute("ALTER TABLE x ADD COLUMN t1 TEXT"));
667 ASSERT_TRUE(db().Execute("UPDATE x SET t1 = 'a test'"));
668 ASSERT_NE(orig_schema, GetSchema(&db()));
669 ASSERT_NE(orig_data, ExecuteWithResults(&db(), kXSql, "|", "\n"));
672 scoped_ptr<sql::Recovery> recovery = sql::Recovery::Begin(&db(), db_path());
673 ASSERT_TRUE(recovery->db()->Execute(kCreateSql));
674 size_t rows = 0;
675 EXPECT_TRUE(recovery->AutoRecoverTable("x", 1, &rows));
676 EXPECT_EQ(2u, rows);
677 ASSERT_TRUE(sql::Recovery::Recovered(recovery.Pass()));
680 // Since the database was not corrupt, the entire schema and all
681 // data should be recovered.
682 ASSERT_TRUE(Reopen());
683 ASSERT_EQ(orig_schema, GetSchema(&db()));
684 ASSERT_EQ(orig_data, ExecuteWithResults(&db(), kXSql, "|", "\n"));
686 #endif // !defined(USE_SYSTEM_SQLITE)
688 } // namespace