(*--------------------------------------------------------------------------- Copyright (c) 2026 Thomas Gazagnaire. All rights reserved. SPDX-License-Identifier: ISC ---------------------------------------------------------------------------*) (* Tests for {!Sqlite.Private.Transfer}: which INSERT INTO t SELECT * FROM s copy records whole, as sqlite3's transfer does (insert.c, xferOptimization), and which decode the rows and insert them again. The two differ where the target has an index: a transfer keeps the rowids of the source, 5 and 10 here, and an insert of decoded rows numbers them from the target's next rowid. Each case is a condition of sqlite3's, driven through the SQL door, and its expected rowids are sqlite3 3.54's for the same statements. *) let cases = [ ( "one shape: the records keep their rowids", [ "CREATE TABLE s (a TEXT, b INTEGER DEFAULT 1)"; "CREATE INDEX s_a ON s (a)"; "INSERT INTO s (rowid, a, b) VALUES (5, 'x', 1), (10, 'y', 2)"; "CREATE TABLE t (a TEXT, b INTEGER DEFAULT 1)"; "CREATE INDEX t_a ON t (a)"; ], "INSERT INTO t SELECT * FROM s", [ 5L; 10L ] ); ( "a column's affinity differs", [ "CREATE TABLE s (a TEXT, b INTEGER DEFAULT 1)"; "CREATE INDEX s_a ON s (a)"; "INSERT INTO s (rowid, a, b) VALUES (5, 'x', 1), (10, 'y', 2)"; "CREATE TABLE t (a INTEGER, b INTEGER DEFAULT 1)"; "CREATE INDEX t_a ON t (a)"; ], "INSERT INTO t SELECT * FROM s", [ 1L; 2L ] ); ( "a column's collating sequence differs", [ "CREATE TABLE s (a TEXT, b INTEGER DEFAULT 1)"; "CREATE INDEX s_a ON s (a)"; "INSERT INTO s (rowid, a, b) VALUES (5, 'x', 1), (10, 'y', 2)"; "CREATE TABLE t (a TEXT COLLATE NOCASE, b INTEGER DEFAULT 1)"; "CREATE INDEX t_a ON t (a)"; ], "INSERT INTO t SELECT * FROM s", [ 1L; 2L ] ); ( "NOT NULL in the target alone", [ "CREATE TABLE s (a TEXT, b INTEGER DEFAULT 1)"; "CREATE INDEX s_a ON s (a)"; "INSERT INTO s (rowid, a, b) VALUES (5, 'x', 1), (10, 'y', 2)"; "CREATE TABLE t (a TEXT NOT NULL, b INTEGER DEFAULT 1)"; "CREATE INDEX t_a ON t (a)"; ], "INSERT INTO t SELECT * FROM s", [ 1L; 2L ] ); ( "NOT NULL in the source alone still transfers", [ "CREATE TABLE s (a TEXT NOT NULL, b INTEGER DEFAULT 1)"; "CREATE INDEX s_a ON s (a)"; "INSERT INTO s (rowid, a, b) VALUES (5, 'x', 1), (10, 'y', 2)"; "CREATE TABLE t (a TEXT, b INTEGER DEFAULT 1)"; "CREATE INDEX t_a ON t (a)"; ], "INSERT INTO t SELECT * FROM s", [ 5L; 10L ] ); ( "a DEFAULT past the first column reads +1 against 1", [ "CREATE TABLE s (a TEXT, b INTEGER DEFAULT 1)"; "CREATE INDEX s_a ON s (a)"; "INSERT INTO s (rowid, a, b) VALUES (5, 'x', 1), (10, 'y', 2)"; "CREATE TABLE t (a TEXT, b INTEGER DEFAULT +1)"; "CREATE INDEX t_a ON t (a)"; ], "INSERT INTO t SELECT * FROM s", [ 1L; 2L ] ); ( "DEFAULT (1) keeps the text 1, which DEFAULT 1 has", [ "CREATE TABLE s (a TEXT, b INTEGER DEFAULT 1)"; "CREATE INDEX s_a ON s (a)"; "INSERT INTO s (rowid, a, b) VALUES (5, 'x', 1), (10, 'y', 2)"; "CREATE TABLE t (a TEXT, b INTEGER DEFAULT (1))"; "CREATE INDEX t_a ON t (a)"; ], "INSERT INTO t SELECT * FROM s", [ 5L; 10L ] ); ( "the first column's DEFAULT is not compared", [ "CREATE TABLE s (a TEXT, b INTEGER DEFAULT 1)"; "CREATE INDEX s_a ON s (a)"; "INSERT INTO s (rowid, a, b) VALUES (5, 'x', 1), (10, 'y', 2)"; "CREATE TABLE t (a TEXT DEFAULT 'z', b INTEGER DEFAULT 1)"; "CREATE INDEX t_a ON t (a)"; ], "INSERT INTO t SELECT * FROM s", [ 5L; 10L ] ); ( "the target's index descends where the source's ascends", [ "CREATE TABLE s (a TEXT, b INTEGER DEFAULT 1)"; "CREATE INDEX s_a ON s (a)"; "INSERT INTO s (rowid, a, b) VALUES (5, 'x', 1), (10, 'y', 2)"; "CREATE TABLE t (a TEXT, b INTEGER DEFAULT 1)"; "CREATE INDEX t_a ON t (a DESC)"; ], "INSERT INTO t SELECT * FROM s", [ 1L; 2L ] ); ( "a UNIQUE index transfers into an empty target", [ "CREATE TABLE s (a TEXT, b INTEGER DEFAULT 1)"; "CREATE UNIQUE INDEX s_a ON s (a)"; "INSERT INTO s (rowid, a, b) VALUES (5, 'x', 1), (10, 'y', 2)"; "CREATE TABLE t (a TEXT, b INTEGER DEFAULT 1)"; "CREATE UNIQUE INDEX t_a ON t (a)"; ], "INSERT INTO t SELECT * FROM s", [ 5L; 10L ] ); ( "a UNIQUE index rules out a target holding a row", [ "CREATE TABLE s (a TEXT, b INTEGER DEFAULT 1)"; "CREATE UNIQUE INDEX s_a ON s (a)"; "INSERT INTO s (rowid, a, b) VALUES (5, 'x', 1), (10, 'y', 2)"; "CREATE TABLE t (a TEXT, b INTEGER DEFAULT 1)"; "CREATE UNIQUE INDEX t_a ON t (a)"; "INSERT INTO t (rowid, a) VALUES (2, 'w')"; ], "INSERT INTO t SELECT * FROM s", [ 2L; 3L; 4L ] ); ( "an INSERT trigger on the target", [ "CREATE TABLE s (a TEXT, b INTEGER DEFAULT 1)"; "CREATE INDEX s_a ON s (a)"; "INSERT INTO s (rowid, a, b) VALUES (5, 'x', 1), (10, 'y', 2)"; "CREATE TABLE t (a TEXT, b INTEGER DEFAULT 1)"; "CREATE INDEX t_a ON t (a)"; "CREATE TABLE log (x)"; "CREATE TRIGGER tr AFTER INSERT ON t BEGIN INSERT INTO log VALUES (1); \ END"; ], "INSERT INTO t SELECT * FROM s", [ 1L; 2L ] ); ( "a CHECK of the target the source lacks", [ "CREATE TABLE s (a TEXT, b INTEGER DEFAULT 1)"; "CREATE INDEX s_a ON s (a)"; "INSERT INTO s (rowid, a, b) VALUES (5, 'x', 1), (10, 'y', 2)"; "CREATE TABLE t (a TEXT, b INTEGER DEFAULT 1, CHECK (b > 0))"; "CREATE INDEX t_a ON t (a)"; ], "INSERT INTO t SELECT * FROM s", [ 1L; 2L ] ); (* insert.c, xferCompatibleCheck: with no INTEGER PRIMARY KEY in the target, no CHECK may read the rowid, whether or not the copy would keep the rowids. *) ( "a CHECK reads the rowid, with no INTEGER PRIMARY KEY", [ "CREATE TABLE s (a TEXT, b INTEGER DEFAULT 1, CHECK (rowid > 0))"; "CREATE INDEX s_a ON s (a)"; "INSERT INTO s (rowid, a, b) VALUES (5, 'x', 1), (10, 'y', 2)"; "CREATE TABLE t (a TEXT, b INTEGER DEFAULT 1, CHECK (rowid > 0))"; "CREATE INDEX t_a ON t (a)"; ], "INSERT INTO t SELECT * FROM s", [ 1L; 2L ] ); ( "a CHECK reads the rowid, each renumbered row checked", [ "CREATE TABLE s (c0 INTEGER DEFAULT 1, CHECK (rowid <> 3))"; "CREATE UNIQUE INDEX s_i0 ON s (c0) WHERE c0 IS NOT NULL"; "INSERT INTO s (rowid, c0) VALUES (1, 1), (18, 2), (40, 3), (41, 4)"; "CREATE TABLE t (c0 INTEGER DEFAULT 1, CHECK (rowid <> 3))"; "CREATE UNIQUE INDEX t_i0 ON t (c0) WHERE c0 IS NOT NULL"; ], "INSERT OR IGNORE INTO t SELECT * FROM s", [ 1L; 2L ] ); ( "a CHECK reads the rowid an INTEGER PRIMARY KEY holds", [ "CREATE TABLE s (k INTEGER PRIMARY KEY, a TEXT, CHECK (rowid > 1))"; "CREATE INDEX s_a ON s (a)"; "INSERT INTO s VALUES (5, 'x'), (10, 'y')"; "CREATE TABLE t (k INTEGER PRIMARY KEY, a TEXT, CHECK (rowid > 1))"; "CREATE INDEX t_a ON t (a)"; ], "INSERT INTO t SELECT * FROM s", [ 5L; 10L ] ); ( "OR IGNORE transfers into an empty target", [ "CREATE TABLE s (a TEXT, b INTEGER DEFAULT 1)"; "CREATE INDEX s_a ON s (a)"; "INSERT INTO s (rowid, a, b) VALUES (5, 'x', 1), (10, 'y', 2)"; "CREATE TABLE t (a TEXT, b INTEGER DEFAULT 1)"; "CREATE INDEX t_a ON t (a)"; ], "INSERT OR IGNORE INTO t SELECT * FROM s", [ 5L; 10L ] ); ( "a foreign key of the target, enforced", [ "CREATE TABLE s (a TEXT, b INTEGER DEFAULT 1)"; "CREATE INDEX s_a ON s (a)"; "INSERT INTO s (rowid, a, b) VALUES (5, 'x', 1), (10, 'y', 2)"; "CREATE TABLE p (a TEXT PRIMARY KEY)"; "INSERT INTO p VALUES ('x'), ('y')"; "CREATE TABLE t (a TEXT REFERENCES p (a), b INTEGER DEFAULT 1)"; "CREATE INDEX t_a ON t (a)"; "PRAGMA foreign_keys = ON"; ], "INSERT INTO t SELECT * FROM s", [ 1L; 2L ] ); ( "a foreign key not enforced", [ "CREATE TABLE s (a TEXT, b INTEGER DEFAULT 1)"; "CREATE INDEX s_a ON s (a)"; "INSERT INTO s (rowid, a, b) VALUES (5, 'x', 1), (10, 'y', 2)"; "CREATE TABLE p (a TEXT PRIMARY KEY)"; "INSERT INTO p VALUES ('x'), ('y')"; "CREATE TABLE t (a TEXT REFERENCES p (a), b INTEGER DEFAULT 1)"; "CREATE INDEX t_a ON t (a)"; ], "INSERT INTO t SELECT * FROM s", [ 5L; 10L ] ); ] let rowids db = List.map (function [ Sqlite.Int r ] -> r | _ -> Alcotest.fail "rowid") (Sqlite.Raw.query db "SELECT rowid FROM t ORDER BY rowid" ~params:[]) let test_case (why, setup, stmt, expected) = Alcotest.test_case why `Quick (fun () -> let db = Sqlite.in_memory () in List.iter (fun sql -> Sqlite.Raw.exec db sql ~params:[]) setup; Sqlite.Raw.exec db stmt ~params:[]; Alcotest.(check (list int64)) why expected (rowids db)) (* A CHECK reading the rowid of a target with no INTEGER PRIMARY KEY runs on each row the insert renumbers: the third row takes rowid 3 and is refused, and the statement with it, as sqlite3 3.54 refuses it ("CHECK constraint failed: rowid <> 3", t empty). A copy keeping the source's rowids stores the four rows. *) let test_check_reading_rowid_refuses () = let db = Sqlite.in_memory () in List.iter (fun sql -> Sqlite.Raw.exec db sql ~params:[]) [ "CREATE TABLE s (c0 INTEGER DEFAULT 1, CHECK (rowid <> 3))"; "CREATE UNIQUE INDEX s_i0 ON s (c0) WHERE c0 IS NOT NULL"; "INSERT INTO s (rowid, c0) VALUES (1, 1), (18, 2), (40, 3), (41, 4)"; "CREATE TABLE t (c0 INTEGER DEFAULT 1, CHECK (rowid <> 3))"; "CREATE UNIQUE INDEX t_i0 ON t (c0) WHERE c0 IS NOT NULL"; ]; let refusal = match Sqlite.Raw.exec db "INSERT OR ABORT INTO t SELECT * FROM s" ~params:[] with | () -> None | exception Sqlite.Error (Constraint _ as m_e) -> let m = Fmt.str "%a" Sqlite.Error.pp m_e in Some m in Alcotest.(check (option string)) "the third row is refused" (Some "CHECK constraint failed: rowid <> 3") refusal; Alcotest.(check (list int64)) "and the statement with it" [] (rowids db) (* A table [sql] declares, empty, read back as a stored one is. *) let table name sql = let pg = Btree.Pager.mem ~page_size:4096 () in let p1 = Btree.Pager.allocate pg in ignore (Btree.Table.v ~root_page:p1 ~header_offset:100 pg); let bt = Btree.Table.v pg in Sqlite.Private.Reconnect.open_table pg ~fresh_rowid:(fun () -> 9L) ~raw_indexes:[] ~raw_explicit:[] (1L, name, Btree.Table.root_page bt, sql) (* The transfer reads as what it does: its source, the rowids kept and checked, the resolution of a violation and the indexes copied. The INTEGER PRIMARY KEY's own ON CONFLICT is the resolution where the statement names none. *) let test_pp () = let sql name = Fmt.str "CREATE TABLE %s (k INTEGER PRIMARY KEY ON CONFLICT ROLLBACK, a)" name in match Sqlite.Private.Transfer.plan ~fk_enforce:false ~triggers:false ~conflict:Sql.Ast.Unspecified ~dst:(table "t" (sql "t")) (table "s" (sql "s")) with | None -> Alcotest.fail "no transfer between tables of one shape" | Some x -> Alcotest.(check string) "the transfer" "transfer from s, rowids kept and checked, ROLLBACK, 0 indexes" (Fmt.to_to_string Sqlite.Private.Transfer.pp x) let suite = ( "transfer", List.map test_case cases @ [ Alcotest.test_case "a CHECK reading the rowid refuses a row" `Quick test_check_reading_rowid_refuses; Alcotest.test_case "pp" `Quick test_pp; ] )