Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(414)

Side by Side Diff: sql/recovery_unittest.cc

Issue 50493012: [sql] Recover Favicons v5 databases, with more recovery automation. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase, merge rows-recovered support. Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « sql/recovery.cc ('k') | sql/test/test_helpers.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #include "base/bind.h" 5 #include "base/bind.h"
6 #include "base/file_util.h" 6 #include "base/file_util.h"
7 #include "base/files/scoped_temp_dir.h" 7 #include "base/files/scoped_temp_dir.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/strings/string_number_conversions.h"
9 #include "base/strings/stringprintf.h" 10 #include "base/strings/stringprintf.h"
10 #include "sql/connection.h" 11 #include "sql/connection.h"
11 #include "sql/meta_table.h" 12 #include "sql/meta_table.h"
12 #include "sql/recovery.h" 13 #include "sql/recovery.h"
13 #include "sql/statement.h" 14 #include "sql/statement.h"
14 #include "sql/test/scoped_error_ignorer.h" 15 #include "sql/test/scoped_error_ignorer.h"
15 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
16 #include "third_party/sqlite/sqlite3.h" 17 #include "third_party/sqlite/sqlite3.h"
17 18
18 namespace { 19 namespace {
19 20
20 // Execute |sql|, and stringify the results with |column_sep| between 21 // Execute |sql|, and stringify the results with |column_sep| between
21 // columns and |row_sep| between rows. 22 // columns and |row_sep| between rows.
22 // TODO(shess): Promote this to a central testing helper. 23 // TODO(shess): Promote this to a central testing helper.
23 std::string ExecuteWithResults(sql::Connection* db, 24 std::string ExecuteWithResults(sql::Connection* db,
24 const char* sql, 25 const char* sql,
25 const char* column_sep, 26 const char* column_sep,
26 const char* row_sep) { 27 const char* row_sep) {
27 sql::Statement s(db->GetUniqueStatement(sql)); 28 sql::Statement s(db->GetUniqueStatement(sql));
28 std::string ret; 29 std::string ret;
29 while (s.Step()) { 30 while (s.Step()) {
30 if (!ret.empty()) 31 if (!ret.empty())
31 ret += row_sep; 32 ret += row_sep;
32 for (int i = 0; i < s.ColumnCount(); ++i) { 33 for (int i = 0; i < s.ColumnCount(); ++i) {
33 if (i > 0) 34 if (i > 0)
34 ret += column_sep; 35 ret += column_sep;
35 ret += s.ColumnString(i); 36 if (s.ColumnType(i) == sql::COLUMN_TYPE_NULL) {
37 ret += "<null>";
38 } else if (s.ColumnType(i) == sql::COLUMN_TYPE_BLOB) {
39 ret += "<x'";
40 ret += base::HexEncode(s.ColumnBlob(i), s.ColumnByteLength(i));
41 ret += "'>";
42 } else {
43 ret += s.ColumnString(i);
44 }
36 } 45 }
37 } 46 }
38 return ret; 47 return ret;
39 } 48 }
40 49
41 // Dump consistent human-readable representation of the database 50 // Dump consistent human-readable representation of the database
42 // schema. For tables or indices, this will contain the sql command 51 // schema. For tables or indices, this will contain the sql command
43 // to create the table or index. For certain automatic SQLite 52 // to create the table or index. For certain automatic SQLite
44 // structures with no sql, the name is used. 53 // structures with no sql, the name is used.
45 std::string GetSchema(sql::Connection* db) { 54 std::string GetSchema(sql::Connection* db) {
(...skipping 367 matching lines...) Expand 10 before | Expand all | Expand 10 after
413 ASSERT_TRUE(Reopen()); 422 ASSERT_TRUE(Reopen());
414 423
415 // The recovered table has consistency between the index and the table. 424 // The recovered table has consistency between the index and the table.
416 EXPECT_EQ("10", ExecuteWithResults(&db(), kCountSql, "|", ",")); 425 EXPECT_EQ("10", ExecuteWithResults(&db(), kCountSql, "|", ","));
417 EXPECT_EQ("10", ExecuteWithResults(&db(), kDistinctSql, "|", ",")); 426 EXPECT_EQ("10", ExecuteWithResults(&db(), kDistinctSql, "|", ","));
418 427
419 // The expected value was retained. 428 // The expected value was retained.
420 const char kSelectSql[] = "SELECT v FROM x WHERE id = 0"; 429 const char kSelectSql[] = "SELECT v FROM x WHERE id = 0";
421 EXPECT_EQ("100", ExecuteWithResults(&db(), kSelectSql, "|", ",")); 430 EXPECT_EQ("100", ExecuteWithResults(&db(), kSelectSql, "|", ","));
422 } 431 }
432
433 TEST_F(SQLRecoveryTest, Meta) {
434 const int kVersion = 3;
435 const int kCompatibleVersion = 2;
436
437 {
438 sql::MetaTable meta;
439 EXPECT_TRUE(meta.Init(&db(), kVersion, kCompatibleVersion));
440 EXPECT_EQ(kVersion, meta.GetVersionNumber());
441 }
442
443 // Test expected case where everything works.
444 {
445 scoped_ptr<sql::Recovery> recovery = sql::Recovery::Begin(&db(), db_path());
446 EXPECT_TRUE(recovery->SetupMeta());
447 int version = 0;
448 EXPECT_TRUE(recovery->GetMetaVersionNumber(&version));
449 EXPECT_EQ(kVersion, version);
450
451 sql::Recovery::Rollback(recovery.Pass());
452 }
453 ASSERT_TRUE(Reopen()); // Handle was poisoned.
454
455 // Test version row missing.
456 EXPECT_TRUE(db().Execute("DELETE FROM meta WHERE key = 'version'"));
457 {
458 scoped_ptr<sql::Recovery> recovery = sql::Recovery::Begin(&db(), db_path());
459 EXPECT_TRUE(recovery->SetupMeta());
460 int version = 0;
461 EXPECT_FALSE(recovery->GetMetaVersionNumber(&version));
462 EXPECT_EQ(0, version);
463
464 sql::Recovery::Rollback(recovery.Pass());
465 }
466 ASSERT_TRUE(Reopen()); // Handle was poisoned.
467
468 // Test meta table missing.
469 EXPECT_TRUE(db().Execute("DROP TABLE meta"));
470 {
471 sql::ScopedErrorIgnorer ignore_errors;
472 ignore_errors.IgnoreError(SQLITE_CORRUPT); // From virtual table.
473 scoped_ptr<sql::Recovery> recovery = sql::Recovery::Begin(&db(), db_path());
474 EXPECT_FALSE(recovery->SetupMeta());
475 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors());
476 }
477 }
478
479 // Baseline AutoRecoverTable() test.
480 TEST_F(SQLRecoveryTest, AutoRecoverTable) {
481 // BIGINT and VARCHAR to test type affinity.
482 const char kCreateSql[] = "CREATE TABLE x (id BIGINT, t TEXT, v VARCHAR)";
483 ASSERT_TRUE(db().Execute(kCreateSql));
484 ASSERT_TRUE(db().Execute("INSERT INTO x VALUES (11, 'This is', 'a test')"));
485 ASSERT_TRUE(db().Execute("INSERT INTO x VALUES (5, 'That was', 'a test')"));
486
487 // Save aside a copy of the original schema and data.
488 const std::string orig_schema(GetSchema(&db()));
489 const char kXSql[] = "SELECT * FROM x ORDER BY 1";
490 const std::string orig_data(ExecuteWithResults(&db(), kXSql, "|", "\n"));
491
492 // Create a lame-duck table which will not be propagated by recovery to
493 // detect that the recovery code actually ran.
494 ASSERT_TRUE(db().Execute("CREATE TABLE y (c TEXT)"));
495 ASSERT_NE(orig_schema, GetSchema(&db()));
496
497 {
498 scoped_ptr<sql::Recovery> recovery = sql::Recovery::Begin(&db(), db_path());
499 ASSERT_TRUE(recovery->db()->Execute(kCreateSql));
500
501 // Save a copy of the temp db's schema before recovering the table.
502 const char kTempSchemaSql[] = "SELECT name, sql FROM sqlite_temp_master";
503 const std::string temp_schema(
504 ExecuteWithResults(recovery->db(), kTempSchemaSql, "|", "\n"));
505
506 size_t rows = 0;
507 EXPECT_TRUE(recovery->AutoRecoverTable("x", 0, &rows));
508 EXPECT_EQ(2u, rows);
509
510 // Test that any additional temp tables were cleaned up.
511 EXPECT_EQ(temp_schema,
512 ExecuteWithResults(recovery->db(), kTempSchemaSql, "|", "\n"));
513
514 ASSERT_TRUE(sql::Recovery::Recovered(recovery.Pass()));
515 }
516
517 // Since the database was not corrupt, the entire schema and all
518 // data should be recovered.
519 ASSERT_TRUE(Reopen());
520 ASSERT_EQ(orig_schema, GetSchema(&db()));
521 ASSERT_EQ(orig_data, ExecuteWithResults(&db(), kXSql, "|", "\n"));
522
523 // Recovery fails if the target table doesn't exist.
524 {
525 scoped_ptr<sql::Recovery> recovery = sql::Recovery::Begin(&db(), db_path());
526 ASSERT_TRUE(recovery->db()->Execute(kCreateSql));
527
528 // TODO(shess): Should this failure implicitly lead to Raze()?
529 size_t rows = 0;
530 EXPECT_FALSE(recovery->AutoRecoverTable("y", 0, &rows));
531
532 sql::Recovery::Unrecoverable(recovery.Pass());
533 }
534 }
535
536 // Test that default values correctly replace nulls. The recovery
537 // virtual table reads directly from the database, so DEFAULT is not
538 // interpretted at that level.
539 TEST_F(SQLRecoveryTest, AutoRecoverTableWithDefault) {
540 ASSERT_TRUE(db().Execute("CREATE TABLE x (id INTEGER)"));
541 ASSERT_TRUE(db().Execute("INSERT INTO x VALUES (5)"));
542 ASSERT_TRUE(db().Execute("INSERT INTO x VALUES (15)"));
543
544 // ALTER effectively leaves the new columns NULL in the first two
545 // rows. The row with 17 will get the default injected at insert
546 // time, while the row with 42 will get the actual value provided.
547 // Embedded "'" to make sure default-handling continues to be quoted
548 // correctly.
549 ASSERT_TRUE(db().Execute("ALTER TABLE x ADD COLUMN t TEXT DEFAULT 'a''a'"));
550 ASSERT_TRUE(db().Execute("ALTER TABLE x ADD COLUMN b BLOB DEFAULT x'AA55'"));
551 ASSERT_TRUE(db().Execute("ALTER TABLE x ADD COLUMN i INT DEFAULT 93"));
552 ASSERT_TRUE(db().Execute("INSERT INTO x (id) VALUES (17)"));
553 ASSERT_TRUE(db().Execute("INSERT INTO x VALUES (42, 'b', x'1234', 12)"));
554
555 // Save aside a copy of the original schema and data.
556 const std::string orig_schema(GetSchema(&db()));
557 const char kXSql[] = "SELECT * FROM x ORDER BY 1";
558 const std::string orig_data(ExecuteWithResults(&db(), kXSql, "|", "\n"));
559
560 // Create a lame-duck table which will not be propagated by recovery to
561 // detect that the recovery code actually ran.
562 ASSERT_TRUE(db().Execute("CREATE TABLE y (c TEXT)"));
563 ASSERT_NE(orig_schema, GetSchema(&db()));
564
565 // Mechanically adjust the stored schema and data to allow detecting
566 // where the default value is coming from. The target table is just
567 // like the original with the default for [t] changed, to signal
568 // defaults coming from the recovery system. The two %5 rows should
569 // get the target-table default for [t], while the others should get
570 // the source-table default.
571 std::string final_schema(orig_schema);
572 std::string final_data(orig_data);
573 size_t pos;
574 while ((pos = final_schema.find("'a''a'")) != std::string::npos) {
575 final_schema.replace(pos, 6, "'c''c'");
576 }
577 while ((pos = final_data.find("5|a'a")) != std::string::npos) {
578 final_data.replace(pos, 5, "5|c'c");
579 }
580
581 {
582 scoped_ptr<sql::Recovery> recovery = sql::Recovery::Begin(&db(), db_path());
583 // Different default to detect which table provides the default.
584 ASSERT_TRUE(recovery->db()->Execute(final_schema.c_str()));
585
586 size_t rows = 0;
587 EXPECT_TRUE(recovery->AutoRecoverTable("x", 0, &rows));
588 EXPECT_EQ(4u, rows);
589
590 ASSERT_TRUE(sql::Recovery::Recovered(recovery.Pass()));
591 }
592
593 // Since the database was not corrupt, the entire schema and all
594 // data should be recovered.
595 ASSERT_TRUE(Reopen());
596 ASSERT_EQ(final_schema, GetSchema(&db()));
597 ASSERT_EQ(final_data, ExecuteWithResults(&db(), kXSql, "|", "\n"));
598 }
599
600 // Test that rows with NULL in a NOT NULL column are filtered
601 // correctly. In the wild, this would probably happen due to
602 // corruption, but here it is simulated by recovering a table which
603 // allowed nulls into a table which does not.
604 TEST_F(SQLRecoveryTest, AutoRecoverTableNullFilter) {
605 const char kOrigSchema[] = "CREATE TABLE x (id INTEGER, t TEXT)";
606 const char kFinalSchema[] = "CREATE TABLE x (id INTEGER, t TEXT NOT NULL)";
607
608 ASSERT_TRUE(db().Execute(kOrigSchema));
609 ASSERT_TRUE(db().Execute("INSERT INTO x VALUES (5, null)"));
610 ASSERT_TRUE(db().Execute("INSERT INTO x VALUES (15, 'this is a test')"));
611
612 // Create a lame-duck table which will not be propagated by recovery to
613 // detect that the recovery code actually ran.
614 ASSERT_EQ(kOrigSchema, GetSchema(&db()));
615 ASSERT_TRUE(db().Execute("CREATE TABLE y (c TEXT)"));
616 ASSERT_NE(kOrigSchema, GetSchema(&db()));
617
618 {
619 scoped_ptr<sql::Recovery> recovery = sql::Recovery::Begin(&db(), db_path());
620 ASSERT_TRUE(recovery->db()->Execute(kFinalSchema));
621
622 size_t rows = 0;
623 EXPECT_TRUE(recovery->AutoRecoverTable("x", 0, &rows));
624 EXPECT_EQ(1u, rows);
625
626 ASSERT_TRUE(sql::Recovery::Recovered(recovery.Pass()));
627 }
628
629 // The schema should be the same, but only one row of data should
630 // have been recovered.
631 ASSERT_TRUE(Reopen());
632 ASSERT_EQ(kFinalSchema, GetSchema(&db()));
633 const char kXSql[] = "SELECT * FROM x ORDER BY 1";
634 ASSERT_EQ("15|this is a test", ExecuteWithResults(&db(), kXSql, "|", "\n"));
635 }
636
637 // Test AutoRecoverTable with a ROWID alias.
638 TEST_F(SQLRecoveryTest, AutoRecoverTableWithRowid) {
639 // The rowid alias is almost always the first column, intentionally
640 // put it later.
641 const char kCreateSql[] =
642 "CREATE TABLE x (t TEXT, id INTEGER PRIMARY KEY NOT NULL)";
643 ASSERT_TRUE(db().Execute(kCreateSql));
644 ASSERT_TRUE(db().Execute("INSERT INTO x VALUES ('This is a test', null)"));
645 ASSERT_TRUE(db().Execute("INSERT INTO x VALUES ('That was a test', null)"));
646
647 // Save aside a copy of the original schema and data.
648 const std::string orig_schema(GetSchema(&db()));
649 const char kXSql[] = "SELECT * FROM x ORDER BY 1";
650 const std::string orig_data(ExecuteWithResults(&db(), kXSql, "|", "\n"));
651
652 // Create a lame-duck table which will not be propagated by recovery to
653 // detect that the recovery code actually ran.
654 ASSERT_TRUE(db().Execute("CREATE TABLE y (c TEXT)"));
655 ASSERT_NE(orig_schema, GetSchema(&db()));
656
657 {
658 scoped_ptr<sql::Recovery> recovery = sql::Recovery::Begin(&db(), db_path());
659 ASSERT_TRUE(recovery->db()->Execute(kCreateSql));
660
661 size_t rows = 0;
662 EXPECT_TRUE(recovery->AutoRecoverTable("x", 0, &rows));
663 EXPECT_EQ(2u, rows);
664
665 ASSERT_TRUE(sql::Recovery::Recovered(recovery.Pass()));
666 }
667
668 // Since the database was not corrupt, the entire schema and all
669 // data should be recovered.
670 ASSERT_TRUE(Reopen());
671 ASSERT_EQ(orig_schema, GetSchema(&db()));
672 ASSERT_EQ(orig_data, ExecuteWithResults(&db(), kXSql, "|", "\n"));
673 }
674
675 // Test that a compound primary key doesn't fire the ROWID code.
676 TEST_F(SQLRecoveryTest, AutoRecoverTableWithCompoundKey) {
677 const char kCreateSql[] =
678 "CREATE TABLE x ("
679 "id INTEGER NOT NULL,"
680 "id2 TEXT NOT NULL,"
681 "t TEXT,"
682 "PRIMARY KEY (id, id2)"
683 ")";
684 ASSERT_TRUE(db().Execute(kCreateSql));
685
686 // NOTE(shess): Do not accidentally use [id] 1, 2, 3, as those will
687 // be the ROWID values.
688 ASSERT_TRUE(db().Execute("INSERT INTO x VALUES (1, 'a', 'This is a test')"));
689 ASSERT_TRUE(db().Execute("INSERT INTO x VALUES (1, 'b', 'That was a test')"));
690 ASSERT_TRUE(db().Execute("INSERT INTO x VALUES (2, 'a', 'Another test')"));
691
692 // Save aside a copy of the original schema and data.
693 const std::string orig_schema(GetSchema(&db()));
694 const char kXSql[] = "SELECT * FROM x ORDER BY 1";
695 const std::string orig_data(ExecuteWithResults(&db(), kXSql, "|", "\n"));
696
697 // Create a lame-duck table which will not be propagated by recovery to
698 // detect that the recovery code actually ran.
699 ASSERT_TRUE(db().Execute("CREATE TABLE y (c TEXT)"));
700 ASSERT_NE(orig_schema, GetSchema(&db()));
701
702 {
703 scoped_ptr<sql::Recovery> recovery = sql::Recovery::Begin(&db(), db_path());
704 ASSERT_TRUE(recovery->db()->Execute(kCreateSql));
705
706 size_t rows = 0;
707 EXPECT_TRUE(recovery->AutoRecoverTable("x", 0, &rows));
708 EXPECT_EQ(3u, rows);
709
710 ASSERT_TRUE(sql::Recovery::Recovered(recovery.Pass()));
711 }
712
713 // Since the database was not corrupt, the entire schema and all
714 // data should be recovered.
715 ASSERT_TRUE(Reopen());
716 ASSERT_EQ(orig_schema, GetSchema(&db()));
717 ASSERT_EQ(orig_data, ExecuteWithResults(&db(), kXSql, "|", "\n"));
718 }
719
720 // Test |extend_columns| support.
721 TEST_F(SQLRecoveryTest, AutoRecoverTableExtendColumns) {
722 const char kCreateSql[] = "CREATE TABLE x (id INTEGER PRIMARY KEY, t0 TEXT)";
723 ASSERT_TRUE(db().Execute(kCreateSql));
724 ASSERT_TRUE(db().Execute("INSERT INTO x VALUES (1, 'This is')"));
725 ASSERT_TRUE(db().Execute("INSERT INTO x VALUES (2, 'That was')"));
726
727 // Save aside a copy of the original schema and data.
728 const std::string orig_schema(GetSchema(&db()));
729 const char kXSql[] = "SELECT * FROM x ORDER BY 1";
730 const std::string orig_data(ExecuteWithResults(&db(), kXSql, "|", "\n"));
731
732 // Modify the table to add a column, and add data to that column.
733 ASSERT_TRUE(db().Execute("ALTER TABLE x ADD COLUMN t1 TEXT"));
734 ASSERT_TRUE(db().Execute("UPDATE x SET t1 = 'a test'"));
735 ASSERT_NE(orig_schema, GetSchema(&db()));
736 ASSERT_NE(orig_data, ExecuteWithResults(&db(), kXSql, "|", "\n"));
737
738 {
739 scoped_ptr<sql::Recovery> recovery = sql::Recovery::Begin(&db(), db_path());
740 ASSERT_TRUE(recovery->db()->Execute(kCreateSql));
741 size_t rows = 0;
742 EXPECT_TRUE(recovery->AutoRecoverTable("x", 1, &rows));
743 EXPECT_EQ(2u, rows);
744 ASSERT_TRUE(sql::Recovery::Recovered(recovery.Pass()));
745 }
746
747 // Since the database was not corrupt, the entire schema and all
748 // data should be recovered.
749 ASSERT_TRUE(Reopen());
750 ASSERT_EQ(orig_schema, GetSchema(&db()));
751 ASSERT_EQ(orig_data, ExecuteWithResults(&db(), kXSql, "|", "\n"));
752 }
423 #endif // !defined(USE_SYSTEM_SQLITE) 753 #endif // !defined(USE_SYSTEM_SQLITE)
424 754
425 } // namespace 755 } // namespace
OLDNEW
« no previous file with comments | « sql/recovery.cc ('k') | sql/test/test_helpers.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698