vibesql-storage 0.2.0

Storage engine for vibesql SQL database
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
// ============================================================================
// Core Database Tests
// ============================================================================
//
// Tests for Database struct functionality including:
// - SQL mode management
// - Change event broadcasting
// - WAL persistence
// - Transaction durability hints

use vibesql_types::{MySqlModeFlags, SqlMode, SqlValue};

use crate::change_events::ChangeEvent;
use crate::database::Database;

// ============================================================================
// SQL Mode Tests
// ============================================================================

#[test]
fn test_set_sql_mode_changes_mode() {
    let mut db = Database::new();

    // Default is MySQL (for SQLLogicTest compatibility - dolthub corpus was regenerated against
    // MySQL 8.x)
    assert!(matches!(db.sql_mode(), SqlMode::MySQL { .. }));

    // Change to SQLite
    db.set_sql_mode(SqlMode::SQLite);
    assert!(matches!(db.sql_mode(), SqlMode::SQLite));

    // Change back to MySQL
    db.set_sql_mode(SqlMode::MySQL { flags: MySqlModeFlags::default() });
    assert!(matches!(db.sql_mode(), SqlMode::MySQL { .. }));
}

#[test]
fn test_set_sql_mode_updates_session_variable() {
    let mut db = Database::new();

    // Set to SQLite mode
    db.set_sql_mode(SqlMode::SQLite);

    // Check session variable reflects the change
    let sql_mode_var = db.get_session_variable("SQL_MODE");
    assert!(sql_mode_var.is_some());
    if let Some(SqlValue::Varchar(mode_str)) = sql_mode_var {
        assert_eq!(mode_str.as_str(), "SQLITE");
    } else {
        panic!("Expected SQL_MODE to be a Varchar");
    }
}

#[test]
fn test_set_sql_mode_mysql_with_flags() {
    let mut db = Database::new();

    // Set MySQL with specific flags
    db.set_sql_mode(SqlMode::MySQL {
        flags: MySqlModeFlags {
            pipes_as_concat: true,
            ansi_quotes: true,
            strict_mode: true,
            sqlite_division_semantics: false,
        },
    });

    // Check session variable contains the flags
    let sql_mode_var = db.get_session_variable("SQL_MODE");
    assert!(sql_mode_var.is_some());
    if let Some(SqlValue::Varchar(mode_str)) = sql_mode_var {
        assert!(mode_str.contains("STRICT_TRANS_TABLES"));
        assert!(mode_str.contains("PIPES_AS_CONCAT"));
        assert!(mode_str.contains("ANSI_QUOTES"));
    } else {
        panic!("Expected SQL_MODE to be a Varchar");
    }
}

#[test]
fn test_set_sql_mode_mysql_default_flags() {
    let mut db = Database::new();

    // Set MySQL with default flags (all false)
    db.set_sql_mode(SqlMode::MySQL { flags: MySqlModeFlags::default() });

    // Check session variable has default MySQL modes
    let sql_mode_var = db.get_session_variable("SQL_MODE");
    assert!(sql_mode_var.is_some());
    if let Some(SqlValue::Varchar(mode_str)) = sql_mode_var {
        // Default should include common MySQL defaults
        assert!(
            mode_str.contains("NO_ZERO_IN_DATE") || mode_str.contains("NO_ENGINE_SUBSTITUTION")
        );
    } else {
        panic!("Expected SQL_MODE to be a Varchar");
    }
}

#[test]
fn test_sql_mode_affects_subsequent_queries() {
    let mut db = Database::new();

    // Start in MySQL mode (default for SQLLogicTest compatibility)
    assert!(matches!(db.sql_mode(), SqlMode::MySQL { .. }));

    // Switch to SQLite
    db.set_sql_mode(SqlMode::SQLite);

    // Verify the mode changed
    let mode = db.sql_mode();
    assert!(matches!(mode, SqlMode::SQLite));
}

// ============================================================================
// Change Event Tests
// ============================================================================

#[test]
fn test_change_events_disabled_by_default() {
    let db = Database::new();
    assert!(!db.change_events_enabled());
    assert!(db.subscribe_changes().is_none());
}

#[test]
fn test_enable_change_events() {
    let mut db = Database::new();
    let _rx = db.enable_change_events(16);
    assert!(db.change_events_enabled());
    assert!(db.subscribe_changes().is_some());
}

#[test]
fn test_insert_emits_change_event() {
    use vibesql_catalog::{ColumnSchema, TableSchema};
    use vibesql_types::DataType;

    let mut db = Database::new();
    let mut rx = db.enable_change_events(16);

    // Create a simple table
    let schema = TableSchema::new(
        "users".to_string(),
        vec![
            ColumnSchema::new("id".to_string(), DataType::Integer, false),
            ColumnSchema::new(
                "name".to_string(),
                DataType::Varchar { max_length: Some(50) },
                false,
            ),
        ],
    );
    db.create_table(schema).unwrap();

    // Insert a row
    let row = crate::Row::new(vec![
        SqlValue::Integer(1),
        SqlValue::Varchar(arcstr::ArcStr::from("Alice")),
    ]);
    db.insert_row("users", row).unwrap();

    // Verify change event was emitted
    let events = rx.recv_all();
    assert_eq!(events.len(), 1);
    match &events[0] {
        ChangeEvent::Insert { table_name, row_index, .. } => {
            assert_eq!(*row_index, 0);
            // Table name will be "users" as passed to insert_row
            assert_eq!(table_name, "users");
        }
        _ => panic!("Expected Insert event, got {:?}", events[0]),
    }
}

#[test]
fn test_batch_insert_emits_multiple_events() {
    use vibesql_catalog::{ColumnSchema, TableSchema};
    use vibesql_types::DataType;

    let mut db = Database::new();
    let mut rx = db.enable_change_events(16);

    // Create a simple table
    let schema = TableSchema::new(
        "products".to_string(),
        vec![
            ColumnSchema::new("id".to_string(), DataType::Integer, false),
            ColumnSchema::new(
                "name".to_string(),
                DataType::Varchar { max_length: Some(50) },
                false,
            ),
        ],
    );
    db.create_table(schema).unwrap();

    // Insert batch of rows
    let rows = vec![
        crate::Row::new(vec![
            SqlValue::Integer(1),
            SqlValue::Varchar(arcstr::ArcStr::from("Product A")),
        ]),
        crate::Row::new(vec![
            SqlValue::Integer(2),
            SqlValue::Varchar(arcstr::ArcStr::from("Product B")),
        ]),
        crate::Row::new(vec![
            SqlValue::Integer(3),
            SqlValue::Varchar(arcstr::ArcStr::from("Product C")),
        ]),
    ];
    db.insert_rows_batch("products", rows).unwrap();

    // Verify 3 change events were emitted
    let events = rx.recv_all();
    assert_eq!(events.len(), 3);
    for (i, event) in events.iter().enumerate() {
        assert!(matches!(event, ChangeEvent::Insert { row_index, .. } if *row_index == i));
    }
}

#[test]
fn test_update_emits_change_event() {
    use vibesql_catalog::{ColumnSchema, TableSchema};
    use vibesql_types::DataType;

    let mut db = Database::new();

    // Create table with primary key
    let schema = TableSchema::with_primary_key(
        "users".to_string(),
        vec![
            ColumnSchema::new("id".to_string(), DataType::Integer, false),
            ColumnSchema::new(
                "name".to_string(),
                DataType::Varchar { max_length: Some(50) },
                false,
            ),
        ],
        vec!["id".to_string()],
    );
    db.create_table(schema).unwrap();

    // Insert a row
    let row = crate::Row::new(vec![
        SqlValue::Integer(1),
        SqlValue::Varchar(arcstr::ArcStr::from("Alice")),
    ]);
    db.insert_row("users", row).unwrap();

    // Now enable change events and update
    let mut rx = db.enable_change_events(16);

    db.update_row_by_pk(
        "users",
        SqlValue::Integer(1),
        vec![("name", SqlValue::Varchar(arcstr::ArcStr::from("Alice Smith")))],
    )
    .unwrap();

    // Verify update event was emitted
    let events = rx.recv_all();
    assert_eq!(events.len(), 1);
    assert!(matches!(&events[0], ChangeEvent::Update { row_index: 0, .. }));
}

#[test]
fn test_multiple_subscribers() {
    use vibesql_catalog::{ColumnSchema, TableSchema};
    use vibesql_types::DataType;

    let mut db = Database::new();
    let mut rx1 = db.enable_change_events(16);
    let mut rx2 = db.subscribe_changes().unwrap();

    // Create table and insert
    let schema = TableSchema::new(
        "test".to_string(),
        vec![ColumnSchema::new("id".to_string(), DataType::Integer, false)],
    );
    db.create_table(schema).unwrap();

    let row = crate::Row::new(vec![SqlValue::Integer(1)]);
    db.insert_row("test", row).unwrap();

    // Both receivers should get the event
    assert_eq!(rx1.recv_all().len(), 1);
    assert_eq!(rx2.recv_all().len(), 1);
}

#[test]
fn test_no_panic_on_lagged_receiver() {
    use vibesql_catalog::{ColumnSchema, TableSchema};
    use vibesql_types::DataType;

    let mut db = Database::new();
    let _rx = db.enable_change_events(2); // Very small buffer

    // Create table
    let schema = TableSchema::new(
        "test".to_string(),
        vec![ColumnSchema::new("id".to_string(), DataType::Integer, false)],
    );
    db.create_table(schema).unwrap();

    // Insert more rows than buffer can hold
    for i in 0..10 {
        let row = crate::Row::new(vec![SqlValue::Integer(i)]);
        db.insert_row("test", row).unwrap();
    }
    // Should not panic - lagged receivers are handled gracefully
}

#[test]
fn test_notify_deletes() {
    let mut db = Database::new();
    let mut rx = db.enable_change_events(16);

    // Directly call notify_deletes (since DELETE is handled by executor)
    db.notify_deletes("users", &[0, 2, 5]);

    let events = rx.recv_all();
    assert_eq!(events.len(), 3);
    assert!(
        matches!(&events[0], ChangeEvent::Delete { table_name, row_index: 0, .. } if table_name == "users")
    );
    assert!(
        matches!(&events[1], ChangeEvent::Delete { table_name, row_index: 2, .. } if table_name == "users")
    );
    assert!(
        matches!(&events[2], ChangeEvent::Delete { table_name, row_index: 5, .. } if table_name == "users")
    );
}

#[test]
fn test_notify_update() {
    let mut db = Database::new();
    let mut rx = db.enable_change_events(16);

    // Directly call notify_update
    db.notify_update("products", 42);

    let events = rx.recv_all();
    assert_eq!(events.len(), 1);
    assert!(
        matches!(&events[0], ChangeEvent::Update { table_name, row_index: 42, .. } if table_name == "products")
    );
}

// ============================================================================
// WAL Persistence Tests
// ============================================================================

#[test]
fn test_persistence_disabled_by_default() {
    let db = Database::new();
    assert!(!db.persistence_enabled());
    assert!(db.persistence_stats().is_none());
}

#[test]
fn test_enable_persistence() {
    use std::io::Cursor;

    use crate::wal::{PersistenceConfig, PersistenceEngine};

    let mut db = Database::new();
    assert!(!db.persistence_enabled());

    // Create a persistence engine with an in-memory writer
    let buf = Vec::new();
    let cursor = Cursor::new(buf);
    let engine = PersistenceEngine::with_writer(cursor, PersistenceConfig::default()).unwrap();

    db.enable_persistence(engine);
    assert!(db.persistence_enabled());
    assert!(db.persistence_stats().is_some());
}

#[test]
fn test_persistence_emits_insert_entries() {
    use std::io::Cursor;

    use vibesql_catalog::{ColumnSchema, TableSchema};
    use vibesql_types::DataType;

    use crate::wal::{PersistenceConfig, PersistenceEngine};

    let mut db = Database::new();

    // Enable persistence
    let buf = Vec::new();
    let cursor = Cursor::new(buf);
    let engine = PersistenceEngine::with_writer(cursor, PersistenceConfig::default()).unwrap();
    db.enable_persistence(engine);

    // Create a table
    let schema = TableSchema::new(
        "users".to_string(),
        vec![
            ColumnSchema::new("id".to_string(), DataType::Integer, false),
            ColumnSchema::new(
                "name".to_string(),
                DataType::Varchar { max_length: Some(50) },
                false,
            ),
        ],
    );
    db.create_table(schema).unwrap();

    // Insert rows
    let row1 = crate::Row::new(vec![
        SqlValue::Integer(1),
        SqlValue::Varchar(arcstr::ArcStr::from("Alice")),
    ]);
    let row2 =
        crate::Row::new(vec![SqlValue::Integer(2), SqlValue::Varchar(arcstr::ArcStr::from("Bob"))]);
    db.insert_row("users", row1).unwrap();
    db.insert_row("users", row2).unwrap();

    // Check stats
    let stats = db.persistence_stats().unwrap();
    // CreateTable + 2 Inserts = 3 entries
    assert_eq!(stats.entries_sent, 3);
}

#[test]
fn test_persistence_emits_transaction_entries() {
    use std::io::Cursor;

    use vibesql_catalog::{ColumnSchema, TableSchema};
    use vibesql_types::DataType;

    use crate::wal::{PersistenceConfig, PersistenceEngine};

    let mut db = Database::new();

    // Enable persistence
    let buf = Vec::new();
    let cursor = Cursor::new(buf);
    let engine = PersistenceEngine::with_writer(cursor, PersistenceConfig::default()).unwrap();
    db.enable_persistence(engine);

    // Create a table
    let schema = TableSchema::new(
        "test".to_string(),
        vec![ColumnSchema::new("id".to_string(), DataType::Integer, false)],
    );
    db.create_table(schema).unwrap();

    // Start transaction
    db.begin_transaction().unwrap();

    // Insert
    let row = crate::Row::new(vec![SqlValue::Integer(1)]);
    db.insert_row("test", row).unwrap();

    // Commit
    db.commit_transaction().unwrap();

    // Check stats: CreateTable + TxnBegin + Insert + TxnCommit = 4
    let stats = db.persistence_stats().unwrap();
    assert_eq!(stats.entries_sent, 4);
}

#[test]
fn test_sync_persistence_no_op_when_disabled() {
    let db = Database::new();
    // Should not error when persistence is disabled
    assert!(db.sync_persistence().is_ok());
}

#[test]
fn test_emit_wal_delete() {
    use std::io::Cursor;

    use crate::wal::{PersistenceConfig, PersistenceEngine};

    let mut db = Database::new();

    // Enable persistence
    let buf = Vec::new();
    let cursor = Cursor::new(buf);
    let engine = PersistenceEngine::with_writer(cursor, PersistenceConfig::default()).unwrap();
    db.enable_persistence(engine);

    // Emit delete entries
    db.emit_wal_delete("users", 0, vec![SqlValue::Integer(1)]);
    db.emit_wal_delete("users", 1, vec![SqlValue::Integer(2)]);

    // Check stats
    let stats = db.persistence_stats().unwrap();
    assert_eq!(stats.entries_sent, 2);
}

#[test]
fn test_emit_wal_create_index() {
    use std::io::Cursor;

    use crate::wal::{PersistenceConfig, PersistenceEngine};

    let mut db = Database::new();

    // Enable persistence
    let buf = Vec::new();
    let cursor = Cursor::new(buf);
    let engine = PersistenceEngine::with_writer(cursor, PersistenceConfig::default()).unwrap();
    db.enable_persistence(engine);

    // Emit create index entry
    db.emit_wal_create_index(1, "idx_users_email", "users", vec![1], false);

    // Check stats
    let stats = db.persistence_stats().unwrap();
    assert_eq!(stats.entries_sent, 1);
}

#[test]
fn test_emit_wal_drop_index() {
    use std::io::Cursor;

    use crate::wal::{PersistenceConfig, PersistenceEngine};

    let mut db = Database::new();

    // Enable persistence
    let buf = Vec::new();
    let cursor = Cursor::new(buf);
    let engine = PersistenceEngine::with_writer(cursor, PersistenceConfig::default()).unwrap();
    db.enable_persistence(engine);

    // Emit drop index entry
    db.emit_wal_drop_index(1, "idx_users_email");

    // Check stats
    let stats = db.persistence_stats().unwrap();
    assert_eq!(stats.entries_sent, 1);
}

#[test]
fn test_emit_wal_no_op_when_disabled() {
    let db = Database::new();

    // These should be no-ops when persistence is disabled (no panic)
    db.emit_wal_delete("users", 0, vec![SqlValue::Integer(1)]);
    db.emit_wal_create_index(1, "idx", "table", vec![0], false);
    db.emit_wal_drop_index(1, "idx");

    // Persistence stats should still be None
    assert!(db.persistence_stats().is_none());
}

// ============================================================================
// Transaction Durability Hint Tests
// ============================================================================

#[test]
fn test_begin_transaction_with_default_durability() {
    use crate::wal::TransactionDurability;

    let mut db = Database::new();

    db.begin_transaction_with_durability(TransactionDurability::Default).unwrap();
    assert!(db.in_transaction());

    // Verify the durability hint is stored
    assert!(db.in_transaction());

    db.rollback_transaction().unwrap();
}

#[test]
fn test_begin_transaction_with_force_durable() {
    use crate::wal::TransactionDurability;

    let mut db = Database::new();

    db.begin_transaction_with_durability(TransactionDurability::ForceDurable).unwrap();
    assert!(db.in_transaction());

    db.rollback_transaction().unwrap();
}

#[test]
fn test_begin_transaction_with_allow_lazy() {
    use crate::wal::TransactionDurability;

    let mut db = Database::new();

    db.begin_transaction_with_durability(TransactionDurability::AllowLazy).unwrap();
    assert!(db.in_transaction());

    db.rollback_transaction().unwrap();
}

#[test]
fn test_begin_transaction_with_force_volatile() {
    use crate::wal::TransactionDurability;

    let mut db = Database::new();

    db.begin_transaction_with_durability(TransactionDurability::ForceVolatile).unwrap();
    assert!(db.in_transaction());

    db.rollback_transaction().unwrap();
}

#[test]
fn test_durability_hint_cleared_on_commit() {
    use crate::wal::TransactionDurability;

    let mut db = Database::new();

    db.begin_transaction_with_durability(TransactionDurability::ForceDurable).unwrap();
    assert!(db.in_transaction());

    db.commit_transaction().unwrap();
    assert!(!db.in_transaction());
}

#[test]
fn test_durability_hint_cleared_on_rollback() {
    use crate::wal::TransactionDurability;

    let mut db = Database::new();

    db.begin_transaction_with_durability(TransactionDurability::ForceDurable).unwrap();
    assert!(db.in_transaction());

    db.rollback_transaction().unwrap();
    assert!(!db.in_transaction());
}

#[test]
fn test_force_durable_triggers_sync() {
    use std::io::Cursor;

    use vibesql_catalog::{ColumnSchema, TableSchema};
    use vibesql_types::DataType;

    use crate::wal::{PersistenceConfig, PersistenceEngine, TransactionDurability};

    let mut db = Database::new();

    // Enable persistence in lazy mode (default - no sync on commit)
    let buf = Vec::new();
    let cursor = Cursor::new(buf);
    let engine = PersistenceEngine::with_writer(cursor, PersistenceConfig::lazy()).unwrap();
    db.enable_persistence(engine);

    // Create a table
    let schema = TableSchema::new(
        "test".to_string(),
        vec![ColumnSchema::new("id".to_string(), DataType::Integer, false)],
    );
    db.create_table(schema).unwrap();

    // Begin transaction with ForceDurable hint
    db.begin_transaction_with_durability(TransactionDurability::ForceDurable).unwrap();

    // Insert a row
    let row = crate::Row::new(vec![SqlValue::Integer(1)]);
    db.insert_row("test", row).unwrap();

    // Commit - should trigger sync because ForceDurable overrides lazy mode
    db.commit_transaction().unwrap();

    // Check stats - explicit_flushes should have been triggered by sync
    let stats = db.persistence_stats().unwrap();
    assert!(stats.explicit_flushes >= 1, "ForceDurable should trigger an explicit flush on commit");
}

#[test]
fn test_default_durability_respects_lazy_mode() {
    use std::io::Cursor;

    use vibesql_catalog::{ColumnSchema, TableSchema};
    use vibesql_types::DataType;

    use crate::wal::{PersistenceConfig, PersistenceEngine, TransactionDurability};

    let mut db = Database::new();

    // Enable persistence in lazy mode
    let buf = Vec::new();
    let cursor = Cursor::new(buf);
    let engine = PersistenceEngine::with_writer(cursor, PersistenceConfig::lazy()).unwrap();
    db.enable_persistence(engine);

    // Create a table
    let schema = TableSchema::new(
        "test".to_string(),
        vec![ColumnSchema::new("id".to_string(), DataType::Integer, false)],
    );
    db.create_table(schema).unwrap();

    // Get initial stats after table creation
    let initial_stats = db.persistence_stats().unwrap();
    let initial_explicit_flushes = initial_stats.explicit_flushes;

    // Begin transaction with Default hint (should follow lazy mode - no sync on commit)
    db.begin_transaction_with_durability(TransactionDurability::Default).unwrap();

    // Insert a row
    let row = crate::Row::new(vec![SqlValue::Integer(1)]);
    db.insert_row("test", row).unwrap();

    // Commit - should NOT trigger sync in lazy mode with default durability
    db.commit_transaction().unwrap();

    // Check stats - no new explicit_flushes should have been triggered
    let final_stats = db.persistence_stats().unwrap();
    assert_eq!(
        final_stats.explicit_flushes, initial_explicit_flushes,
        "Default durability in lazy mode should not trigger explicit flush on commit"
    );
}

#[test]
fn test_durability_hint_no_panic_without_persistence() {
    use crate::wal::TransactionDurability;

    // Create database WITHOUT persistence enabled
    let mut db = Database::new();

    // Begin transaction with ForceDurable hint
    db.begin_transaction_with_durability(TransactionDurability::ForceDurable).unwrap();
    assert!(db.in_transaction());

    // Commit should not panic even though ForceDurable requests sync
    // (sync is a no-op when persistence is not enabled)
    db.commit_transaction().unwrap();
    assert!(!db.in_transaction());
}

#[test]
fn test_allow_lazy_downgrades_durable_mode() {
    use std::io::Cursor;

    use vibesql_catalog::{ColumnSchema, TableSchema};
    use vibesql_types::DataType;

    use crate::wal::{PersistenceConfig, PersistenceEngine, TransactionDurability};

    let mut db = Database::new();

    // Enable persistence in durable mode (sync on every commit by default)
    let buf = Vec::new();
    let cursor = Cursor::new(buf);
    let engine = PersistenceEngine::with_writer(cursor, PersistenceConfig::durable()).unwrap();
    db.enable_persistence(engine);

    // Create a table
    let schema = TableSchema::new(
        "test".to_string(),
        vec![ColumnSchema::new("id".to_string(), DataType::Integer, false)],
    );
    db.create_table(schema).unwrap();

    // Get initial stats after table creation
    let initial_stats = db.persistence_stats().unwrap();
    let initial_explicit_flushes = initial_stats.explicit_flushes;

    // Begin transaction with AllowLazy hint (should downgrade durable to lazy)
    db.begin_transaction_with_durability(TransactionDurability::AllowLazy).unwrap();

    // Insert a row
    let row = crate::Row::new(vec![SqlValue::Integer(1)]);
    db.insert_row("test", row).unwrap();

    // Commit - AllowLazy should prevent sync even in durable mode
    db.commit_transaction().unwrap();

    // Check stats - no new explicit_flushes should have been triggered
    let final_stats = db.persistence_stats().unwrap();
    assert_eq!(
        final_stats.explicit_flushes, initial_explicit_flushes,
        "AllowLazy should downgrade durable mode and not trigger explicit flush on commit"
    );
}

// ============================================================================
// Rollback restores index state (regression for #5413)
// ============================================================================

/// Regression for #5413: `rollback_transaction` is the SHARED rollback path
/// used by standalone transactions too. The B-tree `IndexManager` lives in
/// `Operations`, which is NOT part of the catalog/tables snapshot — so before
/// the fix an index mutated inside a transaction survived ROLLBACK. This test
/// mutates the index manager inside a transaction (CREATE INDEX), rolls back,
/// and asserts the index is gone, proving `Operations` is restored alongside
/// catalog/tables.
#[test]
fn rollback_restores_index_manager_state() {
    use vibesql_ast::IndexColumn;
    use vibesql_catalog::{ColumnSchema, TableSchema};
    use vibesql_types::DataType;

    let mut db = Database::new();

    // A table with a single column to index.
    let schema = TableSchema::new(
        "t".to_string(),
        vec![ColumnSchema::new("id".to_string(), DataType::Integer, false)],
    );
    db.create_table(schema).unwrap();

    db.begin_transaction().unwrap();
    db.create_index(
        "idx_t_id".to_string(),
        "t".to_string(),
        false,
        vec![IndexColumn::new_column("id".to_string(), vibesql_ast::OrderDirection::Asc)],
    )
    .unwrap();
    assert!(db.index_exists("idx_t_id"), "index exists inside the transaction");

    db.rollback_transaction().unwrap();

    // Before the #5413 fix, the IndexManager mutation survived rollback and
    // this assertion failed.
    assert!(
        !db.index_exists("idx_t_id"),
        "ROLLBACK must restore the IndexManager, removing the index created in the txn"
    );
}

/// Regression for #5413: committed index mutations must persist (the fix must
/// only undo rolled-back index changes, never committed ones).
#[test]
fn commit_keeps_index_manager_state() {
    use vibesql_ast::IndexColumn;
    use vibesql_catalog::{ColumnSchema, TableSchema};
    use vibesql_types::DataType;

    let mut db = Database::new();

    let schema = TableSchema::new(
        "t".to_string(),
        vec![ColumnSchema::new("id".to_string(), DataType::Integer, false)],
    );
    db.create_table(schema).unwrap();

    db.begin_transaction().unwrap();
    db.create_index(
        "idx_t_id".to_string(),
        "t".to_string(),
        false,
        vec![IndexColumn::new_column("id".to_string(), vibesql_ast::OrderDirection::Asc)],
    )
    .unwrap();
    db.commit_transaction().unwrap();

    assert!(db.index_exists("idx_t_id"), "COMMIT must keep the index created in the txn");
}

// ============================================================================
// Copy-on-write Operations snapshot (#5419)
// ============================================================================

/// #5419: a read-only transaction must NOT deep-clone the `Operations`
/// (IndexManager + spatial indexes) at BEGIN. Before #5419 the snapshot was
/// eager, so every BEGIN — including the per-read scratch txn used by
/// replicated read-your-own-writes — paid an O(index-keys) clone. With the
/// copy-on-write snapshot, a transaction that only reads triggers zero
/// clones. Asserted deterministically via the clone counter (no timing).
#[test]
fn read_only_transaction_does_not_clone_operations_snapshot() {
    use vibesql_ast::IndexColumn;
    use vibesql_catalog::{ColumnSchema, TableSchema};
    use vibesql_types::DataType;

    let mut db = Database::new();
    let schema = TableSchema::new(
        "t".to_string(),
        vec![ColumnSchema::new("id".to_string(), DataType::Integer, false)],
    );
    db.create_table(schema).unwrap();

    // Build a non-trivial index so an eager clone would be observable work.
    db.create_index(
        "idx_t_id".to_string(),
        "t".to_string(),
        false,
        vec![IndexColumn::new_column("id".to_string(), vibesql_ast::OrderDirection::Asc)],
    )
    .unwrap();
    for i in 0..256 {
        db.insert_row("t", crate::Row::new(vec![SqlValue::Integer(i)])).unwrap();
    }

    let before = db.operations_snapshot_clones();

    // A purely read-only transaction: BEGIN, observe index state, ROLLBACK.
    db.begin_transaction().unwrap();
    assert!(db.index_exists("idx_t_id"), "index visible inside the read-only txn");
    let _ = db.list_indexes();
    db.rollback_transaction().unwrap();

    assert_eq!(
        db.operations_snapshot_clones(),
        before,
        "a read-only transaction must not deep-clone the Operations snapshot (#5419)"
    );
}

/// #5419: a transaction that DOES mutate an index lazily clones the
/// `Operations` snapshot exactly once (on the first mutation), and that
/// snapshot still fully restores index state on ROLLBACK — i.e. the #5413
/// correctness fix is preserved, just deferred. Also verifies the clone is
/// taken once even across multiple index mutations.
#[test]
fn mutating_transaction_clones_operations_snapshot_once_and_rolls_back() {
    use vibesql_ast::IndexColumn;
    use vibesql_catalog::{ColumnSchema, TableSchema};
    use vibesql_types::DataType;

    let mut db = Database::new();
    let schema = TableSchema::new(
        "t".to_string(),
        vec![ColumnSchema::new("id".to_string(), DataType::Integer, false)],
    );
    db.create_table(schema).unwrap();
    db.create_index(
        "idx_t_id".to_string(),
        "t".to_string(),
        false,
        vec![IndexColumn::new_column("id".to_string(), vibesql_ast::OrderDirection::Asc)],
    )
    .unwrap();

    let before = db.operations_snapshot_clones();

    db.begin_transaction().unwrap();
    // First index mutation triggers exactly one lazy clone.
    db.insert_row("t", crate::Row::new(vec![SqlValue::Integer(1)])).unwrap();
    assert_eq!(
        db.operations_snapshot_clones(),
        before + 1,
        "first index mutation must take exactly one snapshot clone"
    );
    // Further mutations do not re-clone.
    db.insert_row("t", crate::Row::new(vec![SqlValue::Integer(2)])).unwrap();
    db.create_index(
        "idx_t_id2".to_string(),
        "t".to_string(),
        false,
        vec![IndexColumn::new_column("id".to_string(), vibesql_ast::OrderDirection::Asc)],
    )
    .unwrap();
    assert_eq!(
        db.operations_snapshot_clones(),
        before + 1,
        "subsequent mutations must reuse the existing snapshot, not re-clone"
    );

    db.rollback_transaction().unwrap();

    // The lazily-captured snapshot must fully restore index state: the
    // index created in the txn is gone, and the pre-existing index survives.
    assert!(
        !db.index_exists("idx_t_id2"),
        "ROLLBACK must restore Operations: index created in the txn is removed (#5413 preserved)"
    );
    assert!(
        db.index_exists("idx_t_id"),
        "the pre-transaction index must survive ROLLBACK"
    );
    assert_eq!(db.get_table("t").map(|t| t.row_count()), Some(0), "rolled-back rows are gone");
}