-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinteractive.rs
More file actions
826 lines (732 loc) · 35.1 KB
/
interactive.rs
File metadata and controls
826 lines (732 loc) · 35.1 KB
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
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
// ABOUTME: Interactive terminal UI for database and table selection
// ABOUTME: Provides multi-step wizard with back navigation using inquire crate
use crate::{
filters::ReplicationFilter,
migration, postgres,
table_rules::{QualifiedTable, TableRules},
};
use anyhow::{Context, Result};
use inquire::{Confirm, MultiSelect, Select, Text};
/// Wizard step state machine
enum WizardStep {
SelectDatabases,
SelectTablesForDb(usize), // index of current database in selected_dbs
SelectSchemaOnlyForDb(usize), // schema-only tables selection
ConfigureTimeFiltersForDb(usize), // time filter configuration
Review,
}
/// Cached table info for a database (to avoid repeated queries)
struct CachedDbTables {
all_tables: Vec<migration::TableInfo>,
table_display_names: Vec<String>,
}
/// Interactive database and table selection with back navigation
///
/// Presents a terminal UI for selecting:
/// 1. Which databases to replicate (multi-select)
/// 2. For each selected database: tables to include (Enter = include all)
/// 3. For each selected database: tables to replicate schema-only (no data)
/// 4. For each selected database: time-based filters
/// 5. Summary and confirmation
///
/// Supports back navigation:
/// - Cancel/Esc from any step → go back to previous step
///
/// Returns a tuple of `(ReplicationFilter, TableRules)` representing the user's selections.
///
/// # Arguments
///
/// * `source_url` - PostgreSQL connection string for source database
///
/// # Returns
///
/// Returns `Ok((ReplicationFilter, TableRules))` with the user's selections or an error if:
/// - Cannot connect to source database
/// - Cannot discover databases or tables
/// - User cancels the operation
///
/// # Examples
///
/// ```no_run
/// # use anyhow::Result;
/// # use database_replicator::interactive::select_databases_and_tables;
/// # async fn example() -> Result<()> {
/// let (filter, rules) = select_databases_and_tables(
/// "postgresql://user:pass@source.example.com/postgres"
/// ).await?;
/// # Ok(())
/// # }
/// ```
pub async fn select_databases_and_tables(
source_url: &str,
) -> Result<(ReplicationFilter, TableRules)> {
tracing::info!("Starting interactive database and table selection...");
println!();
// Connect to source database
tracing::info!("Connecting to source database...");
let source_client = postgres::connect_with_retry(source_url)
.await
.context("Failed to connect to source database")?;
tracing::info!("✓ Connected to source");
println!();
// Discover databases
tracing::info!("Discovering databases on source...");
let all_databases = migration::list_databases(&source_client)
.await
.context("Failed to list databases on source")?;
if all_databases.is_empty() {
tracing::warn!("⚠ No user databases found on source");
tracing::warn!(" Source appears to contain only template databases");
return Ok((ReplicationFilter::empty(), TableRules::default()));
}
tracing::info!("✓ Found {} database(s)", all_databases.len());
println!();
let db_names: Vec<String> = all_databases.iter().map(|db| db.name.clone()).collect();
// State for wizard
let mut selected_db_indices: Vec<usize> = Vec::new();
let mut current_step = WizardStep::SelectDatabases;
// Track selections per database for back navigation
let mut included_tables_by_db: std::collections::HashMap<String, Vec<String>> =
std::collections::HashMap::new();
let mut schema_only_by_db: std::collections::HashMap<String, Vec<(String, String)>> =
std::collections::HashMap::new(); // (schema, table)
let mut time_filters_by_db: std::collections::HashMap<
String,
Vec<(String, String, String, String)>,
> = std::collections::HashMap::new(); // (schema, table, column, window)
// Cache table info per database to avoid repeated queries
let mut table_cache: std::collections::HashMap<String, CachedDbTables> =
std::collections::HashMap::new();
loop {
match current_step {
WizardStep::SelectDatabases => {
print_header("Step 1 of 5: Select Databases");
println!("Navigation: Space to toggle, Enter to confirm, Esc to cancel");
println!();
let defaults: Vec<usize> = selected_db_indices.clone();
let selections =
MultiSelect::new("Select databases to replicate:", db_names.clone())
.with_default(&defaults)
.with_help_message("↑↓ navigate, Space toggle, Enter confirm")
.prompt();
match selections {
Ok(selected) => {
// Convert selected names back to indices
selected_db_indices = selected
.iter()
.filter_map(|name| db_names.iter().position(|n| n == name))
.collect();
if selected_db_indices.is_empty() {
println!();
println!("⚠ Please select at least one database");
continue;
}
// Clear previous selections when re-selecting databases
included_tables_by_db.clear();
schema_only_by_db.clear();
time_filters_by_db.clear();
table_cache.clear();
current_step = WizardStep::SelectTablesForDb(0);
}
Err(inquire::InquireError::OperationCanceled) => {
anyhow::bail!("Operation cancelled by user");
}
Err(inquire::InquireError::OperationInterrupted) => {
anyhow::bail!("Operation interrupted");
}
Err(e) => return Err(e.into()),
}
}
WizardStep::SelectTablesForDb(db_idx) => {
let db_name = &db_names[selected_db_indices[db_idx]].clone();
print_header(&format!(
"Step 2 of 5: Select Tables to Include ({}/{})",
db_idx + 1,
selected_db_indices.len()
));
println!("Database: {}", db_name);
println!("Press Enter without selecting to include ALL tables.");
println!("Navigation: Space to toggle, Enter to continue, Esc to go back");
println!();
// Get or cache tables for this database
let cached = get_or_cache_tables(&mut table_cache, source_url, db_name).await?;
if cached.all_tables.is_empty() {
println!(" No tables found in database '{}'", db_name);
// Skip to next database or next step
if db_idx + 1 < selected_db_indices.len() {
current_step = WizardStep::SelectTablesForDb(db_idx + 1);
} else {
current_step = WizardStep::SelectSchemaOnlyForDb(0);
}
continue;
}
// Get previously included tables for this database (for back navigation)
let previous_inclusions: Vec<usize> = included_tables_by_db
.get(db_name)
.map(|included| {
included
.iter()
.filter_map(|t| {
// Strip db name prefix to match display names
let stripped =
t.strip_prefix(&format!("{}.", db_name)).unwrap_or(t);
cached
.table_display_names
.iter()
.position(|n| n == stripped)
})
.collect()
})
.unwrap_or_default();
let selections = MultiSelect::new(
"Select tables to INCLUDE (Enter = include all):",
cached.table_display_names.clone(),
)
.with_default(&previous_inclusions)
.with_help_message("Space toggle, Enter confirm, Esc go back")
.prompt();
match selections {
Ok(selected_inclusions) => {
// If nothing selected, include all tables
let db_inclusions: Vec<String> = if selected_inclusions.is_empty() {
cached
.table_display_names
.iter()
.map(|table_name| format!("{}.{}", db_name, table_name))
.collect()
} else {
selected_inclusions
.iter()
.map(|table_name| format!("{}.{}", db_name, table_name))
.collect()
};
// Store for back navigation
included_tables_by_db.insert(db_name.clone(), db_inclusions);
// Move to next database or schema-only step
if db_idx + 1 < selected_db_indices.len() {
current_step = WizardStep::SelectTablesForDb(db_idx + 1);
} else {
current_step = WizardStep::SelectSchemaOnlyForDb(0);
}
}
Err(inquire::InquireError::OperationCanceled) => {
// Go back to previous step
if db_idx > 0 {
current_step = WizardStep::SelectTablesForDb(db_idx - 1);
} else {
current_step = WizardStep::SelectDatabases;
}
}
Err(inquire::InquireError::OperationInterrupted) => {
anyhow::bail!("Operation interrupted");
}
Err(e) => return Err(e.into()),
}
}
WizardStep::SelectSchemaOnlyForDb(db_idx) => {
let db_name = &db_names[selected_db_indices[db_idx]].clone();
print_header(&format!(
"Step 3 of 5: Schema-Only Tables ({}/{})",
db_idx + 1,
selected_db_indices.len()
));
println!("Database: {}", db_name);
println!("Schema-only tables replicate structure but NO data.");
println!("Navigation: Space to toggle, Enter to continue, Esc to go back");
println!();
let cached = get_or_cache_tables(&mut table_cache, source_url, db_name).await?;
if cached.all_tables.is_empty() {
// Skip to next database or time filters
if db_idx + 1 < selected_db_indices.len() {
current_step = WizardStep::SelectSchemaOnlyForDb(db_idx + 1);
} else {
current_step = WizardStep::ConfigureTimeFiltersForDb(0);
}
continue;
}
// Filter to only included tables
let included = included_tables_by_db.get(db_name);
let available_tables: Vec<(usize, String)> = cached
.table_display_names
.iter()
.enumerate()
.filter(|(_, name)| {
let full_name = format!("{}.{}", db_name, name);
included.is_some_and(|inc| inc.contains(&full_name))
})
.map(|(idx, name)| (idx, name.clone()))
.collect();
if available_tables.is_empty() {
println!(" No tables included from '{}'", db_name);
if db_idx + 1 < selected_db_indices.len() {
current_step = WizardStep::SelectSchemaOnlyForDb(db_idx + 1);
} else {
current_step = WizardStep::ConfigureTimeFiltersForDb(0);
}
continue;
}
let available_names: Vec<String> =
available_tables.iter().map(|(_, n)| n.clone()).collect();
// Get previous schema-only selections
let previous_schema_only: Vec<usize> = schema_only_by_db
.get(db_name)
.map(|selected| {
selected
.iter()
.filter_map(|(schema, table)| {
let display = if schema == "public" {
table.clone()
} else {
format!("{}.{}", schema, table)
};
available_names.iter().position(|n| n == &display)
})
.collect()
})
.unwrap_or_default();
let selections = MultiSelect::new(
"Select tables to replicate SCHEMA-ONLY (no data):",
available_names.clone(),
)
.with_default(&previous_schema_only)
.with_help_message("Space toggle, Enter confirm, Esc go back")
.prompt();
match selections {
Ok(selected_schema_only) => {
// Convert to (schema, table) pairs
let schema_only_tables: Vec<(String, String)> = selected_schema_only
.iter()
.filter_map(|display_name| {
available_tables
.iter()
.find(|(_, n)| n == display_name)
.map(|(idx, _)| {
let t = &cached.all_tables[*idx];
(t.schema.clone(), t.name.clone())
})
})
.collect();
schema_only_by_db.insert(db_name.clone(), schema_only_tables);
if db_idx + 1 < selected_db_indices.len() {
current_step = WizardStep::SelectSchemaOnlyForDb(db_idx + 1);
} else {
current_step = WizardStep::ConfigureTimeFiltersForDb(0);
}
}
Err(inquire::InquireError::OperationCanceled) => {
// Go back
if db_idx > 0 {
current_step = WizardStep::SelectSchemaOnlyForDb(db_idx - 1);
} else {
let last_db = selected_db_indices.len().saturating_sub(1);
current_step = WizardStep::SelectTablesForDb(last_db);
}
}
Err(inquire::InquireError::OperationInterrupted) => {
anyhow::bail!("Operation interrupted");
}
Err(e) => return Err(e.into()),
}
}
WizardStep::ConfigureTimeFiltersForDb(db_idx) => {
let db_name = &db_names[selected_db_indices[db_idx]].clone();
print_header(&format!(
"Step 4 of 5: Time Filters ({}/{})",
db_idx + 1,
selected_db_indices.len()
));
println!("Database: {}", db_name);
println!("Time filters limit data to recent records (e.g., last 90 days).");
println!();
let cached = get_or_cache_tables(&mut table_cache, source_url, db_name).await?;
if cached.all_tables.is_empty() {
if db_idx + 1 < selected_db_indices.len() {
current_step = WizardStep::ConfigureTimeFiltersForDb(db_idx + 1);
} else {
current_step = WizardStep::Review;
}
continue;
}
// Filter to included tables, excluding schema-only ones
let included = included_tables_by_db.get(db_name);
let schema_only = schema_only_by_db.get(db_name);
let available_tables: Vec<(usize, String)> = cached
.table_display_names
.iter()
.enumerate()
.filter(|(idx, name)| {
let full_name = format!("{}.{}", db_name, name);
let is_included = included.is_some_and(|inc| inc.contains(&full_name));
let t = &cached.all_tables[*idx];
let is_schema_only = schema_only.is_some_and(|so| {
so.iter().any(|(s, n)| s == &t.schema && n == &t.name)
});
is_included && !is_schema_only
})
.map(|(idx, name)| (idx, name.clone()))
.collect();
if available_tables.is_empty() {
println!(" No tables available for time filtering in '{}'", db_name);
if db_idx + 1 < selected_db_indices.len() {
current_step = WizardStep::ConfigureTimeFiltersForDb(db_idx + 1);
} else {
current_step = WizardStep::Review;
}
continue;
}
// Ask if user wants to configure time filters
let configure = Confirm::new("Configure time-based filters for this database?")
.with_default(false)
.with_help_message("Enter to confirm, Esc to go back")
.prompt();
match configure {
Ok(true) => {
// Let user select tables to filter
let available_names: Vec<String> =
available_tables.iter().map(|(_, n)| n.clone()).collect();
let table_selections = MultiSelect::new(
"Select tables to apply time filter:",
available_names.clone(),
)
.with_help_message("Space toggle, Enter confirm")
.prompt();
match table_selections {
Ok(selected_tables) => {
let mut time_filters: Vec<(String, String, String, String)> =
Vec::new();
for display_name in &selected_tables {
if let Some((idx, _)) =
available_tables.iter().find(|(_, n)| n == display_name)
{
let t = &cached.all_tables[*idx];
let db_url = replace_database_in_url(source_url, db_name)?;
let db_client = postgres::connect_with_retry(&db_url)
.await
.context("Failed to connect for column query")?;
// Get timestamp columns
let columns = migration::get_table_columns(
&db_client, &t.schema, &t.name,
)
.await?;
let timestamp_columns: Vec<String> = columns
.iter()
.filter(|c| c.is_timestamp)
.map(|c| format!("{} ({})", c.name, c.data_type))
.collect();
println!();
println!("Configure time filter for '{}':", display_name);
let column = if timestamp_columns.is_empty() {
println!(
" ⚠ No timestamp columns found. Enter column name manually."
);
Text::new(" Column name:")
.with_default("created_at")
.prompt()
.context("Failed to get column name")?
} else {
let mut options = timestamp_columns.clone();
options.push("[Enter custom column name]".to_string());
let selection =
Select::new(" Select timestamp column:", options)
.prompt()
.context("Failed to select column")?;
if selection == "[Enter custom column name]" {
Text::new(" Column name:")
.prompt()
.context("Failed to get column name")?
} else {
// Extract column name from "name (type)" format
selection
.split(" (")
.next()
.unwrap_or(&selection)
.to_string()
}
};
let window = Text::new(
" Time window (e.g., '90 days', '6 months', '1 year'):",
)
.with_default("90 days")
.prompt()
.context("Failed to get time window")?;
time_filters.push((
t.schema.clone(),
t.name.clone(),
column,
window,
));
}
}
time_filters_by_db.insert(db_name.clone(), time_filters);
}
Err(inquire::InquireError::OperationCanceled) => {
// Stay on this step
continue;
}
Err(inquire::InquireError::OperationInterrupted) => {
anyhow::bail!("Operation interrupted");
}
Err(e) => return Err(e.into()),
}
if db_idx + 1 < selected_db_indices.len() {
current_step = WizardStep::ConfigureTimeFiltersForDb(db_idx + 1);
} else {
current_step = WizardStep::Review;
}
}
Ok(false) => {
// Skip time filters for this database
if db_idx + 1 < selected_db_indices.len() {
current_step = WizardStep::ConfigureTimeFiltersForDb(db_idx + 1);
} else {
current_step = WizardStep::Review;
}
}
Err(inquire::InquireError::OperationCanceled) => {
// Go back
if db_idx > 0 {
current_step = WizardStep::ConfigureTimeFiltersForDb(db_idx - 1);
} else {
let last_db = selected_db_indices.len().saturating_sub(1);
current_step = WizardStep::SelectSchemaOnlyForDb(last_db);
}
}
Err(inquire::InquireError::OperationInterrupted) => {
anyhow::bail!("Operation interrupted");
}
Err(e) => return Err(e.into()),
}
}
WizardStep::Review => {
print_header("Step 5 of 5: Review Configuration");
// Collect all inclusions
let included_tables: Vec<String> =
included_tables_by_db.values().flatten().cloned().collect();
let selected_databases: Vec<String> = selected_db_indices
.iter()
.map(|&i| db_names[i].clone())
.collect();
println!();
println!("Databases to replicate: {}", selected_databases.len());
for db in &selected_databases {
println!(" ✓ {}", db);
}
println!();
println!("Tables to replicate: {}", included_tables.len());
if included_tables.len() <= 20 {
for table in &included_tables {
println!(" ✓ {}", table);
}
} else {
// Show first 10 and last 5 with ellipsis
for table in included_tables.iter().take(10) {
println!(" ✓ {}", table);
}
println!(" ... ({} more tables)", included_tables.len() - 15);
for table in included_tables.iter().skip(included_tables.len() - 5) {
println!(" ✓ {}", table);
}
}
println!();
// Show schema-only tables
let schema_only_count: usize = schema_only_by_db.values().map(|v| v.len()).sum();
if schema_only_count > 0 {
println!("Schema-only tables (no data): {}", schema_only_count);
for (db, tables) in &schema_only_by_db {
for (schema, table) in tables {
let display = if schema == "public" {
format!("{}.{}", db, table)
} else {
format!("{}.{}.{}", db, schema, table)
};
println!(" ◇ {}", display);
}
}
println!();
} else {
println!("Schema-only tables: none");
println!();
}
// Show time filters
let time_filter_count: usize = time_filters_by_db.values().map(|v| v.len()).sum();
if time_filter_count > 0 {
println!("Time-filtered tables: {}", time_filter_count);
for (db, filters) in &time_filters_by_db {
for (schema, table, column, window) in filters {
let display = if schema == "public" {
format!("{}.{}", db, table)
} else {
format!("{}.{}.{}", db, schema, table)
};
println!(" ⏱ {} ({} >= last {})", display, column, window);
}
}
println!();
} else {
println!("Time filters: none");
println!();
}
println!("───────────────────────────────────────────────────────────────");
println!();
let confirmed = Confirm::new("Proceed with this configuration?")
.with_default(true)
.with_help_message("Enter confirm, Esc go back")
.prompt();
match confirmed {
Ok(true) => break, // Exit loop, proceed with replication
Ok(false) | Err(inquire::InquireError::OperationCanceled) => {
// Go back to time filters
let last_db = selected_db_indices.len().saturating_sub(1);
current_step = WizardStep::ConfigureTimeFiltersForDb(last_db);
}
Err(inquire::InquireError::OperationInterrupted) => {
anyhow::bail!("Operation interrupted");
}
Err(e) => return Err(e.into()),
}
}
}
}
// Build final filter from selections
let selected_databases: Vec<String> = selected_db_indices
.iter()
.map(|&i| db_names[i].clone())
.collect();
let included_tables: Vec<String> = included_tables_by_db.values().flatten().cloned().collect();
tracing::info!("");
tracing::info!("✓ Configuration confirmed");
tracing::info!("");
// Use include_tables filter (3rd parameter)
let filter = if included_tables.is_empty() {
ReplicationFilter::new(Some(selected_databases), None, None, None)?
} else {
ReplicationFilter::new(Some(selected_databases), None, Some(included_tables), None)?
};
// Build TableRules from selections
let mut table_rules = TableRules::default();
// Add schema-only tables
for (db, tables) in &schema_only_by_db {
for (schema, table) in tables {
let qualified = QualifiedTable::new(Some(db.clone()), schema.clone(), table.clone());
table_rules.add_schema_only_table(qualified)?;
}
}
// Add time filters
for (db, filters) in &time_filters_by_db {
for (schema, table, column, window) in filters {
let qualified = QualifiedTable::new(Some(db.clone()), schema.clone(), table.clone());
table_rules.add_time_filter(qualified, column.clone(), window.clone())?;
}
}
Ok((filter, table_rules))
}
/// Get or cache table info for a database
async fn get_or_cache_tables<'a>(
cache: &'a mut std::collections::HashMap<String, CachedDbTables>,
source_url: &str,
db_name: &str,
) -> Result<&'a CachedDbTables> {
if !cache.contains_key(db_name) {
let db_url = replace_database_in_url(source_url, db_name)?;
let db_client = postgres::connect_with_retry(&db_url)
.await
.context(format!("Failed to connect to database '{}'", db_name))?;
let all_tables = migration::list_tables(&db_client)
.await
.context(format!("Failed to list tables from database '{}'", db_name))?;
let table_display_names: Vec<String> = all_tables
.iter()
.map(|t| {
if t.schema == "public" {
t.name.clone()
} else {
format!("{}.{}", t.schema, t.name)
}
})
.collect();
cache.insert(
db_name.to_string(),
CachedDbTables {
all_tables,
table_display_names,
},
);
}
Ok(cache.get(db_name).unwrap())
}
/// Print a formatted header for wizard steps
fn print_header(title: &str) {
println!();
println!("╔{}╗", "═".repeat(62));
println!("║ {:<60}║", title);
println!("╚{}╝", "═".repeat(62));
println!();
}
/// Replace the database name in a PostgreSQL connection URL
///
/// # Arguments
///
/// * `url` - PostgreSQL connection URL
/// * `new_db_name` - New database name to use
///
/// # Returns
///
/// URL with the database name replaced
fn replace_database_in_url(url: &str, new_db_name: &str) -> Result<String> {
// Split into base URL and query parameters
let parts: Vec<&str> = url.splitn(2, '?').collect();
let base_url = parts[0];
let query_params = parts.get(1);
// Split base URL by '/' to replace the database name
let url_parts: Vec<&str> = base_url.rsplitn(2, '/').collect();
if url_parts.len() != 2 {
anyhow::bail!("Invalid connection URL format: cannot replace database name");
}
// Rebuild URL with new database name
let new_url = if let Some(params) = query_params {
format!("{}/{}?{}", url_parts[1], new_db_name, params)
} else {
format!("{}/{}", url_parts[1], new_db_name)
};
Ok(new_url)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_replace_database_in_url() {
// Basic URL
let url = "postgresql://user:pass@localhost:5432/olddb";
let new_url = replace_database_in_url(url, "newdb").unwrap();
assert_eq!(new_url, "postgresql://user:pass@localhost:5432/newdb");
// URL with query parameters
let url = "postgresql://user:pass@localhost:5432/olddb?sslmode=require";
let new_url = replace_database_in_url(url, "newdb").unwrap();
assert_eq!(
new_url,
"postgresql://user:pass@localhost:5432/newdb?sslmode=require"
);
// URL without port
let url = "postgresql://user:pass@localhost/olddb";
let new_url = replace_database_in_url(url, "newdb").unwrap();
assert_eq!(new_url, "postgresql://user:pass@localhost/newdb");
}
#[tokio::test]
#[ignore]
async fn test_interactive_selection() {
// This test requires a real source database and manual interaction
let source_url = std::env::var("TEST_SOURCE_URL").unwrap();
let result = select_databases_and_tables(&source_url).await;
// This will only work with manual interaction
match &result {
Ok((filter, rules)) => {
println!("✓ Interactive selection completed");
println!("Filter: {:?}", filter);
println!("Rules: {:?}", rules);
}
Err(e) => {
println!("Interactive selection error: {:?}", e);
}
}
}
}