stoolap 0.4.0

High-performance embedded SQL database with MVCC, time-travel queries, and full ACID compliance
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
// Copyright 2025 Stoolap Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Transaction Tests
//!
//! Tests for transaction functionality: BEGIN, COMMIT, ROLLBACK

use stoolap::Database;

#[test]
fn test_basic_commit() {
    let db = Database::open("memory://txn_basic_commit").expect("Failed to create database");

    // Create table
    db.execute(
        "CREATE TABLE txn_test (id INTEGER PRIMARY KEY, value TEXT)",
        (),
    )
    .expect("Failed to create table");

    // Insert data and commit (implicit transaction)
    db.execute(
        "INSERT INTO txn_test (id, value) VALUES (1, 'test value')",
        (),
    )
    .expect("Failed to insert");

    // Verify data is visible
    let value: String = db
        .query_one("SELECT value FROM txn_test WHERE id = 1", ())
        .expect("Failed to query");
    assert_eq!(value, "test value");
}

#[test]
fn test_explicit_transaction_commit() {
    let db = Database::open("memory://txn_explicit_commit").expect("Failed to create database");

    db.execute(
        "CREATE TABLE txn_test (id INTEGER PRIMARY KEY, value TEXT)",
        (),
    )
    .expect("Failed to create table");

    // Start explicit transaction
    db.execute("BEGIN", ())
        .expect("Failed to begin transaction");

    db.execute("INSERT INTO txn_test (id, value) VALUES (1, 'first')", ())
        .expect("Failed to insert first");
    db.execute("INSERT INTO txn_test (id, value) VALUES (2, 'second')", ())
        .expect("Failed to insert second");

    // Commit
    db.execute("COMMIT", ()).expect("Failed to commit");

    // Verify both rows exist
    let count: i64 = db
        .query_one("SELECT COUNT(*) FROM txn_test", ())
        .expect("Failed to count");
    assert_eq!(count, 2);
}

/// Rollback insert test
#[test]
fn test_explicit_transaction_rollback() {
    let db = Database::open("memory://txn_explicit_rollback").expect("Failed to create database");

    db.execute(
        "CREATE TABLE txn_test (id INTEGER PRIMARY KEY, value TEXT)",
        (),
    )
    .expect("Failed to create table");

    // Insert some initial data
    db.execute("INSERT INTO txn_test (id, value) VALUES (1, 'initial')", ())
        .expect("Failed to insert initial");

    // Start transaction and insert more
    db.execute("BEGIN", ())
        .expect("Failed to begin transaction");
    db.execute(
        "INSERT INTO txn_test (id, value) VALUES (2, 'should rollback')",
        (),
    )
    .expect("Failed to insert in transaction");

    // Rollback
    db.execute("ROLLBACK", ()).expect("Failed to rollback");

    // Only initial row should exist
    let count: i64 = db
        .query_one("SELECT COUNT(*) FROM txn_test", ())
        .expect("Failed to count");
    assert_eq!(count, 1, "After rollback, only initial row should exist");

    let value: String = db
        .query_one("SELECT value FROM txn_test WHERE id = 1", ())
        .expect("Failed to query");
    assert_eq!(value, "initial");
}

#[test]
fn test_update_in_transaction() {
    let db = Database::open("memory://txn_update").expect("Failed to create database");

    db.execute(
        "CREATE TABLE txn_test (id INTEGER PRIMARY KEY, value TEXT)",
        (),
    )
    .expect("Failed to create table");

    db.execute(
        "INSERT INTO txn_test (id, value) VALUES (1, 'original')",
        (),
    )
    .expect("Failed to insert");

    // Begin transaction and update
    db.execute("BEGIN", ()).expect("Failed to begin");
    db.execute("UPDATE txn_test SET value = 'modified' WHERE id = 1", ())
        .expect("Failed to update");
    db.execute("COMMIT", ()).expect("Failed to commit");

    let value: String = db
        .query_one("SELECT value FROM txn_test WHERE id = 1", ())
        .expect("Failed to query");
    assert_eq!(value, "modified");
}

#[test]
fn test_delete_in_transaction() {
    let db = Database::open("memory://txn_delete").expect("Failed to create database");

    db.execute(
        "CREATE TABLE txn_test (id INTEGER PRIMARY KEY, value TEXT)",
        (),
    )
    .expect("Failed to create table");

    db.execute(
        "INSERT INTO txn_test (id, value) VALUES (1, 'to delete')",
        (),
    )
    .expect("Failed to insert");
    db.execute(
        "INSERT INTO txn_test (id, value) VALUES (2, 'keep this')",
        (),
    )
    .expect("Failed to insert");

    // Begin transaction and delete
    db.execute("BEGIN", ()).expect("Failed to begin");
    db.execute("DELETE FROM txn_test WHERE id = 1", ())
        .expect("Failed to delete");
    db.execute("COMMIT", ()).expect("Failed to commit");

    // Only row 2 should remain
    let count: i64 = db
        .query_one("SELECT COUNT(*) FROM txn_test", ())
        .expect("Failed to count");
    assert_eq!(count, 1);

    let value: String = db
        .query_one("SELECT value FROM txn_test WHERE id = 2", ())
        .expect("Failed to query");
    assert_eq!(value, "keep this");
}

#[test]
fn test_multiple_operations_in_transaction() {
    let db = Database::open("memory://txn_multi_ops").expect("Failed to create database");

    db.execute(
        "CREATE TABLE txn_test (id INTEGER PRIMARY KEY, value TEXT)",
        (),
    )
    .expect("Failed to create table");

    // Initial data
    db.execute("INSERT INTO txn_test (id, value) VALUES (1, 'a')", ())
        .expect("Failed to insert");
    db.execute("INSERT INTO txn_test (id, value) VALUES (2, 'b')", ())
        .expect("Failed to insert");

    // Transaction with multiple operations
    db.execute("BEGIN", ()).expect("Failed to begin");
    db.execute("INSERT INTO txn_test (id, value) VALUES (3, 'c')", ())
        .expect("Failed to insert");
    db.execute("UPDATE txn_test SET value = 'updated' WHERE id = 1", ())
        .expect("Failed to update");
    db.execute("DELETE FROM txn_test WHERE id = 2", ())
        .expect("Failed to delete");
    db.execute("COMMIT", ()).expect("Failed to commit");

    // Should have 2 rows: id=1 with 'updated', id=3 with 'c'
    let count: i64 = db
        .query_one("SELECT COUNT(*) FROM txn_test", ())
        .expect("Failed to count");
    assert_eq!(count, 2);

    let value1: String = db
        .query_one("SELECT value FROM txn_test WHERE id = 1", ())
        .expect("Failed to query");
    assert_eq!(value1, "updated");

    let value3: String = db
        .query_one("SELECT value FROM txn_test WHERE id = 3", ())
        .expect("Failed to query");
    assert_eq!(value3, "c");
}

/// Rollback update test
#[test]
fn test_rollback_update() {
    let db = Database::open("memory://txn_rollback_update").expect("Failed to create database");

    db.execute(
        "CREATE TABLE txn_test (id INTEGER PRIMARY KEY, value TEXT)",
        (),
    )
    .expect("Failed to create table");

    db.execute(
        "INSERT INTO txn_test (id, value) VALUES (1, 'original')",
        (),
    )
    .expect("Failed to insert");

    // Begin transaction, update, then rollback
    db.execute("BEGIN", ()).expect("Failed to begin");
    db.execute(
        "UPDATE txn_test SET value = 'should not persist' WHERE id = 1",
        (),
    )
    .expect("Failed to update");
    db.execute("ROLLBACK", ()).expect("Failed to rollback");

    // Value should still be original
    let value: String = db
        .query_one("SELECT value FROM txn_test WHERE id = 1", ())
        .expect("Failed to query");
    assert_eq!(
        value, "original",
        "Value should be unchanged after rollback"
    );
}

/// Rollback delete test
#[test]
fn test_rollback_delete() {
    let db = Database::open("memory://txn_rollback_delete").expect("Failed to create database");

    db.execute(
        "CREATE TABLE txn_test (id INTEGER PRIMARY KEY, value TEXT)",
        (),
    )
    .expect("Failed to create table");

    db.execute(
        "INSERT INTO txn_test (id, value) VALUES (1, 'should exist')",
        (),
    )
    .expect("Failed to insert");

    // Begin transaction, delete, then rollback
    db.execute("BEGIN", ()).expect("Failed to begin");
    db.execute("DELETE FROM txn_test WHERE id = 1", ())
        .expect("Failed to delete");
    db.execute("ROLLBACK", ()).expect("Failed to rollback");

    // Row should still exist
    let count: i64 = db
        .query_one("SELECT COUNT(*) FROM txn_test", ())
        .expect("Failed to count");
    assert_eq!(count, 1, "Row should still exist after rollback");
}

/// Nested BEGIN test - verifies that nested BEGIN is a no-op when a transaction is already active
#[test]
fn test_nested_begin_is_noop() {
    let db = Database::open("memory://txn_nested").expect("Failed to create database");

    db.execute(
        "CREATE TABLE txn_test (id INTEGER PRIMARY KEY, value TEXT)",
        (),
    )
    .unwrap();

    // Start first transaction
    db.execute("BEGIN", ()).expect("First BEGIN should succeed");

    // Insert a row
    db.execute("INSERT INTO txn_test (id, value) VALUES (1, 'first')", ())
        .unwrap();

    // Second BEGIN is a no-op (doesn't start a new nested transaction)
    db.execute("BEGIN", ())
        .expect("Nested BEGIN should succeed as no-op");

    // Insert another row (still in the first transaction)
    db.execute("INSERT INTO txn_test (id, value) VALUES (2, 'second')", ())
        .unwrap();

    // Rollback should undo both inserts (since nested BEGIN was a no-op)
    db.execute("ROLLBACK", ()).expect("ROLLBACK should succeed");

    // Both rows should be gone
    let count: i64 = db
        .query_one("SELECT COUNT(*) FROM txn_test", ())
        .expect("Failed to count");
    assert_eq!(
        count, 0,
        "Both rows should be rolled back since nested BEGIN was a no-op"
    );
}

#[test]
fn test_commit_without_begin() {
    let db = Database::open("memory://txn_commit_no_begin").expect("Failed to create database");

    db.execute("CREATE TABLE txn_test (id INTEGER PRIMARY KEY)", ())
        .unwrap();

    // COMMIT without BEGIN should either succeed (auto-commit) or fail gracefully
    // This tests the behavior, whatever it is
    let result = db.execute("COMMIT", ());
    // Don't assert on success/failure - just ensure it doesn't panic
    let _ = result;
}

#[test]
fn test_transaction_insert_partial_columns() {
    // Regression test: transaction INSERT with partial column list (omitting AUTO_INCREMENT)
    // Previously failed because the transaction path validated param count against total
    // table columns instead of the columns specified in the INSERT statement.
    let db = Database::open("memory://txn_partial_cols").expect("Failed to create database");

    db.execute(
        "CREATE TABLE customers (
            id INTEGER PRIMARY KEY AUTO_INCREMENT,
            name TEXT NOT NULL,
            email TEXT NOT NULL,
            country TEXT NOT NULL
        )",
        (),
    )
    .expect("Failed to create table");

    // Non-transaction insert with partial columns (omitting id) - should work
    db.execute(
        "INSERT INTO customers (name, email, country) VALUES ('Alice', 'alice@example.com', 'US')",
        (),
    )
    .expect("Non-transaction partial column insert should work");

    // Transaction insert with partial columns (omitting id) - this was the bug
    db.execute("BEGIN", ()).expect("Failed to begin");
    db.execute(
        "INSERT INTO customers (name, email, country) VALUES ('Bob', 'bob@example.com', 'UK')",
        (),
    )
    .expect("Transaction partial column insert should work");
    db.execute(
        "INSERT INTO customers (name, email, country) VALUES ('Clara', 'clara@example.com', 'DE')",
        (),
    )
    .expect("Second transaction insert should work");
    db.execute("COMMIT", ()).expect("Failed to commit");

    // Verify all 3 rows exist with auto-generated IDs
    let count: i64 = db
        .query_one("SELECT COUNT(*) FROM customers", ())
        .expect("Failed to count");
    assert_eq!(count, 3, "All 3 rows should be inserted");

    // Verify auto-increment IDs are sequential
    let max_id: i64 = db
        .query_one("SELECT MAX(id) FROM customers", ())
        .expect("Failed to get max id");
    assert_eq!(max_id, 3, "Auto-increment IDs should be 1, 2, 3");
}

#[test]
fn test_transaction_insert_with_defaults() {
    // Verify transaction INSERT respects DEFAULT values
    let db = Database::open("memory://txn_defaults").expect("Failed to create database");

    db.execute(
        "CREATE TABLE products (
            id INTEGER PRIMARY KEY AUTO_INCREMENT,
            name TEXT NOT NULL,
            is_active BOOLEAN NOT NULL DEFAULT TRUE,
            category TEXT NOT NULL DEFAULT 'General'
        )",
        (),
    )
    .expect("Failed to create table");

    db.execute("BEGIN", ()).expect("Failed to begin");
    db.execute("INSERT INTO products (name) VALUES ('Widget')", ())
        .expect("Transaction insert with defaults should work");
    db.execute("COMMIT", ()).expect("Failed to commit");

    let category: String = db
        .query_one("SELECT category FROM products WHERE name = 'Widget'", ())
        .expect("Failed to query");
    assert_eq!(category, "General", "Default value should be applied");

    let active: bool = db
        .query_one("SELECT is_active FROM products WHERE name = 'Widget'", ())
        .expect("Failed to query");
    assert!(active, "Default boolean should be true");
}

#[test]
fn test_autocommit_mode() {
    let db = Database::open("memory://txn_autocommit").expect("Failed to create database");

    db.execute(
        "CREATE TABLE txn_test (id INTEGER PRIMARY KEY, value TEXT)",
        (),
    )
    .expect("Failed to create table");

    // Without explicit BEGIN, each statement should auto-commit
    db.execute("INSERT INTO txn_test (id, value) VALUES (1, 'auto1')", ())
        .expect("Failed to insert");
    db.execute("INSERT INTO txn_test (id, value) VALUES (2, 'auto2')", ())
        .expect("Failed to insert");

    // Both rows should be visible immediately
    let count: i64 = db
        .query_one("SELECT COUNT(*) FROM txn_test", ())
        .expect("Failed to count");
    assert_eq!(count, 2);
}