orion-server 0.2.0

Declarative services runtime powered by dataflow-rs
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
//! Migration SQL generator for multiple database backends.
//!
//! Uses sea-query's DDL API to define tables once in Rust, then generates
//! backend-specific SQL for SQLite, PostgreSQL, and MySQL.
//!
//! Run the tests with `cargo test migration_gen` to regenerate migration files.

use sea_query::{
    ColumnDef, Expr, Index, IndexCreateStatement, MysqlQueryBuilder, PostgresQueryBuilder,
    SqliteQueryBuilder, Table, TableCreateStatement,
};

use super::schema::*;
use crate::errors::OrionError;

// ============================================================
// Table definitions
// ============================================================

fn workflows_table() -> TableCreateStatement {
    Table::create()
        .table(Workflows::Table)
        .if_not_exists()
        .col(ColumnDef::new(Workflows::WorkflowId).text().not_null())
        .col(ColumnDef::new(Workflows::Version).integer().not_null())
        .col(ColumnDef::new(Workflows::Name).text().not_null())
        .col(ColumnDef::new(Workflows::Description).text())
        .col(
            ColumnDef::new(Workflows::Priority)
                .integer()
                .not_null()
                .default(0),
        )
        .col(
            ColumnDef::new(Workflows::Status)
                .text()
                .not_null()
                .default("draft"),
        )
        .col(
            ColumnDef::new(Workflows::RolloutPercentage)
                .integer()
                .not_null()
                .default(100),
        )
        .col(
            ColumnDef::new(Workflows::ConditionJson)
                .text()
                .not_null()
                .default("true"),
        )
        .col(
            ColumnDef::new(Workflows::TasksJson)
                .text()
                .not_null()
                .default("[]"),
        )
        .col(
            ColumnDef::new(Workflows::Tags)
                .text()
                .not_null()
                .default("[]"),
        )
        .col(
            ColumnDef::new(Workflows::ContinueOnError)
                .boolean()
                .not_null()
                .default(false),
        )
        .col(
            ColumnDef::new(Workflows::CreatedAt)
                .timestamp()
                .not_null()
                .default(Expr::current_timestamp()),
        )
        .col(
            ColumnDef::new(Workflows::UpdatedAt)
                .timestamp()
                .not_null()
                .default(Expr::current_timestamp()),
        )
        .primary_key(
            Index::create()
                .col(Workflows::WorkflowId)
                .col(Workflows::Version),
        )
        .to_owned()
}

fn channels_table() -> TableCreateStatement {
    Table::create()
        .table(Channels::Table)
        .if_not_exists()
        .col(ColumnDef::new(Channels::ChannelId).text().not_null())
        .col(ColumnDef::new(Channels::Version).integer().not_null())
        .col(ColumnDef::new(Channels::Name).text().not_null())
        .col(ColumnDef::new(Channels::Description).text())
        .col(ColumnDef::new(Channels::ChannelType).text().not_null())
        .col(ColumnDef::new(Channels::Protocol).text().not_null())
        .col(ColumnDef::new(Channels::Methods).text())
        .col(ColumnDef::new(Channels::RoutePattern).text())
        .col(ColumnDef::new(Channels::Topic).text())
        .col(ColumnDef::new(Channels::ConsumerGroup).text())
        .col(
            ColumnDef::new(Channels::TransportConfigJson)
                .text()
                .not_null()
                .default("{}"),
        )
        .col(ColumnDef::new(Channels::WorkflowId).text())
        .col(
            ColumnDef::new(Channels::ConfigJson)
                .text()
                .not_null()
                .default("{}"),
        )
        .col(
            ColumnDef::new(Channels::Status)
                .text()
                .not_null()
                .default("draft"),
        )
        .col(
            ColumnDef::new(Channels::Priority)
                .integer()
                .not_null()
                .default(0),
        )
        .col(
            ColumnDef::new(Channels::CreatedAt)
                .timestamp()
                .not_null()
                .default(Expr::current_timestamp()),
        )
        .col(
            ColumnDef::new(Channels::UpdatedAt)
                .timestamp()
                .not_null()
                .default(Expr::current_timestamp()),
        )
        .primary_key(
            Index::create()
                .col(Channels::ChannelId)
                .col(Channels::Version),
        )
        .to_owned()
}

fn connectors_table() -> TableCreateStatement {
    Table::create()
        .table(Connectors::Table)
        .if_not_exists()
        .col(
            ColumnDef::new(Connectors::Id)
                .text()
                .not_null()
                .primary_key(),
        )
        .col(
            ColumnDef::new(Connectors::Name)
                .text()
                .not_null()
                .unique_key(),
        )
        .col(ColumnDef::new(Connectors::ConnectorType).text().not_null())
        .col(
            ColumnDef::new(Connectors::ConfigJson)
                .text()
                .not_null()
                .default("{}"),
        )
        .col(
            ColumnDef::new(Connectors::Enabled)
                .boolean()
                .not_null()
                .default(true),
        )
        .col(
            ColumnDef::new(Connectors::CreatedAt)
                .timestamp()
                .not_null()
                .default(Expr::current_timestamp()),
        )
        .col(
            ColumnDef::new(Connectors::UpdatedAt)
                .timestamp()
                .not_null()
                .default(Expr::current_timestamp()),
        )
        .to_owned()
}

fn traces_table() -> TableCreateStatement {
    Table::create()
        .table(Traces::Table)
        .if_not_exists()
        .col(ColumnDef::new(Traces::Id).text().not_null().primary_key())
        .col(
            ColumnDef::new(Traces::Channel)
                .text()
                .not_null()
                .default("default"),
        )
        .col(ColumnDef::new(Traces::ChannelId).text())
        .col(
            ColumnDef::new(Traces::Mode)
                .text()
                .not_null()
                .default("sync"),
        )
        .col(
            ColumnDef::new(Traces::Status)
                .text()
                .not_null()
                .default("pending"),
        )
        .col(ColumnDef::new(Traces::InputJson).text())
        .col(ColumnDef::new(Traces::ResultJson).text())
        .col(ColumnDef::new(Traces::ErrorMessage).text())
        .col(ColumnDef::new(Traces::DurationMs).double())
        .col(ColumnDef::new(Traces::StartedAt).timestamp())
        .col(ColumnDef::new(Traces::CompletedAt).timestamp())
        .col(
            ColumnDef::new(Traces::CreatedAt)
                .timestamp()
                .not_null()
                .default(Expr::current_timestamp()),
        )
        .col(
            ColumnDef::new(Traces::UpdatedAt)
                .timestamp()
                .not_null()
                .default(Expr::current_timestamp()),
        )
        .to_owned()
}

fn trace_dlq_table() -> TableCreateStatement {
    Table::create()
        .table(TraceDlq::Table)
        .if_not_exists()
        .col(ColumnDef::new(TraceDlq::Id).text().not_null().primary_key())
        .col(ColumnDef::new(TraceDlq::TraceId).text().not_null())
        .col(ColumnDef::new(TraceDlq::Channel).text().not_null())
        .col(ColumnDef::new(TraceDlq::PayloadJson).text().not_null())
        .col(
            ColumnDef::new(TraceDlq::MetadataJson)
                .text()
                .not_null()
                .default("{}"),
        )
        .col(ColumnDef::new(TraceDlq::ErrorMessage).text().not_null())
        .col(
            ColumnDef::new(TraceDlq::RetryCount)
                .integer()
                .not_null()
                .default(0),
        )
        .col(
            ColumnDef::new(TraceDlq::MaxRetries)
                .integer()
                .not_null()
                .default(5),
        )
        .col(
            ColumnDef::new(TraceDlq::NextRetryAt)
                .timestamp()
                .not_null()
                .default(Expr::current_timestamp()),
        )
        .col(
            ColumnDef::new(TraceDlq::CreatedAt)
                .timestamp()
                .not_null()
                .default(Expr::current_timestamp()),
        )
        .col(
            ColumnDef::new(TraceDlq::UpdatedAt)
                .timestamp()
                .not_null()
                .default(Expr::current_timestamp()),
        )
        .to_owned()
}

// ============================================================
// Index definitions
// ============================================================

fn workflow_indexes() -> Vec<IndexCreateStatement> {
    vec![
        Index::create()
            .name("idx_workflows_status")
            .table(Workflows::Table)
            .col(Workflows::Status)
            .to_owned(),
        Index::create()
            .name("idx_workflows_workflow_id")
            .table(Workflows::Table)
            .col(Workflows::WorkflowId)
            .to_owned(),
        Index::create()
            .name("idx_workflows_priority_name")
            .table(Workflows::Table)
            .col((Workflows::Priority, sea_query::IndexOrder::Desc))
            .col((Workflows::Name, sea_query::IndexOrder::Asc))
            .to_owned(),
    ]
}

fn channel_indexes() -> Vec<IndexCreateStatement> {
    vec![
        Index::create()
            .name("idx_channels_status")
            .table(Channels::Table)
            .col(Channels::Status)
            .to_owned(),
        Index::create()
            .name("idx_channels_name")
            .table(Channels::Table)
            .col(Channels::Name)
            .to_owned(),
        Index::create()
            .name("idx_channels_type_status")
            .table(Channels::Table)
            .col(Channels::ChannelType)
            .col(Channels::Status)
            .to_owned(),
        Index::create()
            .name("idx_channels_channel_id")
            .table(Channels::Table)
            .col(Channels::ChannelId)
            .to_owned(),
        Index::create()
            .name("idx_channels_route")
            .table(Channels::Table)
            .col(Channels::RoutePattern)
            .to_owned(),
        Index::create()
            .name("idx_channels_topic")
            .table(Channels::Table)
            .col(Channels::Topic)
            .to_owned(),
        Index::create()
            .name("idx_channels_workflow")
            .table(Channels::Table)
            .col(Channels::WorkflowId)
            .to_owned(),
    ]
}

fn trace_indexes() -> Vec<IndexCreateStatement> {
    let mut indexes = vec![
        Index::create()
            .name("idx_traces_status")
            .table(Traces::Table)
            .col(Traces::Status)
            .to_owned(),
        Index::create()
            .name("idx_traces_status_channel")
            .table(Traces::Table)
            .col(Traces::Status)
            .col(Traces::Channel)
            .to_owned(),
        Index::create()
            .name("idx_traces_channel")
            .table(Traces::Table)
            .col(Traces::Channel)
            .to_owned(),
        Index::create()
            .name("idx_traces_mode")
            .table(Traces::Table)
            .col(Traces::Mode)
            .to_owned(),
        Index::create()
            .name("idx_traces_created_at")
            .table(Traces::Table)
            .col(Traces::CreatedAt)
            .to_owned(),
    ];

    // channel_id partial index for non-MySQL
    indexes.push(
        Index::create()
            .name("idx_traces_channel_id")
            .table(Traces::Table)
            .col(Traces::ChannelId)
            .to_owned(),
    );

    indexes
}

fn trace_dlq_indexes() -> Vec<IndexCreateStatement> {
    vec![
        Index::create()
            .name("idx_trace_dlq_channel")
            .table(TraceDlq::Table)
            .col(TraceDlq::Channel)
            .to_owned(),
    ]
}

// ============================================================
// Backend-specific raw SQL (CHECK constraints, triggers, views)
// ============================================================

fn views_sql(_backend: &str) -> String {
    // Views use standard SQL that works on all backends
    let mut sql = String::new();

    sql += "\n-- Latest version per workflow_id\n";
    sql += "CREATE VIEW current_workflows AS\n";
    sql += "SELECT w.*\n";
    sql += "FROM workflows w\n";
    sql += "INNER JOIN (\n";
    sql += "  SELECT workflow_id, MAX(version) AS max_version\n";
    sql += "  FROM workflows\n";
    sql += "  GROUP BY workflow_id\n";
    sql += ") latest ON w.workflow_id = latest.workflow_id AND w.version = latest.max_version;\n";

    sql += "\n-- Latest version per channel_id\n";
    sql += "CREATE VIEW current_channels AS\n";
    sql += "SELECT c.*\n";
    sql += "FROM channels c\n";
    sql += "INNER JOIN (\n";
    sql += "  SELECT channel_id, MAX(version) AS max_version\n";
    sql += "  FROM channels\n";
    sql += "  GROUP BY channel_id\n";
    sql += ") latest ON c.channel_id = latest.channel_id AND c.version = latest.max_version;\n";

    sql
}

fn triggers_sql(backend: &str) -> String {
    match backend {
        "sqlite" => sqlite_triggers(),
        "postgres" => postgres_triggers(),
        "mysql" => mysql_triggers(),
        _ => String::new(),
    }
}

fn sqlite_triggers() -> String {
    r#"
-- Auto-update updated_at
CREATE TRIGGER trg_workflows_updated_at AFTER UPDATE ON workflows
BEGIN
  UPDATE workflows SET updated_at = datetime('now')
  WHERE workflow_id = NEW.workflow_id AND version = NEW.version;
END;

CREATE TRIGGER trg_channels_updated_at AFTER UPDATE ON channels
BEGIN
  UPDATE channels SET updated_at = datetime('now')
  WHERE channel_id = NEW.channel_id AND version = NEW.version;
END;

CREATE TRIGGER trg_connectors_updated_at AFTER UPDATE ON connectors
BEGIN
  UPDATE connectors SET updated_at = datetime('now') WHERE id = NEW.id;
END;

CREATE TRIGGER trg_traces_updated_at AFTER UPDATE ON traces
BEGIN
  UPDATE traces SET updated_at = datetime('now') WHERE id = NEW.id;
END;

CREATE TRIGGER trg_trace_dlq_updated_at AFTER UPDATE ON trace_dlq
BEGIN
  UPDATE trace_dlq SET updated_at = datetime('now') WHERE id = NEW.id;
END;

-- Only one draft per workflow_id
CREATE TRIGGER trg_workflows_single_draft
BEFORE INSERT ON workflows
WHEN NEW.status = 'draft'
BEGIN
  SELECT RAISE(ABORT, 'Only one draft version allowed per workflow')
  WHERE EXISTS (
    SELECT 1 FROM workflows
    WHERE workflow_id = NEW.workflow_id AND status = 'draft'
  );
END;

-- Only one draft per channel_id
CREATE TRIGGER trg_channels_single_draft
BEFORE INSERT ON channels
WHEN NEW.status = 'draft'
BEGIN
  SELECT RAISE(ABORT, 'Only one draft version allowed per channel')
  WHERE EXISTS (
    SELECT 1 FROM channels
    WHERE channel_id = NEW.channel_id AND status = 'draft'
  );
END;

-- Prevent content changes on active workflows
CREATE TRIGGER trg_workflows_active_immutable
BEFORE UPDATE ON workflows
WHEN OLD.status = 'active'
  AND NEW.status = 'active'
  AND (OLD.name != NEW.name
    OR OLD.description IS NOT NEW.description
    OR OLD.priority != NEW.priority
    OR OLD.condition_json != NEW.condition_json
    OR OLD.tasks_json != NEW.tasks_json
    OR OLD.tags != NEW.tags
    OR OLD.continue_on_error != NEW.continue_on_error)
BEGIN
  SELECT RAISE(ABORT, 'Cannot modify content of active workflows');
END;

-- Prevent content changes on active channels
CREATE TRIGGER trg_channels_active_immutable
BEFORE UPDATE ON channels
WHEN OLD.status = 'active'
  AND NEW.status = 'active'
  AND (OLD.name != NEW.name
    OR OLD.description IS NOT NEW.description
    OR OLD.channel_type != NEW.channel_type
    OR OLD.protocol != NEW.protocol
    OR OLD.methods IS NOT NEW.methods
    OR OLD.route_pattern IS NOT NEW.route_pattern
    OR OLD.topic IS NOT NEW.topic
    OR OLD.consumer_group IS NOT NEW.consumer_group
    OR OLD.workflow_id IS NOT NEW.workflow_id
    OR OLD.config_json != NEW.config_json)
BEGIN
  SELECT RAISE(ABORT, 'Cannot modify content of active channels');
END;
"#
    .to_string()
}

fn postgres_triggers() -> String {
    r#"
-- Generic updated_at trigger function
CREATE OR REPLACE FUNCTION update_updated_at_column()
RETURNS TRIGGER AS $$
BEGIN
    NEW.updated_at = CURRENT_TIMESTAMP;
    RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER trg_workflows_updated_at
    BEFORE UPDATE ON workflows
    FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();

CREATE TRIGGER trg_channels_updated_at
    BEFORE UPDATE ON channels
    FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();

CREATE TRIGGER trg_connectors_updated_at
    BEFORE UPDATE ON connectors
    FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();

CREATE TRIGGER trg_traces_updated_at
    BEFORE UPDATE ON traces
    FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();

CREATE TRIGGER trg_trace_dlq_updated_at
    BEFORE UPDATE ON trace_dlq
    FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();

-- Single-draft enforcement via partial unique indexes
CREATE UNIQUE INDEX idx_workflows_single_draft
    ON workflows (workflow_id) WHERE status = 'draft';

CREATE UNIQUE INDEX idx_channels_single_draft
    ON channels (channel_id) WHERE status = 'draft';
"#
    .to_string()
}

fn mysql_triggers() -> String {
    r#"
-- Auto-update updated_at
DELIMITER //
CREATE TRIGGER trg_workflows_updated_at
    BEFORE UPDATE ON workflows
    FOR EACH ROW
BEGIN
    SET NEW.updated_at = CURRENT_TIMESTAMP;
END//

CREATE TRIGGER trg_channels_updated_at
    BEFORE UPDATE ON channels
    FOR EACH ROW
BEGIN
    SET NEW.updated_at = CURRENT_TIMESTAMP;
END//

CREATE TRIGGER trg_connectors_updated_at
    BEFORE UPDATE ON connectors
    FOR EACH ROW
BEGIN
    SET NEW.updated_at = CURRENT_TIMESTAMP;
END//

CREATE TRIGGER trg_traces_updated_at
    BEFORE UPDATE ON traces
    FOR EACH ROW
BEGIN
    SET NEW.updated_at = CURRENT_TIMESTAMP;
END//

CREATE TRIGGER trg_trace_dlq_updated_at
    BEFORE UPDATE ON trace_dlq
    FOR EACH ROW
BEGIN
    SET NEW.updated_at = CURRENT_TIMESTAMP;
END//

-- Single-draft enforcement
CREATE TRIGGER trg_workflows_single_draft
    BEFORE INSERT ON workflows
    FOR EACH ROW
BEGIN
    IF NEW.status = 'draft' THEN
        IF EXISTS (SELECT 1 FROM workflows WHERE workflow_id = NEW.workflow_id AND status = 'draft') THEN
            SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Only one draft version allowed per workflow';
        END IF;
    END IF;
END//

CREATE TRIGGER trg_channels_single_draft
    BEFORE INSERT ON channels
    FOR EACH ROW
BEGIN
    IF NEW.status = 'draft' THEN
        IF EXISTS (SELECT 1 FROM channels WHERE channel_id = NEW.channel_id AND status = 'draft') THEN
            SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Only one draft version allowed per channel';
        END IF;
    END IF;
END//
DELIMITER ;
"#
    .to_string()
}

fn partial_index_sql(backend: &str) -> String {
    match backend {
        "sqlite" | "postgres" => r#"
CREATE INDEX idx_channels_route_partial ON channels(route_pattern) WHERE route_pattern IS NOT NULL;
CREATE INDEX idx_channels_topic_partial ON channels(topic) WHERE topic IS NOT NULL;
CREATE INDEX idx_channels_workflow_partial ON channels(workflow_id) WHERE workflow_id IS NOT NULL;
CREATE INDEX idx_traces_channel_id_partial ON traces(channel_id) WHERE channel_id IS NOT NULL;

CREATE INDEX "idx_trace_dlq_next_retry" ON "trace_dlq" ("next_retry_at") WHERE "retry_count" < "max_retries";
"#
        .to_string(),
        // MySQL doesn't support partial indexes; use composite index instead
        "mysql" => r#"
CREATE INDEX `idx_trace_dlq_next_retry` ON `trace_dlq` (`next_retry_at`, `retry_count`);
"#
        .to_string(),
        _ => String::new(),
    }
}

// ============================================================
// Public generator function
// ============================================================

/// Generate the complete migration SQL for a given backend.
pub fn generate_migration(backend: &str) -> Result<String, OrionError> {
    let tables = [
        workflows_table(),
        channels_table(),
        connectors_table(),
        traces_table(),
        trace_dlq_table(),
    ];

    let mut sql = format!(
        "-- Auto-generated migration for {backend} backend\n-- Generated by Orion migration_gen\n\n"
    );

    macro_rules! build_ddl {
        ($builder_expr:expr, $backend:expr) => {
            match $backend {
                "sqlite" => $builder_expr.build(SqliteQueryBuilder),
                "postgres" => $builder_expr.build(PostgresQueryBuilder),
                "mysql" => $builder_expr.build(MysqlQueryBuilder),
                _ => {
                    return Err(OrionError::Config {
                        message: format!("Unsupported migration backend: {}", $backend),
                    })
                }
            }
        };
    }

    // Generate CREATE TABLE statements
    for table in &tables {
        sql += &build_ddl!(table, backend);
        sql += ";\n\n";
    }

    // Generate indexes
    let all_indexes: Vec<IndexCreateStatement> = [
        workflow_indexes(),
        channel_indexes(),
        trace_indexes(),
        trace_dlq_indexes(),
    ]
    .into_iter()
    .flatten()
    .collect();

    for idx in &all_indexes {
        sql += &build_ddl!(idx, backend);
        sql += ";\n";
    }

    // Partial indexes (backend-specific raw SQL)
    sql += &partial_index_sql(backend);

    // Views (standard SQL)
    sql += &views_sql(backend);

    // Triggers (backend-specific raw SQL)
    sql += &triggers_sql(backend);

    Ok(sql)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_generate_sqlite_migration() {
        let sql = generate_migration("sqlite").expect("test");
        assert!(sql.contains("CREATE TABLE IF NOT EXISTS"));
        assert!(sql.contains("workflows"));
        assert!(sql.contains("channels"));
        assert!(sql.contains("connectors"));
        assert!(sql.contains("traces"));
        assert!(sql.contains("trace_dlq"));
        assert!(sql.contains("CREATE VIEW current_workflows"));
        assert!(sql.contains("CREATE TRIGGER trg_workflows_updated_at"));
        assert!(sql.contains("CREATE TRIGGER trg_workflows_single_draft"));
        assert!(sql.contains("CREATE TRIGGER trg_trace_dlq_updated_at"));
    }

    #[test]
    fn test_generate_postgres_migration() {
        let sql = generate_migration("postgres").expect("test");
        assert!(sql.contains("CREATE TABLE IF NOT EXISTS"));
        assert!(sql.contains("update_updated_at_column"));
        assert!(sql.contains("idx_workflows_single_draft"));
        // Postgres uses BOOLEAN not INTEGER for booleans
        assert!(!sql.contains("INTEGER NOT NULL DEFAULT FALSE"));
    }

    #[test]
    fn test_generate_mysql_migration() {
        let sql = generate_migration("mysql").expect("test");
        assert!(sql.contains("CREATE TABLE IF NOT EXISTS"));
        assert!(sql.contains("DELIMITER //"));
        assert!(sql.contains("SIGNAL SQLSTATE"));
    }

    #[test]
    #[ignore = "Run manually with: cargo test test_write_migration -- --ignored"]
    fn test_write_migration_files() {
        let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| ".".to_string());
        for backend in &["sqlite", "postgres", "mysql"] {
            let sql = generate_migration(backend).expect("test");
            let dir = format!("{manifest_dir}/migrations/{backend}");
            std::fs::create_dir_all(&dir).expect("test");
            let path = format!("{dir}/001_initial.sql");
            std::fs::write(&path, &sql).expect("test");
            eprintln!("Wrote {path} ({} bytes)", sql.len());
        }
    }

    #[test]
    fn test_all_backends_have_views() {
        for backend in &["sqlite", "postgres", "mysql"] {
            let sql = generate_migration(backend).expect("test");
            assert!(
                sql.contains("CREATE VIEW current_workflows"),
                "{backend} missing current_workflows view"
            );
            assert!(
                sql.contains("CREATE VIEW current_channels"),
                "{backend} missing current_channels view"
            );
        }
    }
}