Skip to content

Commit

Permalink
libsql-sqlite3: Unify database opening in test_walapi.c
Browse files Browse the repository at this point in the history
  • Loading branch information
penberg committed Nov 26, 2024
1 parent 36b37ac commit c9217df
Showing 1 changed file with 24 additions and 25 deletions.
49 changes: 24 additions & 25 deletions libsql-sqlite3/src/test_walapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,42 +103,41 @@ static void gen_data(sqlite3 *db){
sqlite3_exec(db, "INSERT INTO users (id, name) VALUES (3, 'Jim Beam')", 0, 0, 0);
}

int open_db(const char *path, sqlite3 **db) {
int rc;

rc = sqlite3_open(path, db);
if (rc != SQLITE_OK) {
fprintf(stderr, "Can't open database %s: %s\n", path, sqlite3_errmsg(*db));
return rc;
}
rc = sqlite3_exec(*db, "PRAGMA journal_mode=WAL", NULL, NULL, NULL);
if (rc != SQLITE_OK) {
fprintf(stderr, "Can't set journal mode for %s: %s\n", path, sqlite3_errmsg(*db));
return rc;
}
rc = sqlite3_wal_autocheckpoint(*db, 0);
if (rc != SQLITE_OK) {
fprintf(stderr, "Can't disable checkpointing for %s: %s\n", path, sqlite3_errmsg(*db));
return rc;
}
return rc;
}

int main(int argc, char *argv[])
{
sqlite3 *db_primary, *db_backup;
int rc;

rc = sqlite3_open("primary.db", &db_primary);
rc = open_db("primary.db", &db_primary);
if (rc != SQLITE_OK) {
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db_primary));
return 1;
}
rc = sqlite3_wal_autocheckpoint(db_primary, 0);
if (rc != SQLITE_OK) {
fprintf(stderr, "Can't disable checkpointing: %s\n", sqlite3_errmsg(db_primary));
return 1;
}
sqlite3_exec(db_primary, "PRAGMA journal_mode=WAL", NULL, NULL, NULL);

gen_data(db_primary);

rc = sqlite3_open("backup.db", &db_backup);
if (rc != SQLITE_OK) {
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db_backup));
return 1;
}
rc = sqlite3_wal_autocheckpoint(db_backup, 0);
if (rc != SQLITE_OK) {
fprintf(stderr, "Can't disable checkpointing: %s\n", sqlite3_errmsg(db_backup));
return 1;
}
rc = sqlite3_exec(db_backup, "PRAGMA journal_mode=WAL", NULL, NULL, NULL);
if (rc != SQLITE_OK) {
fprintf(stderr, "Can't set journal mode: %s\n", sqlite3_errmsg(db_backup));
return 1;
}

rc = open_db("backup.db", &db_backup);
sync_db(db_primary, db_backup);

if (cmp_data(db_primary, db_backup)) {
return 1;
}
Expand Down

0 comments on commit c9217df

Please sign in to comment.