From 42410058a56ae56bc2ecf3716b8d1c30113e6e47 Mon Sep 17 00:00:00 2001 From: Jerome Gravel-Niquet Date: Thu, 21 Dec 2023 15:10:36 -0500 Subject: [PATCH] add tie-breaker test --- core/rs/core/src/c.rs | 2 +- core/rs/core/src/changes_vtab_write.rs | 2 +- core/rs/core/src/config.rs | 4 +- core/rs/core/src/lib.rs | 32 +++++++------- .../crsql_correctness.egg-info/SOURCES.txt | 3 ++ py/correctness/tests/test_sync.py | 42 +++++++++++++++++++ 6 files changed, 65 insertions(+), 20 deletions(-) diff --git a/core/rs/core/src/c.rs b/core/rs/core/src/c.rs index 9edab1b44..ffe556fe3 100644 --- a/core/rs/core/src/c.rs +++ b/core/rs/core/src/c.rs @@ -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: bool, + pub tieBreakSameColValue: ::core::ffi::c_int, } #[repr(C)] diff --git a/core/rs/core/src/changes_vtab_write.rs b/core/rs/core/src/changes_vtab_write.rs index c71b4329c..e4fca3d52 100644 --- a/core/rs/core/src/changes_vtab_write.rs +++ b/core/rs/core/src/changes_vtab_write.rs @@ -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 } { + if ret == 0 && unsafe { (*ext_data).tieBreakSameColValue == 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); diff --git a/core/rs/core/src/config.rs b/core/rs/core/src/config.rs index 9445b20ff..e8ee0bd32 100644 --- a/core/rs/core/src/config.rs +++ b/core/rs/core/src/config.rs @@ -14,8 +14,8 @@ pub extern "C" fn crsql_config_set( let name = args[0].text(); match name { - "always-declare-winner" => { - let value = args[1].int() == 1; + "merge-equal-values" => { + let value = args[1].int(); let ext_data = ctx.user_data() as *mut crsql_ExtData; unsafe { (*ext_data).tieBreakSameColValue = value }; } diff --git a/core/rs/core/src/lib.rs b/core/rs/core/src/lib.rs index 1e647e944..819ef2972 100644 --- a/core/rs/core/src/lib.rs +++ b/core/rs/core/src/lib.rs @@ -155,22 +155,6 @@ pub extern "C" fn sqlite3_crsqlcore_init( return null_mut(); } - let rc = db - .create_function_v2( - "crsql_config_set", - 2, - sqlite::UTF8, - None, - Some(crsql_config_set), - None, - None, - None, - ) - .unwrap_or(sqlite::ResultCode::ERROR); - if rc != ResultCode::OK { - return null_mut(); - } - let rc = unpack_columns_vtab::create_module(db).unwrap_or(sqlite::ResultCode::ERROR); if rc != ResultCode::OK { return null_mut(); @@ -454,6 +438,22 @@ pub extern "C" fn sqlite3_crsqlcore_init( return null_mut(); } + let rc = db + .create_function_v2( + "crsql_config_set", + 2, + sqlite::UTF8, + Some(ext_data as *mut c_void), + Some(crsql_config_set), + None, + None, + None, + ) + .unwrap_or(sqlite::ResultCode::ERROR); + if rc != ResultCode::OK { + return null_mut(); + } + return ext_data as *mut c_void; } diff --git a/py/correctness/src/crsql_correctness.egg-info/SOURCES.txt b/py/correctness/src/crsql_correctness.egg-info/SOURCES.txt index 42eedac20..b1ec0f626 100644 --- a/py/correctness/src/crsql_correctness.egg-info/SOURCES.txt +++ b/py/correctness/src/crsql_correctness.egg-info/SOURCES.txt @@ -7,9 +7,11 @@ src/crsql_correctness.egg-info/top_level.txt tests/test_as_ordered.py tests/test_cl_merging.py tests/test_cl_triggers.py +tests/test_commit_alter_perf.py tests/test_crsql_changes_filters.py tests/test_dbversion.py tests/test_insert_new_rows.py +tests/test_lookaside_key_creation.py tests/test_prior_versions.py tests/test_sandbox.py tests/test_schema_modification.py @@ -18,5 +20,6 @@ tests/test_seq.py tests/test_site_id_lookaside.py tests/test_siteid.py tests/test_sync.py +tests/test_sync_bit.py tests/test_sync_prop.py tests/test_update_rows.py \ No newline at end of file diff --git a/py/correctness/tests/test_sync.py b/py/correctness/tests/test_sync.py index c7f801ff4..d223aeae2 100644 --- a/py/correctness/tests/test_sync.py +++ b/py/correctness/tests/test_sync.py @@ -380,6 +380,48 @@ def make_dbs(): site_id = get_site_id(db2) assert (changes == [('foo', b'\x01\t\x01', 'b', 2, 1, 1, site_id, 1, 0)]) +def test_merge_same_w_tie_breaker(): + def make_dbs(): + db1 = create_basic_db() + db2 = create_basic_db() + + db1.execute("INSERT INTO foo (a,b) VALUES (1,2);") + db1.execute("SELECT crsql_config_set('merge-equal-values', 1);") + db1.commit() + + db2.execute("INSERT INTO foo (a,b) VALUES (1,2);") + db2.execute("SELECT crsql_config_set('merge-equal-values', 1);") + db2.commit() + return (db1, db2) + + (db1, db2) = make_dbs() + sync_left_to_right(db1, db2, 0) + changes = db2.execute("SELECT * FROM crsql_changes").fetchall() + + site_id1 = get_site_id(db1) + site_id2 = get_site_id(db2) + + max_site_id = max(site_id1, site_id2) + version = 1; + if max_site_id != site_id1: + version = 2 + + assert (changes == [('foo', b'\x01\t\x01', 'b', 2, 1, version, max_site_id, 1, 0)]) + + (db1, db2) = make_dbs() + sync_left_to_right(db2, db1, 0) + changes = db2.execute("SELECT * FROM crsql_changes").fetchall() + + site_id1 = get_site_id(db1) + site_id2 = get_site_id(db2) + + max_site_id = max(site_id1, site_id2) + version = 1; + if max_site_id != site_id2: + version = 2 + + assert (changes == [('foo', b'\x01\t\x01', 'b', 2, 1, version, max_site_id, 1, 0)]) + def test_merge_matching_clocks_lesser_value(): def make_dbs():