Skip to content

Commit

Permalink
new fract index test that tests without list columns
Browse files Browse the repository at this point in the history
  • Loading branch information
tantaman committed Sep 11, 2023
1 parent 1394527 commit d7de7d1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
15 changes: 10 additions & 5 deletions core/rs/fractindex-core/src/fractindex_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,17 +270,22 @@ pub fn fix_conflict_return_old_key(
"UPDATE \"{table}\" SET \"{order_col}\" = crsql_fract_key_between(
(
SELECT \"{order_col}\" FROM \"{table}\"
JOIN (SELECT {list_columns} FROM \"{table}\" WHERE {pk_predicates}) as t
ON {list_join_predicates} WHERE \"{order_col}\" < ?{target_order_slot} ORDER BY \"{order_col}\" DESC LIMIT 1
{maybe_join} WHERE \"{order_col}\" < ?{target_order_slot} ORDER BY \"{order_col}\" DESC LIMIT 1
),
?{target_order_slot}
) WHERE {pk_predicates} RETURNING \"{order_col}\"",
table = escape_ident(table),
order_col = escape_ident(order_col.text()),
pk_predicates = pk_predicates,
list_join_predicates = list_join_predicates,
list_columns = list_columns,
target_order_slot = pk_values.len() + 1
target_order_slot = pk_values.len() + 1,
maybe_join = if list_columns.len() > 0 {
format!(
"JOIN (SELECT {list_columns} FROM \"{table}\" WHERE {pk_predicates}) as t
ON {list_join_predicates}",
list_columns = list_columns, pk_predicates = pk_predicates, table = escape_ident(table), list_join_predicates = list_join_predicates)
} else {
format!("")
}
);

let stmt = db.prepare_v2(&sql)?;
Expand Down
19 changes: 19 additions & 0 deletions core/rs/integration-check/tests/fract.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use sqlite::{Connection, ResultCode};
use sqlite_nostd as sqlite;

#[test]
fn sort_no_list_col() {
let w = integration_utils::opendb().expect("db opened");
let db = &w.db;

db.exec_safe("CREATE TABLE todo (id primary key, position)")
.expect("table created");
db.exec_safe("SELECT crsql_fract_as_ordered('todo', 'position')")
.expect("as ordered");
db.exec_safe(
"INSERT INTO todo VALUES (1, 'Zm'), (2, 'ZmG'), (3, 'ZmG'), (4, 'ZmV'), (5, 'Zn')",
)
.expect("inserted initial values");
db.exec_safe("UPDATE todo_fractindex SET after_id = 2 WHERE id = 5")
.expect("repositioned id 5");
}

0 comments on commit d7de7d1

Please sign in to comment.