cargo-rustapi 0.1.443

The official CLI tool for the RustAPI framework. Scaffold new projects, run development servers, 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
//! Database migration commands
//!
//! Provides a wrapper around sqlx-cli for database migrations.
//! Supports creating, running, reverting, and checking migration status.

use anyhow::{Context, Result};
use clap::{Args, Subcommand};
use console::{style, Emoji};
use indicatif::{ProgressBar, ProgressStyle};
use std::path::Path;
use std::time::Duration;
use tokio::fs;
use tokio::process::Command;

static CHECK: Emoji<'_, '_> = Emoji("", "+ ");
static WARN: Emoji<'_, '_> = Emoji("⚠️  ", "! ");
static ERROR: Emoji<'_, '_> = Emoji("", "x ");
static DB: Emoji<'_, '_> = Emoji("🗄️  ", "# ");
static ARROW: Emoji<'_, '_> = Emoji("➡️  ", "-> ");

/// Database migration commands
#[derive(Subcommand, Debug)]
pub enum MigrateArgs {
    /// Run all pending migrations
    Run(MigrateRunArgs),

    /// Revert the last migration (or N migrations)
    Revert(MigrateRevertArgs),

    /// Show migration status
    Status(MigrateStatusArgs),

    /// Create a new migration
    Create(MigrateCreateArgs),

    /// Reset database (drop, create, run all migrations)
    Reset(MigrateResetArgs),
}

#[derive(Args, Debug)]
pub struct MigrateRunArgs {
    /// Database URL (overrides DATABASE_URL env var)
    #[arg(long)]
    pub database_url: Option<String>,

    /// Run in dry-run mode (don't actually apply)
    #[arg(long)]
    pub dry_run: bool,

    /// Migrations directory
    #[arg(long, default_value = "migrations")]
    pub source: String,
}

#[derive(Args, Debug)]
pub struct MigrateRevertArgs {
    /// Database URL (overrides DATABASE_URL env var)
    #[arg(long)]
    pub database_url: Option<String>,

    /// Number of migrations to revert
    #[arg(short, long, default_value = "1")]
    pub count: u32,

    /// Run in dry-run mode
    #[arg(long)]
    pub dry_run: bool,

    /// Migrations directory
    #[arg(long, default_value = "migrations")]
    pub source: String,
}

#[derive(Args, Debug)]
pub struct MigrateStatusArgs {
    /// Database URL (overrides DATABASE_URL env var)
    #[arg(long)]
    pub database_url: Option<String>,

    /// Migrations directory
    #[arg(long, default_value = "migrations")]
    pub source: String,
}

#[derive(Args, Debug)]
pub struct MigrateCreateArgs {
    /// Migration name (e.g., "create_users_table")
    pub name: String,

    /// Create reversible migration (with up.sql and down.sql)
    #[arg(short, long)]
    pub reversible: bool,

    /// Migrations directory
    #[arg(long, default_value = "migrations")]
    pub source: String,

    /// Create migration with timestamp prefix instead of sequential
    #[arg(long)]
    pub timestamp: bool,
}

#[derive(Args, Debug)]
pub struct MigrateResetArgs {
    /// Database URL (overrides DATABASE_URL env var)
    #[arg(long)]
    pub database_url: Option<String>,

    /// Migrations directory
    #[arg(long, default_value = "migrations")]
    pub source: String,

    /// Skip confirmation prompt
    #[arg(short, long)]
    pub yes: bool,
}

/// Execute migration commands
pub async fn migrate(args: MigrateArgs) -> Result<()> {
    match args {
        MigrateArgs::Run(args) => {
            ensure_sqlx_installed().await?;
            run_migrations(args).await
        }
        MigrateArgs::Revert(args) => {
            ensure_sqlx_installed().await?;
            revert_migrations(args).await
        }
        MigrateArgs::Status(args) => {
            ensure_sqlx_installed().await?;
            show_status(args).await
        }
        MigrateArgs::Create(args) => create_migration(args).await, // No sqlx needed
        MigrateArgs::Reset(args) => {
            ensure_sqlx_installed().await?;
            reset_database(args).await
        }
    }
}

/// Ensure sqlx-cli is installed
async fn ensure_sqlx_installed() -> Result<()> {
    let output = Command::new("sqlx").arg("--version").output().await;

    match output {
        Ok(out) if out.status.success() => Ok(()),
        _ => {
            println!(
                "{}",
                style("sqlx-cli is not installed. Installing...").yellow()
            );
            println!("{}", style("This may take a few minutes...").dim());

            let status = Command::new("cargo")
                .args([
                    "install",
                    "sqlx-cli",
                    "--no-default-features",
                    "--features",
                    "postgres,mysql,sqlite",
                ])
                .status()
                .await
                .context("Failed to run cargo install")?;

            if !status.success() {
                anyhow::bail!(
                    "Failed to install sqlx-cli. Please install it manually:\n\
                     cargo install sqlx-cli --features postgres,mysql,sqlite"
                );
            }

            println!("{} sqlx-cli installed successfully!", CHECK);
            Ok(())
        }
    }
}

/// Run pending migrations
async fn run_migrations(args: MigrateRunArgs) -> Result<()> {
    println!(
        "{} {} Running migrations...",
        DB,
        style("migrate run").cyan().bold()
    );
    println!();

    // Ensure migrations directory exists
    if !Path::new(&args.source).exists() {
        println!(
            "{} {} No migrations directory found at '{}'",
            WARN,
            style("Warning:").yellow(),
            args.source
        );
        println!(
            "{}",
            style("Create one with: cargo rustapi migrate create <name>").dim()
        );
        return Ok(());
    }

    let mut cmd = Command::new("sqlx");
    cmd.args(["migrate", "run"]);

    if let Some(url) = &args.database_url {
        cmd.arg("--database-url").arg(url);
    }

    cmd.arg("--source").arg(&args.source);

    if args.dry_run {
        cmd.arg("--dry-run");
        println!("{} Running in dry-run mode", style("Note:").yellow());
    }

    let status = cmd.status().await.context("Failed to run sqlx migrate")?;

    if status.success() {
        println!();
        println!("{} Migrations applied successfully!", CHECK);
    } else {
        anyhow::bail!("Migration failed");
    }

    Ok(())
}

/// Revert migrations
async fn revert_migrations(args: MigrateRevertArgs) -> Result<()> {
    println!(
        "{} {} Reverting {} migration(s)...",
        DB,
        style("migrate revert").cyan().bold(),
        args.count
    );
    println!();

    let mut cmd = Command::new("sqlx");
    cmd.args(["migrate", "revert"]);

    if let Some(url) = &args.database_url {
        cmd.arg("--database-url").arg(url);
    }

    cmd.arg("--source").arg(&args.source);

    if args.dry_run {
        cmd.arg("--dry-run");
        println!("{} Running in dry-run mode", style("Note:").yellow());
    }

    // Revert N times
    for i in 0..args.count {
        println!("{} Reverting migration {}...", ARROW, i + 1);
        let status = cmd
            .status()
            .await
            .context("Failed to run sqlx migrate revert")?;
        if !status.success() {
            anyhow::bail!("Failed to revert migration {}", i + 1);
        }
    }

    println!();
    println!("{} {} migration(s) reverted!", CHECK, args.count);

    Ok(())
}

/// Show migration status
async fn show_status(args: MigrateStatusArgs) -> Result<()> {
    println!(
        "{} {} Checking migration status...",
        DB,
        style("migrate status").cyan().bold()
    );
    println!();

    // Check if migrations directory exists
    if !Path::new(&args.source).exists() {
        println!(
            "{} {} No migrations directory found",
            WARN,
            style("Warning:").yellow()
        );
        return Ok(());
    }

    // List local migrations
    let mut local_migrations = Vec::new();
    let mut entries = fs::read_dir(&args.source).await?;
    while let Some(entry) = entries.next_entry().await? {
        let path = entry.path();
        if path.is_dir() {
            if let Some(name) = path.file_name() {
                local_migrations.push(name.to_string_lossy().to_string());
            }
        }
    }
    local_migrations.sort();

    if local_migrations.is_empty() {
        println!("{} No migrations found in '{}'", WARN, args.source);
        return Ok(());
    }

    println!("{}", style("Local migrations:").bold());
    for migration in &local_migrations {
        println!("  {} {}", ARROW, migration);
    }
    println!();

    // Run sqlx migrate info if database is available
    if args.database_url.is_some() || std::env::var("DATABASE_URL").is_ok() {
        let mut cmd = Command::new("sqlx");
        cmd.args(["migrate", "info"]);

        if let Some(url) = &args.database_url {
            cmd.arg("--database-url").arg(url);
        }

        cmd.arg("--source").arg(&args.source);

        println!("{}", style("Database migration status:").bold());
        let _ = cmd.status().await;
    } else {
        println!(
            "{} {} Set DATABASE_URL to see applied migrations",
            WARN,
            style("Tip:").yellow()
        );
    }

    Ok(())
}

/// Create a new migration
async fn create_migration(args: MigrateCreateArgs) -> Result<()> {
    println!(
        "{} {} Creating migration '{}'...",
        DB,
        style("migrate create").cyan().bold(),
        args.name
    );
    println!();

    // Create migrations directory if it doesn't exist
    if !Path::new(&args.source).exists() {
        fs::create_dir_all(&args.source).await?;
        println!(
            "{} Created migrations directory: {}",
            CHECK,
            style(&args.source).cyan()
        );
    }

    // Generate timestamp or sequential prefix
    let timestamp = chrono_timestamp();
    let migration_dir = format!("{}/{}_{}", args.source, timestamp, args.name);

    fs::create_dir_all(&migration_dir).await?;

    if args.reversible {
        // Create up.sql and down.sql
        let up_content = format!(
            "-- Migration: {}\n-- Created at: {}\n\n-- Write your UP migration here\n",
            args.name, timestamp
        );
        let down_content = format!(
            "-- Migration: {} (revert)\n-- Created at: {}\n\n-- Write your DOWN migration here\n",
            args.name, timestamp
        );

        fs::write(format!("{}/up.sql", migration_dir), up_content).await?;
        fs::write(format!("{}/down.sql", migration_dir), down_content).await?;

        println!("{} Created reversible migration:", CHECK);
        println!("   {} {}/up.sql", ARROW, style(&migration_dir).cyan());
        println!("   {} {}/down.sql", ARROW, style(&migration_dir).cyan());
    } else {
        // Create single migration file
        let content = format!(
            "-- Migration: {}\n-- Created at: {}\n\n-- Write your migration here\n",
            args.name, timestamp
        );

        // For simple migrations, sqlx expects just a .sql file in the migrations dir
        let migration_file = format!("{}/{}_{}.sql", args.source, timestamp, args.name);

        // Remove the directory we created and use file instead
        fs::remove_dir(&migration_dir).await.ok();
        fs::write(&migration_file, content).await?;

        println!("{} Created migration:", CHECK);
        println!("   {} {}", ARROW, style(&migration_file).cyan());
    }

    println!();
    println!("{}", style("Edit the migration file(s), then run:").dim());
    println!("   cargo rustapi migrate run");

    Ok(())
}

/// Reset database
async fn reset_database(args: MigrateResetArgs) -> Result<()> {
    println!(
        "{} {} This will DROP and recreate your database!",
        ERROR,
        style("WARNING:").red().bold()
    );
    println!();

    if !args.yes {
        use dialoguer::{theme::ColorfulTheme, Confirm};

        if !Confirm::with_theme(&ColorfulTheme::default())
            .with_prompt("Are you sure you want to reset the database?")
            .default(false)
            .interact()?
        {
            println!("{}", style("Aborted").yellow());
            return Ok(());
        }
    }

    let pb = ProgressBar::new_spinner();
    pb.set_style(
        ProgressStyle::default_spinner()
            .template("{spinner:.red} {msg}")
            .unwrap(),
    );
    pb.enable_steady_tick(Duration::from_millis(80));

    // Drop database
    pb.set_message("Dropping database...");
    let mut drop_cmd = Command::new("sqlx");
    drop_cmd.args(["database", "drop", "-y"]);
    if let Some(url) = &args.database_url {
        drop_cmd.arg("--database-url").arg(url);
    }
    let _ = drop_cmd.status().await; // Ignore error if DB doesn't exist

    // Create database
    pb.set_message("Creating database...");
    let mut create_cmd = Command::new("sqlx");
    create_cmd.args(["database", "create"]);
    if let Some(url) = &args.database_url {
        create_cmd.arg("--database-url").arg(url);
    }
    create_cmd
        .status()
        .await
        .context("Failed to create database")?;

    // Run migrations
    pb.set_message("Running migrations...");
    let mut migrate_cmd = Command::new("sqlx");
    migrate_cmd.args(["migrate", "run"]);
    if let Some(url) = &args.database_url {
        migrate_cmd.arg("--database-url").arg(url);
    }
    migrate_cmd.arg("--source").arg(&args.source);
    migrate_cmd
        .status()
        .await
        .context("Failed to run migrations")?;

    pb.finish_and_clear();

    println!();
    println!("{} Database reset complete!", CHECK);

    Ok(())
}

/// Generate a timestamp for migration names
fn chrono_timestamp() -> String {
    use std::time::{SystemTime, UNIX_EPOCH};

    let duration = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .expect("Time went backwards");

    // Format: YYYYMMDDHHMMSS
    let secs = duration.as_secs();

    // Simple conversion (not timezone aware, but good enough for migration ordering)
    let days = secs / 86400;
    let _years_since_1970 = days / 365;

    // For simplicity, just use the unix timestamp
    // This ensures unique, sortable names
    format!("{}", secs)
}

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

    #[test]
    fn test_timestamp_generation() {
        let ts1 = chrono_timestamp();
        let ts2 = chrono_timestamp();

        // Timestamps should be numeric
        assert!(ts1.chars().all(|c| c.is_ascii_digit()));

        // Should be reasonably close (within same second usually)
        let diff: i64 = ts2.parse::<i64>().unwrap() - ts1.parse::<i64>().unwrap();
        assert!(diff.abs() <= 1);
    }

    #[test]
    fn test_migrate_create_args() {
        let args = MigrateCreateArgs {
            name: "create_users".to_string(),
            reversible: true,
            source: "migrations".to_string(),
            timestamp: false,
        };

        assert_eq!(args.name, "create_users");
        assert!(args.reversible);
    }
}