4
4
//! transactions, and will likely fail if used on a populated database.
5
5
6
6
use chrono:: { DateTime , Utc } ;
7
+ use clap:: { builder:: PossibleValuesParser , ArgAction } ;
7
8
use database:: pool:: { postgres, sqlite, ConnectionManager } ;
8
9
use futures:: StreamExt ;
9
10
use rusqlite:: params;
@@ -462,37 +463,42 @@ async fn main() -> anyhow::Result<()> {
462
463
463
464
let table_names: Vec < _ > = tables. iter ( ) . map ( |table| table. name ( ) ) . collect ( ) ;
464
465
465
- let matches = clap:: App :: new ( "postgres-to-sqlite" )
466
+ let matches = clap:: Command :: new ( "postgres-to-sqlite" )
466
467
. about ( "Exports a rustc-perf Postgres database to a SQLite database" )
467
468
. version ( clap:: crate_version!( ) )
468
469
. arg (
469
470
clap:: Arg :: new ( "exclude-tables" )
471
+ . action ( ArgAction :: Set )
470
472
. long ( "exclude-tables" )
471
- . takes_value ( true )
472
473
. value_name ( "TABLES" )
473
- . possible_values ( & table_names)
474
- . use_delimiter ( true )
474
+ . value_parser ( PossibleValuesParser :: new ( table_names) )
475
+ . takes_value ( true )
476
+ . use_value_delimiter ( true )
475
477
. help ( "Exclude given tables (as foreign key constraints allow)" ) ,
476
478
)
477
479
. arg (
478
480
clap:: Arg :: new ( "no-self-profile" )
481
+ . action ( ArgAction :: SetTrue )
479
482
. long ( "no-self-profile" )
480
483
. help ( "Exclude some potentially large self-profile tables (additive with --exclude-tables)" ) ,
481
484
)
482
485
. arg (
483
486
clap:: Arg :: new ( "since-weeks-ago" )
487
+ . action ( ArgAction :: Set )
484
488
. long ( "since-weeks-ago" )
485
489
. takes_value ( true )
486
490
. value_name ( "WEEKS" )
487
491
. help ( "Exclude data associated with artifacts whose date value precedes <WEEKS> weeks ago" ) ,
488
492
)
489
493
. arg (
490
494
clap:: Arg :: new ( "fast-unsafe" )
495
+ . action ( ArgAction :: SetTrue )
491
496
. long ( "fast-unsafe" )
492
497
. help ( "Enable faster execution at the risk of corrupting SQLite database in the event of a crash" ) ,
493
498
)
494
499
. arg (
495
500
clap:: Arg :: new ( "postgres-db" )
501
+ . action ( ArgAction :: Set )
496
502
. required ( true )
497
503
. value_name ( "POSTGRES_DB" )
498
504
. help (
@@ -502,31 +508,29 @@ async fn main() -> anyhow::Result<()> {
502
508
)
503
509
. arg (
504
510
clap:: Arg :: new ( "sqlite-db" )
511
+ . action ( ArgAction :: Set )
505
512
. required ( true )
506
513
. value_name ( "SQLITE_DB" )
507
514
. help ( "SQLite database file" ) ,
508
515
)
509
516
. get_matches ( ) ;
510
517
511
- let postgres = matches. value_of ( "postgres-db" ) . unwrap ( ) ;
512
- let sqlite = matches. value_of ( "sqlite-db" ) . unwrap ( ) ;
518
+ let postgres = matches. get_one :: < String > ( "postgres-db" ) . unwrap ( ) ;
519
+ let sqlite = matches. get_one :: < String > ( "sqlite-db" ) . unwrap ( ) ;
513
520
514
521
let mut exclude_tables: std:: collections:: HashSet < _ > = matches
515
- . values_of ( "exclude-tables" )
522
+ . get_many :: < String > ( "exclude-tables" )
516
523
. unwrap_or_default ( )
524
+ . cloned ( )
517
525
. collect ( ) ;
518
526
519
- if matches. is_present ( "no-self-profile" ) {
520
- exclude_tables. insert ( SelfProfileQuerySeries . name ( ) ) ;
521
- exclude_tables. insert ( SelfProfileQuery . name ( ) ) ;
527
+ if matches. get_flag ( "no-self-profile" ) {
528
+ exclude_tables. insert ( SelfProfileQuerySeries . name ( ) . to_owned ( ) ) ;
529
+ exclude_tables. insert ( SelfProfileQuery . name ( ) . to_owned ( ) ) ;
522
530
// `RawSelfProfile` is intentionally kept.
523
531
}
524
532
525
- let since_weeks_ago = match matches. value_of_t ( "since-weeks-ago" ) {
526
- Ok ( weeks) => Some ( weeks) ,
527
- Err ( err) if err. kind == clap:: ErrorKind :: ArgumentNotFound => None ,
528
- Err ( err) => err. exit ( ) ,
529
- } ;
533
+ let since_weeks_ago = matches. get_one :: < u32 > ( "since-weeks-ago" ) . copied ( ) ;
530
534
531
535
let mut postgres: tokio_postgres:: Client =
532
536
postgres:: Postgres :: new ( postgres. into ( ) ) . open ( ) . await . into ( ) ;
@@ -537,7 +541,7 @@ async fn main() -> anyhow::Result<()> {
537
541
. into_inner ( )
538
542
. unwrap ( ) ;
539
543
540
- if matches. is_present ( "fast-unsafe" ) {
544
+ if matches. get_flag ( "fast-unsafe" ) {
541
545
sqlite. pragma_update ( None , "journal_mode" , & "OFF" ) . unwrap ( ) ;
542
546
sqlite. pragma_update ( None , "synchronous" , & "OFF" ) . unwrap ( ) ;
543
547
}
@@ -558,7 +562,7 @@ async fn main() -> anyhow::Result<()> {
558
562
let sqlite_tx = sqlite. transaction ( ) . unwrap ( ) ;
559
563
560
564
for & table in tables {
561
- if !exclude_tables. contains ( & table. name ( ) ) {
565
+ if !exclude_tables. contains ( table. name ( ) ) {
562
566
copy ( table, & postgres_tx, & sqlite_tx, since_weeks_ago) . await ;
563
567
}
564
568
}
0 commit comments