tideorm-cli 0.10.0

Command-line interface for TideORM
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
//! TideORM CLI - Command-line interface for TideORM
//!
//! A comprehensive CLI tool for managing TideORM projects including:
//! - Database migrations
//! - Seeders
//! - Model generation
//! - Database utilities
//!
//! # Usage
//!
//! ```bash
//! # Run pending migrations
//! tideorm migrate
//!
//! # Generate a model
//! tideorm make model User --fields="name:string,email:string:unique"
//!
//! # Run seeders
//! tideorm db seed
//! ```

// `db::handle` awaits a chain of TideORM calls in one async fn, and rustc computes the layout of
// the whole generator as a single type. SeaORM 2.x (via sqlx 0.9) nests deeply enough that this
// crosses the default limit of 128 — `query depth increased by 130` — and the build fails before
// any of our own code is at fault. Raising the limit is the documented fix; it costs compile-time
// budget only, and nothing here recurses at runtime.
#![recursion_limit = "256"]

mod commands;
mod config;
mod generators;
mod runtime_db;
mod utils;

use clap::{Parser, Subcommand};
use colored::Colorize;

/// TideORM CLI - A powerful command-line interface for TideORM
#[derive(Parser)]
#[command(name = "tideorm")]
#[command(author = "TideORM Contributors")]
#[command(version)]
#[command(about = "Command-line interface for TideORM - A powerful Rust ORM", long_about = None)]
#[command(propagate_version = true)]
struct Cli {
    /// Path to the TideORM configuration file
    #[arg(short, long, global = true, default_value = "tideorm.toml")]
    config: String,

    /// Enable verbose output
    #[arg(short, long, global = true)]
    verbose: bool,

    #[command(subcommand)]
    command: Commands,
}

#[derive(Subcommand)]
enum Commands {
    // =========================================================================
    // MIGRATION COMMANDS
    // =========================================================================
    /// Migration commands - run with a subcommand, or on its own to execute pending migrations
    Migrate {
        #[command(subcommand)]
        command: Option<MigrateCommands>,
    },

    // =========================================================================
    // MAKE COMMANDS (Generators)
    // =========================================================================
    /// Subcommands for generating files
    #[command(subcommand, name = "make")]
    Make(MakeCommands),

    // =========================================================================
    // DATABASE COMMANDS
    // =========================================================================
    /// Subcommands for database operations
    #[command(subcommand, name = "db")]
    Db(DbCommands),

    // =========================================================================
    // UTILITY COMMANDS
    // =========================================================================
    /// Initialize a new TideORM project
    Init {
        /// Project name
        #[arg(default_value = ".")]
        name: String,

        /// Database type (postgres, mysql, sqlite)
        #[arg(short, long, default_value = "postgres")]
        database: String,
    },

    /// Show TideORM configuration
    Config,

    /// List all models in the project
    Models,

    /// Show schema information
    Schema {
        /// Table name to show schema for
        #[arg(short, long)]
        table: Option<String>,
    },
}

#[derive(Subcommand)]
enum MigrateCommands {
    /// Run all pending migrations
    Run {
        /// Run migrations in a specific directory
        #[arg(short, long)]
        path: Option<String>,

        /// Pretend mode - show SQL without executing
        #[arg(long)]
        pretend: bool,

        /// Force run in production
        #[arg(long)]
        force: bool,

        /// Run a specific migration step
        #[arg(long)]
        step: Option<u32>,
    },

    /// Generate a new migration file
    #[command(name = "generate", alias = "gen")]
    Generate {
        /// Migration name (e.g., create_users_table)
        name: String,

        /// Create table migration
        #[arg(long)]
        create: Option<String>,

        /// Alter table migration
        #[arg(short = 'a', long)]
        table: Option<String>,

        /// Fields to add (format: name:type:modifiers)
        #[arg(short, long)]
        fields: Option<String>,
    },

    /// Run pending migrations - all of them unless --step or --migration narrows it down
    Up {
        /// Number of pending migrations to run (default: all of them)
        #[arg(long)]
        step: Option<u32>,

        /// Specific migration to run
        #[arg(long)]
        migration: Option<String>,

        /// Pretend mode
        #[arg(long)]
        pretend: bool,

        /// Force run in production
        #[arg(long)]
        force: bool,
    },

    /// Roll back applied migrations - only the most recent one unless --step says otherwise
    Down {
        /// Number of migrations to rollback (default: 1)
        #[arg(long, default_value = "1")]
        step: u32,

        /// Specific migration to rollback
        #[arg(long)]
        migration: Option<String>,

        /// Pretend mode
        #[arg(long)]
        pretend: bool,

        /// Force run in production
        #[arg(long)]
        force: bool,
    },

    /// Redo last migration (down then up)
    Redo {
        /// Number of migrations to redo
        #[arg(long, default_value = "1")]
        step: u32,

        /// Pretend mode
        #[arg(long)]
        pretend: bool,

        /// Force run in production
        #[arg(long)]
        force: bool,
    },

    /// Rollback all migrations and re-run
    Fresh {
        /// Also run seeders after migration
        #[arg(long)]
        seed: bool,

        /// Specific seeder to run
        #[arg(long)]
        seeder: Option<String>,

        /// Force run in production
        #[arg(long)]
        force: bool,
    },

    /// Reset all migrations (rollback all)
    Reset {
        /// Force run in production
        #[arg(long)]
        force: bool,

        /// Pretend mode
        #[arg(long)]
        pretend: bool,
    },

    /// Refresh migrations (reset + migrate)
    Refresh {
        /// Also run seeders after migration
        #[arg(long)]
        seed: bool,

        /// Number of migrations to refresh
        #[arg(long)]
        step: Option<u32>,

        /// Force run in production
        #[arg(long)]
        force: bool,
    },

    /// Record a migration as applied without running it
    ///
    /// Escape hatch for backends that commit DDL implicitly (MySQL/MariaDB): when a
    /// migration's schema change survived but its ledger row did not, this reconciles
    /// the ledger so later runs stop failing. Pass --unmark for the opposite repair.
    Mark {
        /// Migration to record (file name, version or logical name)
        #[arg(long)]
        migration: String,

        /// Remove the ledger entry instead of adding it
        #[arg(long)]
        unmark: bool,

        /// Force run in production
        #[arg(long)]
        force: bool,
    },

    /// Show migration status
    Status,

    /// Show migration history
    History {
        /// Number of migrations to show
        #[arg(short, long, default_value = "10")]
        limit: u32,
    },
}

#[derive(Subcommand)]
enum MakeCommands {
    /// Generate a new model
    #[command(name = "model")]
    Model {
        /// Model name (e.g., User, BlogPost)
        name: String,

        /// Table name (defaults to snake_case plural of model name)
        #[arg(short, long)]
        table: Option<String>,

        /// Fields (format: name:type[:modifiers...], comma-separated)
        /// Types: string, text, i32, i64, f32, f64, bool, datetime, date, time, uuid, json, jsonb, decimal, bytes, int_array, bigint_array, text_array, bool_array, float_array, json_array
        /// Modifiers: nullable, unique, indexed, primary_key, auto_increment, default=value
        /// Example: --fields="name:string,email:string:unique,age:i32:nullable"
        #[arg(short, long)]
        fields: Option<String>,

        /// Relations (format: name:type:Model[:foreign_key], comma-separated)
        /// Types: belongs_to, has_one, has_many
        /// Relations are defined as struct fields with proper TideORM types (HasOne, HasMany, BelongsTo)
        /// Example: --relations="posts:has_many:Post,company:belongs_to:Company:company_id"
        #[arg(short, long)]
        relations: Option<String>,

        /// Translatable fields (comma-separated field names)
        /// Example: --translatable="title,description,content"
        #[arg(long, alias = "trans")]
        translatable: Option<String>,

        /// Single attachment fields (comma-separated field names)
        /// Example: --attachments-single="avatar,thumbnail"
        #[arg(long, alias = "attach-single")]
        attachments_single: Option<String>,

        /// Multiple attachment fields (comma-separated field names)
        /// Example: --attachments-multi="photos,documents"
        #[arg(long, alias = "attach-multi")]
        attachments_multi: Option<String>,

        /// Indexed fields (comma-separated field names)
        /// Example: --indexed="email,username"
        #[arg(long, alias = "idx")]
        indexed: Option<String>,

        /// Unique fields (comma-separated field names)
        /// Example: --unique="email,username"
        #[arg(long, alias = "uniq")]
        unique: Option<String>,

        /// Nullable fields (comma-separated field names)
        /// Example: --nullable="bio,avatar_url"
        #[arg(long, alias = "null")]
        nullable: Option<String>,

        /// Enable soft deletes - defaults to `[model] soft_deletes` in tideorm.toml (false)
        #[arg(
            long,
            alias = "soft-delete",
            action = clap::ArgAction::Set,
            num_args = 0..=1,
            default_missing_value = "true"
        )]
        soft_deletes: Option<bool>,

        /// Enable timestamps (created_at, updated_at) - defaults to `[model] timestamps` in tideorm.toml (true); pass --timestamps=false to disable
        #[arg(
            long,
            action = clap::ArgAction::Set,
            num_args = 0..=1,
            default_missing_value = "true"
        )]
        timestamps: Option<bool>,

        /// Enable tokenization - defaults to `[model] tokenize` in tideorm.toml (false)
        #[arg(
            long,
            action = clap::ArgAction::Set,
            num_args = 0..=1,
            default_missing_value = "true"
        )]
        tokenize: Option<bool>,

        /// Output directory for the model file
        #[arg(short, long, default_value = "src/models")]
        output: String,

        /// Also generate a migration for this model
        #[arg(long)]
        migration: bool,

        /// Also generate a seeder for this model
        #[arg(long)]
        seeder: bool,

        /// Also generate a factory for this model
        #[arg(long)]
        factory: bool,

        /// Generate all (migration + seeder + factory)
        #[arg(short, long)]
        all: bool,
    },

    /// Generate a new migration
    #[command(name = "migration")]
    Migration {
        /// Migration name
        name: String,

        /// Create table migration
        #[arg(long)]
        create: Option<String>,

        /// Alter table migration  
        #[arg(short = 'a', long)]
        table: Option<String>,

        /// Fields to add
        #[arg(short, long)]
        fields: Option<String>,

        /// Output directory
        #[arg(short, long, default_value = "src/migrations")]
        output: String,
    },

    /// Generate a new seeder
    #[command(name = "seeder")]
    Seeder {
        /// Seeder name (e.g., UserSeeder)
        name: String,

        /// Model to seed
        #[arg(short, long)]
        model: Option<String>,

        /// Number of records to seed
        #[arg(short = 'n', long, default_value = "10")]
        count: u32,

        /// Output directory
        #[arg(short, long, default_value = "src/seeders")]
        output: String,
    },

    /// Generate a new factory
    #[command(name = "factory")]
    Factory {
        /// Factory name
        name: String,

        /// Model for the factory
        #[arg(short, long)]
        model: Option<String>,

        /// Output directory
        #[arg(short, long, default_value = "src/factories")]
        output: String,
    },
}

#[derive(Subcommand)]
enum DbCommands {
    /// Run database seeders
    Seed {
        /// Specific seeder class to run
        #[arg(short, long, alias = "class")]
        seeder: Option<String>,

        /// Force run in production
        #[arg(long)]
        force: bool,
    },

    /// Drop all tables and re-seed
    Fresh {
        /// Force run in production
        #[arg(long)]
        force: bool,
    },

    /// Show database connection status
    Status,

    /// Initialize TideORM metadata tables in the current database
    Check,

    /// Create the database
    Create {
        /// Database name
        #[arg(short, long)]
        name: Option<String>,
    },

    /// Drop the database
    Drop {
        /// Database name
        #[arg(short, long)]
        name: Option<String>,

        /// Force drop without confirmation
        #[arg(long)]
        force: bool,
    },

    /// Drop every table in the database (schema included - this is not a truncate)
    Wipe {
        /// Also drop user-defined enum types (PostgreSQL only; ignored elsewhere)
        #[arg(long)]
        drop_types: bool,

        /// Force run in production
        #[arg(long)]
        force: bool,
    },

    /// Show table information
    Table {
        /// Table name
        name: String,
    },

    /// List all tables
    Tables,
}

fn main() {
    // The command futures nest deeply - the MySQL driver most of all - and in a
    // debug build `db fresh` overflowed the 1 MiB stack Windows gives the main
    // thread, so the runtime runs on a thread with the 8 MiB Linux defaults to.
    let finished = std::thread::Builder::new()
        .stack_size(8 * 1024 * 1024)
        .spawn(|| {
            tokio::runtime::Builder::new_multi_thread()
                .enable_all()
                .build()
                .expect("failed to start the Tokio runtime")
                .block_on(run())
        })
        .expect("failed to start the CLI thread")
        .join();

    // A panic has already been reported by the thread that panicked.
    if finished.is_err() {
        std::process::exit(101);
    }
}

async fn run() {
    let cli = Cli::parse();

    // Print banner
    if cli.verbose {
        print_banner();
    }

    // Execute command
    let result = match cli.command {
        // `tideorm migrate` with no subcommand runs pending migrations, which is
        // what both the crate documentation and the command help advertise.
        Commands::Migrate { command } => match command {
            Some(cmd) => commands::migrate::handle_subcommand(&cli.config, cmd, cli.verbose).await,
            None => commands::migrate::run(&cli.config, None, false, false, None).await,
        },
        Commands::Make(cmd) => commands::make::handle(&cli.config, cmd, cli.verbose).await,
        Commands::Db(cmd) => commands::db::handle(&cli.config, cmd, cli.verbose).await,
        Commands::Init { name, database } => {
            commands::init::run(&name, &database, cli.verbose).await
        }
        Commands::Config => commands::config::show(&cli.config, cli.verbose).await,
        Commands::Models => commands::models::list(&cli.config, cli.verbose).await,
        Commands::Schema { table } => commands::schema::show(&cli.config, table, cli.verbose).await,
    };

    // Handle result
    match result {
        Ok(()) => {
            if cli.verbose {
                println!("\n{}", "✓ Command completed successfully".green());
            }
        }
        Err(e) => {
            eprintln!("{} {}", "Error:".red().bold(), e);
            std::process::exit(1);
        }
    }
}

fn print_banner() {
    println!(
        "{}",
        r#"
╔════════════════════════════════════════════════════════════════╗
║                                                                ║
║   ████████╗██╗██████╗ ███████╗ ██████╗ ██████╗ ███╗   ███╗    ║
║   ╚══██╔══╝██║██╔══██╗██╔════╝██╔═══██╗██╔══██╗████╗ ████║    ║
║      ██║   ██║██║  ██║█████╗  ██║   ██║██████╔╝██╔████╔██║    ║
║      ██║   ██║██║  ██║██╔══╝  ██║   ██║██╔══██╗██║╚██╔╝██║    ║
║      ██║   ██║██████╔╝███████╗╚██████╔╝██║  ██║██║ ╚═╝ ██║    ║
║      ╚═╝   ╚═╝╚═════╝ ╚══════╝ ╚═════╝ ╚═╝  ╚═╝╚═╝     ╚═╝    ║
║                                                                ║
║                    Command Line Interface                      ║
╚════════════════════════════════════════════════════════════════╝
"#
        .cyan()
    );
}

#[cfg(test)]
mod tests {
    use super::{Cli, Commands, MakeCommands};
    use clap::Parser;

    fn model_flags(args: &[&str]) -> (Option<bool>, Option<bool>, Option<bool>) {
        let cli = Cli::try_parse_from(args).expect("make model should parse");

        match cli.command {
            Commands::Make(MakeCommands::Model {
                timestamps,
                soft_deletes,
                tokenize,
                ..
            }) => (timestamps, soft_deletes, tokenize),
            _ => panic!("expected a `make model` command"),
        }
    }

    /// The `[model]` config can only win when an unsupplied flag is distinguishable from
    /// one that was passed explicitly, which is what `Option<bool>` buys.
    #[test]
    fn model_flags_are_absent_until_supplied() {
        assert_eq!(
            model_flags(&["tideorm", "make", "model", "User"]),
            (None, None, None)
        );
    }

    #[test]
    fn model_flags_record_explicit_values() {
        assert_eq!(
            model_flags(&["tideorm", "make", "model", "User", "--timestamps=true"]),
            (Some(true), None, None)
        );
        assert_eq!(
            model_flags(&["tideorm", "make", "model", "User", "--timestamps=false"]),
            (Some(false), None, None)
        );
        assert_eq!(
            model_flags(&["tideorm", "make", "model", "User", "--timestamps"]),
            (Some(true), None, None)
        );
        assert_eq!(
            model_flags(&["tideorm", "make", "model", "User", "--soft-deletes"]),
            (None, Some(true), None)
        );
        assert_eq!(
            model_flags(&["tideorm", "make", "model", "User", "--tokenize"]),
            (None, None, Some(true))
        );
    }
}