@@ -16,56 +16,58 @@ pub struct InitializedDb {
1616 pub actor_id : ActorId ,
1717}
1818
19- pub async fn setup ( db_path : & crate :: Path , schema : & str ) -> eyre:: Result < InitializedDb > {
20- let partial_schema = corro_types:: schema:: parse_sql ( schema) ?;
21-
22- let actor_id = {
23- // we need to set auto_vacuum before any tables are created
24- let db_conn = rusqlite:: Connection :: open ( db_path) ?;
25- db_conn. execute_batch ( "PRAGMA auto_vacuum = INCREMENTAL" ) ?;
26-
27- let conn = CrConn :: init ( db_conn) ?;
28- conn. query_row ( "SELECT crsql_site_id();" , [ ] , |row| {
29- row. get :: < _ , ActorId > ( 0 )
30- } ) ?
31- } ;
19+ impl InitializedDb {
20+ pub async fn setup ( db_path : & crate :: Path , schema : & str ) -> eyre:: Result < Self > {
21+ let partial_schema = corro_types:: schema:: parse_sql ( schema) ?;
22+
23+ let actor_id = {
24+ // we need to set auto_vacuum before any tables are created
25+ let db_conn = rusqlite:: Connection :: open ( db_path) ?;
26+ db_conn. execute_batch ( "PRAGMA auto_vacuum = INCREMENTAL" ) ?;
27+
28+ let conn = CrConn :: init ( db_conn) ?;
29+ conn. query_row ( "SELECT crsql_site_id();" , [ ] , |row| {
30+ row. get :: < _ , ActorId > ( 0 )
31+ } ) ?
32+ } ;
3233
33- let write_sema = Arc :: new ( tokio:: sync:: Semaphore :: new ( 1 ) ) ;
34- let pool = SplitPool :: create ( & db_path, write_sema. clone ( ) ) . await ?;
34+ let write_sema = Arc :: new ( tokio:: sync:: Semaphore :: new ( 1 ) ) ;
35+ let pool = SplitPool :: create ( & db_path, write_sema. clone ( ) ) . await ?;
3536
36- let clock = Arc :: new (
37- uhlc:: HLCBuilder :: default ( )
38- . with_id ( actor_id. try_into ( ) . unwrap ( ) )
39- . with_max_delta ( std:: time:: Duration :: from_millis ( 300 ) )
40- . build ( ) ,
41- ) ;
37+ let clock = Arc :: new (
38+ uhlc:: HLCBuilder :: default ( )
39+ . with_id ( actor_id. try_into ( ) . unwrap ( ) )
40+ . with_max_delta ( std:: time:: Duration :: from_millis ( 300 ) )
41+ . build ( ) ,
42+ ) ;
4243
43- let schema = {
44- let mut conn = pool. write_priority ( ) . await ?;
44+ let schema = {
45+ let mut conn = pool. write_priority ( ) . await ?;
4546
46- let old_schema = {
47- corro_types:: agent:: migrate ( clock. clone ( ) , & mut conn) ?;
48- let mut schema = corro_types:: schema:: init_schema ( & conn) ?;
49- schema. constrain ( ) ?;
47+ let old_schema = {
48+ corro_types:: agent:: migrate ( clock. clone ( ) , & mut conn) ?;
49+ let mut schema = corro_types:: schema:: init_schema ( & conn) ?;
50+ schema. constrain ( ) ?;
5051
51- schema
52- } ;
52+ schema
53+ } ;
5354
54- tokio:: task:: block_in_place ( || update_schema ( old_schema , partial_schema , & mut conn) ) ?
55- } ;
55+ tokio:: task:: block_in_place ( || update_schema ( & mut conn, old_schema , partial_schema ) ) ?
56+ } ;
5657
57- Ok ( InitializedDb {
58- pool,
59- clock,
60- schema : Arc :: new ( schema) ,
61- actor_id,
62- } )
58+ Ok ( Self {
59+ pool,
60+ clock,
61+ schema : Arc :: new ( schema) ,
62+ actor_id,
63+ } )
64+ }
6365}
6466
6567pub fn update_schema (
68+ conn : & mut WriteConn ,
6669 old_schema : Schema ,
6770 new_schema : Schema ,
68- conn : & mut WriteConn ,
6971) -> eyre:: Result < Schema > {
7072 // clone the previous schema and apply
7173 let mut new_schema = {
0 commit comments