Skip to content

Commit

Permalink
CBL-6534: Fix expiration column upgrade (#2195) (#2219)
Browse files Browse the repository at this point in the history
* CBL-6534: Fix expiration column upgrade (#2195)

Co-authored-by: jianminzhao <[email protected]>
  • Loading branch information
callumbirks and jianminzhao authored Jan 30, 2025
1 parent 2e88f00 commit fcad0a2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
18 changes: 18 additions & 0 deletions C/tests/c4DatabaseTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1369,6 +1369,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.
13 changes: 6 additions & 7 deletions LiteCore/Storage/SQLiteDataFile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -368,18 +368,17 @@ namespace litecore {
(void)upgradeSchema(SchemaVersion::WithExpirationColumn, "Adding `expiration` column", [&] {
// Add the 'expiration' column to every KeyStore:
for ( string& name : allKeyStoreNames() ) {
// Only update data tables, not FTS index tables
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(format(
"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()));
_exec(format("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 fcad0a2

Please sign in to comment.