@@ -4,7 +4,7 @@ use std::hash::{Hash, Hasher};
44
55use schema_connector:: DatabaseSchema ;
66
7- /// A cache for migrations to avoid redundant work during `prisma migrate dev`.
7+ /// A cache for DatabaseSchemas based on the migration directories to avoid redundant work during `prisma migrate dev`.
88pub struct MigrationSchemaCache {
99 migrations : HashMap < String , DatabaseSchema > ,
1010}
@@ -17,7 +17,7 @@ impl MigrationSchemaCache {
1717 }
1818 }
1919
20- /// Gets a migration from the cache, or computes it using the provided async closure if not found.
20+ /// Gets a DatabaseSchema from the cache, or calls the provided async closure if not found and stores its result in the cache .
2121 pub async fn get_or_insert < F , Fut , E , T > (
2222 & mut self ,
2323 migration_directories : & Vec < T > ,
@@ -28,25 +28,16 @@ impl MigrationSchemaCache {
2828 Fut : std:: future:: Future < Output = Result < DatabaseSchema , E > > ,
2929 T : Hash ,
3030 {
31- let key = self . cache_key ( migration_directories) ;
31+ let mut hasher = DefaultHasher :: new ( ) ;
32+ migration_directories. hash ( & mut hasher) ;
33+ let cache_key = hasher. finish ( ) . to_string ( ) ;
3234
33- if !self . migrations . contains_key ( & key) {
34- println ! ( "Cache miss for key: {}" , key) ;
35+ if !self . migrations . contains_key ( & cache_key) {
3536 let schema = f ( ) . await ?;
36- self . migrations . insert ( key. clone ( ) , schema) ;
37- } else {
38- println ! ( "Cache hit for key: {}" , key) ;
37+ self . migrations . insert ( cache_key. clone ( ) , schema) ;
3938 }
4039
41- Ok ( self . migrations . get ( & key) . unwrap ( ) . clone ( ) )
42- }
43-
44- fn cache_key < T : Hash > ( & self , migration_directories : & Vec < T > ) -> String {
45- let mut hasher = DefaultHasher :: new ( ) ;
46-
47- migration_directories. hash ( & mut hasher) ;
48-
49- hasher. finish ( ) . to_string ( )
40+ Ok ( self . migrations . get ( & cache_key) . unwrap ( ) . clone ( ) )
5041 }
5142}
5243
0 commit comments