Home | History | Annotate | Download | only in syncable
      1 // Copyright 2012 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.
      4 
      5 #include "sync/syncable/directory_backing_store.h"
      6 
      7 #include "build/build_config.h"
      8 
      9 #include <limits>
     10 
     11 #include "base/base64.h"
     12 #include "base/debug/trace_event.h"
     13 #include "base/logging.h"
     14 #include "base/rand_util.h"
     15 #include "base/strings/stringprintf.h"
     16 #include "base/time/time.h"
     17 #include "sql/connection.h"
     18 #include "sql/statement.h"
     19 #include "sql/transaction.h"
     20 #include "sync/internal_api/public/base/node_ordinal.h"
     21 #include "sync/protocol/bookmark_specifics.pb.h"
     22 #include "sync/protocol/sync.pb.h"
     23 #include "sync/syncable/syncable-inl.h"
     24 #include "sync/syncable/syncable_columns.h"
     25 #include "sync/syncable/syncable_util.h"
     26 #include "sync/util/time.h"
     27 
     28 using std::string;
     29 
     30 namespace syncer {
     31 namespace syncable {
     32 
     33 // This just has to be big enough to hold an UPDATE or INSERT statement that
     34 // modifies all the columns in the entry table.
     35 static const string::size_type kUpdateStatementBufferSize = 2048;
     36 
     37 // Increment this version whenever updating DB tables.
     38 const int32 kCurrentDBVersion = 86;
     39 
     40 // Iterate over the fields of |entry| and bind each to |statement| for
     41 // updating.  Returns the number of args bound.
     42 void BindFields(const EntryKernel& entry,
     43                 sql::Statement* statement) {
     44   int index = 0;
     45   int i = 0;
     46   for (i = BEGIN_FIELDS; i < INT64_FIELDS_END; ++i) {
     47     statement->BindInt64(index++, entry.ref(static_cast<Int64Field>(i)));
     48   }
     49   for ( ; i < TIME_FIELDS_END; ++i) {
     50     statement->BindInt64(index++,
     51                          TimeToProtoTime(
     52                              entry.ref(static_cast<TimeField>(i))));
     53   }
     54   for ( ; i < ID_FIELDS_END; ++i) {
     55     statement->BindString(index++, entry.ref(static_cast<IdField>(i)).s_);
     56   }
     57   for ( ; i < BIT_FIELDS_END; ++i) {
     58     statement->BindInt(index++, entry.ref(static_cast<BitField>(i)));
     59   }
     60   for ( ; i < STRING_FIELDS_END; ++i) {
     61     statement->BindString(index++, entry.ref(static_cast<StringField>(i)));
     62   }
     63   for ( ; i < PROTO_FIELDS_END; ++i) {
     64     std::string temp;
     65     entry.ref(static_cast<ProtoField>(i)).SerializeToString(&temp);
     66     statement->BindBlob(index++, temp.data(), temp.length());
     67   }
     68   for ( ; i < UNIQUE_POSITION_FIELDS_END; ++i) {
     69     std::string temp;
     70     entry.ref(static_cast<UniquePositionField>(i)).SerializeToString(&temp);
     71     statement->BindBlob(index++, temp.data(), temp.length());
     72   }
     73 }
     74 
     75 // The caller owns the returned EntryKernel*.  Assumes the statement currently
     76 // points to a valid row in the metas table. Returns NULL to indicate that
     77 // it detected a corruption in the data on unpacking.
     78 scoped_ptr<EntryKernel> UnpackEntry(sql::Statement* statement) {
     79   scoped_ptr<EntryKernel> kernel(new EntryKernel());
     80   DCHECK_EQ(statement->ColumnCount(), static_cast<int>(FIELD_COUNT));
     81   int i = 0;
     82   for (i = BEGIN_FIELDS; i < INT64_FIELDS_END; ++i) {
     83     kernel->put(static_cast<Int64Field>(i), statement->ColumnInt64(i));
     84   }
     85   for ( ; i < TIME_FIELDS_END; ++i) {
     86     kernel->put(static_cast<TimeField>(i),
     87                 ProtoTimeToTime(statement->ColumnInt64(i)));
     88   }
     89   for ( ; i < ID_FIELDS_END; ++i) {
     90     kernel->mutable_ref(static_cast<IdField>(i)).s_ =
     91         statement->ColumnString(i);
     92   }
     93   for ( ; i < BIT_FIELDS_END; ++i) {
     94     kernel->put(static_cast<BitField>(i), (0 != statement->ColumnInt(i)));
     95   }
     96   for ( ; i < STRING_FIELDS_END; ++i) {
     97     kernel->put(static_cast<StringField>(i),
     98                 statement->ColumnString(i));
     99   }
    100   for ( ; i < PROTO_FIELDS_END; ++i) {
    101     kernel->mutable_ref(static_cast<ProtoField>(i)).ParseFromArray(
    102         statement->ColumnBlob(i), statement->ColumnByteLength(i));
    103   }
    104   for ( ; i < UNIQUE_POSITION_FIELDS_END; ++i) {
    105     std::string temp;
    106     statement->ColumnBlobAsString(i, &temp);
    107 
    108     sync_pb::UniquePosition proto;
    109     if (!proto.ParseFromString(temp)) {
    110       DVLOG(1) << "Unpacked invalid position.  Assuming the DB is corrupt";
    111       return scoped_ptr<EntryKernel>();
    112     }
    113 
    114     kernel->mutable_ref(static_cast<UniquePositionField>(i)) =
    115         UniquePosition::FromProto(proto);
    116   }
    117   return kernel.Pass();
    118 }
    119 
    120 namespace {
    121 
    122 string ComposeCreateTableColumnSpecs() {
    123   const ColumnSpec* begin = g_metas_columns;
    124   const ColumnSpec* end = g_metas_columns + arraysize(g_metas_columns);
    125   string query;
    126   query.reserve(kUpdateStatementBufferSize);
    127   char separator = '(';
    128   for (const ColumnSpec* column = begin; column != end; ++column) {
    129     query.push_back(separator);
    130     separator = ',';
    131     query.append(column->name);
    132     query.push_back(' ');
    133     query.append(column->spec);
    134   }
    135   query.push_back(')');
    136   return query;
    137 }
    138 
    139 void AppendColumnList(std::string* output) {
    140   const char* joiner = " ";
    141   // Be explicit in SELECT order to match up with UnpackEntry.
    142   for (int i = BEGIN_FIELDS; i < FIELD_COUNT; ++i) {
    143     output->append(joiner);
    144     output->append(ColumnName(i));
    145     joiner = ", ";
    146   }
    147 }
    148 
    149 }  // namespace
    150 
    151 ///////////////////////////////////////////////////////////////////////////////
    152 // DirectoryBackingStore implementation.
    153 
    154 DirectoryBackingStore::DirectoryBackingStore(const string& dir_name)
    155   : db_(new sql::Connection()),
    156     dir_name_(dir_name),
    157     needs_column_refresh_(false) {
    158   db_->set_histogram_tag("SyncDirectory");
    159 }
    160 
    161 DirectoryBackingStore::DirectoryBackingStore(const string& dir_name,
    162                                              sql::Connection* db)
    163   : db_(db),
    164     dir_name_(dir_name),
    165     needs_column_refresh_(false) {
    166 }
    167 
    168 DirectoryBackingStore::~DirectoryBackingStore() {
    169 }
    170 
    171 bool DirectoryBackingStore::DeleteEntries(EntryTable from,
    172                                           const MetahandleSet& handles) {
    173   if (handles.empty())
    174     return true;
    175 
    176   sql::Statement statement;
    177   // Call GetCachedStatement() separately to get different statements for
    178   // different tables.
    179   switch (from) {
    180     case METAS_TABLE:
    181       statement.Assign(db_->GetCachedStatement(
    182           SQL_FROM_HERE, "DELETE FROM metas WHERE metahandle = ?"));
    183       break;
    184     case DELETE_JOURNAL_TABLE:
    185       statement.Assign(db_->GetCachedStatement(
    186           SQL_FROM_HERE, "DELETE FROM deleted_metas WHERE metahandle = ?"));
    187       break;
    188   }
    189 
    190   for (MetahandleSet::const_iterator i = handles.begin(); i != handles.end();
    191        ++i) {
    192     statement.BindInt64(0, *i);
    193     if (!statement.Run())
    194       return false;
    195     statement.Reset(true);
    196   }
    197   return true;
    198 }
    199 
    200 bool DirectoryBackingStore::SaveChanges(
    201     const Directory::SaveChangesSnapshot& snapshot) {
    202   DCHECK(CalledOnValidThread());
    203   DCHECK(db_->is_open());
    204 
    205   // Back out early if there is nothing to write.
    206   bool save_info =
    207     (Directory::KERNEL_SHARE_INFO_DIRTY == snapshot.kernel_info_status);
    208   if (snapshot.dirty_metas.empty() && snapshot.metahandles_to_purge.empty() &&
    209       snapshot.delete_journals.empty() &&
    210       snapshot.delete_journals_to_purge.empty() && !save_info) {
    211     return true;
    212   }
    213 
    214   sql::Transaction transaction(db_.get());
    215   if (!transaction.Begin())
    216     return false;
    217 
    218   PrepareSaveEntryStatement(METAS_TABLE, &save_meta_statment_);
    219   for (EntryKernelSet::const_iterator i = snapshot.dirty_metas.begin();
    220        i != snapshot.dirty_metas.end(); ++i) {
    221     DCHECK((*i)->is_dirty());
    222     if (!SaveEntryToDB(&save_meta_statment_, **i))
    223       return false;
    224   }
    225 
    226   if (!DeleteEntries(METAS_TABLE, snapshot.metahandles_to_purge))
    227     return false;
    228 
    229   PrepareSaveEntryStatement(DELETE_JOURNAL_TABLE,
    230                             &save_delete_journal_statment_);
    231   for (EntryKernelSet::const_iterator i = snapshot.delete_journals.begin();
    232        i != snapshot.delete_journals.end(); ++i) {
    233     if (!SaveEntryToDB(&save_delete_journal_statment_, **i))
    234       return false;
    235   }
    236 
    237   if (!DeleteEntries(DELETE_JOURNAL_TABLE, snapshot.delete_journals_to_purge))
    238     return false;
    239 
    240   if (save_info) {
    241     const Directory::PersistedKernelInfo& info = snapshot.kernel_info;
    242     sql::Statement s1(db_->GetCachedStatement(
    243             SQL_FROM_HERE,
    244             "UPDATE share_info "
    245             "SET store_birthday = ?, "
    246             "next_id = ?, "
    247             "bag_of_chips = ?"));
    248     s1.BindString(0, info.store_birthday);
    249     s1.BindInt64(1, info.next_id);
    250     s1.BindBlob(2, info.bag_of_chips.data(), info.bag_of_chips.size());
    251 
    252     if (!s1.Run())
    253       return false;
    254     DCHECK_EQ(db_->GetLastChangeCount(), 1);
    255 
    256     sql::Statement s2(db_->GetCachedStatement(
    257             SQL_FROM_HERE,
    258             "INSERT OR REPLACE "
    259             "INTO models (model_id, progress_marker, transaction_version) "
    260             "VALUES (?, ?, ?)"));
    261 
    262     ModelTypeSet protocol_types = ProtocolTypes();
    263     for (ModelTypeSet::Iterator iter = protocol_types.First(); iter.Good();
    264          iter.Inc()) {
    265       ModelType type = iter.Get();
    266       // We persist not ModelType but rather a protobuf-derived ID.
    267       string model_id = ModelTypeEnumToModelId(type);
    268       string progress_marker;
    269       info.download_progress[type].SerializeToString(&progress_marker);
    270       s2.BindBlob(0, model_id.data(), model_id.length());
    271       s2.BindBlob(1, progress_marker.data(), progress_marker.length());
    272       s2.BindInt64(2, info.transaction_version[type]);
    273       if (!s2.Run())
    274         return false;
    275       DCHECK_EQ(db_->GetLastChangeCount(), 1);
    276       s2.Reset(true);
    277     }
    278   }
    279 
    280   return transaction.Commit();
    281 }
    282 
    283 bool DirectoryBackingStore::InitializeTables() {
    284   sql::Transaction transaction(db_.get());
    285   if (!transaction.Begin())
    286     return false;
    287 
    288   int version_on_disk = GetVersion();
    289 
    290   // Upgrade from version 67. Version 67 was widely distributed as the original
    291   // Bookmark Sync release. Version 68 removed unique naming.
    292   if (version_on_disk == 67) {
    293     if (MigrateVersion67To68())
    294       version_on_disk = 68;
    295   }
    296   // Version 69 introduced additional datatypes.
    297   if (version_on_disk == 68) {
    298     if (MigrateVersion68To69())
    299       version_on_disk = 69;
    300   }
    301 
    302   if (version_on_disk == 69) {
    303     if (MigrateVersion69To70())
    304       version_on_disk = 70;
    305   }
    306 
    307   // Version 71 changed the sync progress information to be per-datatype.
    308   if (version_on_disk == 70) {
    309     if (MigrateVersion70To71())
    310       version_on_disk = 71;
    311   }
    312 
    313   // Version 72 removed extended attributes, a legacy way to do extensible
    314   // key/value information, stored in their own table.
    315   if (version_on_disk == 71) {
    316     if (MigrateVersion71To72())
    317       version_on_disk = 72;
    318   }
    319 
    320   // Version 73 added a field for notification state.
    321   if (version_on_disk == 72) {
    322     if (MigrateVersion72To73())
    323       version_on_disk = 73;
    324   }
    325 
    326   // Version 74 added state for the autofill migration.
    327   if (version_on_disk == 73) {
    328     if (MigrateVersion73To74())
    329       version_on_disk = 74;
    330   }
    331 
    332   // Version 75 migrated from int64-based timestamps to per-datatype tokens.
    333   if (version_on_disk == 74) {
    334     if (MigrateVersion74To75())
    335       version_on_disk = 75;
    336   }
    337 
    338   // Version 76 removed all (5) autofill migration related columns.
    339   if (version_on_disk == 75) {
    340     if (MigrateVersion75To76())
    341       version_on_disk = 76;
    342   }
    343 
    344   // Version 77 standardized all time fields to ms since the Unix
    345   // epoch.
    346   if (version_on_disk == 76) {
    347     if (MigrateVersion76To77())
    348       version_on_disk = 77;
    349   }
    350 
    351   // Version 78 added the column base_server_specifics to the metas table.
    352   if (version_on_disk == 77) {
    353     if (MigrateVersion77To78())
    354       version_on_disk = 78;
    355   }
    356 
    357   // Version 79 migration is a one-time fix for some users in a bad state.
    358   if (version_on_disk == 78) {
    359     if (MigrateVersion78To79())
    360       version_on_disk = 79;
    361   }
    362 
    363   // Version 80 migration is adding the bag_of_chips column.
    364   if (version_on_disk == 79) {
    365     if (MigrateVersion79To80())
    366       version_on_disk = 80;
    367   }
    368 
    369   // Version 81 replaces the int64 server_position_in_parent_field
    370   // with a blob server_ordinal_in_parent field.
    371   if (version_on_disk == 80) {
    372     if (MigrateVersion80To81())
    373       version_on_disk = 81;
    374   }
    375 
    376   // Version 82 migration added transaction_version column per data type.
    377   if (version_on_disk == 81) {
    378     if (MigrateVersion81To82())
    379       version_on_disk = 82;
    380   }
    381 
    382   // Version 83 migration added transaction_version column per sync entry.
    383   if (version_on_disk == 82) {
    384     if (MigrateVersion82To83())
    385       version_on_disk = 83;
    386   }
    387 
    388   // Version 84 migration added deleted_metas table.
    389   if (version_on_disk == 83) {
    390     if (MigrateVersion83To84())
    391       version_on_disk = 84;
    392   }
    393 
    394   // Version 85 migration removes the initial_sync_ended bits.
    395   if (version_on_disk == 84) {
    396     if (MigrateVersion84To85())
    397       version_on_disk = 85;
    398   }
    399 
    400   // Version 86 migration converts bookmarks to the unique positioning system.
    401   // It also introduces a new field to store a unique ID for each bookmark.
    402   if (version_on_disk == 85) {
    403     if (MigrateVersion85To86())
    404       version_on_disk = 86;
    405   }
    406 
    407   // If one of the migrations requested it, drop columns that aren't current.
    408   // It's only safe to do this after migrating all the way to the current
    409   // version.
    410   if (version_on_disk == kCurrentDBVersion && needs_column_refresh_) {
    411     if (!RefreshColumns())
    412       version_on_disk = 0;
    413   }
    414 
    415   // A final, alternative catch-all migration to simply re-sync everything.
    416   if (version_on_disk != kCurrentDBVersion) {
    417     if (version_on_disk > kCurrentDBVersion)
    418       return false;
    419 
    420     // Fallback (re-sync everything) migration path.
    421     DVLOG(1) << "Old/null sync database, version " << version_on_disk;
    422     // Delete the existing database (if any), and create a fresh one.
    423     DropAllTables();
    424     if (!CreateTables())
    425       return false;
    426   }
    427 
    428   sql::Statement s(db_->GetUniqueStatement(
    429           "SELECT db_create_version, db_create_time FROM share_info"));
    430   if (!s.Step())
    431     return false;
    432   string db_create_version = s.ColumnString(0);
    433   int db_create_time = s.ColumnInt(1);
    434   DVLOG(1) << "DB created at " << db_create_time << " by version " <<
    435       db_create_version;
    436 
    437   return transaction.Commit();
    438 }
    439 
    440 // This function drops unused columns by creating a new table that contains only
    441 // the currently used columns then copying all rows from the old tables into
    442 // this new one.  The tables are then rearranged so the new replaces the old.
    443 bool DirectoryBackingStore::RefreshColumns() {
    444   DCHECK(needs_column_refresh_);
    445 
    446   // Create a new table named temp_metas.
    447   SafeDropTable("temp_metas");
    448   if (!CreateMetasTable(true))
    449     return false;
    450 
    451   // Populate temp_metas from metas.
    452   //
    453   // At this point, the metas table may contain columns belonging to obsolete
    454   // schema versions.  This statement explicitly lists only the columns that
    455   // belong to the current schema version, so the obsolete columns will be
    456   // effectively dropped once we rename temp_metas over top of metas.
    457   std::string query = "INSERT INTO temp_metas (";
    458   AppendColumnList(&query);
    459   query.append(") SELECT ");
    460   AppendColumnList(&query);
    461   query.append(" FROM metas");
    462   if (!db_->Execute(query.c_str()))
    463     return false;
    464 
    465   // Drop metas.
    466   SafeDropTable("metas");
    467 
    468   // Rename temp_metas -> metas.
    469   if (!db_->Execute("ALTER TABLE temp_metas RENAME TO metas"))
    470     return false;
    471 
    472   // Repeat the process for share_info.
    473   SafeDropTable("temp_share_info");
    474   if (!CreateShareInfoTable(true))
    475     return false;
    476 
    477   // TODO(rlarocque, 124140): Remove notification_state.
    478   if (!db_->Execute(
    479           "INSERT INTO temp_share_info (id, name, store_birthday, "
    480           "db_create_version, db_create_time, next_id, cache_guid,"
    481           "notification_state, bag_of_chips) "
    482           "SELECT id, name, store_birthday, db_create_version, "
    483           "db_create_time, next_id, cache_guid, notification_state, "
    484           "bag_of_chips "
    485           "FROM share_info"))
    486     return false;
    487 
    488   SafeDropTable("share_info");
    489   if (!db_->Execute("ALTER TABLE temp_share_info RENAME TO share_info"))
    490     return false;
    491 
    492   needs_column_refresh_ = false;
    493   return true;
    494 }
    495 
    496 bool DirectoryBackingStore::LoadEntries(
    497     Directory::MetahandlesMap* handles_map) {
    498   string select;
    499   select.reserve(kUpdateStatementBufferSize);
    500   select.append("SELECT ");
    501   AppendColumnList(&select);
    502   select.append(" FROM metas");
    503 
    504   sql::Statement s(db_->GetUniqueStatement(select.c_str()));
    505 
    506   while (s.Step()) {
    507     scoped_ptr<EntryKernel> kernel = UnpackEntry(&s);
    508     // A null kernel is evidence of external data corruption.
    509     if (!kernel)
    510       return false;
    511 
    512     int64 handle = kernel->ref(META_HANDLE);
    513     (*handles_map)[handle] = kernel.release();
    514   }
    515   return s.Succeeded();
    516 }
    517 
    518 bool DirectoryBackingStore::LoadDeleteJournals(
    519     JournalIndex* delete_journals) {
    520   string select;
    521   select.reserve(kUpdateStatementBufferSize);
    522   select.append("SELECT ");
    523   AppendColumnList(&select);
    524   select.append(" FROM deleted_metas");
    525 
    526   sql::Statement s(db_->GetUniqueStatement(select.c_str()));
    527 
    528   while (s.Step()) {
    529     scoped_ptr<EntryKernel> kernel = UnpackEntry(&s);
    530     // A null kernel is evidence of external data corruption.
    531     if (!kernel)
    532       return false;
    533     delete_journals->insert(kernel.release());
    534   }
    535   return s.Succeeded();
    536 }
    537 
    538 bool DirectoryBackingStore::LoadInfo(Directory::KernelLoadInfo* info) {
    539   {
    540     sql::Statement s(
    541         db_->GetUniqueStatement(
    542             "SELECT store_birthday, next_id, cache_guid, bag_of_chips "
    543             "FROM share_info"));
    544     if (!s.Step())
    545       return false;
    546 
    547     info->kernel_info.store_birthday = s.ColumnString(0);
    548     info->kernel_info.next_id = s.ColumnInt64(1);
    549     info->cache_guid = s.ColumnString(2);
    550     s.ColumnBlobAsString(3, &(info->kernel_info.bag_of_chips));
    551 
    552     // Verify there was only one row returned.
    553     DCHECK(!s.Step());
    554     DCHECK(s.Succeeded());
    555   }
    556 
    557   {
    558     sql::Statement s(
    559         db_->GetUniqueStatement(
    560             "SELECT model_id, progress_marker, "
    561             "transaction_version FROM models"));
    562 
    563     while (s.Step()) {
    564       ModelType type = ModelIdToModelTypeEnum(s.ColumnBlob(0),
    565                                               s.ColumnByteLength(0));
    566       if (type != UNSPECIFIED && type != TOP_LEVEL_FOLDER) {
    567         info->kernel_info.download_progress[type].ParseFromArray(
    568             s.ColumnBlob(1), s.ColumnByteLength(1));
    569         info->kernel_info.transaction_version[type] = s.ColumnInt64(2);
    570       }
    571     }
    572     if (!s.Succeeded())
    573       return false;
    574   }
    575   {
    576     sql::Statement s(
    577         db_->GetUniqueStatement(
    578             "SELECT MAX(metahandle) FROM metas"));
    579     if (!s.Step())
    580       return false;
    581 
    582     info->max_metahandle = s.ColumnInt64(0);
    583 
    584     // Verify only one row was returned.
    585     DCHECK(!s.Step());
    586     DCHECK(s.Succeeded());
    587   }
    588   return true;
    589 }
    590 
    591 /* static */
    592 bool DirectoryBackingStore::SaveEntryToDB(sql::Statement* save_statement,
    593                                           const EntryKernel& entry) {
    594   save_statement->Reset(true);
    595   BindFields(entry, save_statement);
    596   return save_statement->Run();
    597 }
    598 
    599 bool DirectoryBackingStore::DropDeletedEntries() {
    600   if (!db_->Execute("DELETE FROM metas "
    601                     "WHERE is_del > 0 "
    602                     "AND is_unsynced < 1 "
    603                     "AND is_unapplied_update < 1")) {
    604     return false;
    605   }
    606   if (!db_->Execute("DELETE FROM metas "
    607                     "WHERE is_del > 0 "
    608                     "AND id LIKE 'c%'")) {
    609     return false;
    610   }
    611   return true;
    612 }
    613 
    614 bool DirectoryBackingStore::SafeDropTable(const char* table_name) {
    615   string query = "DROP TABLE IF EXISTS ";
    616   query.append(table_name);
    617   return db_->Execute(query.c_str());
    618 }
    619 
    620 void DirectoryBackingStore::DropAllTables() {
    621   SafeDropTable("metas");
    622   SafeDropTable("temp_metas");
    623   SafeDropTable("share_info");
    624   SafeDropTable("temp_share_info");
    625   SafeDropTable("share_version");
    626   SafeDropTable("extended_attributes");
    627   SafeDropTable("models");
    628   SafeDropTable("temp_models");
    629   needs_column_refresh_ = false;
    630 }
    631 
    632 // static
    633 ModelType DirectoryBackingStore::ModelIdToModelTypeEnum(
    634     const void* data, int size) {
    635   sync_pb::EntitySpecifics specifics;
    636   if (!specifics.ParseFromArray(data, size))
    637     return UNSPECIFIED;
    638   return GetModelTypeFromSpecifics(specifics);
    639 }
    640 
    641 // static
    642 string DirectoryBackingStore::ModelTypeEnumToModelId(ModelType model_type) {
    643   sync_pb::EntitySpecifics specifics;
    644   AddDefaultFieldValue(model_type, &specifics);
    645   return specifics.SerializeAsString();
    646 }
    647 
    648 // static
    649 std::string DirectoryBackingStore::GenerateCacheGUID() {
    650   // Generate a GUID with 128 bits of randomness.
    651   const int kGuidBytes = 128 / 8;
    652   std::string guid;
    653   base::Base64Encode(base::RandBytesAsString(kGuidBytes), &guid);
    654   return guid;
    655 }
    656 
    657 bool DirectoryBackingStore::MigrateToSpecifics(
    658     const char* old_columns,
    659     const char* specifics_column,
    660     void (*handler_function)(sql::Statement* old_value_query,
    661                              int old_value_column,
    662                              sync_pb::EntitySpecifics* mutable_new_value)) {
    663   std::string query_sql = base::StringPrintf(
    664       "SELECT metahandle, %s, %s FROM metas", specifics_column, old_columns);
    665   std::string update_sql = base::StringPrintf(
    666       "UPDATE metas SET %s = ? WHERE metahandle = ?", specifics_column);
    667 
    668   sql::Statement query(db_->GetUniqueStatement(query_sql.c_str()));
    669   sql::Statement update(db_->GetUniqueStatement(update_sql.c_str()));
    670 
    671   while (query.Step()) {
    672     int64 metahandle = query.ColumnInt64(0);
    673     std::string new_value_bytes;
    674     query.ColumnBlobAsString(1, &new_value_bytes);
    675     sync_pb::EntitySpecifics new_value;
    676     new_value.ParseFromString(new_value_bytes);
    677     handler_function(&query, 2, &new_value);
    678     new_value.SerializeToString(&new_value_bytes);
    679 
    680     update.BindBlob(0, new_value_bytes.data(), new_value_bytes.length());
    681     update.BindInt64(1, metahandle);
    682     if (!update.Run())
    683       return false;
    684     update.Reset(true);
    685   }
    686   return query.Succeeded();
    687 }
    688 
    689 bool DirectoryBackingStore::SetVersion(int version) {
    690   sql::Statement s(db_->GetCachedStatement(
    691           SQL_FROM_HERE, "UPDATE share_version SET data = ?"));
    692   s.BindInt(0, version);
    693 
    694   return s.Run();
    695 }
    696 
    697 int DirectoryBackingStore::GetVersion() {
    698   if (!db_->DoesTableExist("share_version"))
    699     return 0;
    700 
    701   sql::Statement statement(db_->GetUniqueStatement(
    702           "SELECT data FROM share_version"));
    703   if (statement.Step()) {
    704     return statement.ColumnInt(0);
    705   } else {
    706     return 0;
    707   }
    708 }
    709 
    710 bool DirectoryBackingStore::MigrateVersion67To68() {
    711   // This change simply removed three columns:
    712   //   string NAME
    713   //   string UNSANITIZED_NAME
    714   //   string SERVER_NAME
    715   // No data migration is necessary, but we should do a column refresh.
    716   SetVersion(68);
    717   needs_column_refresh_ = true;
    718   return true;
    719 }
    720 
    721 bool DirectoryBackingStore::MigrateVersion69To70() {
    722   // Added "unique_client_tag", renamed "singleton_tag" to unique_server_tag
    723   SetVersion(70);
    724   if (!db_->Execute(
    725           "ALTER TABLE metas ADD COLUMN unique_server_tag varchar"))
    726     return false;
    727   if (!db_->Execute(
    728           "ALTER TABLE metas ADD COLUMN unique_client_tag varchar"))
    729     return false;
    730   needs_column_refresh_ = true;
    731 
    732   if (!db_->Execute(
    733           "UPDATE metas SET unique_server_tag = singleton_tag"))
    734     return false;
    735 
    736   return true;
    737 }
    738 
    739 namespace {
    740 
    741 // Callback passed to MigrateToSpecifics for the v68->v69 migration.  See
    742 // MigrateVersion68To69().
    743 void EncodeBookmarkURLAndFavicon(sql::Statement* old_value_query,
    744                                  int old_value_column,
    745                                  sync_pb::EntitySpecifics* mutable_new_value) {
    746   // Extract data from the column trio we expect.
    747   bool old_is_bookmark_object = old_value_query->ColumnBool(old_value_column);
    748   std::string old_url = old_value_query->ColumnString(old_value_column + 1);
    749   std::string old_favicon;
    750   old_value_query->ColumnBlobAsString(old_value_column + 2, &old_favicon);
    751   bool old_is_dir = old_value_query->ColumnBool(old_value_column + 3);
    752 
    753   if (old_is_bookmark_object) {
    754     sync_pb::BookmarkSpecifics* bookmark_data =
    755         mutable_new_value->mutable_bookmark();
    756     if (!old_is_dir) {
    757       bookmark_data->set_url(old_url);
    758       bookmark_data->set_favicon(old_favicon);
    759     }
    760   }
    761 }
    762 
    763 }  // namespace
    764 
    765 bool DirectoryBackingStore::MigrateVersion68To69() {
    766   // In Version 68, there were columns on table 'metas':
    767   //   string BOOKMARK_URL
    768   //   string SERVER_BOOKMARK_URL
    769   //   blob BOOKMARK_FAVICON
    770   //   blob SERVER_BOOKMARK_FAVICON
    771   // In version 69, these columns went away in favor of storing
    772   // a serialized EntrySpecifics protobuf in the columns:
    773   //   protobuf blob SPECIFICS
    774   //   protobuf blob SERVER_SPECIFICS
    775   // For bookmarks, EntrySpecifics is extended as per
    776   // bookmark_specifics.proto. This migration converts bookmarks from the
    777   // former scheme to the latter scheme.
    778 
    779   // First, add the two new columns to the schema.
    780   if (!db_->Execute(
    781           "ALTER TABLE metas ADD COLUMN specifics blob"))
    782     return false;
    783   if (!db_->Execute(
    784           "ALTER TABLE metas ADD COLUMN server_specifics blob"))
    785     return false;
    786 
    787   // Next, fold data from the old columns into the new protobuf columns.
    788   if (!MigrateToSpecifics(("is_bookmark_object, bookmark_url, "
    789                            "bookmark_favicon, is_dir"),
    790                           "specifics",
    791                           &EncodeBookmarkURLAndFavicon)) {
    792     return false;
    793   }
    794   if (!MigrateToSpecifics(("server_is_bookmark_object, "
    795                            "server_bookmark_url, "
    796                            "server_bookmark_favicon, "
    797                            "server_is_dir"),
    798                           "server_specifics",
    799                           &EncodeBookmarkURLAndFavicon)) {
    800     return false;
    801   }
    802 
    803   // Lastly, fix up the "Google Chrome" folder, which is of the TOP_LEVEL_FOLDER
    804   // ModelType: it shouldn't have BookmarkSpecifics.
    805   if (!db_->Execute(
    806           "UPDATE metas SET specifics = NULL, server_specifics = NULL WHERE "
    807           "singleton_tag IN ('google_chrome')"))
    808     return false;
    809 
    810   SetVersion(69);
    811   needs_column_refresh_ = true;  // Trigger deletion of old columns.
    812   return true;
    813 }
    814 
    815 // Version 71, the columns 'initial_sync_ended' and 'last_sync_timestamp'
    816 // were removed from the share_info table.  They were replaced by
    817 // the 'models' table, which has these values on a per-datatype basis.
    818 bool DirectoryBackingStore::MigrateVersion70To71() {
    819   if (!CreateV71ModelsTable())
    820     return false;
    821 
    822   // Move data from the old share_info columns to the new models table.
    823   {
    824     sql::Statement fetch(db_->GetUniqueStatement(
    825             "SELECT last_sync_timestamp, initial_sync_ended FROM share_info"));
    826     if (!fetch.Step())
    827       return false;
    828 
    829     int64 last_sync_timestamp = fetch.ColumnInt64(0);
    830     bool initial_sync_ended = fetch.ColumnBool(1);
    831 
    832     // Verify there were no additional rows returned.
    833     DCHECK(!fetch.Step());
    834     DCHECK(fetch.Succeeded());
    835 
    836     sql::Statement update(db_->GetUniqueStatement(
    837             "INSERT INTO models (model_id, "
    838             "last_download_timestamp, initial_sync_ended) VALUES (?, ?, ?)"));
    839     string bookmark_model_id = ModelTypeEnumToModelId(BOOKMARKS);
    840     update.BindBlob(0, bookmark_model_id.data(), bookmark_model_id.size());
    841     update.BindInt64(1, last_sync_timestamp);
    842     update.BindBool(2, initial_sync_ended);
    843 
    844     if (!update.Run())
    845       return false;
    846   }
    847 
    848   // Drop the columns from the old share_info table via a temp table.
    849   const bool kCreateAsTempShareInfo = true;
    850 
    851   if (!CreateShareInfoTableVersion71(kCreateAsTempShareInfo))
    852     return false;
    853   if (!db_->Execute(
    854           "INSERT INTO temp_share_info (id, name, store_birthday, "
    855           "db_create_version, db_create_time, next_id, cache_guid) "
    856           "SELECT id, name, store_birthday, db_create_version, "
    857           "db_create_time, next_id, cache_guid FROM share_info"))
    858     return false;
    859   SafeDropTable("share_info");
    860   if (!db_->Execute(
    861           "ALTER TABLE temp_share_info RENAME TO share_info"))
    862     return false;
    863   SetVersion(71);
    864   return true;
    865 }
    866 
    867 bool DirectoryBackingStore::MigrateVersion71To72() {
    868   // Version 72 removed a table 'extended_attributes', whose
    869   // contents didn't matter.
    870   SafeDropTable("extended_attributes");
    871   SetVersion(72);
    872   return true;
    873 }
    874 
    875 bool DirectoryBackingStore::MigrateVersion72To73() {
    876   // Version 73 added one column to the table 'share_info': notification_state
    877   if (!db_->Execute(
    878           "ALTER TABLE share_info ADD COLUMN notification_state BLOB"))
    879     return false;
    880   SetVersion(73);
    881   return true;
    882 }
    883 
    884 bool DirectoryBackingStore::MigrateVersion73To74() {
    885   // Version 74 added the following columns to the table 'share_info':
    886   //   autofill_migration_state
    887   //   bookmarks_added_during_autofill_migration
    888   //   autofill_migration_time
    889   //   autofill_entries_added_during_migration
    890   //   autofill_profiles_added_during_migration
    891 
    892   if (!db_->Execute(
    893           "ALTER TABLE share_info ADD COLUMN "
    894           "autofill_migration_state INT default 0"))
    895     return false;
    896 
    897   if (!db_->Execute(
    898           "ALTER TABLE share_info ADD COLUMN "
    899           "bookmarks_added_during_autofill_migration "
    900           "INT default 0"))
    901     return false;
    902 
    903   if (!db_->Execute(
    904           "ALTER TABLE share_info ADD COLUMN autofill_migration_time "
    905           "INT default 0"))
    906     return false;
    907 
    908   if (!db_->Execute(
    909           "ALTER TABLE share_info ADD COLUMN "
    910           "autofill_entries_added_during_migration "
    911           "INT default 0"))
    912     return false;
    913 
    914   if (!db_->Execute(
    915           "ALTER TABLE share_info ADD COLUMN "
    916           "autofill_profiles_added_during_migration "
    917           "INT default 0"))
    918     return false;
    919 
    920   SetVersion(74);
    921   return true;
    922 }
    923 
    924 bool DirectoryBackingStore::MigrateVersion74To75() {
    925   // In version 74, there was a table 'models':
    926   //     blob model_id (entity specifics, primary key)
    927   //     int last_download_timestamp
    928   //     boolean initial_sync_ended
    929   // In version 75, we deprecated the integer-valued last_download_timestamp,
    930   // using insted a protobuf-valued progress_marker field:
    931   //     blob progress_marker
    932   // The progress_marker values are initialized from the value of
    933   // last_download_timestamp, thereby preserving the download state.
    934 
    935   // Move aside the old table and create a new empty one at the current schema.
    936   if (!db_->Execute("ALTER TABLE models RENAME TO temp_models"))
    937     return false;
    938   if (!CreateV75ModelsTable())
    939     return false;
    940 
    941   sql::Statement query(db_->GetUniqueStatement(
    942           "SELECT model_id, last_download_timestamp, initial_sync_ended "
    943           "FROM temp_models"));
    944 
    945   sql::Statement update(db_->GetUniqueStatement(
    946           "INSERT INTO models (model_id, "
    947           "progress_marker, initial_sync_ended) VALUES (?, ?, ?)"));
    948 
    949   while (query.Step()) {
    950     ModelType type = ModelIdToModelTypeEnum(query.ColumnBlob(0),
    951                                             query.ColumnByteLength(0));
    952     if (type != UNSPECIFIED) {
    953       // Set the |timestamp_token_for_migration| on a new
    954       // DataTypeProgressMarker, using the old value of last_download_timestamp.
    955       // The server will turn this into a real token on our behalf the next
    956       // time we check for updates.
    957       sync_pb::DataTypeProgressMarker progress_marker;
    958       progress_marker.set_data_type_id(
    959           GetSpecificsFieldNumberFromModelType(type));
    960       progress_marker.set_timestamp_token_for_migration(query.ColumnInt64(1));
    961       std::string progress_blob;
    962       progress_marker.SerializeToString(&progress_blob);
    963 
    964       update.BindBlob(0, query.ColumnBlob(0), query.ColumnByteLength(0));
    965       update.BindBlob(1, progress_blob.data(), progress_blob.length());
    966       update.BindBool(2, query.ColumnBool(2));
    967       if (!update.Run())
    968         return false;
    969       update.Reset(true);
    970     }
    971   }
    972   if (!query.Succeeded())
    973     return false;
    974 
    975   // Drop the old table.
    976   SafeDropTable("temp_models");
    977 
    978   SetVersion(75);
    979   return true;
    980 }
    981 
    982 bool DirectoryBackingStore::MigrateVersion75To76() {
    983   // This change removed five columns:
    984   //   autofill_migration_state
    985   //   bookmarks_added_during_autofill_migration
    986   //   autofill_migration_time
    987   //   autofill_entries_added_during_migration
    988   //   autofill_profiles_added_during_migration
    989   // No data migration is necessary, but we should do a column refresh.
    990   SetVersion(76);
    991   needs_column_refresh_ = true;
    992   return true;
    993 }
    994 
    995 bool DirectoryBackingStore::MigrateVersion76To77() {
    996   // This change changes the format of stored timestamps to ms since
    997   // the Unix epoch.
    998 #if defined(OS_WIN)
    999 // On Windows, we used to store timestamps in FILETIME format (100s of
   1000 // ns since Jan 1, 1601).  Magic numbers taken from
   1001 // http://stackoverflow.com/questions/5398557/
   1002 //     java-library-for-dealing-with-win32-filetime
   1003 // .
   1004 #define TO_UNIX_TIME_MS(x) #x " = " #x " / 10000 - 11644473600000"
   1005 #else
   1006 // On other platforms, we used to store timestamps in time_t format (s
   1007 // since the Unix epoch).
   1008 #define TO_UNIX_TIME_MS(x) #x " = " #x " * 1000"
   1009 #endif
   1010   sql::Statement update_timestamps(db_->GetUniqueStatement(
   1011           "UPDATE metas SET "
   1012           TO_UNIX_TIME_MS(mtime) ", "
   1013           TO_UNIX_TIME_MS(server_mtime) ", "
   1014           TO_UNIX_TIME_MS(ctime) ", "
   1015           TO_UNIX_TIME_MS(server_ctime)));
   1016 #undef TO_UNIX_TIME_MS
   1017   if (!update_timestamps.Run())
   1018     return false;
   1019   SetVersion(77);
   1020   return true;
   1021 }
   1022 
   1023 bool DirectoryBackingStore::MigrateVersion77To78() {
   1024   // Version 78 added one column to table 'metas': base_server_specifics.
   1025   if (!db_->Execute(
   1026           "ALTER TABLE metas ADD COLUMN base_server_specifics BLOB")) {
   1027     return false;
   1028   }
   1029   SetVersion(78);
   1030   return true;
   1031 }
   1032 
   1033 bool DirectoryBackingStore::MigrateVersion78To79() {
   1034   // Some users are stuck with a DB that causes them to reuse existing IDs.  We
   1035   // perform this one-time fixup on all users to help the few that are stuck.
   1036   // See crbug.com/142987 for details.
   1037   if (!db_->Execute(
   1038           "UPDATE share_info SET next_id = next_id - 65536")) {
   1039     return false;
   1040   }
   1041   SetVersion(79);
   1042   return true;
   1043 }
   1044 
   1045 bool DirectoryBackingStore::MigrateVersion79To80() {
   1046   if (!db_->Execute(
   1047           "ALTER TABLE share_info ADD COLUMN bag_of_chips BLOB"))
   1048     return false;
   1049   sql::Statement update(db_->GetUniqueStatement(
   1050           "UPDATE share_info SET bag_of_chips = ?"));
   1051   // An empty message is serialized to an empty string.
   1052   update.BindBlob(0, NULL, 0);
   1053   if (!update.Run())
   1054     return false;
   1055   SetVersion(80);
   1056   return true;
   1057 }
   1058 
   1059 bool DirectoryBackingStore::MigrateVersion80To81() {
   1060   if(!db_->Execute(
   1061          "ALTER TABLE metas ADD COLUMN server_ordinal_in_parent BLOB"))
   1062     return false;
   1063 
   1064   sql::Statement get_positions(db_->GetUniqueStatement(
   1065       "SELECT metahandle, server_position_in_parent FROM metas"));
   1066 
   1067   sql::Statement put_ordinals(db_->GetUniqueStatement(
   1068       "UPDATE metas SET server_ordinal_in_parent = ?"
   1069       "WHERE metahandle = ?"));
   1070 
   1071   while(get_positions.Step()) {
   1072     int64 metahandle = get_positions.ColumnInt64(0);
   1073     int64 position = get_positions.ColumnInt64(1);
   1074 
   1075     const std::string& ordinal = Int64ToNodeOrdinal(position).ToInternalValue();
   1076     put_ordinals.BindBlob(0, ordinal.data(), ordinal.length());
   1077     put_ordinals.BindInt64(1, metahandle);
   1078 
   1079     if(!put_ordinals.Run())
   1080       return false;
   1081     put_ordinals.Reset(true);
   1082   }
   1083 
   1084   SetVersion(81);
   1085   needs_column_refresh_ = true;
   1086   return true;
   1087 }
   1088 
   1089 bool DirectoryBackingStore::MigrateVersion81To82() {
   1090   if (!db_->Execute(
   1091       "ALTER TABLE models ADD COLUMN transaction_version BIGINT default 0"))
   1092     return false;
   1093   sql::Statement update(db_->GetUniqueStatement(
   1094       "UPDATE models SET transaction_version = 0"));
   1095   if (!update.Run())
   1096     return false;
   1097   SetVersion(82);
   1098   return true;
   1099 }
   1100 
   1101 bool DirectoryBackingStore::MigrateVersion82To83() {
   1102   // Version 83 added transaction_version on sync node.
   1103   if (!db_->Execute(
   1104       "ALTER TABLE metas ADD COLUMN transaction_version BIGINT default 0"))
   1105     return false;
   1106   sql::Statement update(db_->GetUniqueStatement(
   1107       "UPDATE metas SET transaction_version = 0"));
   1108   if (!update.Run())
   1109     return false;
   1110   SetVersion(83);
   1111   return true;
   1112 }
   1113 
   1114 bool DirectoryBackingStore::MigrateVersion83To84() {
   1115   // Version 84 added deleted_metas table to store deleted metas until we know
   1116   // for sure that the deletions are persisted in native models.
   1117   string query = "CREATE TABLE deleted_metas ";
   1118   query.append(ComposeCreateTableColumnSpecs());
   1119   if (!db_->Execute(query.c_str()))
   1120     return false;
   1121   SetVersion(84);
   1122   return true;
   1123 }
   1124 
   1125 bool DirectoryBackingStore::MigrateVersion84To85() {
   1126   // Version 85 removes the initial_sync_ended flag.
   1127   if (!db_->Execute("ALTER TABLE models RENAME TO temp_models"))
   1128     return false;
   1129   if (!CreateModelsTable())
   1130     return false;
   1131   if (!db_->Execute("INSERT INTO models SELECT "
   1132                     "model_id, progress_marker, transaction_version "
   1133                     "FROM temp_models")) {
   1134     return false;
   1135   }
   1136   SafeDropTable("temp_models");
   1137 
   1138   SetVersion(85);
   1139   return true;
   1140 }
   1141 
   1142 bool DirectoryBackingStore::MigrateVersion85To86() {
   1143   // Version 86 removes both server ordinals and local NEXT_ID, PREV_ID and
   1144   // SERVER_{POSITION,ORDINAL}_IN_PARENT and replaces them with UNIQUE_POSITION
   1145   // and SERVER_UNIQUE_POSITION.
   1146   if (!db_->Execute("ALTER TABLE metas ADD COLUMN "
   1147                     "server_unique_position BLOB")) {
   1148     return false;
   1149   }
   1150   if (!db_->Execute("ALTER TABLE metas ADD COLUMN "
   1151                     "unique_position BLOB")) {
   1152     return false;
   1153   }
   1154   if (!db_->Execute("ALTER TABLE metas ADD COLUMN "
   1155                     "unique_bookmark_tag VARCHAR")) {
   1156     return false;
   1157   }
   1158 
   1159   // Fetch the cache_guid from the DB, because we don't otherwise have access to
   1160   // it from here.
   1161   sql::Statement get_cache_guid(db_->GetUniqueStatement(
   1162       "SELECT cache_guid FROM share_info"));
   1163   if (!get_cache_guid.Step()) {
   1164     return false;
   1165   }
   1166   std::string cache_guid = get_cache_guid.ColumnString(0);
   1167   DCHECK(!get_cache_guid.Step());
   1168   DCHECK(get_cache_guid.Succeeded());
   1169 
   1170   sql::Statement get(db_->GetUniqueStatement(
   1171       "SELECT "
   1172       "  metahandle, "
   1173       "  id, "
   1174       "  specifics, "
   1175       "  is_dir, "
   1176       "  unique_server_tag, "
   1177       "  server_ordinal_in_parent "
   1178       "FROM metas"));
   1179 
   1180   // Note that we set both the local and server position based on the server
   1181   // position.  We wll lose any unsynced local position changes.  Unfortunately,
   1182   // there's nothing we can do to avoid that.  The NEXT_ID / PREV_ID values
   1183   // can't be translated into a UNIQUE_POSTION in a reliable way.
   1184   sql::Statement put(db_->GetCachedStatement(
   1185       SQL_FROM_HERE,
   1186       "UPDATE metas SET"
   1187       "  server_unique_position = ?,"
   1188       "  unique_position = ?,"
   1189       "  unique_bookmark_tag = ?"
   1190       "WHERE metahandle = ?"));
   1191 
   1192   while (get.Step()) {
   1193     int64 metahandle = get.ColumnInt64(0);
   1194 
   1195     std::string id_string;
   1196     get.ColumnBlobAsString(1, &id_string);
   1197 
   1198     sync_pb::EntitySpecifics specifics;
   1199     specifics.ParseFromArray(
   1200         get.ColumnBlob(2), get.ColumnByteLength(2));
   1201 
   1202     bool is_dir = get.ColumnBool(3);
   1203 
   1204     std::string server_unique_tag = get.ColumnString(4);
   1205 
   1206     std::string ordinal_string;
   1207     get.ColumnBlobAsString(5, &ordinal_string);
   1208     NodeOrdinal ordinal(ordinal_string);
   1209 
   1210 
   1211     std::string unique_bookmark_tag;
   1212 
   1213     // We only maintain positions for bookmarks that are not server-defined
   1214     // top-level folders.
   1215     UniquePosition position;
   1216     if (GetModelTypeFromSpecifics(specifics) == BOOKMARKS
   1217         && !(is_dir && !server_unique_tag.empty())) {
   1218       if (id_string.at(0) == 'c') {
   1219         // We found an uncommitted item.  This is rare, but fortunate.  This
   1220         // means we can set the bookmark tag according to the originator client
   1221         // item ID and originator cache guid, because (unlike the other case) we
   1222         // know that this client is the originator.
   1223         unique_bookmark_tag = syncable::GenerateSyncableBookmarkHash(
   1224             cache_guid,
   1225             id_string.substr(1));
   1226       } else {
   1227         // If we've already committed the item, then we don't know who the
   1228         // originator was.  We do not have access to the originator client item
   1229         // ID and originator cache guid at this point.
   1230         //
   1231         // We will base our hash entirely on the server ID instead.  This is
   1232         // incorrect, but at least all clients that undergo this migration step
   1233         // will be incorrect in the same way.
   1234         //
   1235         // To get everyone back into a synced state, we will update the bookmark
   1236         // tag according to the originator_cache_guid and originator_item_id
   1237         // when we see updates for this item.  That should ensure that commonly
   1238         // modified items will end up with the proper tag values eventually.
   1239         unique_bookmark_tag = syncable::GenerateSyncableBookmarkHash(
   1240             std::string(), // cache_guid left intentionally blank.
   1241             id_string.substr(1));
   1242       }
   1243 
   1244       int64 int_position = NodeOrdinalToInt64(ordinal);
   1245       position = UniquePosition::FromInt64(int_position, unique_bookmark_tag);
   1246     } else {
   1247       // Leave bookmark_tag and position at their default (invalid) values.
   1248     }
   1249 
   1250     std::string position_blob;
   1251     position.SerializeToString(&position_blob);
   1252     put.BindBlob(0, position_blob.data(), position_blob.length());
   1253     put.BindBlob(1, position_blob.data(), position_blob.length());
   1254     put.BindBlob(2, unique_bookmark_tag.data(), unique_bookmark_tag.length());
   1255     put.BindInt64(3, metahandle);
   1256 
   1257     if (!put.Run())
   1258       return false;
   1259     put.Reset(true);
   1260   }
   1261 
   1262   SetVersion(86);
   1263   needs_column_refresh_ = true;
   1264   return true;
   1265 }
   1266 
   1267 bool DirectoryBackingStore::CreateTables() {
   1268   DVLOG(1) << "First run, creating tables";
   1269   // Create two little tables share_version and share_info
   1270   if (!db_->Execute(
   1271           "CREATE TABLE share_version ("
   1272           "id VARCHAR(128) primary key, data INT)")) {
   1273     return false;
   1274   }
   1275 
   1276   {
   1277     sql::Statement s(db_->GetUniqueStatement(
   1278             "INSERT INTO share_version VALUES(?, ?)"));
   1279     s.BindString(0, dir_name_);
   1280     s.BindInt(1, kCurrentDBVersion);
   1281 
   1282     if (!s.Run())
   1283       return false;
   1284   }
   1285 
   1286   const bool kCreateAsTempShareInfo = false;
   1287   if (!CreateShareInfoTable(kCreateAsTempShareInfo)) {
   1288     return false;
   1289   }
   1290 
   1291   {
   1292     sql::Statement s(db_->GetUniqueStatement(
   1293             "INSERT INTO share_info VALUES"
   1294             "(?, "  // id
   1295             "?, "   // name
   1296             "?, "   // store_birthday
   1297             "?, "   // db_create_version
   1298             "?, "   // db_create_time
   1299             "-2, "  // next_id
   1300             "?, "   // cache_guid
   1301             // TODO(rlarocque, 124140): Remove notification_state field.
   1302             "?, "   // notification_state
   1303             "?);"));  // bag_of_chips
   1304     s.BindString(0, dir_name_);                   // id
   1305     s.BindString(1, dir_name_);                   // name
   1306     s.BindString(2, std::string());               // store_birthday
   1307     // TODO(akalin): Remove this unused db_create_version field. (Or
   1308     // actually use it for something.) http://crbug.com/118356
   1309     s.BindString(3, "Unknown");                   // db_create_version
   1310     s.BindInt(4, static_cast<int32>(time(0)));    // db_create_time
   1311     s.BindString(5, GenerateCacheGUID());         // cache_guid
   1312     // TODO(rlarocque, 124140): Remove this unused notification-state field.
   1313     s.BindBlob(6, NULL, 0);                       // notification_state
   1314     s.BindBlob(7, NULL, 0);                       // bag_of_chips
   1315     if (!s.Run())
   1316       return false;
   1317   }
   1318 
   1319   if (!CreateModelsTable())
   1320     return false;
   1321 
   1322   // Create the big metas table.
   1323   if (!CreateMetasTable(false))
   1324     return false;
   1325 
   1326   {
   1327     // Insert the entry for the root into the metas table.
   1328     const int64 now = TimeToProtoTime(base::Time::Now());
   1329     sql::Statement s(db_->GetUniqueStatement(
   1330             "INSERT INTO metas "
   1331             "( id, metahandle, is_dir, ctime, mtime ) "
   1332             "VALUES ( \"r\", 1, 1, ?, ? )"));
   1333     s.BindInt64(0, now);
   1334     s.BindInt64(1, now);
   1335 
   1336     if (!s.Run())
   1337       return false;
   1338   }
   1339 
   1340   return true;
   1341 }
   1342 
   1343 bool DirectoryBackingStore::CreateMetasTable(bool is_temporary) {
   1344   string query = "CREATE TABLE ";
   1345   query.append(is_temporary ? "temp_metas" : "metas");
   1346   query.append(ComposeCreateTableColumnSpecs());
   1347   if (!db_->Execute(query.c_str()))
   1348     return false;
   1349 
   1350   // Create a deleted_metas table to save copies of deleted metas until the
   1351   // deletions are persisted. For simplicity, don't try to migrate existing
   1352   // data because it's rarely used.
   1353   SafeDropTable("deleted_metas");
   1354   query = "CREATE TABLE deleted_metas ";
   1355   query.append(ComposeCreateTableColumnSpecs());
   1356   return db_->Execute(query.c_str());
   1357 }
   1358 
   1359 bool DirectoryBackingStore::CreateV71ModelsTable() {
   1360   // This is an old schema for the Models table, used from versions 71 to 74.
   1361   return db_->Execute(
   1362       "CREATE TABLE models ("
   1363       "model_id BLOB primary key, "
   1364       "last_download_timestamp INT, "
   1365       // Gets set if the syncer ever gets updates from the
   1366       // server and the server returns 0.  Lets us detect the
   1367       // end of the initial sync.
   1368       "initial_sync_ended BOOLEAN default 0)");
   1369 }
   1370 
   1371 bool DirectoryBackingStore::CreateV75ModelsTable() {
   1372   // This is an old schema for the Models table, used from versions 75 to 80.
   1373   return db_->Execute(
   1374       "CREATE TABLE models ("
   1375       "model_id BLOB primary key, "
   1376       "progress_marker BLOB, "
   1377       // Gets set if the syncer ever gets updates from the
   1378       // server and the server returns 0.  Lets us detect the
   1379       // end of the initial sync.
   1380       "initial_sync_ended BOOLEAN default 0)");
   1381 }
   1382 
   1383 bool DirectoryBackingStore::CreateModelsTable() {
   1384   // This is the current schema for the Models table, from version 81
   1385   // onward.  If you change the schema, you'll probably want to double-check
   1386   // the use of this function in the v84-v85 migration.
   1387   return db_->Execute(
   1388       "CREATE TABLE models ("
   1389       "model_id BLOB primary key, "
   1390       "progress_marker BLOB, "
   1391       // Gets set if the syncer ever gets updates from the
   1392       // server and the server returns 0.  Lets us detect the
   1393       // end of the initial sync.
   1394       "transaction_version BIGINT default 0)");
   1395 }
   1396 
   1397 bool DirectoryBackingStore::CreateShareInfoTable(bool is_temporary) {
   1398   const char* name = is_temporary ? "temp_share_info" : "share_info";
   1399   string query = "CREATE TABLE ";
   1400   query.append(name);
   1401   // This is the current schema for the ShareInfo table, from version 76
   1402   // onward.
   1403   query.append(" ("
   1404       "id TEXT primary key, "
   1405       "name TEXT, "
   1406       "store_birthday TEXT, "
   1407       "db_create_version TEXT, "
   1408       "db_create_time INT, "
   1409       "next_id INT default -2, "
   1410       "cache_guid TEXT, "
   1411       // TODO(rlarocque, 124140): Remove notification_state field.
   1412       "notification_state BLOB, "
   1413       "bag_of_chips BLOB"
   1414       ")");
   1415   return db_->Execute(query.c_str());
   1416 }
   1417 
   1418 bool DirectoryBackingStore::CreateShareInfoTableVersion71(
   1419     bool is_temporary) {
   1420   const char* name = is_temporary ? "temp_share_info" : "share_info";
   1421   string query = "CREATE TABLE ";
   1422   query.append(name);
   1423   // This is the schema for the ShareInfo table used from versions 71 to 72.
   1424   query.append(" ("
   1425       "id TEXT primary key, "
   1426       "name TEXT, "
   1427       "store_birthday TEXT, "
   1428       "db_create_version TEXT, "
   1429       "db_create_time INT, "
   1430       "next_id INT default -2, "
   1431       "cache_guid TEXT )");
   1432   return db_->Execute(query.c_str());
   1433 }
   1434 
   1435 // This function checks to see if the given list of Metahandles has any nodes
   1436 // whose PARENT_ID values refer to ID values that do not actually exist.
   1437 // Returns true on success.
   1438 bool DirectoryBackingStore::VerifyReferenceIntegrity(
   1439     const Directory::MetahandlesMap* handles_map) {
   1440   TRACE_EVENT0("sync", "SyncDatabaseIntegrityCheck");
   1441   using namespace syncable;
   1442   typedef base::hash_set<std::string> IdsSet;
   1443 
   1444   IdsSet ids_set;
   1445   bool is_ok = true;
   1446 
   1447   for (Directory::MetahandlesMap::const_iterator it = handles_map->begin();
   1448        it != handles_map->end(); ++it) {
   1449     EntryKernel* entry = it->second;
   1450     bool is_duplicate_id = !(ids_set.insert(entry->ref(ID).value()).second);
   1451     is_ok = is_ok && !is_duplicate_id;
   1452   }
   1453 
   1454   IdsSet::iterator end = ids_set.end();
   1455   for (Directory::MetahandlesMap::const_iterator it = handles_map->begin();
   1456        it != handles_map->end(); ++it) {
   1457     EntryKernel* entry = it->second;
   1458     bool parent_exists = (ids_set.find(entry->ref(PARENT_ID).value()) != end);
   1459     if (!parent_exists) {
   1460       return false;
   1461     }
   1462   }
   1463   return is_ok;
   1464 }
   1465 
   1466 void DirectoryBackingStore::PrepareSaveEntryStatement(
   1467     EntryTable table, sql::Statement* save_statement) {
   1468   if (save_statement->is_valid())
   1469     return;
   1470 
   1471   string query;
   1472   query.reserve(kUpdateStatementBufferSize);
   1473   switch (table) {
   1474     case METAS_TABLE:
   1475       query.append("INSERT OR REPLACE INTO metas ");
   1476       break;
   1477     case DELETE_JOURNAL_TABLE:
   1478       query.append("INSERT OR REPLACE INTO deleted_metas ");
   1479       break;
   1480   }
   1481 
   1482   string values;
   1483   values.reserve(kUpdateStatementBufferSize);
   1484   values.append(" VALUES ");
   1485   const char* separator = "( ";
   1486   int i = 0;
   1487   for (i = BEGIN_FIELDS; i < FIELD_COUNT; ++i) {
   1488     query.append(separator);
   1489     values.append(separator);
   1490     separator = ", ";
   1491     query.append(ColumnName(i));
   1492     values.append("?");
   1493   }
   1494   query.append(" ) ");
   1495   values.append(" )");
   1496   query.append(values);
   1497   save_statement->Assign(db_->GetUniqueStatement(
   1498       base::StringPrintf(query.c_str(), "metas").c_str()));
   1499 }
   1500 
   1501 }  // namespace syncable
   1502 }  // namespace syncer
   1503