vespertide-macro 0.1.49

Macro entry for runtime migration execution
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
// MigrationOptions and MigrationError are now in vespertide-core

use std::env;
use std::path::PathBuf;

use proc_macro::TokenStream;
use quote::quote;
use syn::parse::{Parse, ParseStream};
use syn::{Expr, Ident, Token};
use vespertide_loader::{
    load_config_or_default, load_migrations_at_compile_time, load_models_at_compile_time,
};
use vespertide_planner::apply_action;
use vespertide_query::{DatabaseBackend, build_plan_queries};

struct MacroInput {
    pool: Expr,
    version_table: Option<String>,
    verbose: bool,
}

impl Parse for MacroInput {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let pool = input.parse()?;
        let mut version_table = None;
        let mut verbose = false;

        while !input.is_empty() {
            input.parse::<Token![,]>()?;
            if input.is_empty() {
                break;
            }

            let key: Ident = input.parse()?;
            if key == "version_table" {
                input.parse::<Token![=]>()?;
                let value: syn::LitStr = input.parse()?;
                version_table = Some(value.value());
            } else if key == "verbose" {
                verbose = true;
            } else {
                return Err(syn::Error::new(
                    key.span(),
                    "unsupported option for vespertide_migration!",
                ));
            }
        }

        Ok(MacroInput {
            pool,
            version_table,
            verbose,
        })
    }
}

pub(crate) fn build_migration_block(
    migration: &vespertide_core::MigrationPlan,
    baseline_schema: &mut Vec<vespertide_core::TableDef>,
    verbose: bool,
) -> Result<proc_macro2::TokenStream, String> {
    let version = migration.version;
    let migration_id = &migration.id;

    // Use the current baseline schema (from all previous migrations)
    let queries = build_plan_queries(migration, baseline_schema).map_err(|e| {
        format!(
            "Failed to build queries for migration version {}: {}",
            version, e
        )
    })?;

    // Update baseline schema incrementally by applying each action
    for action in &migration.actions {
        let _ = apply_action(baseline_schema, action);
    }

    // Generate version guard and SQL execution block
    let version_str = format!("v{}", version);
    let comment_str = migration.comment.as_deref().unwrap_or("").to_string();

    let block = if verbose {
        // Verbose mode: preserve per-action grouping with action descriptions
        let total_sql_count: usize = queries
            .iter()
            .map(|q| q.postgres.len().max(q.mysql.len()).max(q.sqlite.len()))
            .sum();
        let total_sql_count_lit = total_sql_count;

        let mut action_blocks = Vec::new();
        let mut global_idx: usize = 0;

        for (action_idx, q) in queries.iter().enumerate() {
            let action_desc = format!("{}", q.action);
            let action_num = action_idx + 1;
            let total_actions = queries.len();

            let pg: Vec<String> = q
                .postgres
                .iter()
                .map(|s| s.build(DatabaseBackend::Postgres))
                .collect();
            let mysql: Vec<String> = q
                .mysql
                .iter()
                .map(|s| s.build(DatabaseBackend::MySql))
                .collect();
            let sqlite: Vec<String> = q
                .sqlite
                .iter()
                .map(|s| s.build(DatabaseBackend::Sqlite))
                .collect();

            // Build per-SQL execution with global index
            let sql_count = pg.len().max(mysql.len()).max(sqlite.len());
            let mut sql_exec_blocks = Vec::new();

            for i in 0..sql_count {
                let idx = global_idx + i + 1;
                let pg_sql = pg.get(i).cloned().unwrap_or_default();
                let mysql_sql = mysql.get(i).cloned().unwrap_or_default();
                let sqlite_sql = sqlite.get(i).cloned().unwrap_or_default();

                sql_exec_blocks.push(quote! {
                    {
                        let sql: &str = match backend {
                            sea_orm::DatabaseBackend::Postgres => #pg_sql,
                            sea_orm::DatabaseBackend::MySql => #mysql_sql,
                            sea_orm::DatabaseBackend::Sqlite => #sqlite_sql,
                            _ => #pg_sql,
                        };
                        if !sql.is_empty() {
                            eprintln!("[vespertide]     [{}/{}] {}", #idx, #total_sql_count_lit, sql);
                            let stmt = sea_orm::Statement::from_string(backend, sql);
                            __txn.execute_raw(stmt).await.map_err(|e| {
                                ::vespertide::MigrationError::DatabaseError(format!("Failed to execute SQL '{}': {}", sql, e))
                            })?;
                        }
                    }
                });
            }
            global_idx += sql_count;

            action_blocks.push(quote! {
                eprintln!("[vespertide]   Action {}/{}: {}", #action_num, #total_actions, #action_desc);
                #(#sql_exec_blocks)*
            });
        }

        quote! {
            if __version < #version {
                // Validate migration id against database if version already tracked
                if let Some(db_id) = __version_ids.get(&#version) {
                    let expected_id: &str = #migration_id;
                    if !expected_id.is_empty() && !db_id.is_empty() && db_id != expected_id {
                        return Err(::vespertide::MigrationError::IdMismatch {
                            version: #version,
                            expected: expected_id.to_string(),
                            found: db_id.clone(),
                        });
                    }
                }

                eprintln!("[vespertide] Applying migration {} ({})", #version_str, #comment_str);
                #(#action_blocks)*

                let insert_sql = format!("INSERT INTO {q}{}{q} (version, id) VALUES ({}, '{}')", __version_table, #version, #migration_id);
                let stmt = sea_orm::Statement::from_string(backend, insert_sql);
                __txn.execute_raw(stmt).await.map_err(|e| {
                    ::vespertide::MigrationError::DatabaseError(format!("Failed to insert version: {}", e))
                })?;

                eprintln!("[vespertide] Migration {} applied successfully", #version_str);
            }
        }
    } else {
        // Non-verbose: flatten all SQL into one array (minimal overhead)
        let mut pg_sqls = Vec::new();
        let mut mysql_sqls = Vec::new();
        let mut sqlite_sqls = Vec::new();

        for q in &queries {
            for stmt in &q.postgres {
                pg_sqls.push(stmt.build(DatabaseBackend::Postgres));
            }
            for stmt in &q.mysql {
                mysql_sqls.push(stmt.build(DatabaseBackend::MySql));
            }
            for stmt in &q.sqlite {
                sqlite_sqls.push(stmt.build(DatabaseBackend::Sqlite));
            }
        }

        quote! {
            if __version < #version {
                // Validate migration id against database if version already tracked
                if let Some(db_id) = __version_ids.get(&#version) {
                    let expected_id: &str = #migration_id;
                    if !expected_id.is_empty() && !db_id.is_empty() && db_id != expected_id {
                        return Err(::vespertide::MigrationError::IdMismatch {
                            version: #version,
                            expected: expected_id.to_string(),
                            found: db_id.clone(),
                        });
                    }
                }

                let sqls: &[&str] = match backend {
                    sea_orm::DatabaseBackend::Postgres => &[#(#pg_sqls),*],
                    sea_orm::DatabaseBackend::MySql => &[#(#mysql_sqls),*],
                    sea_orm::DatabaseBackend::Sqlite => &[#(#sqlite_sqls),*],
                    _ => &[#(#pg_sqls),*],
                };

                for sql in sqls {
                    if !sql.is_empty() {
                        let stmt = sea_orm::Statement::from_string(backend, *sql);
                        __txn.execute_raw(stmt).await.map_err(|e| {
                            ::vespertide::MigrationError::DatabaseError(format!("Failed to execute SQL '{}': {}", sql, e))
                        })?;
                    }
                }

                let insert_sql = format!("INSERT INTO {q}{}{q} (version, id) VALUES ({}, '{}')", __version_table, #version, #migration_id);
                let stmt = sea_orm::Statement::from_string(backend, insert_sql);
                __txn.execute_raw(stmt).await.map_err(|e| {
                    ::vespertide::MigrationError::DatabaseError(format!("Failed to insert version: {}", e))
                })?;
            }
        }
    };

    Ok(block)
}

fn generate_migration_code(
    pool: &Expr,
    version_table: &str,
    migration_blocks: Vec<proc_macro2::TokenStream>,
    verbose: bool,
) -> proc_macro2::TokenStream {
    let verbose_current_version = if verbose {
        quote! {
            eprintln!("[vespertide] Current database version: {}", __version);
        }
    } else {
        quote! {}
    };

    quote! {
        async {
            use sea_orm::{ConnectionTrait, TransactionTrait};
            let __pool = &#pool;
            let __version_table = #version_table;
            let backend = __pool.get_database_backend();
            let q = if matches!(backend, sea_orm::DatabaseBackend::MySql) { '`' } else { '"' };

            // Create version table if it does not exist (outside transaction)
            let create_table_sql = format!(
                "CREATE TABLE IF NOT EXISTS {q}{}{q} (version INTEGER PRIMARY KEY, id TEXT DEFAULT '', created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP)",
                __version_table
            );
            let stmt = sea_orm::Statement::from_string(backend, create_table_sql);
            __pool.execute_raw(stmt).await.map_err(|e| {
                ::vespertide::MigrationError::DatabaseError(format!("Failed to create version table: {}", e))
            })?;

            // Add id column for existing tables that don't have it yet (backward compatibility).
            // We use a try-and-ignore approach: if the column already exists, the ALTER will fail
            // and we simply ignore the error.
            let alter_sql = format!(
                "ALTER TABLE {q}{}{q} ADD COLUMN id TEXT DEFAULT ''",
                __version_table
            );
            let stmt = sea_orm::Statement::from_string(backend, alter_sql);
            let _ = __pool.execute_raw(stmt).await;

            // Single transaction for the entire migration process.
            // This prevents race conditions when multiple connections exist
            // (e.g. SQLite with max_connections > 1).
            let __txn = __pool.begin().await.map_err(|e| {
                ::vespertide::MigrationError::DatabaseError(format!("Failed to begin transaction: {}", e))
            })?;

            // Read current maximum version inside the transaction (holds lock)
            let select_sql = format!("SELECT MAX(version) as version FROM {q}{}{q}", __version_table);
            let stmt = sea_orm::Statement::from_string(backend, select_sql);
            let version_result = __txn.query_one_raw(stmt).await.map_err(|e| {
                ::vespertide::MigrationError::DatabaseError(format!("Failed to read version: {}", e))
            })?;

            let __version = version_result
                .and_then(|row| row.try_get::<i32>("", "version").ok())
                .unwrap_or(0) as u32;

            // Load all existing (version, id) pairs for id mismatch validation
            let select_ids_sql = format!("SELECT version, id FROM {q}{}{q}", __version_table);
            let stmt = sea_orm::Statement::from_string(backend, select_ids_sql);
            let id_rows = __txn.query_all_raw(stmt).await.map_err(|e| {
                ::vespertide::MigrationError::DatabaseError(format!("Failed to read version ids: {}", e))
            })?;

            let mut __version_ids = std::collections::HashMap::<u32, String>::new();
            for row in &id_rows {
                if let Ok(v) = row.try_get::<i32>("", "version") {
                    let id = row.try_get::<String>("", "id").unwrap_or_default();
                    __version_ids.insert(v as u32, id);
                }
            }

            #verbose_current_version

            // Execute each migration block within the same transaction
            #(#migration_blocks)*

            // Commit the entire migration
            __txn.commit().await.map_err(|e| {
                ::vespertide::MigrationError::DatabaseError(format!("Failed to commit transaction: {}", e))
            })?;

            Ok::<(), ::vespertide::MigrationError>(())
        }
    }
}

/// Inner implementation that works with proc_macro2::TokenStream for testability.
pub(crate) fn vespertide_migration_impl(
    input: proc_macro2::TokenStream,
) -> proc_macro2::TokenStream {
    let input: MacroInput = match syn::parse2(input) {
        Ok(input) => input,
        Err(e) => return e.to_compile_error(),
    };
    let pool = &input.pool;
    let verbose = input.verbose;

    // Get project root from CARGO_MANIFEST_DIR (same as load_migrations_at_compile_time)
    let project_root = match env::var("CARGO_MANIFEST_DIR") {
        Ok(dir) => Some(PathBuf::from(dir)),
        Err(_) => None,
    };

    // Load config to get prefix
    let config = match load_config_or_default(project_root) {
        Ok(config) => config,
        #[cfg(not(tarpaulin_include))]
        Err(e) => {
            return syn::Error::new(
                proc_macro2::Span::call_site(),
                format!("Failed to load config at compile time: {}", e),
            )
            .to_compile_error();
        }
    };
    let prefix = config.prefix();

    // Apply prefix to version_table if not explicitly provided
    let version_table = input
        .version_table
        .map(|vt| config.apply_prefix(&vt))
        .unwrap_or_else(|| config.apply_prefix("vespertide_version"));

    // Load migration files and build SQL at compile time
    let migrations = match load_migrations_at_compile_time() {
        Ok(migrations) => migrations,
        Err(e) => {
            return syn::Error::new(
                proc_macro2::Span::call_site(),
                format!("Failed to load migrations at compile time: {}", e),
            )
            .to_compile_error();
        }
    };
    let _models = match load_models_at_compile_time() {
        Ok(models) => models,
        #[cfg(not(tarpaulin_include))]
        Err(e) => {
            return syn::Error::new(
                proc_macro2::Span::call_site(),
                format!("Failed to load models at compile time: {}", e),
            )
            .to_compile_error();
        }
    };

    // Apply prefix to migrations and build SQL using incremental baseline schema
    let mut baseline_schema = Vec::new();
    let mut migration_blocks = Vec::new();

    #[cfg(not(tarpaulin_include))]
    for migration in &migrations {
        // Apply prefix to migration table names
        let prefixed_migration = migration.clone().with_prefix(prefix);
        match build_migration_block(&prefixed_migration, &mut baseline_schema, verbose) {
            Ok(block) => migration_blocks.push(block),
            Err(e) => {
                return syn::Error::new(proc_macro2::Span::call_site(), e).to_compile_error();
            }
        }
    }

    generate_migration_code(pool, &version_table, migration_blocks, verbose)
}

/// Zero-runtime migration entry point.
#[cfg(not(tarpaulin_include))]
#[proc_macro]
pub fn vespertide_migration(input: TokenStream) -> TokenStream {
    vespertide_migration_impl(input.into()).into()
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs::File;
    use std::io::Write;
    use tempfile::tempdir;
    use vespertide_core::{
        ColumnDef, ColumnType, MigrationAction, MigrationPlan, SimpleColumnType, StrOrBoolOrArray,
    };

    #[test]
    fn test_macro_expansion_with_runtime_macros() {
        // Create a temporary directory with test files
        let dir = tempdir().unwrap();

        // Create a test file that uses the macro
        let test_file_path = dir.path().join("test_macro.rs");
        let mut test_file = File::create(&test_file_path).unwrap();
        writeln!(
            test_file,
            r#"vespertide_migration!(pool, version_table = "test_versions");"#
        )
        .unwrap();

        // Use runtime-macros to emulate macro expansion
        let file = File::open(&test_file_path).unwrap();
        let result = runtime_macros::emulate_functionlike_macro_expansion(
            file,
            &[("vespertide_migration", vespertide_migration_impl)],
        );

        // The macro will fail because there's no vespertide config, but
        // the important thing is that it runs and covers the macro code
        // We expect an error due to missing config
        assert!(result.is_ok() || result.is_err());
    }

    #[test]
    fn test_macro_with_simple_pool() {
        let dir = tempdir().unwrap();
        let test_file_path = dir.path().join("test_simple.rs");
        let mut test_file = File::create(&test_file_path).unwrap();
        writeln!(test_file, r#"vespertide_migration!(db_pool);"#).unwrap();

        let file = File::open(&test_file_path).unwrap();
        let result = runtime_macros::emulate_functionlike_macro_expansion(
            file,
            &[("vespertide_migration", vespertide_migration_impl)],
        );

        assert!(result.is_ok() || result.is_err());
    }

    #[test]
    fn test_macro_parsing_invalid_option() {
        // Test that invalid options produce a compile error
        let input: proc_macro2::TokenStream = "pool, invalid_option = \"value\"".parse().unwrap();
        let output = vespertide_migration_impl(input);
        let output_str = output.to_string();
        // Should contain an error message about unsupported option
        assert!(output_str.contains("unsupported option"));
    }

    #[test]
    fn test_macro_parsing_valid_input() {
        // Test that valid input is parsed correctly
        // The macro will either succeed (if migrations dir exists and is empty)
        // or fail with a migration loading error
        let input: proc_macro2::TokenStream = "my_pool".parse().unwrap();
        let output = vespertide_migration_impl(input);
        let output_str = output.to_string();
        // Should produce output (either success or migration loading error)
        assert!(!output_str.is_empty());
        // If error, it should mention "Failed to load"
        // If success, it should contain "async"
        assert!(
            output_str.contains("async") || output_str.contains("Failed to load"),
            "Unexpected output: {}",
            output_str
        );
    }

    #[test]
    fn test_macro_parsing_with_version_table() {
        let input: proc_macro2::TokenStream =
            r#"pool, version_table = "custom_versions""#.parse().unwrap();
        let output = vespertide_migration_impl(input);
        let output_str = output.to_string();
        assert!(!output_str.is_empty());
    }

    #[test]
    fn test_macro_parsing_trailing_comma() {
        let input: proc_macro2::TokenStream = "pool,".parse().unwrap();
        let output = vespertide_migration_impl(input);
        let output_str = output.to_string();
        assert!(!output_str.is_empty());
    }

    fn test_column(name: &str) -> ColumnDef {
        ColumnDef {
            name: name.into(),
            r#type: ColumnType::Simple(SimpleColumnType::Integer),
            nullable: false,
            default: None,
            comment: None,
            primary_key: None,
            unique: None,
            index: None,
            foreign_key: None,
        }
    }

    #[test]
    fn test_build_migration_block_create_table() {
        let migration = MigrationPlan {
            id: String::new(),
            version: 1,
            comment: None,
            created_at: None,
            actions: vec![MigrationAction::CreateTable {
                table: "users".into(),
                columns: vec![test_column("id")],
                constraints: vec![],
            }],
        };

        let mut baseline = Vec::new();
        let result = build_migration_block(&migration, &mut baseline, false);

        assert!(result.is_ok());
        let block = result.unwrap();
        let block_str = block.to_string();

        // Verify the generated block contains expected elements
        assert!(block_str.contains("version < 1u32"));
        assert!(block_str.contains("CREATE TABLE"));

        // Verify baseline schema was updated
        assert_eq!(baseline.len(), 1);
        assert_eq!(baseline[0].name, "users");
    }

    #[test]
    fn test_build_migration_block_add_column() {
        // First create the table
        let create_migration = MigrationPlan {
            id: String::new(),
            version: 1,
            comment: None,
            created_at: None,
            actions: vec![MigrationAction::CreateTable {
                table: "users".into(),
                columns: vec![test_column("id")],
                constraints: vec![],
            }],
        };

        let mut baseline = Vec::new();
        let _ = build_migration_block(&create_migration, &mut baseline, false);

        // Now add a column
        let add_column_migration = MigrationPlan {
            id: String::new(),
            version: 2,
            comment: None,
            created_at: None,
            actions: vec![MigrationAction::AddColumn {
                table: "users".into(),
                column: Box::new(ColumnDef {
                    name: "email".into(),
                    r#type: ColumnType::Simple(SimpleColumnType::Text),
                    nullable: true,
                    default: None,
                    comment: None,
                    primary_key: None,
                    unique: None,
                    index: None,
                    foreign_key: None,
                }),
                fill_with: None,
            }],
        };

        let result = build_migration_block(&add_column_migration, &mut baseline, false);
        assert!(result.is_ok());
        let block = result.unwrap();
        let block_str = block.to_string();

        assert!(block_str.contains("version < 2u32"));
        assert!(block_str.contains("ALTER TABLE"));
        assert!(block_str.contains("ADD COLUMN"));
    }

    #[test]
    fn test_build_migration_block_multiple_actions() {
        let migration = MigrationPlan {
            id: String::new(),
            version: 1,
            comment: None,
            created_at: None,
            actions: vec![
                MigrationAction::CreateTable {
                    table: "users".into(),
                    columns: vec![test_column("id")],
                    constraints: vec![],
                },
                MigrationAction::CreateTable {
                    table: "posts".into(),
                    columns: vec![test_column("id")],
                    constraints: vec![],
                },
            ],
        };

        let mut baseline = Vec::new();
        let result = build_migration_block(&migration, &mut baseline, false);

        assert!(result.is_ok());
        assert_eq!(baseline.len(), 2);
    }

    #[test]
    fn test_generate_migration_code() {
        let pool: Expr = syn::parse_str("db_pool").unwrap();
        let version_table = "test_versions";

        // Create a simple migration block
        let migration = MigrationPlan {
            id: String::new(),
            version: 1,
            comment: None,
            created_at: None,
            actions: vec![MigrationAction::CreateTable {
                table: "users".into(),
                columns: vec![test_column("id")],
                constraints: vec![],
            }],
        };

        let mut baseline = Vec::new();
        let block = build_migration_block(&migration, &mut baseline, false).unwrap();

        let generated = generate_migration_code(&pool, version_table, vec![block], false);
        let generated_str = generated.to_string();

        // Verify the generated code structure
        assert!(generated_str.contains("async"));
        assert!(generated_str.contains("db_pool"));
        assert!(generated_str.contains("test_versions"));
        assert!(generated_str.contains("CREATE TABLE IF NOT EXISTS"));
        assert!(generated_str.contains("SELECT MAX"));
    }

    #[test]
    fn test_generate_migration_code_empty_migrations() {
        let pool: Expr = syn::parse_str("pool").unwrap();
        let version_table = "vespertide_version";

        let generated = generate_migration_code(&pool, version_table, vec![], false);
        let generated_str = generated.to_string();

        // Should still generate the wrapper code
        assert!(generated_str.contains("async"));
        assert!(generated_str.contains("vespertide_version"));
    }

    #[test]
    fn test_generate_migration_code_multiple_blocks() {
        let pool: Expr = syn::parse_str("connection").unwrap();

        let mut baseline = Vec::new();

        let migration1 = MigrationPlan {
            id: String::new(),
            version: 1,
            comment: None,
            created_at: None,
            actions: vec![MigrationAction::CreateTable {
                table: "users".into(),
                columns: vec![test_column("id")],
                constraints: vec![],
            }],
        };
        let block1 = build_migration_block(&migration1, &mut baseline, false).unwrap();

        let migration2 = MigrationPlan {
            id: String::new(),
            version: 2,
            comment: None,
            created_at: None,
            actions: vec![MigrationAction::CreateTable {
                table: "posts".into(),
                columns: vec![test_column("id")],
                constraints: vec![],
            }],
        };
        let block2 = build_migration_block(&migration2, &mut baseline, false).unwrap();

        let generated = generate_migration_code(&pool, "migrations", vec![block1, block2], false);
        let generated_str = generated.to_string();

        // Both version checks should be present
        assert!(generated_str.contains("version < 1u32"));
        assert!(generated_str.contains("version < 2u32"));
    }

    #[test]
    fn test_build_migration_block_generates_all_backends() {
        let migration = MigrationPlan {
            id: String::new(),
            version: 1,
            comment: None,
            created_at: None,
            actions: vec![MigrationAction::CreateTable {
                table: "test_table".into(),
                columns: vec![test_column("id")],
                constraints: vec![],
            }],
        };

        let mut baseline = Vec::new();
        let result = build_migration_block(&migration, &mut baseline, false);
        assert!(result.is_ok());

        let block_str = result.unwrap().to_string();

        // The generated block should have backend matching
        assert!(block_str.contains("DatabaseBackend :: Postgres"));
        assert!(block_str.contains("DatabaseBackend :: MySql"));
        assert!(block_str.contains("DatabaseBackend :: Sqlite"));
    }

    #[test]
    fn test_build_migration_block_with_delete_table() {
        // First create the table
        let create_migration = MigrationPlan {
            id: String::new(),
            version: 1,
            comment: None,
            created_at: None,
            actions: vec![MigrationAction::CreateTable {
                table: "temp_table".into(),
                columns: vec![test_column("id")],
                constraints: vec![],
            }],
        };

        let mut baseline = Vec::new();
        let _ = build_migration_block(&create_migration, &mut baseline, false);
        assert_eq!(baseline.len(), 1);

        // Now delete it
        let delete_migration = MigrationPlan {
            id: String::new(),
            version: 2,
            comment: None,
            created_at: None,
            actions: vec![MigrationAction::DeleteTable {
                table: "temp_table".into(),
            }],
        };

        let result = build_migration_block(&delete_migration, &mut baseline, false);
        assert!(result.is_ok());
        let block_str = result.unwrap().to_string();
        assert!(block_str.contains("DROP TABLE"));

        // Baseline should be empty after delete
        assert_eq!(baseline.len(), 0);
    }

    #[test]
    fn test_build_migration_block_with_index() {
        let migration = MigrationPlan {
            id: String::new(),
            version: 1,
            comment: None,
            created_at: None,
            actions: vec![MigrationAction::CreateTable {
                table: "users".into(),
                columns: vec![
                    test_column("id"),
                    ColumnDef {
                        name: "email".into(),
                        r#type: ColumnType::Simple(SimpleColumnType::Text),
                        nullable: true,
                        default: None,
                        comment: None,
                        primary_key: None,
                        unique: None,
                        index: Some(StrOrBoolOrArray::Bool(true)),
                        foreign_key: None,
                    },
                ],
                constraints: vec![],
            }],
        };

        let mut baseline = Vec::new();
        let result = build_migration_block(&migration, &mut baseline, false);
        assert!(result.is_ok());

        // Table should be normalized with index
        let table = &baseline[0];
        let normalized = table.clone().normalize();
        assert!(normalized.is_ok());
    }

    #[test]
    fn test_build_migration_block_error_nonexistent_table() {
        // Try to add column to a table that doesn't exist - should fail
        let migration = MigrationPlan {
            id: String::new(),
            version: 1,
            comment: None,
            created_at: None,
            actions: vec![MigrationAction::AddColumn {
                table: "nonexistent_table".into(),
                column: Box::new(test_column("new_col")),
                fill_with: None,
            }],
        };

        let mut baseline = Vec::new();
        let result = build_migration_block(&migration, &mut baseline, false);

        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err.contains("Failed to build queries for migration version 1"));
    }

    #[test]
    fn test_vespertide_migration_impl_loading_error() {
        // Save original CARGO_MANIFEST_DIR
        let original = std::env::var("CARGO_MANIFEST_DIR").ok();

        // Remove CARGO_MANIFEST_DIR to trigger loading error
        unsafe {
            std::env::remove_var("CARGO_MANIFEST_DIR");
        }

        let input: proc_macro2::TokenStream = "pool".parse().unwrap();
        let output = vespertide_migration_impl(input);
        let output_str = output.to_string();

        // Should contain error about failed loading
        assert!(
            output_str.contains("Failed to load migrations at compile time"),
            "Expected loading error, got: {}",
            output_str
        );

        // Restore CARGO_MANIFEST_DIR
        if let Some(val) = original {
            unsafe {
                std::env::set_var("CARGO_MANIFEST_DIR", val);
            }
        }
    }

    #[test]
    fn test_vespertide_migration_impl_with_valid_project() {
        use std::fs;

        // Create a temporary directory with a valid vespertide project
        let dir = tempdir().unwrap();
        let project_dir = dir.path();

        // Create vespertide.json config
        let config_content = r#"{
            "modelsDir": "models",
            "migrationsDir": "migrations",
            "tableNamingCase": "snake",
            "columnNamingCase": "snake",
            "modelFormat": "json"
        }"#;
        fs::write(project_dir.join("vespertide.json"), config_content).unwrap();

        // Create empty models and migrations directories
        fs::create_dir_all(project_dir.join("models")).unwrap();
        fs::create_dir_all(project_dir.join("migrations")).unwrap();

        // Save original CARGO_MANIFEST_DIR and set to temp dir
        let original = std::env::var("CARGO_MANIFEST_DIR").ok();
        unsafe {
            std::env::set_var("CARGO_MANIFEST_DIR", project_dir);
        }

        let input: proc_macro2::TokenStream = "pool".parse().unwrap();
        let output = vespertide_migration_impl(input);
        let output_str = output.to_string();

        // Should produce valid async code since there are no migrations
        assert!(
            output_str.contains("async"),
            "Expected async block, got: {}",
            output_str
        );
        assert!(
            output_str.contains("CREATE TABLE IF NOT EXISTS"),
            "Expected version table creation, got: {}",
            output_str
        );

        // Restore CARGO_MANIFEST_DIR
        if let Some(val) = original {
            unsafe {
                std::env::set_var("CARGO_MANIFEST_DIR", val);
            }
        } else {
            unsafe {
                std::env::remove_var("CARGO_MANIFEST_DIR");
            }
        }
    }

    #[test]
    fn test_build_migration_block_verbose_create_table() {
        let migration = MigrationPlan {
            id: String::new(),
            version: 1,
            comment: Some("initial setup".into()),
            created_at: None,
            actions: vec![MigrationAction::CreateTable {
                table: "users".into(),
                columns: vec![test_column("id")],
                constraints: vec![],
            }],
        };

        let mut baseline = Vec::new();
        let result = build_migration_block(&migration, &mut baseline, true);

        assert!(result.is_ok());
        let block_str = result.unwrap().to_string();

        // Verbose mode should contain eprintln statements with action descriptions
        assert!(block_str.contains("vespertide"));
        assert!(block_str.contains("Action"));
        assert!(block_str.contains("version < 1u32"));
    }

    #[test]
    fn test_build_migration_block_verbose_multiple_actions() {
        let migration = MigrationPlan {
            id: String::new(),
            version: 1,
            comment: None,
            created_at: None,
            actions: vec![
                MigrationAction::CreateTable {
                    table: "users".into(),
                    columns: vec![test_column("id")],
                    constraints: vec![],
                },
                MigrationAction::CreateTable {
                    table: "posts".into(),
                    columns: vec![test_column("id")],
                    constraints: vec![],
                },
            ],
        };

        let mut baseline = Vec::new();
        let result = build_migration_block(&migration, &mut baseline, true);

        assert!(result.is_ok());
        let block_str = result.unwrap().to_string();

        // Should have action numbering for both actions
        assert!(block_str.contains("Action"));
        assert_eq!(baseline.len(), 2);
    }

    #[test]
    fn test_build_migration_block_verbose_add_column() {
        // Create table first
        let create = MigrationPlan {
            id: String::new(),
            version: 1,
            comment: None,
            created_at: None,
            actions: vec![MigrationAction::CreateTable {
                table: "users".into(),
                columns: vec![test_column("id")],
                constraints: vec![],
            }],
        };
        let mut baseline = Vec::new();
        let _ = build_migration_block(&create, &mut baseline, true);

        // Add column in verbose mode
        let add_col = MigrationPlan {
            id: String::new(),
            version: 2,
            comment: Some("add email".into()),
            created_at: None,
            actions: vec![MigrationAction::AddColumn {
                table: "users".into(),
                column: Box::new(ColumnDef {
                    name: "email".into(),
                    r#type: ColumnType::Simple(SimpleColumnType::Text),
                    nullable: true,
                    default: None,
                    comment: None,
                    primary_key: None,
                    unique: None,
                    index: None,
                    foreign_key: None,
                }),
                fill_with: None,
            }],
        };

        let result = build_migration_block(&add_col, &mut baseline, true);
        assert!(result.is_ok());
        let block_str = result.unwrap().to_string();
        assert!(block_str.contains("vespertide"));
        assert!(block_str.contains("version < 2u32"));
    }

    #[test]
    fn test_generate_migration_code_verbose() {
        let pool: Expr = syn::parse_str("db_pool").unwrap();
        let version_table = "test_versions";

        let migration = MigrationPlan {
            id: String::new(),
            version: 1,
            comment: None,
            created_at: None,
            actions: vec![MigrationAction::CreateTable {
                table: "users".into(),
                columns: vec![test_column("id")],
                constraints: vec![],
            }],
        };

        let mut baseline = Vec::new();
        let block = build_migration_block(&migration, &mut baseline, true).unwrap();

        let generated = generate_migration_code(&pool, version_table, vec![block], true);
        let generated_str = generated.to_string();

        // Verbose mode should include current version eprintln
        assert!(generated_str.contains("Current database version"));
        assert!(generated_str.contains("async"));
    }

    #[test]
    fn test_macro_parsing_verbose_flag() {
        // Test parsing the "verbose" keyword
        let input: proc_macro2::TokenStream = "pool, verbose".parse().unwrap();
        let output = vespertide_migration_impl(input);
        let output_str = output.to_string();
        // Should produce output (either success or migration loading error)
        assert!(!output_str.is_empty());
    }

    #[test]
    fn test_vespertide_migration_impl_with_migrations() {
        use std::fs;

        // Create a temporary directory with a valid vespertide project and migrations
        let dir = tempdir().unwrap();
        let project_dir = dir.path();

        // Create vespertide.json config
        let config_content = r#"{
            "modelsDir": "models",
            "migrationsDir": "migrations",
            "tableNamingCase": "snake",
            "columnNamingCase": "snake",
            "modelFormat": "json"
        }"#;
        fs::write(project_dir.join("vespertide.json"), config_content).unwrap();

        // Create models and migrations directories
        fs::create_dir_all(project_dir.join("models")).unwrap();
        fs::create_dir_all(project_dir.join("migrations")).unwrap();

        // Create a migration file
        let migration_content = r#"{
            "version": 1,
            "actions": [
                {
                    "type": "create_table",
                    "table": "users",
                    "columns": [
                        {"name": "id", "type": "integer", "nullable": false}
                    ],
                    "constraints": []
                }
            ]
        }"#;
        fs::write(
            project_dir.join("migrations").join("0001_initial.json"),
            migration_content,
        )
        .unwrap();

        // Save original CARGO_MANIFEST_DIR and set to temp dir
        let original = std::env::var("CARGO_MANIFEST_DIR").ok();
        unsafe {
            std::env::set_var("CARGO_MANIFEST_DIR", project_dir);
        }

        let input: proc_macro2::TokenStream = "pool".parse().unwrap();
        let output = vespertide_migration_impl(input);
        let output_str = output.to_string();

        // Should produce valid async code with migration
        assert!(
            output_str.contains("async"),
            "Expected async block, got: {}",
            output_str
        );

        // Restore CARGO_MANIFEST_DIR
        if let Some(val) = original {
            unsafe {
                std::env::set_var("CARGO_MANIFEST_DIR", val);
            }
        } else {
            unsafe {
                std::env::remove_var("CARGO_MANIFEST_DIR");
            }
        }
    }
}