@@ -1002,3 +1002,42 @@ pub(crate) fn histogram_bounds(
10021002 . map ( |bounds| bounds. map ( |b| b. bounds ) . unwrap_or_default ( ) )
10031003 . map_err ( StoreError :: from)
10041004}
1005+
1006+ /// Return the name of the sequence that Postgres uses to handle
1007+ /// auto-incrementing columns. This takes Postgres' way of dealing with long
1008+ /// table and sequence names into account.
1009+ pub ( crate ) fn seq_name ( table_name : & str , column_name : & str ) -> String {
1010+ // Postgres limits all identifiers to 63 characters. When it
1011+ // constructs the name of a sequence for a column in a table, it
1012+ // truncates the table name so that appending '_{column}_seq' to
1013+ // it is at most 63 characters
1014+ let len = 63 - ( 5 + column_name. len ( ) ) ;
1015+ let len = len. min ( table_name. len ( ) ) ;
1016+ format ! ( "{}_{column_name}_seq" , & table_name[ 0 ..len] )
1017+ }
1018+
1019+ #[ cfg( test) ]
1020+ mod test {
1021+ use super :: seq_name;
1022+
1023+ #[ test]
1024+ fn seq_name_works ( ) {
1025+ // Pairs of (table_name, vid_seq_name)
1026+ const DATA : & [ ( & str , & str ) ] = & [
1027+ ( "token" , "token_vid_seq" ) ,
1028+ (
1029+ "frax_vst_curve_strategy_total_reward_token_collected_event" ,
1030+ "frax_vst_curve_strategy_total_reward_token_collected_ev_vid_seq" ,
1031+ ) ,
1032+ (
1033+ "rolling_asset_sent_for_last_24_hours_per_chain_and_token" ,
1034+ "rolling_asset_sent_for_last_24_hours_per_chain_and_toke_vid_seq" ,
1035+ ) ,
1036+ ] ;
1037+
1038+ for ( tbl, exp) in DATA {
1039+ let act = seq_name ( tbl, "vid" ) ;
1040+ assert_eq ! ( exp, & act) ;
1041+ }
1042+ }
1043+ }
0 commit comments