OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "chrome/browser/sync/syncable/directory_backing_store.h" | 5 #include "chrome/browser/sync/syncable/directory_backing_store.h" |
6 | 6 |
7 #include "build/build_config.h" | 7 #include "build/build_config.h" |
8 | 8 |
9 #if defined(OS_MACOSX) | 9 #if defined(OS_MACOSX) |
10 #include <CoreFoundation/CoreFoundation.h> | 10 #include <CoreFoundation/CoreFoundation.h> |
(...skipping 26 matching lines...) Expand all Loading... |
37 using std::string; | 37 using std::string; |
38 | 38 |
39 namespace syncable { | 39 namespace syncable { |
40 | 40 |
41 // This just has to be big enough to hold an UPDATE or INSERT statement that | 41 // This just has to be big enough to hold an UPDATE or INSERT statement that |
42 // modifies all the columns in the entry table. | 42 // modifies all the columns in the entry table. |
43 static const string::size_type kUpdateStatementBufferSize = 2048; | 43 static const string::size_type kUpdateStatementBufferSize = 2048; |
44 | 44 |
45 // Increment this version whenever updating DB tables. | 45 // Increment this version whenever updating DB tables. |
46 extern const int32 kCurrentDBVersion; // Global visibility for our unittest. | 46 extern const int32 kCurrentDBVersion; // Global visibility for our unittest. |
47 const int32 kCurrentDBVersion = 73; | 47 const int32 kCurrentDBVersion = 74; |
48 | 48 |
49 namespace { | 49 namespace { |
50 | 50 |
51 int ExecQuery(sqlite3* dbhandle, const char* query) { | 51 int ExecQuery(sqlite3* dbhandle, const char* query) { |
52 SQLStatement statement; | 52 SQLStatement statement; |
53 int result = statement.prepare(dbhandle, query); | 53 int result = statement.prepare(dbhandle, query); |
54 if (SQLITE_OK != result) | 54 if (SQLITE_OK != result) |
55 return result; | 55 return result; |
56 do { | 56 do { |
57 result = statement.step(); | 57 result = statement.step(); |
(...skipping 314 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
372 | 372 |
373 if (!DeleteEntries(snapshot.metahandles_to_purge)) | 373 if (!DeleteEntries(snapshot.metahandles_to_purge)) |
374 return false; | 374 return false; |
375 | 375 |
376 if (save_info) { | 376 if (save_info) { |
377 const Directory::PersistedKernelInfo& info = snapshot.kernel_info; | 377 const Directory::PersistedKernelInfo& info = snapshot.kernel_info; |
378 SQLStatement update; | 378 SQLStatement update; |
379 update.prepare(dbhandle, "UPDATE share_info " | 379 update.prepare(dbhandle, "UPDATE share_info " |
380 "SET store_birthday = ?, " | 380 "SET store_birthday = ?, " |
381 "next_id = ?, " | 381 "next_id = ?, " |
382 "notification_state = ?"); | 382 "notification_state = ?, " |
| 383 "autofill_migration_state = ?, " |
| 384 "bookmarks_added_during_autofill_migration = ?, " |
| 385 "autofill_migration_time = ?, " |
| 386 "autofill_entries_added_during_migration = ?, " |
| 387 "autofill_profiles_added_during_migration = ? "); |
383 update.bind_string(0, info.store_birthday); | 388 update.bind_string(0, info.store_birthday); |
384 update.bind_int64(1, info.next_id); | 389 update.bind_int64(1, info.next_id); |
385 update.bind_blob(2, info.notification_state.data(), | 390 update.bind_blob(2, info.notification_state.data(), |
386 info.notification_state.size()); | 391 info.notification_state.size()); |
| 392 update.bind_int(3, info.autofill_migration_state); |
| 393 update.bind_int(4, |
| 394 info.autofill_migration_debug_info.bookmarks_added_during_migration); |
| 395 update.bind_int64(5, |
| 396 info.autofill_migration_debug_info.autofill_migration_time); |
| 397 update.bind_int(6, |
| 398 info.autofill_migration_debug_info.autofill_entries_added_during_migrati
on); |
| 399 update.bind_int(7, |
| 400 info.autofill_migration_debug_info.autofill_profile_added_during_migrati
on); |
387 | 401 |
388 if (!(SQLITE_DONE == update.step() && | 402 if (!(SQLITE_DONE == update.step() && |
389 SQLITE_OK == update.reset() && | 403 SQLITE_OK == update.reset() && |
390 1 == update.changes())) { | 404 1 == update.changes())) { |
391 return false; | 405 return false; |
392 } | 406 } |
393 | 407 |
394 for (int i = FIRST_REAL_MODEL_TYPE; i < MODEL_TYPE_COUNT; ++i) { | 408 for (int i = FIRST_REAL_MODEL_TYPE; i < MODEL_TYPE_COUNT; ++i) { |
395 SQLStatement op; | 409 SQLStatement op; |
396 op.prepare(dbhandle, "INSERT OR REPLACE INTO models (model_id, " | 410 op.prepare(dbhandle, "INSERT OR REPLACE INTO models (model_id, " |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
449 if (MigrateVersion71To72()) | 463 if (MigrateVersion71To72()) |
450 version_on_disk = 72; | 464 version_on_disk = 72; |
451 } | 465 } |
452 | 466 |
453 // Version 73 added a field for notification state. | 467 // Version 73 added a field for notification state. |
454 if (version_on_disk == 72) { | 468 if (version_on_disk == 72) { |
455 if (MigrateVersion72To73()) | 469 if (MigrateVersion72To73()) |
456 version_on_disk = 73; | 470 version_on_disk = 73; |
457 } | 471 } |
458 | 472 |
| 473 if (version_on_disk == 73) { |
| 474 if (MigrateVersion73To74()) |
| 475 version_on_disk = 74; |
| 476 } |
| 477 |
459 // If one of the migrations requested it, drop columns that aren't current. | 478 // If one of the migrations requested it, drop columns that aren't current. |
460 // It's only safe to do this after migrating all the way to the current | 479 // It's only safe to do this after migrating all the way to the current |
461 // version. | 480 // version. |
462 if (version_on_disk == kCurrentDBVersion && needs_column_refresh_) { | 481 if (version_on_disk == kCurrentDBVersion && needs_column_refresh_) { |
463 if (!RefreshColumns()) | 482 if (!RefreshColumns()) |
464 version_on_disk = 0; | 483 version_on_disk = 0; |
465 } | 484 } |
466 | 485 |
467 // A final, alternative catch-all migration to simply re-sync everything. | 486 // A final, alternative catch-all migration to simply re-sync everything. |
468 if (version_on_disk != kCurrentDBVersion) { | 487 if (version_on_disk != kCurrentDBVersion) { |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
549 entry_bucket->insert(kernel); | 568 entry_bucket->insert(kernel); |
550 } | 569 } |
551 return SQLITE_DONE == query_result; | 570 return SQLITE_DONE == query_result; |
552 } | 571 } |
553 | 572 |
554 bool DirectoryBackingStore::LoadInfo(Directory::KernelLoadInfo* info) { | 573 bool DirectoryBackingStore::LoadInfo(Directory::KernelLoadInfo* info) { |
555 { | 574 { |
556 SQLStatement query; | 575 SQLStatement query; |
557 query.prepare(load_dbhandle_, | 576 query.prepare(load_dbhandle_, |
558 "SELECT store_birthday, next_id, cache_guid, " | 577 "SELECT store_birthday, next_id, cache_guid, " |
559 "notification_state FROM share_info"); | 578 "notification_state, autofill_migration_state, " |
| 579 "bookmarks_added_during_autofill_migration, " |
| 580 "autofill_migration_time, " |
| 581 "autofill_entries_added_during_migration, " |
| 582 "autofill_profiles_added_during_migration " |
| 583 "FROM share_info"); |
560 if (SQLITE_ROW != query.step()) | 584 if (SQLITE_ROW != query.step()) |
561 return false; | 585 return false; |
562 info->kernel_info.store_birthday = query.column_string(0); | 586 info->kernel_info.store_birthday = query.column_string(0); |
563 info->kernel_info.next_id = query.column_int64(1); | 587 info->kernel_info.next_id = query.column_int64(1); |
564 info->cache_guid = query.column_string(2); | 588 info->cache_guid = query.column_string(2); |
565 query.column_blob_as_string(3, &info->kernel_info.notification_state); | 589 query.column_blob_as_string(3, &info->kernel_info.notification_state); |
| 590 info->kernel_info.autofill_migration_state = |
| 591 static_cast<Directory::PersistedKernelInfo::AutofillMigrationState> |
| 592 (query.column_int(4)); |
| 593 syncable::AutofillMigrationDebugInfo& debug_info = |
| 594 info->kernel_info.autofill_migration_debug_info; |
| 595 debug_info.bookmarks_added_during_migration = |
| 596 query.column_int(5); |
| 597 debug_info.autofill_migration_time = |
| 598 query.column_int64(6); |
| 599 debug_info.autofill_entries_added_during_migration = |
| 600 query.column_int(7); |
| 601 debug_info.autofill_profile_added_during_migration = |
| 602 query.column_int(8); |
566 } | 603 } |
567 { | 604 { |
568 SQLStatement query; | 605 SQLStatement query; |
569 query.prepare(load_dbhandle_, | 606 query.prepare(load_dbhandle_, |
570 "SELECT model_id, last_download_timestamp, initial_sync_ended " | 607 "SELECT model_id, last_download_timestamp, initial_sync_ended " |
571 "FROM models"); | 608 "FROM models"); |
572 while (SQLITE_ROW == query.step()) { | 609 while (SQLITE_ROW == query.step()) { |
573 string model_id; | 610 string model_id; |
574 query.column_blob_as_string(0, &model_id); | 611 query.column_blob_as_string(0, &model_id); |
575 ModelType type = ModelIdToModelTypeEnum(model_id); | 612 ModelType type = ModelIdToModelTypeEnum(model_id); |
(...skipping 343 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
919 bool DirectoryBackingStore::MigrateVersion72To73() { | 956 bool DirectoryBackingStore::MigrateVersion72To73() { |
920 int result = | 957 int result = |
921 ExecQuery(load_dbhandle_, | 958 ExecQuery(load_dbhandle_, |
922 "ALTER TABLE share_info ADD COLUMN notification_state BLOB"); | 959 "ALTER TABLE share_info ADD COLUMN notification_state BLOB"); |
923 if (result != SQLITE_DONE) | 960 if (result != SQLITE_DONE) |
924 return false; | 961 return false; |
925 SetVersion(73); | 962 SetVersion(73); |
926 return true; | 963 return true; |
927 } | 964 } |
928 | 965 |
| 966 bool DirectoryBackingStore::MigrateVersion73To74() { |
| 967 int result = |
| 968 ExecQuery(load_dbhandle_, |
| 969 "ALTER TABLE share_info ADD COLUMN autofill_migration_state " |
| 970 "INT default 0"); |
| 971 if (result != SQLITE_DONE) |
| 972 return false; |
| 973 |
| 974 result = |
| 975 ExecQuery(load_dbhandle_, |
| 976 "ALTER TABLE share_info ADD COLUMN " |
| 977 "bookmarks_added_during_autofill_migration " |
| 978 "INT default 0"); |
| 979 |
| 980 if (result != SQLITE_DONE) |
| 981 return false; |
| 982 |
| 983 result = |
| 984 ExecQuery(load_dbhandle_, |
| 985 "ALTER TABLE share_info ADD COLUMN autofill_migration_time " |
| 986 "INT default 0"); |
| 987 |
| 988 if (result != SQLITE_DONE) |
| 989 return false; |
| 990 |
| 991 result = |
| 992 ExecQuery(load_dbhandle_, |
| 993 "ALTER TABLE share_info ADD COLUMN " |
| 994 "autofill_entries_added_during_migration " |
| 995 "INT default 0"); |
| 996 |
| 997 if (result != SQLITE_DONE) |
| 998 return false; |
| 999 |
| 1000 result = |
| 1001 ExecQuery(load_dbhandle_, |
| 1002 "ALTER TABLE share_info ADD COLUMN " |
| 1003 "autofill_profiles_added_during_migration " |
| 1004 "INT default 0"); |
| 1005 |
| 1006 if (result != SQLITE_DONE) |
| 1007 return false; |
| 1008 |
| 1009 SetVersion(74); |
| 1010 return true; |
| 1011 } |
| 1012 |
| 1013 |
929 int DirectoryBackingStore::CreateTables() { | 1014 int DirectoryBackingStore::CreateTables() { |
930 VLOG(1) << "First run, creating tables"; | 1015 VLOG(1) << "First run, creating tables"; |
931 // Create two little tables share_version and share_info | 1016 // Create two little tables share_version and share_info |
932 int result = ExecQuery(load_dbhandle_, | 1017 int result = ExecQuery(load_dbhandle_, |
933 "CREATE TABLE share_version (" | 1018 "CREATE TABLE share_version (" |
934 "id VARCHAR(128) primary key, data INT)"); | 1019 "id VARCHAR(128) primary key, data INT)"); |
935 if (result != SQLITE_DONE) | 1020 if (result != SQLITE_DONE) |
936 return result; | 1021 return result; |
937 { | 1022 { |
938 SQLStatement statement; | 1023 SQLStatement statement; |
(...skipping 14 matching lines...) Expand all Loading... |
953 { | 1038 { |
954 SQLStatement statement; | 1039 SQLStatement statement; |
955 statement.prepare(load_dbhandle_, "INSERT INTO share_info VALUES" | 1040 statement.prepare(load_dbhandle_, "INSERT INTO share_info VALUES" |
956 "(?, " // id | 1041 "(?, " // id |
957 "?, " // name | 1042 "?, " // name |
958 "?, " // store_birthday | 1043 "?, " // store_birthday |
959 "?, " // db_create_version | 1044 "?, " // db_create_version |
960 "?, " // db_create_time | 1045 "?, " // db_create_time |
961 "-2, " // next_id | 1046 "-2, " // next_id |
962 "?, " // cache_guid | 1047 "?, " // cache_guid |
963 "?);"); // notification_state | 1048 "?, " // autofill_migration_state |
| 1049 "?, " // bookmarks_added |
| 1050 // _during_autofill_migration |
| 1051 "?, " // autofill_migration_time |
| 1052 "?, " // autofill_entries |
| 1053 // _added_during_migration |
| 1054 "?, " // autofill_profiles_added |
| 1055 // _during_migration |
| 1056 "?);"); // notification_state |
964 statement.bind_string(0, dir_name_); // id | 1057 statement.bind_string(0, dir_name_); // id |
965 statement.bind_string(1, dir_name_); // name | 1058 statement.bind_string(1, dir_name_); // name |
966 statement.bind_string(2, ""); // store_birthday | 1059 statement.bind_string(2, ""); // store_birthday |
967 statement.bind_string(3, SYNC_ENGINE_VERSION_STRING); // db_create_version | 1060 statement.bind_string(3, SYNC_ENGINE_VERSION_STRING); // db_create_version |
968 statement.bind_int(4, static_cast<int32>(time(0))); // db_create_time | 1061 statement.bind_int(4, static_cast<int32>(time(0))); // db_create_time |
969 statement.bind_string(5, GenerateCacheGUID()); // cache_guid | 1062 statement.bind_string(5, GenerateCacheGUID()); // cache_guid |
970 statement.bind_blob(6, NULL, 0); // notification_state | 1063 statement.bind_int(6, 0); // autofill_migration_state |
| 1064 statement.bind_int(7, 0); // autofill_migration_time |
| 1065 statement.bind_int(8, 0); // bookmarks_added_during_autofill_migration |
| 1066 statement.bind_int(9, 0); // autofill_entries_added_during_migration |
| 1067 statement.bind_int(10, 0); // autofill_profiles_added_during_migration |
| 1068 statement.bind_blob(11, NULL, 0); // notification_state |
971 result = statement.step(); | 1069 result = statement.step(); |
972 } | 1070 } |
973 if (result != SQLITE_DONE) | 1071 if (result != SQLITE_DONE) |
974 return result; | 1072 return result; |
975 | 1073 |
976 result = CreateModelsTable(); | 1074 result = CreateModelsTable(); |
977 if (result != SQLITE_DONE) | 1075 if (result != SQLITE_DONE) |
978 return result; | 1076 return result; |
979 // Create the big metas table. | 1077 // Create the big metas table. |
980 result = CreateMetasTable(false); | 1078 result = CreateMetasTable(false); |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1033 // This is the current schema for the ShareInfo table, from version 71 | 1131 // This is the current schema for the ShareInfo table, from version 71 |
1034 // onward. If you change the schema, you'll probably want to double-check | 1132 // onward. If you change the schema, you'll probably want to double-check |
1035 // the use of this function in the v70-v71 migration. | 1133 // the use of this function in the v70-v71 migration. |
1036 query.append(" (" | 1134 query.append(" (" |
1037 "id TEXT primary key, " | 1135 "id TEXT primary key, " |
1038 "name TEXT, " | 1136 "name TEXT, " |
1039 "store_birthday TEXT, " | 1137 "store_birthday TEXT, " |
1040 "db_create_version TEXT, " | 1138 "db_create_version TEXT, " |
1041 "db_create_time INT, " | 1139 "db_create_time INT, " |
1042 "next_id INT default -2, " | 1140 "next_id INT default -2, " |
1043 "cache_guid TEXT"); | 1141 "cache_guid TEXT, " |
| 1142 "autofill_migration_state INT default 0, " |
| 1143 "bookmarks_added_during_autofill_migration INT default 0, " |
| 1144 "autofill_migration_time INT default 0, " |
| 1145 "autofill_entries_added_during_migration INT default 0, " |
| 1146 "autofill_profiles_added_during_migration INT default 0 " |
| 1147 ); |
1044 if (with_notification_state) { | 1148 if (with_notification_state) { |
1045 query.append(", notification_state BLOB"); | 1149 query.append(", notification_state BLOB"); |
1046 } | 1150 } |
1047 query.append(")"); | 1151 query.append(")"); |
1048 return ExecQuery(load_dbhandle_, query.c_str()); | 1152 return ExecQuery(load_dbhandle_, query.c_str()); |
1049 } | 1153 } |
1050 | 1154 |
1051 } // namespace syncable | 1155 } // namespace syncable |
OLD | NEW |