Skip to content

Commit

Permalink
CBL-6591: Fix expiration upgrade (#2196)
Browse files Browse the repository at this point in the history
  • Loading branch information
callumbirks authored Dec 20, 2024
1 parent 27d3530 commit 6b639a7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
18 changes: 18 additions & 0 deletions C/tests/c4DatabaseTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1311,6 +1311,24 @@ TEST_CASE("Database Upgrade From 2.8 with Index", "[Database][Upgrade][C]") {
}
}

// CBL-6534
TEST_CASE("Database Upgrade from 3.1 with peerCheckpoints table", "[Database][Upgrade][C]") {
string dbPath = "upgrade_3.1_peerCheckpoints.cblite2";

C4DatabaseFlags withFlags{0};

SECTION("Revision Tree") {}
SECTION("Version Vector") { withFlags = kC4DB_VersionVectors; }

C4Log("---- Opening copy of db %s with flags 0x%x", dbPath.c_str(), withFlags);
C4DatabaseConfig2 config = {slice(TempDir()), withFlags};
auto name = C4Test::copyFixtureDB(kVersionedFixturesSubDir + dbPath);
C4Log("---- copy Fixture to: %s/%s", TempDir().c_str(), name.asString().c_str());
C4Error err;
c4::ref<C4Database> db = c4db_openNamed(name, &config, WITH_ERROR(&err));
CHECK(db);
}

static void setRemoteRev(C4Database* db, slice docID, slice revID, C4RemoteID remote) {
auto defaultColl = c4db_getDefaultCollection(db, nullptr);
C4Document* doc = c4coll_getDoc(defaultColl, docID, true, kDocGetAll, ERROR_INFO());
Expand Down
Binary file not shown.
10 changes: 5 additions & 5 deletions LiteCore/Storage/SQLiteDataFile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -344,17 +344,17 @@ namespace litecore {
// Add the 'expiration' column to every KeyStore:
for ( string& name : allKeyStoreNames() ) {
if ( name.find("::") == string::npos ) {
string tableName = SQLiteKeyStore::tableName(name);
string sql;
// We need to check for existence of the expiration column first.
// Do not add it if it already exists in the table.
if ( getSchema("kv_" + name, "table", "kv_" + name, sql)
&& sql.find("expiration") != string::npos )
if ( getSchema(tableName, "table", tableName, sql) && sql.find("expiration") != string::npos )
continue;
// Only update data tables, not FTS index tables
_exec(stringprintf(
"ALTER TABLE \"kv_%s\" ADD COLUMN expiration INTEGER; "
"CREATE INDEX \"kv_%s_expiration\" ON \"kv_%s\" (expiration) WHERE expiration not null",
name.c_str(), name.c_str(), name.c_str()));
"ALTER TABLE \"%s\" ADD COLUMN expiration INTEGER; "
"CREATE INDEX \"%s_expiration\" ON \"%s\" (expiration) WHERE expiration not null",
tableName.c_str(), tableName.c_str(), tableName.c_str()));
}
}
});
Expand Down

0 comments on commit 6b639a7

Please sign in to comment.