Skip to content

Commit

Permalink
fix config not being loaded properly on init
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromegn committed Dec 26, 2023
1 parent 3a01980 commit bdc0710
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 24 deletions.
4 changes: 2 additions & 2 deletions core/rs/bundle/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions core/rs/core/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions core/rs/core/src/c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub struct crsql_ExtData {
pub pSetSiteIdOrdinalStmt: *mut sqlite::stmt,
pub pSelectSiteIdOrdinalStmt: *mut sqlite::stmt,
pub pSelectClockTablesStmt: *mut sqlite::stmt,
pub tieBreakSameColValue: ::core::ffi::c_int,
pub mergeEqualValues: ::core::ffi::c_int,
}

#[repr(C)]
Expand Down Expand Up @@ -454,13 +454,13 @@ fn bindgen_test_layout_crsql_ExtData() {
)
);
assert_eq!(
unsafe { ::std::ptr::addr_of!((*ptr).tieBreakSameColValue) as usize - ptr as usize },
unsafe { ::std::ptr::addr_of!((*ptr).mergeEqualValues) as usize - ptr as usize },
128usize,
concat!(
"Offset of field: ",
stringify!(crsql_ExtData),
"::",
stringify!(tieBreakSameColValue)
stringify!(mergeEqualValues)
)
);
}
2 changes: 1 addition & 1 deletion core/rs/core/src/changes_vtab_write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ fn did_cid_win(
let local_value = col_val_stmt.column_value(0)?;
let mut ret = crsql_compare_sqlite_values(insert_val, local_value);
reset_cached_stmt(col_val_stmt.stmt)?;
if ret == 0 && unsafe { (*ext_data).tieBreakSameColValue == 1 } {
if ret == 0 && unsafe { (*ext_data).mergeEqualValues == 1 } {
// values are the same (ret == 0) and the option to tie break on site_id is true
ret = unsafe {
let my_site_id = core::slice::from_raw_parts((*ext_data).siteId, 16);
Expand Down
4 changes: 2 additions & 2 deletions core/rs/core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub extern "C" fn crsql_config_set(
MERGE_EQUAL_VALUES => {
let value = args[1];
let ext_data = ctx.user_data() as *mut crsql_ExtData;
unsafe { (*ext_data).tieBreakSameColValue = value.int() };
unsafe { (*ext_data).mergeEqualValues = value.int() };
value
}
_ => {
Expand Down Expand Up @@ -74,7 +74,7 @@ pub extern "C" fn crsql_config_get(
match name {
MERGE_EQUAL_VALUES => {
let ext_data = ctx.user_data() as *mut crsql_ExtData;
ctx.result_int(unsafe { (*ext_data).tieBreakSameColValue });
ctx.result_int(unsafe { (*ext_data).mergeEqualValues });
}
_ => {
ctx.result_error("Unknown setting name");
Expand Down
4 changes: 3 additions & 1 deletion core/rs/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ pub extern "C" fn sqlite3_crsqlcore_init(

let ext_data = unsafe { crsql_newExtData(db, site_id_buffer as *mut c_char) };
if ext_data.is_null() {
sqlite::free(site_id_buffer as *mut c_void);
// no need to free the site id buffer here, this is cleaned up already.
return null_mut();
}

Expand Down Expand Up @@ -486,6 +486,7 @@ pub extern "C" fn sqlite3_crsqlcore_init(
)
.unwrap_or(sqlite::ResultCode::ERROR);
if rc != ResultCode::OK {
unsafe { crsql_freeExtData(ext_data) };
return null_mut();
}

Expand All @@ -502,6 +503,7 @@ pub extern "C" fn sqlite3_crsqlcore_init(
)
.unwrap_or(sqlite::ResultCode::ERROR);
if rc != ResultCode::OK {
unsafe { crsql_freeExtData(ext_data) };
return null_mut();
}

Expand Down
2 changes: 1 addition & 1 deletion core/rs/core/src/sha.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// The sha of the commit that this version of crsqlite was built from.
pub const SHA: &'static str = "";
pub const SHA: &'static str = "3a01980562615001d765eec61e3ed58147a93f93";
18 changes: 11 additions & 7 deletions core/src/ext-data.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "ext-data.h"

#include <stdio.h>
#include <string.h>

#include "consts.h"

Expand All @@ -11,6 +12,8 @@ void crsql_drop_table_info_vec(crsql_ExtData *pExtData);
crsql_ExtData *crsql_newExtData(sqlite3 *db, unsigned char *siteIdBuffer) {
crsql_ExtData *pExtData = sqlite3_malloc(sizeof *pExtData);

pExtData->siteId = siteIdBuffer;

pExtData->pPragmaSchemaVersionStmt = 0;
int rc = sqlite3_prepare_v3(db, "PRAGMA schema_version", -1,
SQLITE_PREPARE_PERSISTENT,
Expand Down Expand Up @@ -47,7 +50,6 @@ crsql_ExtData *crsql_newExtData(sqlite3 *db, unsigned char *siteIdBuffer) {
pExtData->pragmaSchemaVersion = -1;
pExtData->pragmaDataVersion = -1;
pExtData->pragmaSchemaVersionForTableInfos = -1;
pExtData->siteId = siteIdBuffer;
pExtData->pDbVersionStmt = 0;
pExtData->tableInfos = 0;
pExtData->rowsImpacted = 0;
Expand All @@ -67,14 +69,16 @@ crsql_ExtData *crsql_newExtData(sqlite3 *db, unsigned char *siteIdBuffer) {
}

// set defaults!
pExtData->tieBreakSameColValue = 0;
pExtData->mergeEqualValues = 0;

while (sqlite3_step(pStmt) == SQLITE_ROW) {
const unsigned char *name = sqlite3_column_text(pStmt, 1);
if (sqlite3_stricmp(name, "merge-equal-values")) {
if (sqlite3_column_type(pStmt, 2) == SQLITE_INTEGER) {
const int value = sqlite3_column_int(pStmt, 2);
pExtData->tieBreakSameColValue = value;
const unsigned char *name = sqlite3_column_text(pStmt, 0);
int colType = sqlite3_column_type(pStmt, 1);

if (strcmp("merge-equal-values", (char *)name) == 0) {
if (colType == SQLITE_INTEGER) {
const int value = sqlite3_column_int(pStmt, 1);
pExtData->mergeEqualValues = value;
} else {
// broken setting...
crsql_freeExtData(pExtData);
Expand Down
2 changes: 1 addition & 1 deletion core/src/ext-data.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ struct crsql_ExtData {
sqlite3_stmt *pSelectSiteIdOrdinalStmt;
sqlite3_stmt *pSelectClockTablesStmt;

int tieBreakSameColValue;
int mergeEqualValues;
};

crsql_ExtData *crsql_newExtData(sqlite3 *db, unsigned char *siteIdBuffer);
Expand Down
19 changes: 15 additions & 4 deletions py/correctness/tests/test_config.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from crsql_correctness import connect, close, get_site_id
import pprint
from crsql_correctness import connect, close
import pathlib

def test_config_merge_equal_values():
db = connect(":memory:")
dbfile = "./config.db"
pathlib.Path(dbfile).unlink(missing_ok=True)
db = connect(dbfile)
value = db.execute("SELECT crsql_config_set('merge-equal-values', 1);").fetchone()
assert (value == (1,))
db.commit()
Expand All @@ -11,4 +13,13 @@ def test_config_merge_equal_values():
assert (value == (1,))

value = db.execute("SELECT crsql_config_get('merge-equal-values');").fetchone()
assert (value == (1,))
assert (value == (1,))

close(db)
db = connect(dbfile)

value = db.execute("SELECT value FROM crsql_master WHERE key = 'config.merge-equal-values'").fetchone()
assert (value == (1,))

value = db.execute("SELECT crsql_config_get('merge-equal-values')").fetchone()
assert (value == (1,))

0 comments on commit bdc0710

Please sign in to comment.