pg2any_lib 0.9.0

PostgreSQL to Any database library with Change Data Capture (CDC) and logical replication support
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
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
mod common;

use chrono::Utc;
use common::{format_column_value, wrap_in_transaction};
use pg2any_lib::{
    destinations::{DestinationFactory, DestinationHandler},
    types::{ChangeEvent, DestinationType, ReplicaIdentity},
    Transaction,
};
use pg_walstream::{ColumnValue, Lsn, RowData};
use std::path::PathBuf;
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::fs;

/// Helper function to convert Transaction to SQL commands
fn transaction_to_sql_commands(tx: &Transaction) -> Vec<String> {
    tx.events.iter().filter_map(|e| event_to_sql(e)).collect()
}

fn event_to_sql(event: &ChangeEvent) -> Option<String> {
    use pg2any_lib::types::EventType;
    match &event.event_type {
        EventType::Insert { table, data, .. } => {
            let cols: Vec<String> = data.iter().map(|(k, _)| k.to_string()).collect();
            let vals: Vec<String> = data.iter().map(|(_, v)| format_column_value(v)).collect();
            Some(format!(
                "INSERT INTO \"{}\" ({}) VALUES ({});",
                table,
                cols.iter()
                    .map(|c| format!("\"{}\"", c))
                    .collect::<Vec<_>>()
                    .join(", "),
                vals.join(", ")
            ))
        }
        EventType::Update {
            table,
            new_data,
            old_data,
            key_columns,
            ..
        } => {
            let set: Vec<String> = new_data
                .iter()
                .map(|(k, v)| format!("\"{}\" = {}", k, format_column_value(v)))
                .collect();
            let cond: Vec<String> = key_columns
                .iter()
                .filter_map(|k| {
                    old_data
                        .as_ref()
                        .and_then(|d| d.get(k.as_ref()))
                        .or_else(|| new_data.get(k.as_ref()))
                        .map(|v| format!("\"{}\" = {}", k, format_column_value(v)))
                })
                .collect();
            if cond.is_empty() {
                return None;
            }
            Some(format!(
                "UPDATE \"{}\" SET {} WHERE {};",
                table,
                set.join(", "),
                cond.join(" AND ")
            ))
        }
        EventType::Delete {
            table,
            old_data,
            key_columns,
            ..
        } => {
            let cond: Vec<String> = key_columns
                .iter()
                .filter_map(|k| {
                    old_data
                        .get(k.as_ref())
                        .map(|v| format!("\"{}\" = {}", k, format_column_value(v)))
                })
                .collect();
            if cond.is_empty() {
                return None;
            }
            Some(format!(
                "DELETE FROM \"{}\" WHERE {};",
                table,
                cond.join(" AND ")
            ))
        }
        _ => None,
    }
}

#[cfg(feature = "sqlite")]
use pg2any_lib::destinations::sqlite::SQLiteDestination;
#[cfg(feature = "sqlite")]
use sqlx::{Row, SqlitePool};

/// Helper struct to manage temporary SQLite database files
struct TempDatabase {
    path: PathBuf,
}

impl TempDatabase {
    fn new(test_name: &str) -> Self {
        let mut path = std::env::temp_dir();
        path.push(format!(
            "pg2any_comprehensive_test_{}_{}.db",
            test_name,
            std::process::id()
        ));

        // Ensure the file doesn't exist before use
        let _ = fs::remove_file(&path);

        Self { path }
    }

    fn connection_string(&self) -> String {
        self.path.to_string_lossy().into_owned()
    }
}

impl Drop for TempDatabase {
    fn drop(&mut self) {
        // Clean up the temporary database file
        let _ = fs::remove_file(&self.path);

        // Also clean up WAL and SHM files
        let wal_path = self.path.with_extension("db-wal");
        let shm_path = self.path.with_extension("db-shm");
        let _ = fs::remove_file(&wal_path);
        let _ = fs::remove_file(&shm_path);
    }
}

#[cfg(feature = "sqlite")]
async fn create_comprehensive_test_table(pool: &SqlitePool) -> Result<(), sqlx::Error> {
    sqlx::query(
        "CREATE TABLE IF NOT EXISTS comprehensive_test (
            id INTEGER PRIMARY KEY,
            text_field TEXT,
            int_field INTEGER,
            real_field REAL,
            blob_field BLOB,
            bool_field BOOLEAN,
            date_field TEXT,
            nullable_field TEXT,
            json_field TEXT,
            unique_field TEXT UNIQUE,
            index_field TEXT
        )",
    )
    .execute(pool)
    .await?;

    // Create an index for testing
    sqlx::query("CREATE INDEX IF NOT EXISTS idx_index_field ON comprehensive_test(index_field)")
        .execute(pool)
        .await?;

    Ok(())
}

#[cfg(feature = "sqlite")]
async fn create_users_table(pool: &SqlitePool) -> Result<(), sqlx::Error> {
    sqlx::query(
        "CREATE TABLE IF NOT EXISTS users (
            id INTEGER PRIMARY KEY,
            name TEXT NOT NULL,
            age INTEGER,
            email TEXT UNIQUE,
            active BOOLEAN DEFAULT 1,
            salary REAL,
            created_at TEXT DEFAULT CURRENT_TIMESTAMP
        )",
    )
    .execute(pool)
    .await?;
    Ok(())
}

// ================================
// EDGE CASE TESTS
// ================================

#[cfg(feature = "sqlite")]
#[tokio::test]
async fn test_sqlite_empty_string_handling() {
    let temp_db = TempDatabase::new("empty_strings");
    let connection_string = temp_db.connection_string();

    let mut destination = SQLiteDestination::new();
    destination.connect(&connection_string).await.unwrap();

    let pool = SqlitePool::connect(&format!("sqlite://{}", connection_string))
        .await
        .unwrap();
    create_comprehensive_test_table(&pool).await.unwrap();

    // Test empty string insertion
    let data = RowData::from_pairs(vec![
        ("id", ColumnValue::text("1")),
        ("text_field", ColumnValue::text("")),
        ("nullable_field", ColumnValue::text("")),
    ]);

    let event = ChangeEvent::insert("main", "comprehensive_test", 123, data, Lsn::from(100));
    let result = destination
        .execute_sql_batch_with_hook(
            &transaction_to_sql_commands(&wrap_in_transaction(event)),
            None,
        )
        .await;
    assert!(result.is_ok());

    // Verify empty strings are preserved
    let row = sqlx::query("SELECT text_field, nullable_field FROM comprehensive_test WHERE id = 1")
        .fetch_one(&pool)
        .await
        .unwrap();

    let text_field: String = row.get("text_field");
    let nullable_field: String = row.get("nullable_field");
    assert_eq!(text_field, "");
    assert_eq!(nullable_field, "");

    pool.close().await;
    let _ = destination.close().await;
}

#[cfg(feature = "sqlite")]
#[tokio::test]
async fn test_sqlite_null_value_handling() {
    let temp_db = TempDatabase::new("null_values");
    let connection_string = temp_db.connection_string();

    let mut destination = SQLiteDestination::new();
    destination.connect(&connection_string).await.unwrap();

    let pool = SqlitePool::connect(&format!("sqlite://{}", connection_string))
        .await
        .unwrap();
    create_comprehensive_test_table(&pool).await.unwrap();

    // Test null value insertion
    let data = RowData::from_pairs(vec![
        ("id", ColumnValue::text("1")),
        ("text_field", ColumnValue::text("test")),
        ("nullable_field", ColumnValue::Null),
        ("int_field", ColumnValue::Null),
        ("real_field", ColumnValue::Null),
    ]);

    let event = ChangeEvent::insert("main", "comprehensive_test", 123, data, Lsn::from(100));
    let result = destination
        .execute_sql_batch_with_hook(
            &transaction_to_sql_commands(&wrap_in_transaction(event)),
            None,
        )
        .await;
    assert!(result.is_ok());

    // Verify null values are handled correctly
    let row = sqlx::query(
        "SELECT nullable_field, int_field, real_field FROM comprehensive_test WHERE id = 1",
    )
    .fetch_one(&pool)
    .await
    .unwrap();

    let nullable_field: Option<String> = row.get("nullable_field");
    let int_field: Option<i32> = row.get("int_field");
    let real_field: Option<f64> = row.get("real_field");

    assert!(nullable_field.is_none());
    assert!(int_field.is_none());
    assert!(real_field.is_none());

    pool.close().await;
    let _ = destination.close().await;
}

#[cfg(feature = "sqlite")]
#[tokio::test]
async fn test_sqlite_unicode_and_special_characters() {
    let temp_db = TempDatabase::new("unicode");
    let connection_string = temp_db.connection_string();

    let mut destination = SQLiteDestination::new();
    destination.connect(&connection_string).await.unwrap();

    let pool = SqlitePool::connect(&format!("sqlite://{}", connection_string))
        .await
        .unwrap();
    create_comprehensive_test_table(&pool).await.unwrap();

    // Test unicode and special character insertion
    let data = RowData::from_pairs(vec![
        ("id", ColumnValue::text("1")),
        (
            "text_field",
            ColumnValue::text("🚀 Hello 世界! Special chars: áéíóú ñüç"),
        ),
        (
            "json_field",
            ColumnValue::text("{\"emoji\": \"😀\", \"chinese\": \"你好\"}"),
        ),
    ]);

    let event = ChangeEvent::insert("main", "comprehensive_test", 123, data, Lsn::from(100));
    let result = destination
        .execute_sql_batch_with_hook(
            &transaction_to_sql_commands(&wrap_in_transaction(event)),
            None,
        )
        .await;
    assert!(result.is_ok());

    // Verify unicode characters are preserved
    let row = sqlx::query("SELECT text_field, json_field FROM comprehensive_test WHERE id = 1")
        .fetch_one(&pool)
        .await
        .unwrap();

    let text_field: String = row.get("text_field");
    let json_field: String = row.get("json_field");

    assert!(text_field.contains("🚀"));
    assert!(text_field.contains("世界"));
    assert!(text_field.contains("áéíóú"));
    assert!(json_field.contains("😀"));
    assert!(json_field.contains("你好"));

    pool.close().await;
    let _ = destination.close().await;
}

#[cfg(feature = "sqlite")]
#[tokio::test]
async fn test_sqlite_large_data_handling() {
    let temp_db = TempDatabase::new("large_data");
    let connection_string = temp_db.connection_string();

    let mut destination = SQLiteDestination::new();
    destination.connect(&connection_string).await.unwrap();

    let pool = SqlitePool::connect(&format!("sqlite://{}", connection_string))
        .await
        .unwrap();
    create_comprehensive_test_table(&pool).await.unwrap();

    // Create large text data (1MB)
    let large_text = "A".repeat(1024 * 1024);
    let large_json = format!(
        "{{\"data\": \"{}\", \"nested\": {{\"more_data\": \"{}\"}}}}",
        "B".repeat(500000),
        "C".repeat(500000)
    );

    let data = RowData::from_pairs(vec![
        ("id", ColumnValue::text("1")),
        ("text_field", ColumnValue::text(&large_text)),
        ("json_field", ColumnValue::text(&large_json)),
    ]);

    let event = ChangeEvent::insert("main", "comprehensive_test", 123, data, Lsn::from(100));
    let result = destination
        .execute_sql_batch_with_hook(
            &transaction_to_sql_commands(&wrap_in_transaction(event)),
            None,
        )
        .await;
    assert!(result.is_ok());

    // Verify large data is stored correctly
    let row = sqlx::query("SELECT length(text_field) as text_len, length(json_field) as json_len FROM comprehensive_test WHERE id = 1")
        .fetch_one(&pool)
        .await
        .unwrap();

    let text_len: i32 = row.get("text_len");
    let json_len: i32 = row.get("json_len");

    assert_eq!(text_len, 1024 * 1024);
    assert!(json_len > 1000000); // Should be large due to JSON structure

    pool.close().await;
    let _ = destination.close().await;
}

#[cfg(feature = "sqlite")]
#[tokio::test]
async fn test_sqlite_numeric_precision() {
    let temp_db = TempDatabase::new("numeric_precision");
    let connection_string = temp_db.connection_string();

    let mut destination = SQLiteDestination::new();
    destination.connect(&connection_string).await.unwrap();

    let pool = SqlitePool::connect(&format!("sqlite://{}", connection_string))
        .await
        .unwrap();
    create_comprehensive_test_table(&pool).await.unwrap();

    // Test various numeric types and precision
    let test_cases: Vec<(i32, ColumnValue)> = vec![
        (1, ColumnValue::text("9223372036854775807")), // i64 max
        (2, ColumnValue::text("-9223372036854775808")), // i64 min
        (3, ColumnValue::text("3.141592653589793")),   // High precision float
        (4, ColumnValue::text("1.7976931348623157e308")), // Large float
        (5, ColumnValue::text("2.2250738585072014e-308")), // Small float
    ];

    for (id, value) in &test_cases {
        let data = RowData::from_pairs(vec![
            ("id", ColumnValue::text(&id.to_string())),
            ("real_field", value.clone()),
        ]);

        let event = ChangeEvent::insert("main", "comprehensive_test", 123, data, Lsn::from(100));
        let result = destination
            .execute_sql_batch_with_hook(
                &transaction_to_sql_commands(&wrap_in_transaction(event)),
                None,
            )
            .await;
        assert!(result.is_ok(), "Failed to insert value: {:?}", value);
    }

    // Verify precision is maintained
    let rows = sqlx::query("SELECT id, real_field FROM comprehensive_test ORDER BY id")
        .fetch_all(&pool)
        .await
        .unwrap();

    assert_eq!(rows.len(), 5);

    // Check specific values
    let pi_value: f64 = rows[2].get("real_field");
    assert!((pi_value - 3.141592653589793).abs() < 1e-10);

    pool.close().await;
    let _ = destination.close().await;
}

// ================================
// ERROR HANDLING TESTS
// ================================

#[cfg(feature = "sqlite")]
#[tokio::test]
async fn test_sqlite_invalid_connection_string() {
    let mut destination = SQLiteDestination::new();

    // Test various invalid connection strings
    let invalid_connections = vec!["/nonexistent/directory/that/cannot/be/created/test.db", ""];

    for conn_str in invalid_connections {
        let result = destination.connect(conn_str).await;
        // Some might fail, some might succeed (empty string creates in-memory DB)
        // Just ensure it doesn't panic
        let _ = result;
    }
}

#[cfg(feature = "sqlite")]
#[tokio::test]
async fn test_sqlite_constraint_violations() {
    let temp_db = TempDatabase::new("constraints");
    let connection_string = temp_db.connection_string();

    let mut destination = SQLiteDestination::new();
    destination.connect(&connection_string).await.unwrap();

    let pool = SqlitePool::connect(&format!("sqlite://{}", connection_string))
        .await
        .unwrap();
    create_comprehensive_test_table(&pool).await.unwrap();

    // Insert initial data
    let data1 = RowData::from_pairs(vec![
        ("id", ColumnValue::text("1")),
        ("unique_field", ColumnValue::text("unique_value")),
    ]);

    let event1 = ChangeEvent::insert("main", "comprehensive_test", 123, data1, Lsn::from(100));
    let result1 = destination
        .execute_sql_batch_with_hook(
            &transaction_to_sql_commands(&wrap_in_transaction(event1)),
            None,
        )
        .await;
    assert!(result1.is_ok());

    // Try to insert duplicate unique value - should fail
    let data2 = RowData::from_pairs(vec![
        ("id", ColumnValue::text("2")),
        ("unique_field", ColumnValue::text("unique_value")),
    ]);

    let event2 = ChangeEvent::insert("main", "comprehensive_test", 124, data2, Lsn::from(100));
    let result2 = destination
        .execute_sql_batch_with_hook(
            &transaction_to_sql_commands(&wrap_in_transaction(event2)),
            None,
        )
        .await;
    assert!(
        result2.is_err(),
        "Should fail due to unique constraint violation"
    );

    pool.close().await;
    let _ = destination.close().await;
}

#[cfg(feature = "sqlite")]
#[tokio::test]
async fn test_sqlite_missing_key_columns_error() {
    let temp_db = TempDatabase::new("missing_keys");
    let connection_string = temp_db.connection_string();

    let mut destination = SQLiteDestination::new();
    destination.connect(&connection_string).await.unwrap();

    let pool = SqlitePool::connect(&format!("sqlite://{}", connection_string))
        .await
        .unwrap();
    create_comprehensive_test_table(&pool).await.unwrap();

    // Insert initial data
    let data = RowData::from_pairs(vec![
        ("id", ColumnValue::text("1")),
        ("text_field", ColumnValue::text("test")),
    ]);

    let event = ChangeEvent::insert(
        "main",
        "comprehensive_test",
        123,
        data.clone(),
        Lsn::from(100),
    );
    let result = destination
        .execute_sql_batch_with_hook(
            &transaction_to_sql_commands(&wrap_in_transaction(event)),
            None,
        )
        .await;
    assert!(result.is_ok());

    // Try UPDATE with empty key columns - should fail
    let update_event = ChangeEvent::update(
        "main",
        "comprehensive_test",
        124,
        Some(data.clone()),
        data,
        ReplicaIdentity::Default,
        vec![], // Empty key columns
        Lsn::from(300),
    );

    let result = destination
        .execute_sql_batch_with_hook(
            &transaction_to_sql_commands(&wrap_in_transaction(update_event)),
            None,
        )
        .await;
    // With the SQL-based workflow, events with missing key columns are skipped (generate no SQL)
    // so execute_sql_batch succeeds with empty batch
    assert!(result.is_ok(), "Empty SQL batch should succeed");

    pool.close().await;
    let _ = destination.close().await;
}

// ================================
// PERFORMANCE AND STRESS TESTS
// ================================

#[cfg(feature = "sqlite")]
#[tokio::test]
async fn test_sqlite_bulk_operations_performance() {
    let temp_db = TempDatabase::new("bulk_perf");
    let connection_string = temp_db.connection_string();

    let mut destination = SQLiteDestination::new();
    destination.connect(&connection_string).await.unwrap();

    let pool = SqlitePool::connect(&format!("sqlite://{}", connection_string))
        .await
        .unwrap();
    create_users_table(&pool).await.unwrap();

    let start_time = Instant::now();
    let batch_size = 1000;

    // Bulk INSERT operations
    for i in 0..batch_size {
        let data = RowData::from_pairs(vec![
            ("id", ColumnValue::text(&i.to_string())),
            ("name", ColumnValue::text(&format!("User {}", i))),
            ("age", ColumnValue::text(&(20 + (i % 50)).to_string())),
            (
                "email",
                ColumnValue::text(&format!("user{}@example.com", i)),
            ),
            ("active", ColumnValue::text("true")),
            (
                "salary",
                ColumnValue::text(&format!("{}", 50000.0 + (i as f64 * 10.0))),
            ),
        ]);

        let event = ChangeEvent::insert("main", "users", 123 + i as u32, data, Lsn::from(100));
        let result = destination
            .execute_sql_batch_with_hook(
                &transaction_to_sql_commands(&wrap_in_transaction(event)),
                None,
            )
            .await;
        assert!(result.is_ok(), "Failed to insert record {}", i);
    }

    let insert_duration = start_time.elapsed();
    println!(
        "Bulk INSERT of {} records took: {:?}",
        batch_size, insert_duration
    );

    // Verify all records were inserted
    let count_row = sqlx::query("SELECT COUNT(*) as count FROM users")
        .fetch_one(&pool)
        .await
        .unwrap();
    let count: i32 = count_row.get("count");
    assert_eq!(count, batch_size as i32);

    // Bulk UPDATE operations
    let update_start = Instant::now();
    for i in 0..batch_size / 2 {
        let old_data = RowData::from_pairs(vec![("id", ColumnValue::text(&i.to_string()))]);

        let new_data = RowData::from_pairs(vec![
            ("id", ColumnValue::text(&i.to_string())),
            ("name", ColumnValue::text(&format!("Updated User {}", i))),
            ("age", ColumnValue::text(&(25 + (i % 50)).to_string())),
            (
                "email",
                ColumnValue::text(&format!("updated_user{}@example.com", i)),
            ),
            ("active", ColumnValue::text("true")),
            (
                "salary",
                ColumnValue::text(&format!("{}", 60000.0 + (i as f64 * 15.0))),
            ),
        ]);

        let event = ChangeEvent::update(
            "main",
            "users",
            200 + i as u32,
            Some(old_data),
            new_data,
            ReplicaIdentity::Default,
            vec![Arc::from("id")],
            Lsn::from(300),
        );

        let result = destination
            .execute_sql_batch_with_hook(
                &transaction_to_sql_commands(&wrap_in_transaction(event)),
                None,
            )
            .await;
        assert!(result.is_ok(), "Failed to update record {}", i);
    }

    let update_duration = update_start.elapsed();
    println!(
        "Bulk UPDATE of {} records took: {:?}",
        batch_size / 2,
        update_duration
    );

    // Performance assertions - these are generous to account for CI environment
    assert!(
        insert_duration < Duration::from_secs(30),
        "INSERT operations too slow: {:?}",
        insert_duration
    );
    assert!(
        update_duration < Duration::from_secs(30),
        "UPDATE operations too slow: {:?}",
        update_duration
    );

    pool.close().await;
    let _ = destination.close().await;
}

#[cfg(feature = "sqlite")]
#[tokio::test]
async fn test_sqlite_concurrent_operations() {
    let temp_db = TempDatabase::new("sequential");
    let connection_string = temp_db.connection_string();

    // Create destination and connect
    let mut destination = SQLiteDestination::new();
    destination.connect(&connection_string).await.unwrap();

    // Set up the database schema through the destination
    let pool = SqlitePool::connect(&format!("sqlite://{}", &connection_string))
        .await
        .unwrap();
    create_users_table(&pool).await.unwrap();
    pool.close().await;

    let mut total_records = 0;

    // Simulate multiple "sessions" operating sequentially
    // This tests that SQLite can handle repeated operations robustly
    for session_id in 0..3 {
        // Each session inserts 15 records
        for i in 0..15 {
            let record_id = session_id * 1000 + i;
            let data = RowData::from_pairs(vec![
                ("id", ColumnValue::text(&record_id.to_string())),
                (
                    "name",
                    ColumnValue::text(&format!("Session {} Record {}", session_id, i)),
                ),
                ("age", ColumnValue::text(&(25 + i % 30).to_string())),
                (
                    "email",
                    ColumnValue::text(&format!("session{}record{}@example.com", session_id, i)),
                ),
            ]);

            let event =
                ChangeEvent::insert("main", "users", record_id as u32, data, Lsn::from(100));
            let result = destination
                .execute_sql_batch_with_hook(
                    &transaction_to_sql_commands(&wrap_in_transaction(event)),
                    None,
                )
                .await;
            assert!(
                result.is_ok(),
                "Session {} failed to insert record {}: {:?}",
                session_id,
                i,
                result
            );
            total_records += 1;
        }
    }

    // Verify final record count
    let pool = SqlitePool::connect(&format!("sqlite://{}", &connection_string))
        .await
        .unwrap();

    let count_row = sqlx::query("SELECT COUNT(*) as count FROM users")
        .fetch_one(&pool)
        .await
        .unwrap();
    let count: i32 = count_row.get("count");

    assert_eq!(
        count, total_records,
        "Expected {} records, got {}",
        total_records, count
    );

    pool.close().await;
    let _ = destination.close().await;
}

// ================================
// TRANSACTION AND CONSISTENCY TESTS
// ================================

#[cfg(feature = "sqlite")]
#[tokio::test]
async fn test_sqlite_transaction_events() {
    let temp_db = TempDatabase::new("transactions");
    let connection_string = temp_db.connection_string();

    let mut destination = SQLiteDestination::new();
    destination.connect(&connection_string).await.unwrap();

    let pool = SqlitePool::connect(&format!("sqlite://{}", connection_string))
        .await
        .unwrap();
    create_users_table(&pool).await.unwrap();

    // Test transaction BEGIN/COMMIT events
    let timestamp = Utc::now();
    let begin_event = ChangeEvent::begin(100, Lsn::from(0), timestamp, Lsn::from(100));
    let commit_event =
        ChangeEvent::commit(timestamp, Lsn::from(100), Lsn::from(110), Lsn::from(110));

    // These should not fail (even though SQLite handles transactions automatically)
    let begin_result = destination
        .execute_sql_batch_with_hook(
            &transaction_to_sql_commands(&wrap_in_transaction(begin_event)),
            None,
        )
        .await;
    assert!(begin_result.is_ok());

    // Insert some data between transaction events
    let data = RowData::from_pairs(vec![
        ("id", ColumnValue::text("1")),
        ("name", ColumnValue::text("Transaction Test")),
        ("email", ColumnValue::text("txn@example.com")),
    ]);

    let insert_event = ChangeEvent::insert("main", "users", 105, data, Lsn::from(100));
    let insert_result = destination
        .execute_sql_batch_with_hook(
            &transaction_to_sql_commands(&wrap_in_transaction(insert_event)),
            None,
        )
        .await;
    assert!(insert_result.is_ok());

    let commit_result = destination
        .execute_sql_batch_with_hook(
            &transaction_to_sql_commands(&wrap_in_transaction(commit_event)),
            None,
        )
        .await;
    assert!(commit_result.is_ok());

    // Verify data was inserted
    let row = sqlx::query("SELECT COUNT(*) as count FROM users WHERE id = 1")
        .fetch_one(&pool)
        .await
        .unwrap();
    let count: i32 = row.get("count");
    assert_eq!(count, 1);

    pool.close().await;
    let _ = destination.close().await;
}

#[cfg(feature = "sqlite")]
#[tokio::test]
async fn test_sqlite_metadata_events() {
    let temp_db = TempDatabase::new("metadata");
    let connection_string = temp_db.connection_string();

    let mut destination = SQLiteDestination::new();
    destination.connect(&connection_string).await.unwrap();

    // Test various metadata events that should be safely ignored
    let metadata_events = vec![
        ChangeEvent::relation(
            0,
            "public",
            "test_table",
            ReplicaIdentity::Default,
            vec![],
            Lsn::from(0),
        ),
        ChangeEvent::type_event(0, "public", "test_type", Lsn::from(0)),
        ChangeEvent::origin(Lsn::from(0), "test_origin", Lsn::from(0)),
        ChangeEvent::message(
            0,
            Lsn::from(0),
            "test_prefix",
            bytes::Bytes::new(),
            Lsn::from(0),
        ),
    ];

    for event in metadata_events {
        let tx = wrap_in_transaction(event.clone());
        let commands = transaction_to_sql_commands(&tx);
        let result = destination
            .execute_sql_batch_with_hook(&commands, None)
            .await;
        assert!(
            result.is_ok(),
            "Metadata event should not fail: {:?}",
            event.event_type
        );
    }

    let _ = destination.close().await;
}

// ================================
// RECOVERY AND ROBUSTNESS TESTS
// ================================

#[cfg(feature = "sqlite")]
#[tokio::test]
async fn test_sqlite_connection_recovery() {
    let temp_db = TempDatabase::new("recovery");
    let connection_string = temp_db.connection_string();

    let mut destination = SQLiteDestination::new();
    destination.connect(&connection_string).await.unwrap();

    // Verify connection works by creating a simple transaction
    let pool = SqlitePool::connect(&format!("sqlite://{}", connection_string))
        .await
        .unwrap();
    create_users_table(&pool).await.unwrap();

    let values = RowData::from_pairs(vec![
        ("id", ColumnValue::text("1")),
        ("name", ColumnValue::text("Alice")),
    ]);
    let event = ChangeEvent::insert("public", "users", 1, values, Lsn::from(100));
    let transaction = wrap_in_transaction(event);
    let process_result = destination
        .execute_sql_batch_with_hook(&transaction_to_sql_commands(&transaction), None)
        .await;
    assert!(process_result.is_ok());

    // Close and reconnect
    let close_result = destination.close().await;
    assert!(close_result.is_ok());

    // Reconnect
    let reconnect_result = destination.connect(&connection_string).await;
    assert!(reconnect_result.is_ok());

    // Verify connection works again
    let values2 = RowData::from_pairs(vec![
        ("id", ColumnValue::text("2")),
        ("name", ColumnValue::text("Bob")),
    ]);
    let event2 = ChangeEvent::insert("public", "users", 2, values2, Lsn::from(100));
    let transaction2 = wrap_in_transaction(event2);
    let process_result2 = destination
        .execute_sql_batch_with_hook(&transaction_to_sql_commands(&transaction2), None)
        .await;
    assert!(process_result2.is_ok());

    let _ = destination.close().await;
}

#[cfg(feature = "sqlite")]
#[tokio::test]
async fn test_sqlite_file_permissions_and_paths() {
    // Test different path formats and edge cases
    let test_cases = vec![
        ("simple.db", true),
        ("./relative_path.db", true),
        ("nested/directory/test.db", true),
        ("file://explicit_file_protocol.db", true),
        ("sqlite://explicit_sqlite_protocol.db", true),
    ];

    for (path_format, should_succeed) in test_cases {
        println!("Testing path format: {}", path_format);

        let mut temp_path = std::env::temp_dir();
        temp_path.push(format!("pg2any_path_test_{}", std::process::id()));

        let full_path = if path_format.contains("/")
            && !path_format.starts_with("file://")
            && !path_format.starts_with("sqlite://")
        {
            temp_path.join(path_format).to_string_lossy().to_string()
        } else if path_format.starts_with("file://") {
            format!(
                "file://{}/{}",
                temp_path.to_string_lossy(),
                path_format.strip_prefix("file://").unwrap()
            )
        } else if path_format.starts_with("sqlite://") {
            format!(
                "sqlite://{}/{}",
                temp_path.to_string_lossy(),
                path_format.strip_prefix("sqlite://").unwrap()
            )
        } else {
            temp_path.join(path_format).to_string_lossy().to_string()
        };

        let mut destination = SQLiteDestination::new();
        let result = destination.connect(&full_path).await;

        if should_succeed {
            assert!(
                result.is_ok(),
                "Path format '{}' should succeed",
                path_format
            );
            let _ = destination.close().await;
        } else {
            assert!(result.is_err(), "Path format '{}' should fail", path_format);
        }

        // Clean up
        let _ = fs::remove_dir_all(&temp_path);
    }
}

// ================================
// INTEGRATION AND END-TO-END TESTS
// ================================

#[cfg(feature = "sqlite")]
#[tokio::test]
async fn test_sqlite_complete_crud_cycle() {
    let temp_db = TempDatabase::new("crud_cycle");
    let connection_string = temp_db.connection_string();

    let mut destination = SQLiteDestination::new();
    destination.connect(&connection_string).await.unwrap();

    let pool = SqlitePool::connect(&format!("sqlite://{}", connection_string))
        .await
        .unwrap();
    create_users_table(&pool).await.unwrap();

    // 1. CREATE (INSERT)
    let create_data = RowData::from_pairs(vec![
        ("id", ColumnValue::text("1")),
        ("name", ColumnValue::text("John Doe")),
        ("age", ColumnValue::text("30")),
        ("email", ColumnValue::text("john@example.com")),
        ("active", ColumnValue::text("true")),
        ("salary", ColumnValue::text("50000")),
    ]);

    let create_event = ChangeEvent::insert("main", "users", 1, create_data.clone(), Lsn::from(100));
    let create_result = destination
        .execute_sql_batch_with_hook(
            &transaction_to_sql_commands(&wrap_in_transaction(create_event)),
            None,
        )
        .await;
    assert!(create_result.is_ok());

    // Verify insertion
    let count_after_insert = sqlx::query("SELECT COUNT(*) as count FROM users WHERE id = 1")
        .fetch_one(&pool)
        .await
        .unwrap()
        .get::<i32, _>("count");
    assert_eq!(count_after_insert, 1);

    // 2. READ (verify data integrity)
    let row = sqlx::query("SELECT * FROM users WHERE id = 1")
        .fetch_one(&pool)
        .await
        .unwrap();

    assert_eq!(row.get::<String, _>("name"), "John Doe");
    assert_eq!(row.get::<i32, _>("age"), 30);
    assert_eq!(row.get::<String, _>("email"), "john@example.com");

    // 3. UPDATE
    let update_data = RowData::from_pairs(vec![
        ("id", ColumnValue::text("1")),
        ("name", ColumnValue::text("John Smith")),
        ("age", ColumnValue::text("31")),
        ("email", ColumnValue::text("john.smith@example.com")),
        ("active", ColumnValue::text("true")),
        ("salary", ColumnValue::text("55000")),
    ]);

    let update_event = ChangeEvent::update(
        "main",
        "users",
        2,
        Some(create_data),
        update_data,
        ReplicaIdentity::Default,
        vec![Arc::from("id")],
        Lsn::from(300),
    );

    let update_result = destination
        .execute_sql_batch_with_hook(
            &transaction_to_sql_commands(&wrap_in_transaction(update_event)),
            None,
        )
        .await;
    assert!(update_result.is_ok());

    // Verify update
    let updated_row = sqlx::query("SELECT name, age, email, salary FROM users WHERE id = 1")
        .fetch_one(&pool)
        .await
        .unwrap();

    assert_eq!(updated_row.get::<String, _>("name"), "John Smith");
    assert_eq!(updated_row.get::<i32, _>("age"), 31);
    assert_eq!(
        updated_row.get::<String, _>("email"),
        "john.smith@example.com"
    );
    assert_eq!(updated_row.get::<f64, _>("salary"), 55000.0);

    // 4. DELETE
    let delete_data = RowData::from_pairs(vec![("id", ColumnValue::text("1"))]);

    let delete_event = ChangeEvent::delete(
        "main",
        "users",
        3,
        delete_data,
        ReplicaIdentity::Default,
        vec![Arc::from("id")],
        Lsn::from(200),
    );

    let delete_result = destination
        .execute_sql_batch_with_hook(
            &transaction_to_sql_commands(&wrap_in_transaction(delete_event)),
            None,
        )
        .await;
    assert!(delete_result.is_ok());

    // Verify deletion
    let count_after_delete = sqlx::query("SELECT COUNT(*) as count FROM users WHERE id = 1")
        .fetch_one(&pool)
        .await
        .unwrap()
        .get::<i32, _>("count");
    assert_eq!(count_after_delete, 0);

    pool.close().await;
    let _ = destination.close().await;
}

#[cfg(feature = "sqlite")]
#[tokio::test]
async fn test_sqlite_destination_factory_integration() {
    // Test that the factory creates a working SQLite destination
    let mut destination = DestinationFactory::create(&DestinationType::SQLite).unwrap();

    let temp_db = TempDatabase::new("factory_integration");
    let connection_string = temp_db.connection_string();

    // Test connection through factory-created destination
    let connect_result = destination.connect(&connection_string).await;
    assert!(connect_result.is_ok());

    // Test basic operation
    let pool = SqlitePool::connect(&format!("sqlite://{}", connection_string))
        .await
        .unwrap();
    create_users_table(&pool).await.unwrap();

    let data = RowData::from_pairs(vec![
        ("id", ColumnValue::text("1")),
        ("name", ColumnValue::text("Factory Test")),
    ]);

    let event = ChangeEvent::insert("main", "users", 1, data, Lsn::from(100));
    let process_result = destination
        .execute_sql_batch_with_hook(
            &transaction_to_sql_commands(&wrap_in_transaction(event)),
            None,
        )
        .await;
    assert!(process_result.is_ok());

    pool.close().await;
    let _ = destination.close().await;
}