pgmt 0.4.8

PostgreSQL migration tool that keeps your schema files as the source of truth
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
use anyhow::Result;
use dialoguer::{Confirm, Input, MultiSelect, Select};
use std::path::PathBuf;

use super::commands::ExistingConfigDefaults;
use super::import::ImportSource;
use super::{BaselineCreationConfig, DatabaseState, InitOptions, ObjectManagementConfig};
use crate::config::types::Directories;
use crate::prompts::ShadowDatabaseInput;

/// Gather initialization options using CLI arguments when available
/// If `existing_defaults` is provided, those values will be used as defaults in prompts
pub async fn gather_init_options_with_args(
    args: &super::InitArgs,
    existing_defaults: Option<&ExistingConfigDefaults>,
) -> Result<InitOptions> {
    // Current directory as default project directory
    let project_dir = std::env::current_dir()?;

    // Get directory defaults
    let dir_defaults = Directories::default();

    // Database URL - use CLI arg or prompt
    let (dev_database_url, detected_pg_version) = if let Some(url) = &args.dev_url {
        // CLI arg provided - test connection and detect version
        print!("🔄 Testing connection...");
        match sqlx::PgPool::connect(url).await {
            Ok(pool) => {
                let version: Result<(String,), _> =
                    sqlx::query_as("SHOW server_version").fetch_one(&pool).await;
                pool.close().await;
                match version {
                    Ok((v,)) => {
                        let pg_version = v.split_whitespace().next().unwrap_or(&v).to_string();
                        println!(" ✅ (PostgreSQL {})", pg_version);
                        (url.clone(), Some(pg_version))
                    }
                    Err(_) => {
                        println!("");
                        (url.clone(), None)
                    }
                }
            }
            Err(e) => {
                println!("");
                return Err(anyhow::anyhow!("Connection failed: {}", e));
            }
        }
    } else if args.defaults {
        // In defaults mode, use existing value if available, otherwise use default
        let url = existing_defaults
            .and_then(|d| d.dev_url.clone())
            .unwrap_or_else(|| "postgres://localhost/pgmt_dev".to_string());
        (url, None)
    } else {
        // Interactive prompt - pass existing value as default
        let existing_url = existing_defaults.and_then(|d| d.dev_url.clone());
        let result =
            crate::prompts::prompt_database_url_with_guidance_and_default(existing_url).await?;
        (result.url, result.pg_version)
    };

    // Shadow database configuration
    // - If --shadow-pg-version is specified, use auto mode with that version
    // - If --auto-shadow is specified, use auto mode
    // - If defaults mode, use auto mode
    // - Otherwise prompt (but we'll simplify this prompt too)
    let shadow_config = if args.auto_shadow || args.shadow_pg_version.is_some() || args.defaults {
        crate::prompts::ShadowDatabaseInput::Auto
    } else {
        crate::prompts::prompt_shadow_mode_with_explanation().await?
    };

    // Schema directory - CLI arg > existing > default
    let schema_dir = if args.schema_dir != "schema" {
        // CLI arg was explicitly set (not default value)
        PathBuf::from(&args.schema_dir)
    } else {
        // Use existing or default
        existing_defaults
            .and_then(|d| d.schema_dir.clone())
            .map(PathBuf::from)
            .unwrap_or_else(|| PathBuf::from("schema"))
    };

    // Directory configuration - CLI args > existing > defaults
    let migrations_dir = args
        .migrations_dir
        .clone()
        .or_else(|| existing_defaults.and_then(|d| d.migrations_dir.clone()))
        .unwrap_or_else(|| dir_defaults.migrations.clone());

    let baselines_dir = args
        .baselines_dir
        .clone()
        .or_else(|| existing_defaults.and_then(|d| d.baselines_dir.clone()))
        .unwrap_or_else(|| dir_defaults.baselines.clone());

    // Import existing schema option - use CLI arg or prompt
    let import_source = if args.no_import || args.defaults {
        None
    } else {
        prompt_import_source().await?
    };

    // Object management configuration - use defaults for now
    // Will be prompted later with catalog context in the main flow
    let object_config = ObjectManagementConfig::default();

    // Baseline configuration - use CLI args or set up for prompting
    let baseline_config = if args.create_baseline {
        BaselineCreationConfig {
            create_baseline: Some(true),
            description: args.baseline_description.clone(),
        }
    } else if args.no_baseline || args.defaults {
        BaselineCreationConfig {
            create_baseline: Some(false),
            description: None,
        }
    } else {
        BaselineCreationConfig {
            create_baseline: None, // Will prompt user based on database state
            description: args.baseline_description.clone(),
        }
    };

    // Roles file handling:
    // 1. If --roles-file provided, use that path
    // 2. Otherwise, auto-detect roles.sql in project directory
    let roles_file = if let Some(path) = &args.roles_file {
        println!("   Using roles file: {}", path);
        Some(path.clone())
    } else if project_dir.join("roles.sql").exists() {
        println!("   Found roles.sql - will use for shadow database setup");
        Some("roles.sql".to_string())
    } else {
        None
    };

    Ok(InitOptions {
        project_dir,
        dev_database_url,
        shadow_config,
        shadow_pg_version: args.shadow_pg_version.clone(),
        detected_pg_version,
        schema_dir,
        migrations_dir,
        baselines_dir,
        import_source,
        object_config,
        baseline_config,
        tracking_table: crate::config::types::TrackingTable::default(),
        roles_file,
    })
}

/// Prompt for schema import options
pub async fn prompt_import_source() -> Result<Option<ImportSource>> {
    let options = vec![
        (0, "None (empty project)"),
        (1, "SQL dump file (e.g., from pg_dump)"),
        (2, "Directory with SQL files"),
        (3, "Live database connection"),
    ];

    let choice = crate::prompts::prompt_select("📥 Import from", options)?;

    let source = match choice {
        0 => return Ok(None),
        1 => {
            let file = prompt_import_sql_file()?;
            ImportSource::SqlFile(file)
        }
        2 => {
            let dir = prompt_import_directory()?;
            ImportSource::Directory(dir)
        }
        3 => {
            let url = prompt_import_database_url().await?;
            ImportSource::Database(url)
        }
        _ => return Err(anyhow::anyhow!("Invalid choice")),
    };

    Ok(Some(source))
}

/// Prompt for import directory with validation
fn prompt_import_directory() -> Result<PathBuf> {
    loop {
        let dir = crate::prompts::prompt_directory_with_validation(
            "📁 SQL files directory",
            Some("./db/migrate"),
        )?;

        // Validate directory contains SQL files
        match crate::db::sql_executor::discover_sql_files_ordered(&dir) {
            Ok(files) if files.is_empty() => {
                println!("⚠️  Directory '{}' contains no SQL files.", dir.display());
                let retry = Confirm::new()
                    .with_prompt("Try a different directory?")
                    .default(true)
                    .interact()?;

                if !retry {
                    return Err(anyhow::anyhow!("No SQL files found for import"));
                }
                continue;
            }
            Ok(files) => {
                println!("✅ Found {} SQL files in directory", files.len());
                return Ok(dir);
            }
            Err(e) => {
                println!("❌ Error reading directory: {}", e);
                let retry = Confirm::new()
                    .with_prompt("Try a different directory?")
                    .default(true)
                    .interact()?;

                if !retry {
                    return Err(anyhow::anyhow!("Cannot read directory for import"));
                }
                continue;
            }
        }
    }
}

/// Prompt for SQL file with validation
fn prompt_import_sql_file() -> Result<PathBuf> {
    loop {
        let file_path: String = Input::new()
            .with_prompt("📄 SQL file path")
            .default("./dump.sql".to_string())
            .interact_text()?;

        let file_path = PathBuf::from(file_path.trim());

        // Validate file exists and is readable
        match super::import::sql_file::validate_sql_file(&file_path) {
            Ok(_) => {
                println!("✅ SQL file validation passed");
                return Ok(file_path);
            }
            Err(e) => {
                println!("❌ SQL file validation failed: {}", e);
                let retry = Confirm::new()
                    .with_prompt("Try a different file?")
                    .default(true)
                    .interact()?;

                if !retry {
                    return Err(anyhow::anyhow!("Invalid SQL file for import"));
                }
                continue;
            }
        }
    }
}

/// Prompt for database URL for import
async fn prompt_import_database_url() -> Result<String> {
    crate::prompts::prompt_database_url_simple("🔗 Source database URL").await
}

/// Prompt for object management configuration (without catalog context)
pub fn prompt_object_management_config() -> Result<ObjectManagementConfig> {
    let advanced_explanation = "
⚙️  Advanced Options (Optional)
   pgmt can manage additional database objects like comments, user permissions,
   triggers, and extensions. The defaults work well for most projects.";

    println!("{}", advanced_explanation);

    let configure_advanced = Confirm::new()
        .with_prompt("Configure advanced object management?")
        .default(false)
        .interact()?;

    if !configure_advanced {
        return Ok(ObjectManagementConfig::default());
    }

    let items = vec![
        "Comments (table/column documentation)",
        "Grants (user permissions)",
        "Triggers (automated actions)",
        "Extensions (PostgreSQL extensions)",
    ];

    println!("\n🎯 Select object types to manage (use Space to toggle, Enter to confirm):");
    let selections = MultiSelect::new()
        .with_prompt("Which object types should pgmt manage?")
        .items(&items)
        .defaults(&[true, true, true, true]) // All enabled by default
        .interact()?;

    // Convert selections to individual booleans
    let comments = selections.contains(&0);
    let grants = selections.contains(&1);
    let triggers = selections.contains(&2);
    let extensions = selections.contains(&3);

    Ok(ObjectManagementConfig {
        comments,
        grants,
        triggers,
        extensions,
    })
}

/// Prompt for object management configuration WITH catalog context
/// Uses smart defaults, only prompts for grants if there are many (>100)
pub fn prompt_object_management_config_with_context(
    catalog: &crate::catalog::Catalog,
) -> Result<ObjectManagementConfig> {
    // Count objects
    let comment_count = count_commentable_objects(catalog);
    let grant_count = catalog.grants.len();
    let trigger_count = catalog.triggers.len();
    let extension_count = catalog.extensions.len();

    // Smart defaults: enable what exists in the database
    let mut config = ObjectManagementConfig {
        comments: comment_count > 0,
        grants: grant_count > 0,
        triggers: trigger_count > 0,
        extensions: extension_count > 0,
    };

    // Only prompt for grants if there are many (complex permission setup)
    // Simple projects with few/no grants don't need to think about this
    if grant_count > 100 {
        println!(
            "\n   Your schema has {} grant definitions across multiple roles.",
            grant_count
        );
        let manage_grants = Confirm::new()
            .with_prompt("🔑 Manage GRANTs/permissions?")
            .default(true)
            .interact()?;
        config.grants = manage_grants;
    }

    Ok(config)
}

/// Count objects that can have comments (tables, views, functions)
fn count_commentable_objects(catalog: &crate::catalog::Catalog) -> usize {
    catalog.tables.len() + catalog.views.len() + catalog.functions.len()
}

/// Prompt user for baseline creation based on database state with detailed explanations
pub fn prompt_baseline_creation(database_state: &DatabaseState) -> Result<bool> {
    match database_state {
        DatabaseState::Empty => {
            println!("\n📊 Database Analysis: Empty database detected");
            println!("   No baseline needed for empty databases.");
            println!("   You can create schema files and use 'pgmt apply' to build your schema.\n");
            Ok(false)
        }
        DatabaseState::Existing { object_count } => {
            println!("\n📊 Database Analysis: Existing database detected");
            println!(
                "   Found {} database objects that can be imported into schema files.",
                object_count
            );

            let explanation = "
❓ Should we create a baseline from your existing database?

💡 CREATE a baseline if:
   ✅ This database has EVER been deployed (production, staging, etc.)
   ✅ Other developers or environments use this schema
   ✅ You want clean migration history going forward

   A baseline captures your current state as 'migration zero' so future
   migrations only contain NEW changes, not recreating existing objects.

⚠️  SKIP baseline ONLY if:
   ❌ This database has NEVER been deployed anywhere
   ❌ You're the only developer and this is purely local
   ❌ You want the first migration to recreate everything from scratch

   Without a baseline, 'pgmt migrate new' will try to DROP and recreate
   ALL existing objects, which will fail on deployed databases.

🔍 What is a baseline?
   A baseline is a SQL snapshot of your current database schema.
   It gets marked as 'applied' so future migrations build on top of it.";

            println!("{}", explanation);

            let create_baseline = Confirm::new()
                .with_prompt("Create baseline? (Yes if this has EVER been deployed)")
                .default(true)
                .interact()?;

            if create_baseline {
                println!("✅ Baseline will be created and marked as applied");
                println!("   Future migrations will only contain new changes");
            } else {
                println!("⚠️  Baseline creation skipped");
                println!("   Next migration will attempt to recreate existing objects");
            }

            Ok(create_baseline)
        }
    }
}

/// Prompt for project creation confirmation with summary
/// Returns whether to proceed with initialization
pub fn prompt_project_confirmation(options: &InitOptions) -> Result<bool> {
    // Build compact summary line
    let shadow_version = options
        .shadow_pg_version
        .as_ref()
        .or(options
            .detected_pg_version
            .as_ref()
            .map(|v| {
                // Extract major version from detected (e.g., "15" from "15.4")
                crate::prompts::extract_major_version(v)
            })
            .as_ref())
        .cloned()
        .unwrap_or_else(|| "auto".to_string());

    let shadow_desc = match &options.shadow_config {
        ShadowDatabaseInput::Auto => {
            if options.detected_pg_version.is_some() {
                format!("Docker (PG {} - matches dev)", shadow_version)
            } else {
                format!("Docker (PG {})", shadow_version)
            }
        }
        ShadowDatabaseInput::Manual(url) => format!("Manual ({})", mask_sensitive_url(url)),
    };

    // Build roles info if present
    let roles_info = options
        .roles_file
        .as_ref()
        .map(|f| format!(" | Roles: {}", f))
        .unwrap_or_default();

    println!(
        "\n📋 Setup: {}",
        mask_sensitive_url(&options.dev_database_url)
    );
    println!("   💡 Shadow: {}{}", shadow_desc, roles_info);
    println!(
        "   📁 Directories: {} | {} | {}",
        options.schema_dir.display(),
        options.migrations_dir,
        options.baselines_dir
    );

    if let Some(ref import_source) = options.import_source {
        println!("   📥 Import: {}", import_source.description());
    }

    // Show confirmation prompt
    let choices = vec!["Yes, proceed", "No, cancel"];

    let selection = Select::new()
        .with_prompt("🚀 Proceed with these settings?")
        .items(&choices)
        .default(0)
        .interact()?;

    Ok(selection == 0)
}

/// Prompt to customize directories
/// Returns (schema_dir, migrations_dir, baselines_dir)
#[allow(dead_code)]
pub fn prompt_directory_customization(
    current_schema: &str,
    current_migrations: &str,
    current_baselines: &str,
) -> Result<(String, String, String)> {
    println!("\n📁 Directory Configuration:");

    let schema_dir: String = Input::new()
        .with_prompt("Schema directory")
        .default(current_schema.to_string())
        .interact_text()?;

    let migrations_dir: String = Input::new()
        .with_prompt("Migrations directory")
        .default(current_migrations.to_string())
        .interact_text()?;

    let baselines_dir: String = Input::new()
        .with_prompt("Baselines directory")
        .default(current_baselines.to_string())
        .interact_text()?;

    Ok((schema_dir, migrations_dir, baselines_dir))
}

/// Mask sensitive parts of database URL for display
fn mask_sensitive_url(url: &str) -> String {
    // Handle case where URL doesn't contain ://
    if !url.contains("://") {
        return "Invalid URL".to_string();
    }

    // Split on :// to remove protocol
    let parts: Vec<&str> = url.splitn(2, "://").collect();
    if parts.len() != 2 {
        return "Invalid URL".to_string();
    }

    let authority_and_path = parts[1];

    // Check if there's user info (user:pass@host or user@host)
    if let Some(at_pos) = authority_and_path.find('@') {
        let user_info = &authority_and_path[..at_pos];
        let host_and_path = &authority_and_path[at_pos + 1..];

        // Extract just the username part (before any colon)
        let username = if let Some(colon_pos) = user_info.find(':') {
            &user_info[..colon_pos]
        } else {
            user_info
        };

        format!("{}@{}", username, host_and_path)
    } else {
        // No user info, just return host and path
        authority_and_path.to_string()
    }
}

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

    #[test]
    fn test_mask_sensitive_url() {
        assert_eq!(
            mask_sensitive_url("postgres://user:pass@localhost:5432/mydb"),
            "user@localhost:5432/mydb"
        );

        assert_eq!(
            mask_sensitive_url("postgres://localhost/mydb"),
            "localhost/mydb"
        );

        assert_eq!(mask_sensitive_url("invalid url"), "Invalid URL");
    }
}