stackql-deploy 2.0.9

Infrastructure-as-code framework for declarative cloud resource management using StackQL
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
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
// lib/utils.rs

//! # Utility Functions
//!
//! Low-level StackQL execution utilities, retry logic, export handling,
//! provider management, and script execution.
//! Matches the Python `lib/utils.py` implementation.

use std::collections::HashMap;
use std::process;
use std::thread;
use std::time::{Duration, Instant};

use log::{debug, error, info, warn};

use crate::core::errors::check_fatal_error;
use crate::utils::pgwire::PgwireLite;
use crate::utils::query::{execute_query, QueryResult};

/// Exit with error message. Matches Python's `catch_error_and_exit`.
pub fn catch_error_and_exit(msg: &str) -> ! {
    error!("{}", msg);
    // Stop the local server before exiting to avoid stale sessions
    crate::utils::server::stop_local_server();
    crate::utils::display::print_unicode_box(
        "stackql-deploy operation failed",
        crate::utils::display::BorderColor::Red,
    );
    process::exit(1);
}

/// Execute a StackQL SELECT query with retry logic.
/// Returns rows as Vec<HashMap<String, String>>.
/// Matches Python's `run_stackql_query`.
pub fn run_stackql_query(
    query: &str,
    client: &mut PgwireLite,
    suppress_errors: bool,
    retries: u32,
    delay: u32,
) -> Vec<HashMap<String, String>> {
    let mut attempt = 0;
    let mut last_error: Option<String> = None;

    while attempt <= retries {
        match execute_query(query, client) {
            Ok(result) => match result {
                QueryResult::Data {
                    columns,
                    rows,
                    notices,
                } => {
                    // Check for error notices
                    for notice in &notices {
                        if notice.contains("error") || notice.starts_with("ERROR") {
                            last_error = Some(notice.clone());
                            if !suppress_errors && attempt == retries {
                                catch_error_and_exit(&format!(
                                    "Error during stackql query execution:\n\n{}\n",
                                    notice
                                ));
                            }
                        }
                    }

                    if rows.is_empty() {
                        debug!("Query returned no results");
                        if attempt < retries {
                            thread::sleep(Duration::from_secs(delay as u64));
                            attempt += 1;
                            continue;
                        }
                        return Vec::new();
                    }

                    // Convert to Vec<HashMap>
                    let col_names: Vec<String> = columns.iter().map(|c| c.name.clone()).collect();

                    let result_maps: Vec<HashMap<String, String>> = rows
                        .iter()
                        .map(|row| {
                            let mut map = HashMap::new();
                            for (i, col_name) in col_names.iter().enumerate() {
                                let value = row
                                    .values
                                    .get(i)
                                    .cloned()
                                    .unwrap_or_else(|| "NULL".to_string());
                                map.insert(col_name.clone(), value);
                            }
                            map
                        })
                        .collect();

                    // Check for error in results
                    if !result_maps.is_empty() {
                        if let Some(err) = result_maps[0].get("error") {
                            last_error = Some(err.clone());
                            // Check for fatal errors even when suppressing
                            if let Some(pattern) = check_fatal_error(err) {
                                catch_error_and_exit(&format!(
                                    "Fatal error (matched '{}'):\n\n{}\n",
                                    pattern, err
                                ));
                            }
                            if !suppress_errors {
                                if attempt == retries {
                                    catch_error_and_exit(&format!(
                                        "Error during stackql query execution:\n\n{}\n",
                                        err
                                    ));
                                } else {
                                    error!("Attempt {} failed:\n\n{}\n", attempt + 1, err);
                                }
                            }
                            thread::sleep(Duration::from_secs(delay as u64));
                            attempt += 1;
                            continue;
                        }

                        // Check for count query
                        if let Some(count_str) = result_maps[0].get("count") {
                            if let Ok(json) = serde_json::to_string_pretty(&result_maps) {
                                debug!(
                                    "Stackql query executed successfully, count: {}\n\nresults:\n\n{}\n",
                                    count_str, json
                                );
                            }
                            if let Ok(count) = count_str.parse::<i64>() {
                                if count > 1 {
                                    catch_error_and_exit(&format!(
                                        "Detected more than one resource matching query criteria, expected 0 or 1, got {}",
                                        count
                                    ));
                                }
                            }
                            return result_maps;
                        }
                    }

                    if let Ok(json) = serde_json::to_string_pretty(&result_maps) {
                        debug!(
                            "Stackql query executed successfully, retrieved {} items.\n\nresults:\n\n{}\n",
                            result_maps.len(), json
                        );
                    }
                    return result_maps;
                }
                QueryResult::Command(msg) => {
                    debug!("Command result: {}", msg);
                    return Vec::new();
                }
                QueryResult::Empty => {
                    debug!("Query returned no results");
                    if attempt < retries {
                        thread::sleep(Duration::from_secs(delay as u64));
                        attempt += 1;
                        continue;
                    }
                    return Vec::new();
                }
            },
            Err(e) => {
                last_error = Some(e.clone());
                debug!("Query error on attempt {}: {}", attempt + 1, e);
                // Check for fatal errors (network, auth) that should not be retried
                if let Some(pattern) = check_fatal_error(&e) {
                    catch_error_and_exit(&format!(
                        "Fatal error (matched '{}'):\n\n{}\n",
                        pattern, e
                    ));
                }
                if attempt == retries && !suppress_errors {
                    catch_error_and_exit(&format!(
                        "Exception during stackql query execution:\n\n{}\n",
                        e
                    ));
                }
            }
        }

        thread::sleep(Duration::from_secs(delay as u64));
        attempt += 1;
    }

    // If suppress_errors and we have an error, return error marker
    if suppress_errors {
        if let Some(err) = last_error {
            let mut error_map = HashMap::new();
            error_map.insert("_stackql_deploy_error".to_string(), err);
            return vec![error_map];
        }
    }

    Vec::new()
}

/// Execute a StackQL DML/DDL command with retry logic.
/// Matches Python's `run_stackql_command`.
pub fn run_stackql_command(
    command: &str,
    client: &mut PgwireLite,
    ignore_errors: bool,
    retries: u32,
    retry_delay: u32,
) -> String {
    let mut attempt = 0;

    // Handle REGISTRY PULL command format
    let processed_command = if command.starts_with("REGISTRY PULL") {
        let re = regex::Regex::new(r"(REGISTRY PULL \w+)(::v[\d\.]+)?").unwrap();
        if let Some(caps) = re.captures(command) {
            let provider = caps.get(1).map_or("", |m| m.as_str());
            if let Some(version) = caps.get(2) {
                format!("{} {}", provider, &version.as_str()[2..])
            } else {
                command.to_string()
            }
        } else {
            command.to_string()
        }
    } else {
        command.to_string()
    };

    while attempt <= retries {
        match execute_query(&processed_command, client) {
            Ok(result) => {
                match result {
                    QueryResult::Data {
                        notices,
                        columns,
                        rows,
                    } => {
                        // Check for errors in notices
                        for notice in &notices {
                            if error_detected_in_notice(notice) && !ignore_errors {
                                if attempt < retries {
                                    debug!(
                                        "Command notice on attempt {}/{}, retrying in {} seconds: {}",
                                        attempt + 1, retries + 1, retry_delay, notice
                                    );
                                    thread::sleep(Duration::from_secs(retry_delay as u64));
                                    attempt += 1;
                                    continue;
                                } else {
                                    catch_error_and_exit(&format!(
                                        "Error during stackql command execution:\n\n{}\n\nlast rendered query:\n\n{}\n",
                                        notice, processed_command
                                    ));
                                }
                            }
                        }
                        // Log returned data (e.g. from RETURNING clause) at debug level
                        if !rows.is_empty() {
                            let col_names: Vec<&str> =
                                columns.iter().map(|c| c.name.as_str()).collect();
                            let result_maps: Vec<HashMap<String, String>> = rows
                                .iter()
                                .map(|row| {
                                    col_names
                                        .iter()
                                        .enumerate()
                                        .map(|(i, &name)| {
                                            let val =
                                                row.values.get(i).cloned().unwrap_or_default();
                                            (name.to_string(), val)
                                        })
                                        .collect()
                                })
                                .collect();
                            if let Ok(json) = serde_json::to_string_pretty(&result_maps) {
                                debug!("Command returned data:\n\n{}\n", json);
                            }
                        }
                        let msg = notices.join("\n");
                        if !msg.is_empty() {
                            debug!("Command notices:\n\n{}\n", msg);
                        }
                        return msg;
                    }
                    QueryResult::Command(msg) => {
                        debug!("Stackql command executed successfully:\n\n{}\n", msg);
                        return msg;
                    }
                    QueryResult::Empty => {
                        debug!("Command executed with empty result");
                        return String::new();
                    }
                }
            }
            Err(e) => {
                // Check for fatal errors (network, auth) before retrying
                if let Some(pattern) = check_fatal_error(&e) {
                    catch_error_and_exit(&format!(
                        "Fatal error (matched '{}'):\n\n{}\n",
                        pattern, e
                    ));
                }
                if !ignore_errors {
                    if attempt < retries {
                        debug!(
                            "Command returned error on attempt {}/{}, retrying in {} seconds: {}",
                            attempt + 1,
                            retries + 1,
                            retry_delay,
                            e
                        );
                        thread::sleep(Duration::from_secs(retry_delay as u64));
                        attempt += 1;
                        continue;
                    }
                    catch_error_and_exit(&format!(
                        "Exception during stackql command execution:\n\n{}\n",
                        e
                    ));
                } else {
                    debug!("Command failed (ignored): {}", e);
                    return String::new();
                }
            }
        }
    }

    String::new()
}

/// Check if a notice/message indicates an error.
///
/// Patterns can appear either at the start of the notice message or inside
/// the `DETAIL:` payload (stackql wraps provider errors as a generic "a
/// notice level event has occurred" message with the real HTTP status in
/// the detail), so match against the whole notice string.
fn error_detected_in_notice(msg: &str) -> bool {
    msg.contains("http response status code: 4")
        || msg.contains("http response status code: 5")
        || msg.starts_with("error:")
        || msg.contains("\nDETAIL: error:")
        || msg.starts_with("disparity in fields to insert")
        || msg.starts_with("cannot find matching operation")
}

/// Run a test query and check if count == 1 (exists) or count == 0 (deleted).
/// Matches Python's `run_test`.
pub fn run_test(
    resource_name: &str,
    query: &str,
    client: &mut PgwireLite,
    delete_test: bool,
) -> bool {
    run_test_with_fields(resource_name, query, client, delete_test).0
}

/// Run a test query and capture any non-count fields from the result.
///
/// Returns `(bool, Option<HashMap<String, String>>)`:
/// - The bool indicates whether the test passed (resource exists / is deleted).
/// - If the exists query returns fields OTHER than `count`, those fields are
///   captured and returned so the caller can inject them into the template
///   context (e.g. as `{{ this.identifier }}`).
pub fn run_test_with_fields(
    resource_name: &str,
    query: &str,
    client: &mut PgwireLite,
    delete_test: bool,
) -> (bool, Option<HashMap<String, String>>) {
    let result = run_stackql_query(query, client, true, 0, 5);

    if result.is_empty() {
        if delete_test {
            debug!("Delete test result true for [{}]", resource_name);
            return (true, None);
        } else {
            debug!("Test result false for [{}]", resource_name);
            return (false, None);
        }
    }

    // Check for error markers
    if result[0].contains_key("_stackql_deploy_error") || result[0].contains_key("error") {
        if delete_test {
            return (true, None);
        }
        return (false, None);
    }

    if let Some(count_str) = result[0].get("count") {
        if let Ok(count) = count_str.parse::<i64>() {
            if delete_test {
                if count == 0 {
                    debug!("Delete test result true for [{}]", resource_name);
                    return (true, None);
                } else {
                    debug!(
                        "Delete test result false for [{}], expected 0 got {}",
                        resource_name, count
                    );
                    return (false, None);
                }
            } else if count == 1 {
                debug!("Test result true for [{}]", resource_name);
                // Capture any extra fields beyond "count"
                let extra = extract_non_count_fields(&result[0]);
                return (true, extra);
            } else {
                debug!(
                    "Test result false for [{}], expected 1 got {}",
                    resource_name, count
                );
                return (false, None);
            }
        }
    }

    // If no count field, for non-delete test consider any result as exists
    // and capture all returned fields.
    // However, if multiple rows are returned this is a fatal error — the
    // exists (identifier) query must return exactly 0 or 1 rows.
    if !delete_test && result.len() > 1 {
        catch_error_and_exit(&format!(
            "Exists query for [{}] returned {} rows (expected 0 or 1). \
             This indicates an ambiguous resource identifier — fix the \
             exists query or tag configuration so it returns a single row.",
            resource_name,
            result.len()
        ));
    }

    // However, if all non-trivial field values are "null" or empty, treat
    // as "does not exist" (e.g. a CASE WHEN that returned NULL).
    if !delete_test && !result.is_empty() {
        let row = &result[0];
        let all_null = row.values().all(|v| v == "null" || v.is_empty());
        if all_null {
            debug!(
                "Test result false for [{}]: all field values are null/empty",
                resource_name
            );
            return (false, None);
        }
        let fields = Some(row.clone());
        return (true, fields);
    }

    (false, None)
}

/// Extract fields from an exists query result row, excluding the `count` field.
/// Returns `Some(map)` if there are non-count fields, `None` otherwise.
fn extract_non_count_fields(row: &HashMap<String, String>) -> Option<HashMap<String, String>> {
    let extra: HashMap<String, String> = row
        .iter()
        .filter(|(k, _)| k.as_str() != "count")
        .map(|(k, v)| (k.clone(), v.clone()))
        .collect();
    if extra.is_empty() {
        None
    } else {
        Some(extra)
    }
}

/// Perform retries on a test query.
/// Matches Python's `perform_retries`.
pub fn perform_retries(
    resource_name: &str,
    query: &str,
    retries: u32,
    delay: u32,
    client: &mut PgwireLite,
    delete_test: bool,
) -> bool {
    perform_retries_with_fields(resource_name, query, retries, delay, client, delete_test).0
}

/// Perform retries on a test query, capturing any non-count fields from the result.
pub fn perform_retries_with_fields(
    resource_name: &str,
    query: &str,
    retries: u32,
    delay: u32,
    client: &mut PgwireLite,
    delete_test: bool,
) -> (bool, Option<HashMap<String, String>>) {
    let start = Instant::now();
    let mut attempt = 0;

    while attempt < retries {
        let (result, fields) = run_test_with_fields(resource_name, query, client, delete_test);
        if result {
            return (true, fields);
        }
        let elapsed = start.elapsed().as_secs();
        info!(
            "attempt {}/{}: retrying in {} seconds ({} seconds elapsed).",
            attempt + 1,
            retries,
            delay,
            elapsed
        );
        thread::sleep(Duration::from_secs(delay as u64));
        attempt += 1;
    }

    // Pre-create exists checks run with retries == 1 as a "fast fail" where
    // a negative result is the expected outcome (the resource does not yet
    // exist and needs to be created). Only surface the query when the
    // caller configured real retries — i.e. a statecheck, exports proxy,
    // or post-deploy exists check where exhaustion signals a stack failure.
    if retries > 1 {
        warn!(
            "retries exhausted for [{}], last rendered query:\n\n{}\n",
            resource_name, query
        );
    }
    (false, None)
}

/// Show a query in logs if show_queries is enabled.
pub fn show_query(show_queries: bool, query: &str) {
    if show_queries {
        info!("query:\n\n{}\n", query);
    }
}

/// Pull providers using the StackQL server.
/// Matches Python's `pull_providers`.
pub fn pull_providers(providers: &[String], client: &mut PgwireLite) {
    let installed = run_stackql_query("SHOW PROVIDERS", client, false, 0, 5);

    for provider in providers {
        if provider.contains("::") {
            // Versioned provider
            let parts: Vec<&str> = provider.splitn(2, "::").collect();
            let name = parts[0];
            let version = parts[1];

            let found = installed.iter().any(|p| {
                p.get("name").is_some_and(|n| n == name)
                    && p.get("version").is_some_and(|v| v == version)
            });

            if found {
                info!("Provider '{}' is already installed.", provider);
            } else {
                // Check if a higher version is installed
                let higher_installed = installed.iter().any(|p| {
                    p.get("name").is_some_and(|n| n == name)
                        && p.get("version")
                            .is_some_and(|v| is_version_higher(v, version))
                });

                if higher_installed {
                    info!(
                        "Provider '{}' - a higher version is already installed.",
                        provider
                    );
                } else {
                    info!("Pulling provider '{}'...", provider);
                    let cmd = format!("REGISTRY PULL {}", provider);
                    let msg = run_stackql_command(&cmd, client, false, 0, 5);
                    if !msg.is_empty() {
                        info!("{}", msg);
                    }
                }
            }
        } else {
            let found = installed.iter().any(|p| p.get("name") == Some(provider));

            if found {
                info!("Provider '{}' is already installed.", provider);
            } else {
                info!("Pulling provider '{}'...", provider);
                let cmd = format!("REGISTRY PULL {}", provider);
                let msg = run_stackql_command(&cmd, client, false, 0, 5);
                if !msg.is_empty() {
                    info!("{}", msg);
                }
            }
        }
    }
}

/// Compare version strings. Returns true if installed > requested.
fn is_version_higher(installed: &str, requested: &str) -> bool {
    let parse = |v: &str| -> u64 { v.replace(['v', '.'], "").parse::<u64>().unwrap_or(0) };
    parse(installed) > parse(requested)
}

/// Update global context with exported values.
///
/// Each export is stored under two keys:
///
/// - **`{key}`** — the global (unscoped) key.  This can be overridden by a
///   subsequent resource that exports a variable with the same name, so it
///   always reflects the *most recent* export value.
///
/// - **`{resource_name}.{key}`** — the resource-scoped (fully qualified) key.
///   This is written **once** and never overwritten, so it is immutable once
///   set.  Consumers that need an unambiguous reference should use this form.
///
/// Matches Python's `export_vars`.
pub fn export_vars(
    global_context: &mut HashMap<String, String>,
    resource_name: &str,
    export_data: &HashMap<String, String>,
    protected_exports: &[String],
) {
    for (key, value) in export_data {
        let is_protected = protected_exports.contains(key);
        let display_value = if is_protected {
            "*".repeat(value.len())
        } else {
            value.clone()
        };

        // --- resource-scoped key (immutable: only written if not already set) ---
        let scoped_key = format!("{}.{}", resource_name, key);
        global_context.entry(scoped_key.clone()).or_insert_with(|| {
            debug!(
                "set {} [{}] to [{}] in exports",
                if is_protected {
                    "protected variable"
                } else {
                    "variable"
                },
                scoped_key,
                display_value,
            );
            value.clone()
        });

        // --- global (unscoped) key (can be overridden by later resources) ---
        info!(
            "set {} [{}] to [{}] in exports",
            if is_protected {
                "protected variable"
            } else {
                "variable"
            },
            key,
            display_value,
        );
        global_context.insert(key.clone(), value.clone());
    }
}

/// Check if exports result can serve as a statecheck proxy.
/// Returns true if result is non-empty and has no errors.
/// Matches Python's `check_exports_as_statecheck_proxy`.
pub fn check_exports_as_statecheck_proxy(exports_result: &[HashMap<String, String>]) -> bool {
    debug!(
        "Checking exports result as statecheck proxy: {} rows",
        exports_result.len()
    );

    if exports_result.is_empty() {
        debug!("Empty exports result, treating as statecheck failure");
        return false;
    }

    // Check for error conditions
    if exports_result[0].contains_key("_stackql_deploy_error") {
        debug!("Error in exports result, treating as statecheck failure");
        return false;
    }
    if exports_result[0].contains_key("error") {
        debug!("Error in exports result, treating as statecheck failure");
        return false;
    }

    debug!("Valid exports result, treating as statecheck success");
    true
}

/// Check if all items in exports list are dicts (HashMap-like).
/// In Rust, exports from YAML can be strings or maps.
/// Matches Python's `check_all_dicts`.
pub fn check_all_dicts(items: &[serde_yaml::Value]) -> bool {
    if items.is_empty() {
        return false;
    }
    items.iter().all(|item| item.is_mapping())
}

/// Run an external script and capture output.
/// Matches Python's `run_ext_script`.
pub fn run_ext_script(
    cmd: &str,
    expected_exports: Option<&[String]>,
) -> Option<HashMap<String, String>> {
    debug!("Running external script: {}", cmd);

    let output = match std::process::Command::new("sh").arg("-c").arg(cmd).output() {
        Ok(output) => output,
        Err(e) => {
            catch_error_and_exit(&format!("Script failed: {}", e));
        }
    };

    let stdout = String::from_utf8_lossy(&output.stdout).to_string();
    debug!("Script output: {}", stdout);

    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        catch_error_and_exit(&format!(
            "Script failed with status {:?}: {}",
            output.status.code(),
            stderr
        ));
    }

    match expected_exports {
        Some(exports) if !exports.is_empty() => {
            match serde_json::from_str::<HashMap<String, String>>(&stdout) {
                Ok(exported_vars) => {
                    for export in exports {
                        if !exported_vars.contains_key(export) {
                            catch_error_and_exit(&format!(
                                "Exported variable '{}' not found in script output",
                                export
                            ));
                        }
                    }
                    Some(exported_vars)
                }
                Err(_) => {
                    catch_error_and_exit(&format!(
                        "External scripts must return valid JSON: {}",
                        stdout
                    ));
                }
            }
        }
        _ => None,
    }
}

// ---------------------------------------------------------------------------
// RETURNING * capture helpers
// ---------------------------------------------------------------------------

/// Return `true` if the rendered query string contains a `RETURNING` clause.
/// Case-insensitive match; used to decide whether to capture a DML result.
pub fn has_returning_clause(query: &str) -> bool {
    query.to_uppercase().contains("RETURNING")
}

/// Remove a trailing `RETURNING ...` clause from a DML query.
///
/// Matches case-insensitively on the last `RETURNING` keyword occurrence and
/// truncates from there, preserving any trailing `;`.
pub fn strip_returning_clause(query: &str) -> String {
    let upper = query.to_uppercase();
    if let Some(idx) = upper.rfind("RETURNING") {
        let trailing_semi = query.trim_end().ends_with(';');
        let mut out = query[..idx].trim_end().to_string();
        if trailing_semi {
            out.push(';');
        }
        out
    } else {
        query.to_string()
    }
}

/// Execute a DML command (INSERT / UPDATE / DELETE), optionally capturing
/// the `RETURNING *` result as the first row.
///
/// Returns `(command_message, Option<first_row>)`.  When the DML includes
/// `RETURNING *` and the provider returns rows, the first row is captured.
/// If no rows are returned (provider returned no body), `None` is returned –
/// this is **not** an error.
pub fn run_stackql_dml_returning(
    command: &str,
    client: &mut PgwireLite,
    ignore_errors: bool,
    retries: u32,
    retry_delay: u32,
) -> (String, Option<HashMap<String, String>>) {
    let mut attempt = 0u32;

    while attempt <= retries {
        match execute_query(command, client) {
            Ok(result) => match result {
                QueryResult::Data {
                    columns,
                    rows,
                    notices,
                } => {
                    // Check for errors in notices before accepting the result.
                    let mut error_noticed = false;
                    for notice in &notices {
                        if error_detected_in_notice(notice) && !ignore_errors {
                            if attempt < retries {
                                debug!(
                                    "DML notice on attempt {}/{}, retrying in {} seconds: {}",
                                    attempt + 1,
                                    retries + 1,
                                    retry_delay,
                                    notice
                                );
                                thread::sleep(Duration::from_secs(retry_delay as u64));
                                attempt += 1;
                                error_noticed = true;
                                break;
                            } else {
                                catch_error_and_exit(&format!(
                                    "Error during stackql DML execution:\n\n{}\n\nlast rendered query:\n\n{}\n",
                                    notice, command
                                ));
                            }
                        }
                    }
                    if error_noticed {
                        continue;
                    }

                    // Capture RETURNING * first row (if any).
                    let first_row = if !rows.is_empty() {
                        let col_names: Vec<String> =
                            columns.iter().map(|c| c.name.clone()).collect();
                        let row = &rows[0];
                        let mut map = HashMap::new();
                        for (i, col_name) in col_names.iter().enumerate() {
                            let value = row.values.get(i).cloned().unwrap_or_default();
                            map.insert(col_name.clone(), value);
                        }
                        Some(map)
                    } else {
                        None
                    };

                    let msg = notices.join("\n");
                    return (msg, first_row);
                }
                QueryResult::Command(msg) => {
                    return (msg, None);
                }
                QueryResult::Empty => {
                    return (String::new(), None);
                }
            },
            Err(e) => {
                // Check for fatal errors (network, auth) before retrying
                if let Some(pattern) = check_fatal_error(&e) {
                    catch_error_and_exit(&format!(
                        "Fatal error (matched '{}'):\n\n{}\n",
                        pattern, e
                    ));
                }
                if !ignore_errors {
                    if attempt < retries {
                        debug!(
                            "DML error on attempt {}/{}, retrying in {} seconds: {}",
                            attempt + 1,
                            retries + 1,
                            retry_delay,
                            e
                        );
                        thread::sleep(Duration::from_secs(retry_delay as u64));
                        attempt += 1;
                        continue;
                    }
                    catch_error_and_exit(&format!(
                        "Exception during stackql DML execution:\n\n{}\n",
                        e
                    ));
                } else {
                    debug!("DML failed (ignored): {}", e);
                    return (String::new(), None);
                }
            }
        }
    }

    (String::new(), None)
}

/// Flatten a single RETURNING * row into dotted context keys and insert them
/// into `context`.
///
/// For each column `col` in `row`:
/// - `callback.{col}` is set (shorthand for the current resource's own `.iql`
///   templates).
/// - `{resource_name}.callback.{col}` is set (fully-qualified key accessible
///   by downstream resources).
///
/// If a column value is a JSON object it is recursively expanded:
/// `"ProgressEvent" = {"OperationStatus":"SUCCESS","RequestToken":"abc"}`
/// produces:
/// ```text
/// callback.ProgressEvent.OperationStatus = SUCCESS
/// callback.ProgressEvent.RequestToken    = abc
/// ```
pub fn flatten_returning_row(
    row: &HashMap<String, String>,
    resource_name: &str,
    context: &mut HashMap<String, String>,
) {
    for (col, val) in row {
        let short_prefix = format!("callback.{}", col);
        let full_prefix = format!("{}.callback.{}", resource_name, col);
        flatten_value_into_context(&short_prefix, &full_prefix, val, context);
    }
}

/// Recursively expand a string value (possibly JSON) into dotted context keys.
fn flatten_value_into_context(
    short_prefix: &str,
    full_prefix: &str,
    value: &str,
    context: &mut HashMap<String, String>,
) {
    if let Ok(json) = serde_json::from_str::<serde_json::Value>(value) {
        if json.is_object() {
            flatten_json_into_context(short_prefix, full_prefix, &json, context);
            return;
        }
    }
    context.insert(short_prefix.to_string(), value.to_string());
    context.insert(full_prefix.to_string(), value.to_string());
}

fn flatten_json_into_context(
    short_prefix: &str,
    full_prefix: &str,
    value: &serde_json::Value,
    context: &mut HashMap<String, String>,
) {
    match value {
        serde_json::Value::Object(map) => {
            for (k, v) in map {
                let new_short = format!("{}.{}", short_prefix, k);
                let new_full = format!("{}.{}", full_prefix, k);
                flatten_json_into_context(&new_short, &new_full, v, context);
            }
        }
        serde_json::Value::String(s) => {
            context.insert(short_prefix.to_string(), s.clone());
            context.insert(full_prefix.to_string(), s.clone());
        }
        other => {
            let s = other.to_string();
            context.insert(short_prefix.to_string(), s.clone());
            context.insert(full_prefix.to_string(), s);
        }
    }
}

/// Check whether a short-circuit condition is met using already-captured
/// callback data.
///
/// `field` is a dot-path into the captured result (e.g.
/// `"ProgressEvent.OperationStatus"`), looked up as `callback.{field}` in
/// `context`.  Returns `true` if the value equals `expected_value`.
/// Returns `false` (no short-circuit) if the field is absent.
pub fn check_short_circuit(
    context: &HashMap<String, String>,
    field: &str,
    expected_value: &str,
) -> bool {
    let lookup_key = format!("callback.{}", field);
    match context.get(&lookup_key) {
        Some(val) => {
            let result = val == expected_value;
            if result {
                info!(
                    "short-circuit condition met: {} = {} (skipping callback poll)",
                    lookup_key, expected_value
                );
            }
            result
        }
        None => {
            debug!(
                "short-circuit field '{}' not found in context, proceeding with callback poll",
                lookup_key
            );
            false
        }
    }
}

/// Poll a callback query until the `success` (or `count`) column returns a
/// truthy value, or `retries` are exhausted.
///
/// Returns `true` on success, `false` when retries are exhausted (the caller
/// is responsible for treating exhaustion as an error).
pub fn run_callback_poll(
    resource_name: &str,
    query: &str,
    retries: u32,
    retry_delay: u32,
    client: &mut PgwireLite,
) -> bool {
    let mut attempt = 0u32;

    while attempt <= retries {
        debug!(
            "Callback poll for [{}] attempt {}:\n\n{}\n",
            resource_name,
            attempt + 1,
            query
        );

        let result = run_stackql_query(query, client, true, 0, 0);

        if !result.is_empty() {
            let row = &result[0];

            // Check `success` column (primary).
            if let Some(success_val) = row.get("success") {
                if success_val == "1" || success_val.to_lowercase() == "true" {
                    info!(
                        "[{}] callback poll succeeded on attempt {}",
                        resource_name,
                        attempt + 1
                    );
                    return true;
                }
            }

            // Check `count` column (alternative).
            if let Some(count_val) = row.get("count") {
                if count_val == "1" {
                    info!(
                        "[{}] callback poll succeeded (count=1) on attempt {}",
                        resource_name,
                        attempt + 1
                    );
                    return true;
                }
            }
        }

        if attempt < retries {
            info!(
                "[{}] callback poll attempt {}/{}: retrying in {} seconds...",
                resource_name,
                attempt + 1,
                retries + 1,
                retry_delay
            );
            thread::sleep(Duration::from_secs(retry_delay as u64));
        }
        attempt += 1;
    }

    false
}

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

    // ------------------------------------------------------------------
    // export_vars
    // ------------------------------------------------------------------

    #[test]
    fn test_export_vars_sets_global_and_scoped_key() {
        let mut ctx: HashMap<String, String> = HashMap::new();
        let mut data: HashMap<String, String> = HashMap::new();
        data.insert("role_name".to_string(), "my-role".to_string());

        export_vars(&mut ctx, "aws_cross_account_role", &data, &[]);

        // Global key
        assert_eq!(ctx.get("role_name").map(|s| s.as_str()), Some("my-role"));
        // Resource-scoped key
        assert_eq!(
            ctx.get("aws_cross_account_role.role_name")
                .map(|s| s.as_str()),
            Some("my-role"),
        );
    }

    #[test]
    fn test_export_vars_global_key_is_overridable() {
        let mut ctx: HashMap<String, String> = HashMap::new();

        // First resource exports role_name
        let mut data1 = HashMap::new();
        data1.insert("role_name".to_string(), "first-role".to_string());
        export_vars(&mut ctx, "resource_a", &data1, &[]);

        // Second resource exports role_name with a different value
        let mut data2 = HashMap::new();
        data2.insert("role_name".to_string(), "second-role".to_string());
        export_vars(&mut ctx, "resource_b", &data2, &[]);

        // Global key reflects the most recent export
        assert_eq!(
            ctx.get("role_name").map(|s| s.as_str()),
            Some("second-role")
        );
    }

    #[test]
    fn test_export_vars_scoped_key_is_immutable() {
        let mut ctx: HashMap<String, String> = HashMap::new();

        // First resource exports role_name
        let mut data1 = HashMap::new();
        data1.insert("role_name".to_string(), "original-role".to_string());
        export_vars(&mut ctx, "resource_a", &data1, &[]);

        // Simulate an accidental re-export of the same resource (e.g. called
        // twice): the scoped key must not be overwritten.
        let mut data2 = HashMap::new();
        data2.insert("role_name".to_string(), "should-not-overwrite".to_string());
        export_vars(&mut ctx, "resource_a", &data2, &[]);

        // Scoped key is unchanged
        assert_eq!(
            ctx.get("resource_a.role_name").map(|s| s.as_str()),
            Some("original-role"),
        );
        // Global key reflects the latest call (expected)
        assert_eq!(
            ctx.get("role_name").map(|s| s.as_str()),
            Some("should-not-overwrite"),
        );
    }

    #[test]
    fn test_export_vars_protected_values_are_stored_normally() {
        // Protection only affects log-masking, not what is stored
        let mut ctx: HashMap<String, String> = HashMap::new();
        let mut data = HashMap::new();
        data.insert("secret_key".to_string(), "super-secret".to_string());

        export_vars(&mut ctx, "vault", &data, &["secret_key".to_string()]);

        assert_eq!(
            ctx.get("secret_key").map(|s| s.as_str()),
            Some("super-secret")
        );
        assert_eq!(
            ctx.get("vault.secret_key").map(|s| s.as_str()),
            Some("super-secret"),
        );
    }

    // ------------------------------------------------------------------
    // has_returning_clause
    // ------------------------------------------------------------------

    #[test]
    fn test_has_returning_clause_positive() {
        assert!(has_returning_clause(
            "INSERT INTO awscc.s3.buckets(BucketName, region) SELECT 'my-bucket', 'us-east-1' RETURNING *"
        ));
    }

    #[test]
    fn test_has_returning_clause_case_insensitive() {
        assert!(has_returning_clause("DELETE FROM t WHERE id=1 returning *"));
    }

    #[test]
    fn test_has_returning_clause_negative() {
        assert!(!has_returning_clause("INSERT INTO t(col) SELECT 'val'"));
    }

    // ------------------------------------------------------------------
    // flatten_returning_row
    // ------------------------------------------------------------------

    #[test]
    fn test_flatten_returning_row_simple_string_values() {
        let mut row = HashMap::new();
        row.insert("RequestToken".to_string(), "tok-123".to_string());
        row.insert("OperationStatus".to_string(), "SUCCESS".to_string());

        let mut ctx: HashMap<String, String> = HashMap::new();
        flatten_returning_row(&row, "my_resource", &mut ctx);

        assert_eq!(
            ctx.get("callback.RequestToken").map(|s| s.as_str()),
            Some("tok-123")
        );
        assert_eq!(
            ctx.get("my_resource.callback.RequestToken")
                .map(|s| s.as_str()),
            Some("tok-123")
        );
        assert_eq!(
            ctx.get("callback.OperationStatus").map(|s| s.as_str()),
            Some("SUCCESS")
        );
        assert_eq!(
            ctx.get("my_resource.callback.OperationStatus")
                .map(|s| s.as_str()),
            Some("SUCCESS")
        );
    }

    #[test]
    fn test_flatten_returning_row_nested_json() {
        // Provider returns ProgressEvent as a JSON object string.
        let mut row = HashMap::new();
        row.insert(
            "ProgressEvent".to_string(),
            r#"{"OperationStatus":"SUCCESS","RequestToken":"abc"}"#.to_string(),
        );

        let mut ctx: HashMap<String, String> = HashMap::new();
        flatten_returning_row(&row, "aws_s3_bucket", &mut ctx);

        assert_eq!(
            ctx.get("callback.ProgressEvent.OperationStatus")
                .map(|s| s.as_str()),
            Some("SUCCESS")
        );
        assert_eq!(
            ctx.get("callback.ProgressEvent.RequestToken")
                .map(|s| s.as_str()),
            Some("abc")
        );
        assert_eq!(
            ctx.get("aws_s3_bucket.callback.ProgressEvent.OperationStatus")
                .map(|s| s.as_str()),
            Some("SUCCESS")
        );
        assert_eq!(
            ctx.get("aws_s3_bucket.callback.ProgressEvent.RequestToken")
                .map(|s| s.as_str()),
            Some("abc")
        );
    }

    #[test]
    fn test_flatten_returning_row_empty_row_is_noop() {
        let row: HashMap<String, String> = HashMap::new();
        let mut ctx: HashMap<String, String> = HashMap::new();
        flatten_returning_row(&row, "res", &mut ctx);
        assert!(ctx.is_empty());
    }

    // ------------------------------------------------------------------
    // check_short_circuit
    // ------------------------------------------------------------------

    #[test]
    fn test_check_short_circuit_matches() {
        let mut ctx: HashMap<String, String> = HashMap::new();
        ctx.insert(
            "callback.ProgressEvent.OperationStatus".to_string(),
            "SUCCESS".to_string(),
        );
        assert!(check_short_circuit(
            &ctx,
            "ProgressEvent.OperationStatus",
            "SUCCESS"
        ));
    }

    #[test]
    fn test_check_short_circuit_no_match() {
        let mut ctx: HashMap<String, String> = HashMap::new();
        ctx.insert(
            "callback.ProgressEvent.OperationStatus".to_string(),
            "IN_PROGRESS".to_string(),
        );
        assert!(!check_short_circuit(
            &ctx,
            "ProgressEvent.OperationStatus",
            "SUCCESS"
        ));
    }

    #[test]
    fn test_check_short_circuit_missing_field() {
        let ctx: HashMap<String, String> = HashMap::new();
        // Field not present in context → no short-circuit.
        assert!(!check_short_circuit(
            &ctx,
            "ProgressEvent.OperationStatus",
            "SUCCESS"
        ));
    }
}