@@ -24,6 +24,7 @@ use futures_util::StreamExt;
2424use humantime:: Duration ;
2525use once_cell:: sync:: OnceCell ;
2626use tokio:: runtime:: { Builder , Runtime } ;
27+ use tracing:: info;
2728use tracing_log:: LogTracer ;
2829use tracing_subscriber:: { filter:: Directive , prelude:: * , EnvFilter } ;
2930
@@ -496,6 +497,9 @@ enum DatabaseSubcommand {
496497 /// Backfill GitHub/Gitlab stats for crates.
497498 BackfillRepositoryStats ,
498499
500+ /// Backfill crate owner kind from crates.io API
501+ BackfillCrateOwnerKind ,
502+
499503 /// Updates info for a crate from the registry's API
500504 UpdateCrateRegistryFields {
501505 #[ arg( name = "CRATE" ) ]
@@ -582,6 +586,37 @@ impl DatabaseSubcommand {
582586 . block_on ( ctx. repository_stats_updater ( ) ?. backfill_repositories ( ) ) ?;
583587 }
584588
589+ Self :: BackfillCrateOwnerKind => {
590+ let pool = ctx. pool ( ) ?;
591+ ctx. runtime ( ) ?
592+ . block_on ( async {
593+ let mut list_crates_conn = pool. get_async ( ) . await ?;
594+ let mut update_crates_conn = pool. get_async ( ) . await ?;
595+
596+ let mut result_stream =
597+ sqlx:: query!( "SELECT id, name FROM crates ORDER BY name" )
598+ . fetch ( & mut * list_crates_conn) ;
599+
600+ while let Some ( row) = result_stream. next ( ) . await {
601+ let row = row?;
602+ let registry_data =
603+ ctx. registry_api ( ) ?. get_crate_data ( & row. name ) . await ?;
604+
605+ info ! ( "Updating crate {}" , row. name) ;
606+
607+ db:: update_crate_data_in_db_by_id (
608+ & mut update_crates_conn,
609+ row. id ,
610+ & registry_data,
611+ )
612+ . await ?;
613+ }
614+
615+ Ok :: < ( ) , anyhow:: Error > ( ( ) )
616+ } )
617+ . context ( "Failed to backfill crate owner kind" ) ?
618+ }
619+
585620 Self :: UpdateCrateRegistryFields { name } => ctx. runtime ( ) ?. block_on ( async move {
586621 let mut conn = ctx. pool ( ) ?. get_async ( ) . await ?;
587622 let registry_data = ctx. registry_api ( ) ?. get_crate_data ( & name) . await ?;
0 commit comments