nautilus-infrastructure 0.61.0

Infrastructure components for the Nautilus trading engine
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
// -------------------------------------------------------------------------------------------------
//  Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
//  https://nautechsystems.io
//
//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
//  You may not use this file except in compliance with the License.
//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
// -------------------------------------------------------------------------------------------------

use derive_builder::Builder;
use regex::Regex;
use serde::{Deserialize, Serialize};
use sqlx::{
    AssertSqlSafe, ConnectOptions, PgPool,
    postgres::{PgConnectOptions, PgConnection},
};

fn validate_sql_identifier(value: &str, label: &str) -> anyhow::Result<()> {
    if value.is_empty() {
        anyhow::bail!("{label} must not be empty");
    }

    if !value.chars().all(|c| c.is_ascii_alphanumeric() || c == '_') {
        anyhow::bail!(
            "{label} contains invalid characters (only alphanumeric and underscore allowed): {value}"
        );
    }
    Ok(())
}

fn escape_sql_string(value: &str) -> String {
    value.replace('\'', "''")
}

#[derive(Debug, Clone, Serialize, Deserialize, Builder)]
#[serde(deny_unknown_fields)]
#[builder(default)]
#[cfg_attr(
    feature = "python",
    pyo3::pyclass(
        module = "nautilus_trader.core.nautilus_pyo3.infrastructure",
        from_py_object
    )
)]
#[cfg_attr(
    feature = "python",
    pyo3_stub_gen::derive::gen_stub_pyclass(module = "nautilus_trader.infrastructure")
)]
#[allow(
    clippy::unsafe_derive_deserialize,
    reason = "config type deserializes plain field values; unsafe PyO3 methods are unrelated"
)]
pub struct PostgresConnectOptions {
    pub host: String,
    pub port: u16,
    pub username: String,
    pub password: String,
    pub database: String,
}

impl PostgresConnectOptions {
    /// Creates a new [`PostgresConnectOptions`] instance.
    #[must_use]
    pub const fn new(
        host: String,
        port: u16,
        username: String,
        password: String,
        database: String,
    ) -> Self {
        Self {
            host,
            port,
            username,
            password,
            database,
        }
    }

    #[must_use]
    pub fn connection_string(&self) -> String {
        format!(
            "postgres://{username}:{password}@{host}:{port}/{database}",
            username = self.username,
            password = self.password,
            host = self.host,
            port = self.port,
            database = self.database
        )
    }

    /// Returns the connection string with the password masked for safe logging.
    #[must_use]
    pub fn connection_string_masked(&self) -> String {
        format!(
            "postgres://{username}:***@{host}:{port}/{database}",
            username = self.username,
            host = self.host,
            port = self.port,
            database = self.database
        )
    }

    #[must_use]
    pub fn default_administrator() -> Self {
        Self::new(
            String::from("localhost"),
            5432,
            String::from("nautilus"),
            String::from("pass"),
            String::from("nautilus"),
        )
    }
}

impl Default for PostgresConnectOptions {
    fn default() -> Self {
        Self::new(
            String::from("localhost"),
            5432,
            String::from("nautilus"),
            String::from("pass"),
            String::from("nautilus"),
        )
    }
}

impl From<PostgresConnectOptions> for PgConnectOptions {
    fn from(opt: PostgresConnectOptions) -> Self {
        Self::new()
            .host(opt.host.as_str())
            .port(opt.port)
            .username(opt.username.as_str())
            .password(opt.password.as_str())
            .database(opt.database.as_str())
            .disable_statement_logging()
    }
}

/// Constructs `PostgresConnectOptions` by merging provided arguments, environment variables, and defaults.
///
/// # Panics
///
/// Panics if an environment variable for port cannot be parsed into a `u16`.
#[must_use]
pub fn get_postgres_connect_options(
    host: Option<String>,
    port: Option<u16>,
    username: Option<String>,
    password: Option<String>,
    database: Option<String>,
) -> PostgresConnectOptions {
    let defaults = PostgresConnectOptions::default_administrator();
    let host = host
        .or_else(|| std::env::var("POSTGRES_HOST").ok())
        .unwrap_or(defaults.host);
    let port = port
        .or_else(|| {
            std::env::var("POSTGRES_PORT")
                .map(|port| port.parse::<u16>().unwrap())
                .ok()
        })
        .unwrap_or(defaults.port);
    let username = username
        .or_else(|| std::env::var("POSTGRES_USERNAME").ok())
        .unwrap_or(defaults.username);
    let database = database
        .or_else(|| std::env::var("POSTGRES_DATABASE").ok())
        .unwrap_or(defaults.database);
    let password = password
        .or_else(|| std::env::var("POSTGRES_PASSWORD").ok())
        .unwrap_or(defaults.password);
    PostgresConnectOptions::new(host, port, username, password, database)
}

/// Connects to a Postgres database with the provided connection `options` returning a connection pool.
///
/// # Errors
///
/// Returns an error if establishing the database connection fails.
pub async fn connect_pg(options: PgConnectOptions) -> anyhow::Result<PgPool> {
    Ok(PgPool::connect_with(options).await?)
}

/// Scans the current working directory for the `nautilus_trader` repository
/// and constructs the path to the SQL schema directory.
///
/// # Errors
///
/// Returns an error if the `SCHEMA_DIR` environment variable is not set and the repository
/// cannot be located in the current directory path.
///
/// # Panics
///
/// Panics if the current working directory cannot be determined or contains invalid UTF-8.
fn get_schema_dir() -> anyhow::Result<String> {
    std::env::var("SCHEMA_DIR").or_else(|_| {
        let nautilus_git_repo_name = "nautilus_trader";
        let binding = std::env::current_dir().unwrap();
        let current_dir = binding.to_str().unwrap();
        match current_dir.find(nautilus_git_repo_name){
            Some(index) => {
                let schema_path = current_dir[0..index + nautilus_git_repo_name.len()].to_string() + "/schema/sql";
                Ok(schema_path)
            }
            None => anyhow::bail!("Could not calculate schema dir from current directory path or SCHEMA_DIR env variable")
        }
    })
}

/// Initializes the Postgres database by creating schema, roles, and executing SQL files from `schema_dir`.
///
/// # Errors
///
/// Returns an error if any SQL execution or file system operation fails.
///
/// # Panics
///
/// Panics if `schema_dir` is missing and cannot be determined or if other unwraps fail.
pub async fn init_postgres(
    pg: &PgPool,
    database: String,
    password: String,
    schema_dir: Option<String>,
) -> anyhow::Result<()> {
    log::info!("Initializing Postgres database with target permissions and schema");

    validate_sql_identifier(&database, "database")?;
    let mut connection = pg.acquire().await?;

    // Create public schema
    match sqlx::query("CREATE SCHEMA IF NOT EXISTS public;")
        .execute(&mut *connection)
        .await
    {
        Ok(_) => log::info!("Schema public created successfully"),
        Err(e) => log::error!("Error creating schema public: {e:?}"),
    }

    // Create role if not exists
    let escaped_password = escape_sql_string(&password);
    match sqlx::query(AssertSqlSafe(format!(
        "CREATE ROLE {database} PASSWORD '{escaped_password}' LOGIN;"
    )))
    .execute(&mut *connection)
    .await
    {
        Ok(_) => log::info!("Role {database} created successfully"),
        Err(e) => {
            if e.to_string().contains("already exists") {
                log::info!("Role {database} already exists");
            } else {
                log::error!("Error creating role {database}: {e:?}");
            }
        }
    }

    let schema_dir = schema_dir.unwrap_or_else(|| get_schema_dir().unwrap());
    assign_schema_ownership(&mut connection, &database).await?;
    sqlx::query(AssertSqlSafe(format!(
        "ALTER DATABASE {database} OWNER TO {database};"
    )))
    .execute(&mut *connection)
    .await?;
    sqlx::query(AssertSqlSafe(format!(
        "ALTER SCHEMA public OWNER TO {database};"
    )))
    .execute(&mut *connection)
    .await?;
    execute_schema_as_role(&mut connection, &database, &schema_dir).await?;

    // Grant connect
    match sqlx::query(AssertSqlSafe(format!(
        "GRANT CONNECT ON DATABASE {database} TO {database};"
    )))
    .execute(&mut *connection)
    .await
    {
        Ok(_) => log::info!("Connect privileges granted to role {database}"),
        Err(e) => log::error!("Error granting connect privileges to role {database}: {e:?}"),
    }

    // Grant all schema privileges to the role
    match sqlx::query(AssertSqlSafe(format!(
        "GRANT ALL PRIVILEGES ON SCHEMA public TO {database};"
    )))
    .execute(&mut *connection)
    .await
    {
        Ok(_) => log::info!("All schema privileges granted to role {database}"),
        Err(e) => log::error!("Error granting all privileges to role {database}: {e:?}"),
    }

    // Grant all table privileges to the role
    match sqlx::query(AssertSqlSafe(format!(
        "GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO {database};"
    )))
    .execute(&mut *connection)
    .await
    {
        Ok(_) => log::info!("All tables privileges granted to role {database}"),
        Err(e) => log::error!("Error granting all privileges to role {database}: {e:?}"),
    }

    // Grant all sequence privileges to the role
    match sqlx::query(AssertSqlSafe(format!(
        "GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO {database};"
    )))
    .execute(&mut *connection)
    .await
    {
        Ok(_) => log::info!("All sequences privileges granted to role {database}"),
        Err(e) => log::error!("Error granting all privileges to role {database}: {e:?}"),
    }

    // Grant all function privileges to the role
    match sqlx::query(AssertSqlSafe(format!(
        "GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA public TO {database};"
    )))
    .execute(&mut *connection)
    .await
    {
        Ok(_) => log::info!("All functions privileges granted to role {database}"),
        Err(e) => log::error!("Error granting all privileges to role {database}: {e:?}"),
    }

    Ok(())
}

#[expect(
    clippy::too_many_lines,
    reason = "The catalog query stays intact as one ownership migration boundary"
)]
async fn assign_schema_ownership(
    connection: &mut PgConnection,
    database: &str,
) -> anyhow::Result<()> {
    let statements: Vec<String> = sqlx::query_scalar(
        "
        SELECT statement
        FROM (
            SELECT
                1 AS object_order,
                CASE c.relkind
                    WHEN 'S' THEN format(
                        'ALTER SEQUENCE %I.%I OWNER TO %I',
                        n.nspname,
                        c.relname,
                        $1
                    )
                    WHEN 'v' THEN format(
                        'ALTER VIEW %I.%I OWNER TO %I',
                        n.nspname,
                        c.relname,
                        $1
                    )
                    WHEN 'm' THEN format(
                        'ALTER MATERIALIZED VIEW %I.%I OWNER TO %I',
                        n.nspname,
                        c.relname,
                        $1
                    )
                    WHEN 'f' THEN format(
                        'ALTER FOREIGN TABLE %I.%I OWNER TO %I',
                        n.nspname,
                        c.relname,
                        $1
                    )
                    ELSE format(
                        'ALTER TABLE %I.%I OWNER TO %I',
                        n.nspname,
                        c.relname,
                        $1
                    )
                END AS statement
            FROM pg_class c
            JOIN pg_namespace n ON n.oid = c.relnamespace
            WHERE n.nspname = 'public'
              AND c.relkind IN ('r', 'p', 'v', 'm', 'S', 'f')
              AND (
                  c.relkind <> 'S'
                  OR NOT EXISTS (
                      SELECT 1
                      FROM pg_depend d
                      WHERE d.classid = 'pg_class'::regclass
                        AND d.objid = c.oid
                        AND d.refclassid = 'pg_class'::regclass
                        AND d.deptype IN ('a', 'i')
                  )
              )

            UNION ALL

            SELECT
                2 AS object_order,
                format(
                    'ALTER %s %I.%I(%s) OWNER TO %I',
                    CASE p.prokind WHEN 'p' THEN 'PROCEDURE' ELSE 'FUNCTION' END,
                    n.nspname,
                    p.proname,
                    pg_get_function_identity_arguments(p.oid),
                    $1
                ) AS statement
            FROM pg_proc p
            JOIN pg_namespace n ON n.oid = p.pronamespace
            WHERE n.nspname = 'public'
              AND p.prokind IN ('f', 'p', 'w')

            UNION ALL

            SELECT
                3 AS object_order,
                CASE t.typtype
                    WHEN 'd' THEN format(
                        'ALTER DOMAIN %I.%I OWNER TO %I',
                        n.nspname,
                        t.typname,
                        $1
                    )
                    ELSE format(
                        'ALTER TYPE %I.%I OWNER TO %I',
                        n.nspname,
                        t.typname,
                        $1
                    )
                END AS statement
            FROM pg_type t
            JOIN pg_namespace n ON n.oid = t.typnamespace
            WHERE n.nspname = 'public'
              AND t.typtype IN ('d', 'e')
        ) objects
        ORDER BY object_order, statement
        ",
    )
    .bind(database)
    .fetch_all(&mut *connection)
    .await?;

    for statement in statements {
        sqlx::query(AssertSqlSafe(statement))
            .execute(&mut *connection)
            .await?;
    }

    Ok(())
}

async fn execute_schema_as_role(
    connection: &mut PgConnection,
    database: &str,
    schema_dir: &str,
) -> anyhow::Result<()> {
    sqlx::query(AssertSqlSafe(format!("SET ROLE {database};")))
        .execute(&mut *connection)
        .await?;

    let result = async {
        let sql_files = ["types.sql", "functions.sql", "partitions.sql", "tables.sql"];
        let plpgsql_regex =
            Regex::new(r"\$\$ LANGUAGE plpgsql(?:[ \t\r\n]+SECURITY[ \t\r\n]+DEFINER)?;")?;

        for file_name in sql_files {
            log::info!("Executing schema file: {file_name:?}");
            let file_path = format!("{schema_dir}/{file_name}");
            let sql_content = std::fs::read_to_string(&file_path)?;
            let sql_statements = match file_name {
                "functions.sql" | "partitions.sql" => {
                    let mut statements = Vec::new();
                    let mut last_end = 0;

                    for mat in plpgsql_regex.find_iter(&sql_content) {
                        let statement = sql_content[last_end..mat.end()].to_string();
                        if !statement.trim().is_empty() {
                            statements.push(statement);
                        }
                        last_end = mat.end();
                    }
                    statements
                }
                _ => split_sql_statements(&sql_content),
            };

            for sql_statement in sql_statements {
                if let Err(e) = sqlx::query(AssertSqlSafe(sql_statement.as_str()))
                    .execute(&mut *connection)
                    .await
                {
                    if e.to_string().contains("already exists") {
                        log::info!("Already exists error on statement, skipping");
                    } else {
                        anyhow::bail!(
                            "Error executing statement {sql_statement} with error: {e:?}"
                        );
                    }
                }
            }
        }

        Ok(())
    }
    .await;

    let reset_result = sqlx::query("RESET ROLE;").execute(connection).await;
    match (result, reset_result) {
        (Err(e), Err(reset_error)) => {
            log::error!("Error resetting Postgres role after schema failure: {reset_error:?}");
            Err(e)
        }
        (Err(e), Ok(_)) => Err(e),
        (Ok(()), Err(e)) => Err(e.into()),
        (Ok(()), Ok(_)) => Ok(()),
    }
}

// Splits semicolon-delimited SQL into individual statements.
//
// Skips `--` line comments and respects single-quoted string literals and `$$` dollar-quoted
// bodies, so a semicolon inside a comment, string literal, or `DO` block does not split a
// statement. Tagged `$tag$` quoting is not recognised; keep the schema files on bare `$$`.
// Used for the plain DDL schema files; the PL/pgSQL files are split separately on their
// function terminators.
fn split_sql_statements(sql: &str) -> Vec<String> {
    let mut statements = Vec::new();
    let mut current = String::new();
    let mut chars = sql.chars().peekable();
    let mut in_string = false;
    let mut in_dollar_quote = false;

    while let Some(c) = chars.next() {
        match c {
            '\'' if !in_dollar_quote => {
                // A `''` escape toggles twice, leaving the state unchanged, which is correct
                in_string = !in_string;
                current.push(c);
            }

            '$' if !in_string && chars.peek() == Some(&'$') => {
                chars.next();
                in_dollar_quote = !in_dollar_quote;
                current.push_str("$$");
            }

            '-' if !in_string && !in_dollar_quote && chars.peek() == Some(&'-') => {
                for next in chars.by_ref() {
                    if next == '\n' {
                        current.push('\n');
                        break;
                    }
                }
            }

            ';' if !in_string && !in_dollar_quote => {
                let trimmed = current.trim();
                if !trimmed.is_empty() {
                    statements.push(format!("{trimmed};"));
                }
                current.clear();
            }
            _ => current.push(c),
        }
    }

    let trimmed = current.trim();
    if !trimmed.is_empty() {
        statements.push(format!("{trimmed};"));
    }

    statements
}

/// Drops the Postgres database with the given name using the provided connection pool.
///
/// # Errors
///
/// Returns an error if the DROP DATABASE command fails.
pub async fn drop_postgres(pg: &PgPool, database: String) -> anyhow::Result<()> {
    validate_sql_identifier(&database, "database")?;

    sqlx::query(AssertSqlSafe(format!(
        "ALTER DATABASE {database} OWNER TO SESSION_USER"
    )))
    .execute(pg)
    .await?;

    // Execute drop owned
    match sqlx::query(AssertSqlSafe(format!("DROP OWNED BY {database}")))
        .execute(pg)
        .await
    {
        Ok(_) => log::info!("Dropped owned objects by role {database}"),
        Err(e) => {
            let err_msg = e.to_string();
            if err_msg.contains("2BP01") || err_msg.contains("required by the database system") {
                log::warn!("Skipping system-required objects for role {database}");
            } else {
                log::error!("Error dropping owned by role {database}: {e:?}");
            }
        }
    }

    // Revoke connect
    match sqlx::query(AssertSqlSafe(format!(
        "REVOKE CONNECT ON DATABASE {database} FROM {database};"
    )))
    .execute(pg)
    .await
    {
        Ok(_) => log::info!("Revoked connect privileges from role {database}"),
        Err(e) => log::error!("Error revoking connect privileges from role {database}: {e:?}"),
    }

    // Revoke privileges
    match sqlx::query(AssertSqlSafe(format!(
        "REVOKE ALL PRIVILEGES ON DATABASE {database} FROM {database};"
    )))
    .execute(pg)
    .await
    {
        Ok(_) => log::info!("Revoked all privileges from role {database}"),
        Err(e) => log::error!("Error revoking all privileges from role {database}: {e:?}"),
    }

    // Execute drop schema
    match sqlx::query("DROP SCHEMA IF EXISTS public CASCADE")
        .execute(pg)
        .await
    {
        Ok(_) => log::info!("Dropped schema public"),
        Err(e) => log::error!("Error dropping schema public: {e:?}"),
    }

    // Drop role
    match sqlx::query(AssertSqlSafe(format!("DROP ROLE IF EXISTS {database};")))
        .execute(pg)
        .await
    {
        Ok(_) => log::info!("Dropped role {database}"),
        Err(e) => {
            let err_msg = e.to_string();
            if err_msg.contains("55006") || err_msg.contains("current user cannot be dropped") {
                log::warn!("Cannot drop currently connected role {database}");
            } else {
                anyhow::bail!("Error dropping role {database}: {e:?}");
            }
        }
    }
    Ok(())
}

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

    use super::*;

    #[rstest]
    fn test_postgres_connect_options_toml_round_trip() {
        let config: PostgresConnectOptions = toml::from_str(
            r#"
host = "localhost"
port = 5432
username = "nautilus"
password = "secret"
database = "nautilus"
"#,
        )
        .unwrap();

        assert_eq!(config.host, "localhost");
        assert_eq!(config.port, 5432);
        assert_eq!(config.username, "nautilus");
        assert_eq!(config.database, "nautilus");
    }

    #[rstest]
    fn test_split_sql_statements_basic() {
        let sql = "CREATE TABLE a (id INT); CREATE TABLE b (id INT);";
        assert_eq!(
            split_sql_statements(sql),
            vec!["CREATE TABLE a (id INT);", "CREATE TABLE b (id INT);"]
        );
    }

    #[rstest]
    fn test_split_sql_statements_ignores_semicolon_in_line_comment() {
        // Regression: a `;` inside a `--` comment must not split the following statement
        let sql = "\
-- start points; a later run re-validates them.
ALTER TABLE pool_snapshot ADD COLUMN IF NOT EXISTS validation_state TEXT;";
        assert_eq!(
            split_sql_statements(sql),
            vec!["ALTER TABLE pool_snapshot ADD COLUMN IF NOT EXISTS validation_state TEXT;"]
        );
    }

    #[rstest]
    fn test_split_sql_statements_keeps_code_before_trailing_comment() {
        let sql = "CREATE TABLE a (\n  id INT,  -- REFERENCES x;\n  name TEXT\n);";
        assert_eq!(
            split_sql_statements(sql),
            vec!["CREATE TABLE a (\n  id INT,  \n  name TEXT\n);"]
        );
    }

    #[rstest]
    fn test_split_sql_statements_keeps_dollar_quoted_body_intact() {
        // The guarded column migrations are `DO $$ ... $$` blocks whose bodies carry their own
        // semicolons; splitting on those would hand Postgres a fragment.
        let sql = "\
DO $$
BEGIN
    IF EXISTS (SELECT 1 FROM information_schema.columns WHERE column_name = 'avg_px') THEN
        ALTER TABLE \"order\" ALTER COLUMN avg_px TYPE NUMERIC;
    END IF;
END $$;
SELECT 1;";
        let statements = split_sql_statements(sql);

        assert_eq!(statements.len(), 2);
        assert!(statements[0].starts_with("DO $$"));
        assert!(statements[0].ends_with("END $$;"));
        assert!(statements[0].contains("ALTER COLUMN avg_px TYPE NUMERIC;"));
        assert_eq!(statements[1], "SELECT 1;");
    }

    #[rstest]
    fn test_split_sql_statements_ignores_semicolon_in_string_literal() {
        let sql = "INSERT INTO t VALUES ('a;b'); SELECT 1;";
        assert_eq!(
            split_sql_statements(sql),
            vec!["INSERT INTO t VALUES ('a;b');", "SELECT 1;"]
        );
    }

    #[rstest]
    fn test_split_sql_statements_drops_comment_only_lines() {
        let sql =
            "------------------- ENUMS -------------------\nCREATE TYPE x AS ENUM ('A', 'B');";
        assert_eq!(
            split_sql_statements(sql),
            vec!["CREATE TYPE x AS ENUM ('A', 'B');"]
        );
    }
}