prax-orm-cli 0.12.2

CLI tool for the Prax ORM
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
//! Docker-gated round-trip introspection integration tests.
//!
//! For each SQL backend these tests seed a known schema in a live database,
//! introspect it through the CLI's `introspect_database` dispatcher, map the
//! result to a `prax_schema::Schema`, and assert:
//!
//! 1. Diffing a matching `.prax` against the introspected source is **empty**
//!    (the no-spurious-churn property — the single most important test).
//! 2. Diffing a `.prax` that adds one column yields exactly that delta.
//!
//! Every test is `#[ignore]` and self-skips unless `PRAX_E2E=1`, following the
//! workspace convention. Run via docker compose:
//!
//! ```sh
//! docker compose up -d postgres mysql mssql
//! PRAX_E2E=1 POSTGRES_URL=... MYSQL_URL=... MSSQL_URL=... SQLITE_URL=file:./e2e.db \
//!   cargo test -p prax-orm-cli --features postgres,mysql,sqlite,mssql \
//!   --test introspect_roundtrip -- --ignored
//! ```

#![cfg(test)]

#[allow(unused_imports)]
use prax_cli::commands::introspect::{IntrospectionOptions, introspect_database};
#[allow(unused_imports)]
use prax_cli::commands::schema_from_db::schema_from_database;
#[allow(unused_imports)]
use prax_migrate::{IntrospectionConfig, SchemaDiffer};

/// Whether E2E tests should run.
#[allow(dead_code)]
fn e2e() -> bool {
    std::env::var("PRAX_E2E").ok().as_deref() == Some("1")
}

/// Build the diff of `target_prax` against a `.prax` introspected from `db`.
#[allow(dead_code)]
fn diff_against_source(target_prax: &str, source: prax_schema::Schema) -> prax_migrate::SchemaDiff {
    let target = prax_schema::parse_schema(target_prax).expect("target .prax parses");
    SchemaDiffer::new(target)
        .with_source(source)
        .diff()
        .expect("diff")
}

// ============================================================================
// PostgreSQL
// ============================================================================

#[cfg(feature = "postgres")]
mod postgres {
    use super::*;

    fn url() -> Option<String> {
        std::env::var("POSTGRES_URL")
            .or_else(|_| std::env::var("DATABASE_URL"))
            .ok()
    }

    async fn exec(url: &str, sql: &str) {
        let (client, connection) = tokio_postgres::connect(url, tokio_postgres::NoTls)
            .await
            .expect("connect postgres");
        tokio::spawn(async move {
            let _ = connection.await;
        });
        client.batch_execute(sql).await.expect("exec ddl");
    }

    const SEED: &str = "
        DROP TABLE IF EXISTS pg_rt_posts CASCADE;
        DROP TABLE IF EXISTS pg_rt_users CASCADE;
        CREATE TABLE pg_rt_users (
            id BIGINT PRIMARY KEY,
            email TEXT NOT NULL UNIQUE
        );
        CREATE TABLE pg_rt_posts (
            id BIGINT PRIMARY KEY,
            title TEXT NOT NULL,
            author_id BIGINT NOT NULL,
            CONSTRAINT pg_rt_posts_author_fk FOREIGN KEY (author_id) REFERENCES pg_rt_users (id)
        );
    ";

    fn prax_v1() -> &'static str {
        r#"
        model User {
            id    BigInt @id
            email String @unique
            @@map("pg_rt_users")
        }
        model Post {
            id        BigInt @id
            title     String
            author_id BigInt
            author    User   @relation(fields: [author_id], references: [id], map: "pg_rt_posts_author_fk")
            @@map("pg_rt_posts")
        }
        "#
    }

    #[tokio::test]
    #[ignore = "requires running PostgreSQL via docker-compose"]
    async fn roundtrip_empty_and_delta() {
        if !e2e() {
            eprintln!("skipping: PRAX_E2E not set");
            return;
        }
        let url = url().expect("POSTGRES_URL required");
        exec(&url, SEED).await;

        let opts = IntrospectionOptions {
            table_filter: Some("pg_rt_*".to_string()),
            ..Default::default()
        };
        let db = introspect_database("postgres", &url, &opts)
            .await
            .expect("introspect");
        let source = schema_from_database(&db, IntrospectionConfig::default())
            .expect("map")
            .schema;

        let empty = diff_against_source(prax_v1(), source.clone());
        assert!(
            empty.is_empty(),
            "expected empty diff, got: {}",
            empty.summary()
        );

        // v2 adds a nullable column on users.
        let v2 = r#"
        model User {
            id    BigInt  @id
            email String  @unique
            bio   String?
            @@map("pg_rt_users")
        }
        model Post {
            id        BigInt @id
            title     String
            author_id BigInt
            author    User   @relation(fields: [author_id], references: [id], map: "pg_rt_posts_author_fk")
            @@map("pg_rt_posts")
        }
        "#;
        let delta = diff_against_source(v2, source);
        assert!(delta.create_models.is_empty(), "no new tables");
        assert_eq!(delta.alter_models.len(), 1, "one altered model");
        assert_eq!(delta.alter_models[0].add_fields.len(), 1);
        assert_eq!(delta.alter_models[0].add_fields[0].column_name, "bio");
    }
}

// ============================================================================
// MySQL
// ============================================================================

#[cfg(feature = "mysql")]
mod mysql {
    use super::*;
    use prax_mysql::{MysqlPool, MysqlRawEngine};

    fn url() -> Option<String> {
        std::env::var("MYSQL_URL").ok()
    }

    async fn engine(url: &str) -> MysqlRawEngine {
        let pool = MysqlPool::builder()
            .url(url)
            .build()
            .await
            .expect("connect mysql");
        MysqlRawEngine::new(pool)
    }

    fn prax_v1() -> &'static str {
        r#"
        model User {
            id    BigInt @id
            email String @unique
            @@map("my_rt_users")
        }
        model Post {
            id        BigInt @id
            title     String
            author_id BigInt
            author    User   @relation(fields: [author_id], references: [id], map: "my_rt_posts_author_fk")
            @@map("my_rt_posts")
        }
        "#
    }

    #[tokio::test]
    #[ignore = "requires running MySQL via docker-compose"]
    async fn roundtrip_empty_and_delta() {
        if !e2e() {
            eprintln!("skipping: PRAX_E2E not set");
            return;
        }
        let url = url().expect("MYSQL_URL required");
        let eng = engine(&url).await;
        for stmt in [
            "DROP TABLE IF EXISTS my_rt_posts",
            "DROP TABLE IF EXISTS my_rt_users",
            "CREATE TABLE my_rt_users (id BIGINT PRIMARY KEY, email VARCHAR(255) NOT NULL UNIQUE)",
            "CREATE TABLE my_rt_posts (id BIGINT PRIMARY KEY, title TEXT NOT NULL, author_id BIGINT NOT NULL, \
             CONSTRAINT my_rt_posts_author_fk FOREIGN KEY (author_id) REFERENCES my_rt_users (id))",
        ] {
            eng.raw_sql_execute(stmt, &[]).await.expect("seed ddl");
        }

        // MySQL scopes information_schema by database; pass the DB name.
        let db_name = url
            .rsplit('/')
            .next()
            .and_then(|s| s.split('?').next())
            .unwrap_or("prax_test")
            .to_string();
        let opts = IntrospectionOptions {
            schema: Some(db_name),
            table_filter: Some("my_rt_*".to_string()),
            ..Default::default()
        };
        let db = introspect_database("mysql", &url, &opts)
            .await
            .expect("introspect");
        let source = schema_from_database(&db, IntrospectionConfig::default())
            .expect("map")
            .schema;

        let empty = diff_against_source(prax_v1(), source.clone());
        assert!(
            empty.is_empty(),
            "expected empty diff, got: {}",
            empty.summary()
        );

        let v2 = r#"
        model User {
            id    BigInt  @id
            email String  @unique
            bio   String?
            @@map("my_rt_users")
        }
        model Post {
            id        BigInt @id
            title     String
            author_id BigInt
            author    User   @relation(fields: [author_id], references: [id], map: "my_rt_posts_author_fk")
            @@map("my_rt_posts")
        }
        "#;
        let delta = diff_against_source(v2, source);
        assert!(delta.create_models.is_empty(), "no new tables");
        assert_eq!(delta.alter_models.len(), 1, "one altered model");
        assert_eq!(delta.alter_models[0].add_fields[0].column_name, "bio");
    }
}

// ============================================================================
// SQLite
// ============================================================================

#[cfg(feature = "sqlite")]
mod sqlite {
    use super::*;
    use prax_sqlite::{SqlitePool, SqliteRawEngine};

    #[tokio::test]
    #[ignore = "requires PRAX_E2E (uses a temp file DB, no external service)"]
    async fn roundtrip_empty_and_delta() {
        if !e2e() {
            eprintln!("skipping: PRAX_E2E not set");
            return;
        }
        // A dedicated temp file DB — no external service needed, but still
        // gated so the default `cargo test` stays hermetic.
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("rt.db");
        let url = format!("file:{}", path.display());

        let pool = SqlitePool::builder()
            .url(&url)
            .build()
            .await
            .expect("open sqlite");
        let eng = SqliteRawEngine::new(pool);
        for stmt in [
            "CREATE TABLE sq_rt_users (id INTEGER PRIMARY KEY, email TEXT NOT NULL UNIQUE)",
            "CREATE TABLE sq_rt_posts (id INTEGER PRIMARY KEY, title TEXT NOT NULL, author_id INTEGER NOT NULL, \
             CONSTRAINT sq_rt_posts_author_fk FOREIGN KEY (author_id) REFERENCES sq_rt_users (id))",
        ] {
            eng.raw_sql_execute(stmt, &[]).await.expect("seed ddl");
        }

        // SQLite: single INTEGER PRIMARY KEY auto-increments (rowid alias);
        // the .prax marks id @auto to match. FK is synthesized as
        // fk_<table>_<col> by the introspector (SQLite FKs are unnamed), so
        // the .prax pins that name via @relation(map:).
        let prax_v1 = r#"
        model User {
            id    Int    @id @auto
            email String @unique
            @@map("sq_rt_users")
        }
        model Post {
            id        Int    @id @auto
            title     String
            author_id Int
            author    User   @relation(fields: [author_id], references: [id], map: "fk_sq_rt_posts_author_id")
            @@map("sq_rt_posts")
        }
        "#;

        let opts = IntrospectionOptions {
            table_filter: Some("sq_rt_*".to_string()),
            ..Default::default()
        };
        let db = introspect_database("sqlite", &url, &opts)
            .await
            .expect("introspect");
        let source = schema_from_database(&db, IntrospectionConfig::default())
            .expect("map")
            .schema;

        let empty = diff_against_source(prax_v1, source.clone());
        assert!(
            empty.is_empty(),
            "expected empty diff, got: {}",
            empty.summary()
        );

        let v2 = r#"
        model User {
            id    Int     @id @auto
            email String  @unique
            bio   String?
            @@map("sq_rt_users")
        }
        model Post {
            id        Int    @id @auto
            title     String
            author_id Int
            author    User   @relation(fields: [author_id], references: [id], map: "fk_sq_rt_posts_author_id")
            @@map("sq_rt_posts")
        }
        "#;
        let delta = diff_against_source(v2, source);
        assert!(delta.create_models.is_empty(), "no new tables");
        assert_eq!(delta.alter_models.len(), 1, "one altered model");
        assert_eq!(delta.alter_models[0].add_fields[0].column_name, "bio");
    }
}

// ============================================================================
// MSSQL
// ============================================================================

#[cfg(feature = "mssql")]
mod mssql {
    use super::*;
    use prax_mssql::MssqlPool;

    fn url() -> Option<String> {
        std::env::var("MSSQL_URL").ok()
    }

    fn prax_v1() -> &'static str {
        r#"
        model User {
            id    BigInt @id
            email String @unique
            @@map("ms_rt_users")
        }
        model Post {
            id        BigInt @id
            title     String
            author_id BigInt
            author    User   @relation(fields: [author_id], references: [id], map: "ms_rt_posts_author_fk")
            @@map("ms_rt_posts")
        }
        "#
    }

    #[tokio::test]
    #[ignore = "requires running MSSQL via docker-compose"]
    async fn roundtrip_empty_and_delta() {
        if !e2e() {
            eprintln!("skipping: PRAX_E2E not set");
            return;
        }
        let url = url().expect("MSSQL_URL required");
        let pool = MssqlPool::builder()
            .connection_string(url.clone())
            .build()
            .await
            .expect("connect mssql");
        {
            let mut conn = pool.get().await.expect("conn");
            // Drop child first, then parent.
            for stmt in [
                "IF OBJECT_ID('ms_rt_posts','U') IS NOT NULL DROP TABLE ms_rt_posts",
                "IF OBJECT_ID('ms_rt_users','U') IS NOT NULL DROP TABLE ms_rt_users",
                "CREATE TABLE ms_rt_users (id BIGINT PRIMARY KEY, email NVARCHAR(255) NOT NULL UNIQUE)",
                "CREATE TABLE ms_rt_posts (id BIGINT PRIMARY KEY, title NVARCHAR(MAX) NOT NULL, author_id BIGINT NOT NULL, \
                 CONSTRAINT ms_rt_posts_author_fk FOREIGN KEY (author_id) REFERENCES ms_rt_users (id))",
            ] {
                conn.execute(stmt, &[]).await.expect("seed ddl");
            }
        }

        let opts = IntrospectionOptions {
            schema: Some("dbo".to_string()),
            table_filter: Some("ms_rt_*".to_string()),
            ..Default::default()
        };
        let db = introspect_database("mssql", &url, &opts)
            .await
            .expect("introspect");
        let source = schema_from_database(&db, IntrospectionConfig::default())
            .expect("map")
            .schema;

        let empty = diff_against_source(prax_v1(), source.clone());
        assert!(
            empty.is_empty(),
            "expected empty diff, got: {}",
            empty.summary()
        );

        let v2 = r#"
        model User {
            id    BigInt  @id
            email String  @unique
            bio   String?
            @@map("ms_rt_users")
        }
        model Post {
            id        BigInt @id
            title     String
            author_id BigInt
            author    User   @relation(fields: [author_id], references: [id], map: "ms_rt_posts_author_fk")
            @@map("ms_rt_posts")
        }
        "#;
        let delta = diff_against_source(v2, source);
        assert!(delta.create_models.is_empty(), "no new tables");
        assert_eq!(delta.alter_models.len(), 1, "one altered model");
        assert_eq!(delta.alter_models[0].add_fields[0].column_name, "bio");
    }
}