Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CBL-6534: Fix expiration column upgrade #2195

Merged
merged 4 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Binary file not shown.
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 @@ -341,18 +341,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
Loading