ormada-cli 0.1.0

CLI tool for Ormada ORM migrations - generate, run, and manage database migrations
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
//! Migration file generator

use anyhow::{Context, Result};
use chrono::Utc;
use std::path::{Path, PathBuf};

use ormada_schema::{ColumnType, OnDeleteAction, SchemaOperation, TableSchema};

/// Generates migration files from schema changes
pub struct MigrationGenerator<'a> {
    migrations_path: &'a Path,
}

impl<'a> MigrationGenerator<'a> {
    /// Create a new generator
    pub fn new(migrations_path: &'a Path) -> Self {
        Self { migrations_path }
    }

    /// Generate a migration file
    pub fn generate(
        &self,
        migration_id: &str,
        models: &[TableSchema],
        operations: &[SchemaOperation],
    ) -> Result<PathBuf> {
        let file_name = format!("{}.rs", migration_id);
        let file_path = self.migrations_path.join(&file_name);

        let content = self.generate_content(migration_id, models, operations);

        std::fs::write(&file_path, content)
            .with_context(|| format!("Failed to write migration file: {}", file_path.display()))?;

        // Update mod.rs
        self.update_mod_rs(migration_id)?;

        Ok(file_path)
    }

    /// Generate migration file content
    fn generate_content(
        &self,
        migration_id: &str,
        models: &[TableSchema],
        operations: &[SchemaOperation],
    ) -> String {
        let mut content = String::new();

        let timestamp = Utc::now().format("%Y-%m-%d %H:%M:%S UTC");

        // Header - no pub mod wrapper, standalone file
        content.push_str(&format!(
            r#"//! Migration: {}
//!
//! Generated: {}
//! Generated by `ormada migrate make`
//!
//! Review this file before applying.

use ormada::prelude::*;

"#,
            migration_id, timestamp
        ));

        // Generate schema structs for each table involved
        let mut tables_written = std::collections::HashSet::new();

        for op in operations {
            match op {
                SchemaOperation::CreateTable(schema) => {
                    if tables_written.insert(&schema.name) {
                        content.push_str(&self.generate_table_schema(migration_id, schema, false));
                        content.push('\n');
                    }
                }
                SchemaOperation::AddColumn { table, .. }
                | SchemaOperation::DropColumn { table, .. }
                | SchemaOperation::RenameColumn { table, .. }
                | SchemaOperation::AlterColumn { table, .. } => {
                    if tables_written.insert(table) {
                        // Find the model for this table
                        if let Some(model) = models.iter().find(|m| &m.name == table) {
                            content.push_str(&self.generate_table_schema(
                                migration_id,
                                model,
                                true,
                            ));
                            content.push('\n');
                        }
                    }
                }
                _ => {}
            }
        }

        content
    }

    /// Generate a table schema struct
    fn generate_table_schema(
        &self,
        migration_id: &str,
        schema: &TableSchema,
        is_delta: bool,
    ) -> String {
        let mut s = String::new();

        // Struct name from table name (PascalCase)
        let struct_name = to_pascal_case(&schema.name);

        // Attributes - no indentation since we removed pub mod wrapper
        if is_delta {
            s.push_str(&format!(
                "#[ormada_schema(table = \"{}\", migration = \"{}\", extends = {})]\n",
                schema.name, migration_id, struct_name
            ));
        } else {
            s.push_str(&format!(
                "#[ormada_schema(table = \"{}\", migration = \"{}\")]\n",
                schema.name, migration_id
            ));
        }

        s.push_str(&format!("pub struct {} {{\n", struct_name));

        // Fields
        for col in &schema.columns {
            // Skip dropped columns in output
            if col.dropped {
                s.push_str("    #[drop]\n");
                s.push_str(&format!("    pub {}: (),\n", col.name));
                continue;
            }

            // Rename attribute - only 'from' needed, 'to' is inferred from field name
            if let Some(ref from) = col.renamed_from {
                s.push_str(&format!("    #[rename(from = \"{}\")]\n", from));
            }

            // Primary key
            if col.primary_key {
                if col.auto_increment {
                    s.push_str("    #[primary_key]\n");
                } else {
                    s.push_str("    #[primary_key(auto_increment = false)]\n");
                }
            }

            // Foreign key - look up in schema's foreign_keys
            if let Some(fk) = schema.foreign_keys.iter().find(|fk| fk.column == col.name) {
                let on_delete = format_on_delete(&fk.on_delete);
                let ref_table = to_pascal_case(&fk.references_table);
                if let Some(on_del) = on_delete {
                    s.push_str(&format!(
                        "    #[foreign_key({}, on_delete = {})]\n",
                        ref_table, on_del
                    ));
                } else {
                    s.push_str(&format!("    #[foreign_key({})]\n", ref_table));
                }
            }

            // Index
            if col.indexed {
                if let Some(ref name) = col.index_name {
                    s.push_str(&format!("    #[index(name = \"{}\")]\n", name));
                } else {
                    s.push_str("    #[index]\n");
                }
            }

            // Unique
            if col.unique {
                s.push_str("    #[unique]\n");
            }

            // Max length
            if let Some(len) = col.max_length {
                s.push_str(&format!("    #[max_length({})]\n", len));
            }

            // Default
            if let Some(ref default) = col.default {
                s.push_str(&format!("    #[default({})]\n", default));
            }

            // Soft delete
            if col.soft_delete {
                s.push_str("    #[soft_delete]\n");
            }

            // Nullable - add explicit attribute for clarity
            if col.nullable {
                s.push_str("    #[nullable]\n");
            }

            // Field definition
            let rust_type = column_type_to_rust(&col.column_type, col.nullable);
            s.push_str(&format!("    pub {}: {},\n", col.name, rust_type));
        }

        s.push_str("}\n");
        s
    }

    /// Update mod.rs to include the new migration
    fn update_mod_rs(&self, migration_id: &str) -> Result<()> {
        let mod_path = self.migrations_path.join("mod.rs");

        let mut content = if mod_path.exists() {
            std::fs::read_to_string(&mod_path)?
        } else {
            "//! Database migrations\n\n".to_string()
        };

        // Add module declaration if not already present
        let mod_line = format!("pub mod {};", migration_id);
        if !content.contains(&mod_line) {
            content.push_str(&format!("{}\n", mod_line));
        }

        std::fs::write(&mod_path, content)?;
        Ok(())
    }
}

/// Convert snake_case to PascalCase
fn to_pascal_case(s: &str) -> String {
    s.split('_')
        .map(|word| {
            let mut chars = word.chars();
            match chars.next() {
                Some(first) => first.to_uppercase().chain(chars).collect(),
                None => String::new(),
            }
        })
        .collect()
}

/// Format OnDeleteAction for code generation
fn format_on_delete(action: &OnDeleteAction) -> Option<&'static str> {
    match action {
        OnDeleteAction::Cascade => Some("Cascade"),
        OnDeleteAction::SetNull => Some("SetNull"),
        OnDeleteAction::SetDefault => Some("SetDefault"),
        OnDeleteAction::Restrict => Some("Restrict"),
        OnDeleteAction::NoAction => None, // Default, don't need to specify
    }
}

/// Convert ColumnType to Rust type string
fn column_type_to_rust(col_type: &ColumnType, nullable: bool) -> String {
    let base_type = match col_type {
        ColumnType::Boolean => "bool",
        ColumnType::SmallInteger => "i16",
        ColumnType::Integer => "i32",
        ColumnType::BigInteger => "i64",
        ColumnType::Float => "f32",
        ColumnType::Double => "f64",
        ColumnType::Decimal { .. } => "rust_decimal::Decimal",
        ColumnType::String(_) => "String",
        ColumnType::Text => "String",
        ColumnType::Binary => "Vec<u8>",
        ColumnType::Date => "chrono::NaiveDate",
        ColumnType::Time => "chrono::NaiveTime",
        ColumnType::DateTime => "chrono::NaiveDateTime",
        ColumnType::TimestampTz => "DateTimeWithTimeZone",
        ColumnType::Uuid => "uuid::Uuid",
        ColumnType::Json | ColumnType::JsonB => "serde_json::Value",
    };

    if nullable {
        format!("Option<{}>", base_type)
    } else {
        base_type.to_string()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use ormada_schema::{ColumnSchema, ForeignKeySchema};

    #[test]
    fn test_to_pascal_case() {
        assert_eq!(to_pascal_case("books"), "Books");
        assert_eq!(to_pascal_case("book_authors"), "BookAuthors");
        assert_eq!(to_pascal_case("user_profile_settings"), "UserProfileSettings");
    }

    #[test]
    fn test_column_type_to_rust() {
        assert_eq!(column_type_to_rust(&ColumnType::Integer, false), "i32");
        assert_eq!(column_type_to_rust(&ColumnType::Integer, true), "Option<i32>");
        assert_eq!(column_type_to_rust(&ColumnType::String(Some(200)), false), "String");
        assert_eq!(
            column_type_to_rust(&ColumnType::TimestampTz, true),
            "Option<DateTimeWithTimeZone>"
        );
    }

    #[test]
    fn test_format_on_delete() {
        assert_eq!(format_on_delete(&OnDeleteAction::Cascade), Some("Cascade"));
        assert_eq!(format_on_delete(&OnDeleteAction::SetNull), Some("SetNull"));
        assert_eq!(format_on_delete(&OnDeleteAction::NoAction), None);
    }

    fn make_test_generator() -> MigrationGenerator<'static> {
        MigrationGenerator::new(std::path::Path::new("/tmp/migrations"))
    }

    #[test]
    fn test_generate_table_with_primary_key() {
        let gen = make_test_generator();
        let mut schema = TableSchema::new("users");
        let mut id_col = ColumnSchema::new("id", ColumnType::Integer);
        id_col.primary_key = true;
        id_col.auto_increment = true;
        schema.columns.push(id_col);

        let output = gen.generate_table_schema("m001", &schema, false);
        assert!(output.contains("#[primary_key]"));
        assert!(output.contains("pub id: i32"));
    }

    #[test]
    fn test_generate_table_with_foreign_key() {
        let gen = make_test_generator();
        let mut schema = TableSchema::new("books");

        let mut id_col = ColumnSchema::new("id", ColumnType::Integer);
        id_col.primary_key = true;
        schema.columns.push(id_col);

        let author_id_col = ColumnSchema::new("author_id", ColumnType::Integer);
        schema.columns.push(author_id_col);

        // Add foreign key constraint
        schema.foreign_keys.push(
            ForeignKeySchema::new("author_id", "authors", "id").on_delete(OnDeleteAction::Cascade),
        );

        let output = gen.generate_table_schema("m001", &schema, false);
        assert!(output.contains("#[foreign_key(Authors, on_delete = Cascade)]"));
        assert!(output.contains("pub author_id: i32"));
    }

    #[test]
    fn test_generate_table_with_nullable() {
        let gen = make_test_generator();
        let mut schema = TableSchema::new("users");

        let mut col = ColumnSchema::new("bio", ColumnType::Text);
        col.nullable = true;
        schema.columns.push(col);

        let output = gen.generate_table_schema("m001", &schema, false);
        assert!(output.contains("#[nullable]"));
        assert!(output.contains("pub bio: Option<String>"));
    }

    #[test]
    fn test_generate_table_with_index() {
        let gen = make_test_generator();
        let mut schema = TableSchema::new("users");

        let mut col = ColumnSchema::new("email", ColumnType::String(Some(255)));
        col.indexed = true;
        schema.columns.push(col);

        let output = gen.generate_table_schema("m001", &schema, false);
        assert!(output.contains("#[index]"));
    }

    #[test]
    fn test_generate_table_with_unique() {
        let gen = make_test_generator();
        let mut schema = TableSchema::new("users");

        let mut col = ColumnSchema::new("email", ColumnType::String(Some(255)));
        col.unique = true;
        schema.columns.push(col);

        let output = gen.generate_table_schema("m001", &schema, false);
        assert!(output.contains("#[unique]"));
    }

    #[test]
    fn test_generate_table_with_max_length() {
        let gen = make_test_generator();
        let mut schema = TableSchema::new("users");

        let mut col = ColumnSchema::new("name", ColumnType::String(Some(100)));
        col.max_length = Some(100);
        schema.columns.push(col);

        let output = gen.generate_table_schema("m001", &schema, false);
        assert!(output.contains("#[max_length(100)]"));
    }

    #[test]
    fn test_generate_table_with_default() {
        let gen = make_test_generator();
        let mut schema = TableSchema::new("users");

        let mut col = ColumnSchema::new("active", ColumnType::Boolean);
        col.default = Some("true".to_string());
        schema.columns.push(col);

        let output = gen.generate_table_schema("m001", &schema, false);
        assert!(output.contains("#[default(true)]"));
    }

    #[test]
    fn test_generate_table_with_soft_delete() {
        let gen = make_test_generator();
        let mut schema = TableSchema::new("users");

        let mut col = ColumnSchema::new("deleted_at", ColumnType::TimestampTz);
        col.nullable = true;
        col.soft_delete = true;
        schema.columns.push(col);

        let output = gen.generate_table_schema("m001", &schema, false);
        assert!(output.contains("#[soft_delete]"));
        assert!(output.contains("#[nullable]"));
    }

    #[test]
    fn test_generate_no_pub_mod_wrapper() {
        let gen = make_test_generator();
        let mut schema = TableSchema::new("users");
        schema.columns.push(ColumnSchema::new("id", ColumnType::Integer));

        let ops = vec![SchemaOperation::CreateTable(schema.clone())];
        let content = gen.generate_content("m001_test", &[schema], &ops);

        // Should NOT contain pub mod wrapper
        assert!(!content.contains("pub mod m001_test {"));
        // Should contain direct struct definition
        assert!(content.contains("pub struct Users {"));
        // Should have timestamp in header
        assert!(content.contains("Generated:"));
    }

    #[test]
    fn test_generate_datetime_field_types() {
        let gen = make_test_generator();
        let mut schema = TableSchema::new("posts");

        schema.columns.push(ColumnSchema::new("id", ColumnType::Integer));
        schema.columns.push(ColumnSchema::new("created_at", ColumnType::TimestampTz));
        schema.columns.push(ColumnSchema::new("published_date", ColumnType::Date));
        schema.columns.push(ColumnSchema::new("event_time", ColumnType::Time));

        let output = gen.generate_table_schema("m001", &schema, false);

        assert!(output.contains("pub created_at: DateTimeWithTimeZone"));
        assert!(output.contains("pub published_date: chrono::NaiveDate"));
        assert!(output.contains("pub event_time: chrono::NaiveTime"));
    }

    #[test]
    fn test_generate_nullable_datetime() {
        let gen = make_test_generator();
        let mut schema = TableSchema::new("users");

        let mut col = ColumnSchema::new("deleted_at", ColumnType::TimestampTz);
        col.nullable = true;
        schema.columns.push(col);

        let output = gen.generate_table_schema("m001", &schema, false);

        assert!(output.contains("#[nullable]"));
        assert!(output.contains("pub deleted_at: Option<DateTimeWithTimeZone>"));
    }

    #[test]
    fn test_generate_complete_model() {
        let gen = make_test_generator();
        let mut schema = TableSchema::new("books");

        // Primary key
        let mut id_col = ColumnSchema::new("id", ColumnType::Integer);
        id_col.primary_key = true;
        id_col.auto_increment = true;
        schema.columns.push(id_col);

        // Regular string
        schema.columns.push(ColumnSchema::new("title", ColumnType::String(Some(200))));

        // Foreign key
        let author_id_col = ColumnSchema::new("author_id", ColumnType::Integer);
        schema.columns.push(author_id_col);
        schema.foreign_keys.push(
            ForeignKeySchema::new("author_id", "authors", "id").on_delete(OnDeleteAction::Cascade),
        );

        // Timestamps
        schema.columns.push(ColumnSchema::new("created_at", ColumnType::TimestampTz));
        schema.columns.push(ColumnSchema::new("updated_at", ColumnType::TimestampTz));

        let output = gen.generate_table_schema("m001_initial", &schema, false);

        // Verify all parts are present
        assert!(
            output.contains("#[ormada_schema(table = \"books\", migration = \"m001_initial\")]")
        );
        assert!(output.contains("pub struct Books {"));
        assert!(output.contains("#[primary_key]"));
        assert!(output.contains("pub id: i32"));
        assert!(output.contains("pub title: String"));
        assert!(output.contains("#[foreign_key(Authors, on_delete = Cascade)]"));
        assert!(output.contains("pub author_id: i32"));
        assert!(output.contains("pub created_at: DateTimeWithTimeZone"));
        assert!(output.contains("pub updated_at: DateTimeWithTimeZone"));
    }

    #[test]
    fn test_generate_all_column_types() {
        let gen = make_test_generator();
        let mut schema = TableSchema::new("all_types");

        schema.columns.push(ColumnSchema::new("bool_col", ColumnType::Boolean));
        schema.columns.push(ColumnSchema::new("i16_col", ColumnType::SmallInteger));
        schema.columns.push(ColumnSchema::new("i32_col", ColumnType::Integer));
        schema.columns.push(ColumnSchema::new("i64_col", ColumnType::BigInteger));
        schema.columns.push(ColumnSchema::new("f32_col", ColumnType::Float));
        schema.columns.push(ColumnSchema::new("f64_col", ColumnType::Double));
        schema.columns.push(ColumnSchema::new("string_col", ColumnType::String(None)));
        schema.columns.push(ColumnSchema::new("text_col", ColumnType::Text));
        schema.columns.push(ColumnSchema::new("binary_col", ColumnType::Binary));
        schema.columns.push(ColumnSchema::new("date_col", ColumnType::Date));
        schema.columns.push(ColumnSchema::new("time_col", ColumnType::Time));
        schema.columns.push(ColumnSchema::new("datetime_col", ColumnType::DateTime));
        schema.columns.push(ColumnSchema::new("timestamp_col", ColumnType::TimestampTz));
        schema.columns.push(ColumnSchema::new("uuid_col", ColumnType::Uuid));
        schema.columns.push(ColumnSchema::new("json_col", ColumnType::Json));

        let output = gen.generate_table_schema("m001", &schema, false);

        assert!(output.contains("pub bool_col: bool"));
        assert!(output.contains("pub i16_col: i16"));
        assert!(output.contains("pub i32_col: i32"));
        assert!(output.contains("pub i64_col: i64"));
        assert!(output.contains("pub f32_col: f32"));
        assert!(output.contains("pub f64_col: f64"));
        assert!(output.contains("pub string_col: String"));
        assert!(output.contains("pub text_col: String"));
        assert!(output.contains("pub binary_col: Vec<u8>"));
        assert!(output.contains("pub date_col: chrono::NaiveDate"));
        assert!(output.contains("pub time_col: chrono::NaiveTime"));
        assert!(output.contains("pub datetime_col: chrono::NaiveDateTime"));
        assert!(output.contains("pub timestamp_col: DateTimeWithTimeZone"));
        assert!(output.contains("pub uuid_col: uuid::Uuid"));
        assert!(output.contains("pub json_col: serde_json::Value"));
    }
}