@@ -736,6 +736,13 @@ pub(crate) mod native_storage {
736736 pub const STORE_SESSIONS : & str = "sessions" ;
737737 pub const STORE_CUSTOM_EXERCISES : & str = "custom_exercises" ;
738738 pub const STORE_EXERCISES : & str = "exercises" ;
739+ /// Name of the application data sub-directory under the OS data dir.
740+ const APP_DATA_DIR_NAME : & str = "log-out" ;
741+ /// File name of the `SQLite` database within the application data directory.
742+ pub const DB_FILENAME : & str = "log-out.db" ;
743+ /// `SQLite` `user_version` value written on a successful schema migration.
744+ /// Any database with a lower version is wiped and recreated from scratch.
745+ const SCHEMA_VERSION : u32 = 2 ;
739746 /// Structured error type for native (`SQLite`) storage operations.
740747 #[ derive( Debug , thiserror:: Error ) ]
741748 pub enum StorageError {
@@ -781,10 +788,10 @@ pub(crate) mod native_storage {
781788 }
782789 dirs:: data_local_dir ( )
783790 . unwrap_or_else ( || PathBuf :: from ( "." ) )
784- . join ( "log-out" )
791+ . join ( APP_DATA_DIR_NAME )
785792 }
786793 fn db_path ( ) -> PathBuf {
787- data_dir ( ) . join ( "log-out.db" )
794+ data_dir ( ) . join ( DB_FILENAME )
788795 }
789796 /// Runs incremental schema migrations to bring the database up to the current version.
790797 ///
@@ -796,8 +803,9 @@ pub(crate) mod native_storage {
796803 /// reset without needing to re-create the long-lived connection.
797804 fn apply_migration_if_needed ( conn : & Connection ) -> Result < ( ) , StorageError > {
798805 let schema_version: u32 = conn. query_row ( "PRAGMA user_version" , [ ] , |r| r. get ( 0 ) ) ?;
799- if schema_version < 2 {
806+ if schema_version < SCHEMA_VERSION {
800807 // Fresh install or outdated schema: drop everything and start clean.
808+ // SCHEMA_VERSION must match the `PRAGMA user_version` value at the end.
801809 conn. execute_batch (
802810 "DROP TABLE IF EXISTS sessions;
803811 DROP TABLE IF EXISTS custom_exercises;
@@ -1500,7 +1508,7 @@ mod tests {
15001508 let _g = lock ( ) ;
15011509 native_storage:: get_all :: < WorkoutSession > ( native_storage:: STORE_SESSIONS ) . ok ( ) ;
15021510 {
1503- let db_path = native_storage:: data_dir ( ) . join ( "log-out.db" ) ;
1511+ let db_path = native_storage:: data_dir ( ) . join ( native_storage :: DB_FILENAME ) ;
15041512 if db_path. exists ( ) {
15051513 let conn = rusqlite:: Connection :: open ( & db_path) . unwrap ( ) ;
15061514 conn. execute_batch (
@@ -1544,7 +1552,7 @@ mod tests {
15441552 #[ test]
15451553 fn get_all_skips_corrupt_rows ( ) {
15461554 let _g = lock ( ) ;
1547- let db_path = native_storage:: data_dir ( ) . join ( "log-out.db" ) ;
1555+ let db_path = native_storage:: data_dir ( ) . join ( native_storage :: DB_FILENAME ) ;
15481556 native_storage:: get_all :: < WorkoutSession > ( native_storage:: STORE_SESSIONS ) . unwrap ( ) ;
15491557 {
15501558 let conn = rusqlite:: Connection :: open ( & db_path) . unwrap ( ) ;
0 commit comments