| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "sync/syncable/directory_backing_store.h" | 5 #include "sync/syncable/directory_backing_store.h" |
| 6 | 6 |
| 7 #include "build/build_config.h" | 7 #include "build/build_config.h" |
| 8 | 8 |
| 9 #include <limits> | 9 #include <limits> |
| 10 | 10 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 33 | 33 |
| 34 namespace syncer { | 34 namespace syncer { |
| 35 namespace syncable { | 35 namespace syncable { |
| 36 | 36 |
| 37 // This just has to be big enough to hold an UPDATE or INSERT statement that | 37 // This just has to be big enough to hold an UPDATE or INSERT statement that |
| 38 // modifies all the columns in the entry table. | 38 // modifies all the columns in the entry table. |
| 39 static const string::size_type kUpdateStatementBufferSize = 2048; | 39 static const string::size_type kUpdateStatementBufferSize = 2048; |
| 40 | 40 |
| 41 // Increment this version whenever updating DB tables. | 41 // Increment this version whenever updating DB tables. |
| 42 extern const int32 kCurrentDBVersion; // Global visibility for our unittest. | 42 extern const int32 kCurrentDBVersion; // Global visibility for our unittest. |
| 43 const int32 kCurrentDBVersion = 82; | 43 const int32 kCurrentDBVersion = 83; |
| 44 | 44 |
| 45 // Iterate over the fields of |entry| and bind each to |statement| for | 45 // Iterate over the fields of |entry| and bind each to |statement| for |
| 46 // updating. Returns the number of args bound. | 46 // updating. Returns the number of args bound. |
| 47 void BindFields(const EntryKernel& entry, | 47 void BindFields(const EntryKernel& entry, |
| 48 sql::Statement* statement) { | 48 sql::Statement* statement) { |
| 49 int index = 0; | 49 int index = 0; |
| 50 int i = 0; | 50 int i = 0; |
| 51 for (i = BEGIN_FIELDS; i < INT64_FIELDS_END; ++i) { | 51 for (i = BEGIN_FIELDS; i < INT64_FIELDS_END; ++i) { |
| 52 statement->BindInt64(index++, entry.ref(static_cast<Int64Field>(i))); | 52 statement->BindInt64(index++, entry.ref(static_cast<Int64Field>(i))); |
| 53 } | 53 } |
| (...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 351 if (MigrateVersion80To81()) | 351 if (MigrateVersion80To81()) |
| 352 version_on_disk = 81; | 352 version_on_disk = 81; |
| 353 } | 353 } |
| 354 | 354 |
| 355 // Version 82 migration added transaction_version column per data type. | 355 // Version 82 migration added transaction_version column per data type. |
| 356 if (version_on_disk == 81) { | 356 if (version_on_disk == 81) { |
| 357 if (MigrateVersion81To82()) | 357 if (MigrateVersion81To82()) |
| 358 version_on_disk = 82; | 358 version_on_disk = 82; |
| 359 } | 359 } |
| 360 | 360 |
| 361 // Version 83 migration added transaction_version column per sync entry. |
| 362 if (version_on_disk == 82) { |
| 363 if (MigrateVersion82To83()) |
| 364 version_on_disk = 83; |
| 365 } |
| 366 |
| 361 // If one of the migrations requested it, drop columns that aren't current. | 367 // If one of the migrations requested it, drop columns that aren't current. |
| 362 // It's only safe to do this after migrating all the way to the current | 368 // It's only safe to do this after migrating all the way to the current |
| 363 // version. | 369 // version. |
| 364 if (version_on_disk == kCurrentDBVersion && needs_column_refresh_) { | 370 if (version_on_disk == kCurrentDBVersion && needs_column_refresh_) { |
| 365 if (!RefreshColumns()) | 371 if (!RefreshColumns()) |
| 366 version_on_disk = 0; | 372 version_on_disk = 0; |
| 367 } | 373 } |
| 368 | 374 |
| 369 // A final, alternative catch-all migration to simply re-sync everything. | 375 // A final, alternative catch-all migration to simply re-sync everything. |
| 370 // | 376 // |
| (...skipping 694 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1065 "ALTER TABLE models ADD COLUMN transaction_version BIGINT default 0")) | 1071 "ALTER TABLE models ADD COLUMN transaction_version BIGINT default 0")) |
| 1066 return false; | 1072 return false; |
| 1067 sql::Statement update(db_->GetUniqueStatement( | 1073 sql::Statement update(db_->GetUniqueStatement( |
| 1068 "UPDATE models SET transaction_version = 0")); | 1074 "UPDATE models SET transaction_version = 0")); |
| 1069 if (!update.Run()) | 1075 if (!update.Run()) |
| 1070 return false; | 1076 return false; |
| 1071 SetVersion(82); | 1077 SetVersion(82); |
| 1072 return true; | 1078 return true; |
| 1073 } | 1079 } |
| 1074 | 1080 |
| 1081 bool DirectoryBackingStore::MigrateVersion82To83() { |
| 1082 // Version 83 added transaction_version on sync node. |
| 1083 if (!db_->Execute( |
| 1084 "ALTER TABLE metas ADD COLUMN transaction_version BIGINT default 0")) |
| 1085 return false; |
| 1086 sql::Statement update(db_->GetUniqueStatement( |
| 1087 "UPDATE metas SET transaction_version = 0")); |
| 1088 if (!update.Run()) |
| 1089 return false; |
| 1090 SetVersion(83); |
| 1091 return true; |
| 1092 } |
| 1093 |
| 1075 bool DirectoryBackingStore::CreateTables() { | 1094 bool DirectoryBackingStore::CreateTables() { |
| 1076 DVLOG(1) << "First run, creating tables"; | 1095 DVLOG(1) << "First run, creating tables"; |
| 1077 // Create two little tables share_version and share_info | 1096 // Create two little tables share_version and share_info |
| 1078 if (!db_->Execute( | 1097 if (!db_->Execute( |
| 1079 "CREATE TABLE share_version (" | 1098 "CREATE TABLE share_version (" |
| 1080 "id VARCHAR(128) primary key, data INT)")) { | 1099 "id VARCHAR(128) primary key, data INT)")) { |
| 1081 return false; | 1100 return false; |
| 1082 } | 1101 } |
| 1083 | 1102 |
| 1084 { | 1103 { |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1247 bool prev_exists = (ids_set.find(entry->ref(PREV_ID).value()) != end); | 1266 bool prev_exists = (ids_set.find(entry->ref(PREV_ID).value()) != end); |
| 1248 bool parent_exists = (ids_set.find(entry->ref(PARENT_ID).value()) != end); | 1267 bool parent_exists = (ids_set.find(entry->ref(PARENT_ID).value()) != end); |
| 1249 bool next_exists = (ids_set.find(entry->ref(NEXT_ID).value()) != end); | 1268 bool next_exists = (ids_set.find(entry->ref(NEXT_ID).value()) != end); |
| 1250 is_ok = is_ok && prev_exists && parent_exists && next_exists; | 1269 is_ok = is_ok && prev_exists && parent_exists && next_exists; |
| 1251 } | 1270 } |
| 1252 return is_ok; | 1271 return is_ok; |
| 1253 } | 1272 } |
| 1254 | 1273 |
| 1255 } // namespace syncable | 1274 } // namespace syncable |
| 1256 } // namespace syncer | 1275 } // namespace syncer |
| OLD | NEW |