Skip to content

Commit

Permalink
libsql-sqlite3: Fix libsql_wal_frame_count() for empty databases
Browse files Browse the repository at this point in the history
If a database is empty, the pagerUseWal() returns false. In that case,
make libsql_wal_frame_count() return zero instead of a logic error.
  • Loading branch information
penberg committed Nov 26, 2024
1 parent 876a4a7 commit 98a5345
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
3 changes: 2 additions & 1 deletion libsql-ffi/bundled/SQLite3MultipleCiphers/src/sqlite3.c
Original file line number Diff line number Diff line change
Expand Up @@ -65291,7 +65291,8 @@ SQLITE_PRIVATE int sqlite3PagerWalFrameCount(Pager *pPager, unsigned int *pnFram
if( pagerUseWal(pPager) ){
return pPager->wal->methods.xFrameCount(pPager->wal->pData, 0, pnFrames);
}else{
return SQLITE_ERROR;
*pnFrames = 0;
return SQLITE_OK;
}
}

Expand Down
3 changes: 2 additions & 1 deletion libsql-ffi/bundled/src/sqlite3.c
Original file line number Diff line number Diff line change
Expand Up @@ -65291,7 +65291,8 @@ SQLITE_PRIVATE int sqlite3PagerWalFrameCount(Pager *pPager, unsigned int *pnFram
if( pagerUseWal(pPager) ){
return pPager->wal->methods.xFrameCount(pPager->wal->pData, 0, pnFrames);
}else{
return SQLITE_ERROR;
*pnFrames = 0;
return SQLITE_OK;
}
}

Expand Down
3 changes: 2 additions & 1 deletion libsql-sqlite3/src/pager.c
Original file line number Diff line number Diff line change
Expand Up @@ -7780,7 +7780,8 @@ int sqlite3PagerWalFrameCount(Pager *pPager, unsigned int *pnFrames){
if( pagerUseWal(pPager) ){
return pPager->wal->methods.xFrameCount(pPager->wal->pData, 0, pnFrames);
}else{
return SQLITE_ERROR;
*pnFrames = 0;
return SQLITE_OK;
}
}

Expand Down
12 changes: 12 additions & 0 deletions libsql-sqlite3/src/test_walapi.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <sqlite3.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>

#if 0
static void dump_frame(unsigned char *frame, size_t size){
Expand Down Expand Up @@ -127,6 +128,7 @@ int open_db(const char *path, sqlite3 **db) {
int main(int argc, char *argv[])
{
sqlite3 *db_primary, *db_backup;
unsigned int max_frame;
int rc;

rc = open_db("primary.db", &db_primary);
Expand All @@ -136,6 +138,16 @@ int main(int argc, char *argv[])
gen_data(db_primary);

rc = open_db("backup.db", &db_backup);
if (rc != SQLITE_OK) {
return 1;
}
rc = libsql_wal_frame_count(db_backup, &max_frame);
if (rc != SQLITE_OK) {
fprintf(stderr, "Can't get frame count for backup: %s\n", sqlite3_errmsg(db_backup));
return 1;
}
assert(max_frame == 0);

sync_db(db_primary, db_backup);

if (cmp_data(db_primary, db_backup)) {
Expand Down

0 comments on commit 98a5345

Please sign in to comment.