@@ -6,11 +6,12 @@ use std::{
6
6
} ;
7
7
8
8
use chrono:: NaiveDateTime ;
9
- use semver:: Version ;
10
9
use sqlite:: { Connection , Row , Value } ;
11
10
12
11
use crate :: sqlite:: { HydrationError , Projection , ProjectionField , Provider , SqLiteEntity } ;
13
12
13
+ use super :: DbVersion ;
14
+
14
15
/// Application using a database
15
16
#[ derive( Debug , Clone , PartialEq , Eq ) ]
16
17
pub enum ApplicationNodeType {
@@ -41,11 +42,11 @@ impl Display for ApplicationNodeType {
41
42
}
42
43
}
43
44
44
- /// Entity related to the `app_version ` database table.
45
+ /// Entity related to the `db_version ` database table.
45
46
#[ derive( Debug , PartialEq , Eq , Clone ) ]
46
47
pub struct DatabaseVersion {
47
- /// Semver of the database structure.
48
- pub semver : Version ,
48
+ /// Version of the database structure.
49
+ pub version : DbVersion ,
49
50
50
51
/// Name of the application.
51
52
pub application_type : ApplicationNodeType ,
@@ -58,8 +59,7 @@ pub struct DatabaseVersion {
58
59
impl SqLiteEntity for DatabaseVersion {
59
60
fn hydrate ( row : Row ) -> Result < Self , HydrationError > {
60
61
Ok ( Self {
61
- semver : Version :: parse ( & row. get :: < String , _ > ( 0 ) )
62
- . map_err ( |e| HydrationError :: InvalidData ( format ! ( "{}" , e) ) ) ?,
62
+ version : row. get :: < i64 , _ > ( 0 ) ,
63
63
application_type : ApplicationNodeType :: new ( & row. get :: < String , _ > ( 1 ) )
64
64
. map_err ( |e| HydrationError :: InvalidData ( format ! ( "{}" , e) ) ) ?,
65
65
updated_at : NaiveDateTime :: parse_from_str (
@@ -76,7 +76,7 @@ impl PartialOrd for DatabaseVersion {
76
76
if self . application_type != other. application_type {
77
77
None
78
78
} else {
79
- self . semver . partial_cmp ( & other. semver )
79
+ self . version . partial_cmp ( & other. version )
80
80
}
81
81
}
82
82
}
@@ -98,13 +98,13 @@ impl Projection for DatabaseVersionProjection {
98
98
impl DatabaseVersionProjection {
99
99
pub fn new ( ) -> Self {
100
100
let mut projection = Self { fields : Vec :: new ( ) } ;
101
- projection. add_field ( "semver " , "{:app_version :}.semver " , "text" ) ;
101
+ projection. add_field ( "version " , "{:db_version :}.version " , "text" ) ;
102
102
projection. add_field (
103
103
"application_type" ,
104
- "{:app_version :}.application_type" ,
104
+ "{:db_version :}.application_type" ,
105
105
"text" ,
106
106
) ;
107
- projection. add_field ( "updated_at" , "{:app_version :}.updated_at" , "timestamp" ) ;
107
+ projection. add_field ( "updated_at" , "{:db_version :}.updated_at" , "timestamp" ) ;
108
108
109
109
projection
110
110
}
@@ -129,7 +129,7 @@ impl<'conn> DatabaseVersionProvider<'conn> {
129
129
/// This code is temporary and should not last.
130
130
pub fn create_table_if_not_exists ( & self ) -> Result < ( ) , Box < dyn Error > > {
131
131
let connection = self . get_connection ( ) ;
132
- let sql = "select exists(select name from sqlite_master where type='table' and name='app_version ') as table_exists" ;
132
+ let sql = "select exists(select name from sqlite_master where type='table' and name='db_version ') as table_exists" ;
133
133
let table_exists = connection
134
134
. prepare ( sql) ?
135
135
. into_cursor ( )
@@ -141,7 +141,7 @@ impl<'conn> DatabaseVersionProvider<'conn> {
141
141
142
142
if !table_exists {
143
143
let sql = r#"
144
- create table app_version (application_type text not null primary key, semver text not null, updated_at timestamp not null default CURRENT_TIMESTAMP)
144
+ create table db_version (application_type text not null primary key, version integer not null, updated_at timestamp not null default CURRENT_TIMESTAMP)
145
145
"# ;
146
146
connection. execute ( sql) ?;
147
147
}
@@ -176,13 +176,13 @@ impl<'conn> Provider<'conn> for DatabaseVersionProvider<'conn> {
176
176
fn get_definition ( & self , condition : Option < & str > ) -> String {
177
177
let where_clause = condition. unwrap_or ( "true" ) ;
178
178
let mut aliases = HashMap :: new ( ) ;
179
- let _ = aliases. insert ( "{:app_version :}" . to_string ( ) , "app_version " . to_string ( ) ) ;
179
+ let _ = aliases. insert ( "{:db_version :}" . to_string ( ) , "db_version " . to_string ( ) ) ;
180
180
let projection = self . get_projection ( ) . expand ( aliases) ;
181
181
182
182
format ! (
183
183
r#"
184
184
select {projection}
185
- from app_version
185
+ from db_version
186
186
where {where_clause}
187
187
"#
188
188
)
@@ -209,7 +209,7 @@ impl<'conn> DatabaseVersionUpdater<'conn> {
209
209
pub fn save ( & self , version : DatabaseVersion ) -> Result < DatabaseVersion , Box < dyn Error > > {
210
210
let params = [
211
211
Value :: String ( format ! ( "{}" , version. application_type) ) ,
212
- Value :: String ( version. semver . to_string ( ) ) ,
212
+ Value :: Integer ( version. version ) ,
213
213
] ;
214
214
let entity = self
215
215
. find ( None , & params) ?
@@ -234,13 +234,13 @@ impl<'conn> Provider<'conn> for DatabaseVersionUpdater<'conn> {
234
234
fn get_definition ( & self , condition : Option < & str > ) -> String {
235
235
let _where_clause = condition. unwrap_or ( "true" ) ;
236
236
let mut aliases = HashMap :: new ( ) ;
237
- let _ = aliases. insert ( "{:app_version :}" . to_string ( ) , "app_version " . to_string ( ) ) ;
237
+ let _ = aliases. insert ( "{:db_version :}" . to_string ( ) , "db_version " . to_string ( ) ) ;
238
238
let projection = self . get_projection ( ) . expand ( aliases) ;
239
239
240
240
format ! (
241
241
r#"
242
- insert into app_version (application_type, semver ) values (?, ?)
243
- on conflict (application_type) do update set semver = excluded.semver , updated_at = CURRENT_TIMESTAMP
242
+ insert into db_version (application_type, version ) values (?, ?)
243
+ on conflict (application_type) do update set version = excluded.version , updated_at = CURRENT_TIMESTAMP
244
244
returning {projection}
245
245
"#
246
246
)
@@ -255,10 +255,10 @@ mod tests {
255
255
fn test_projection ( ) {
256
256
let projection = DatabaseVersionProjection :: new ( ) ;
257
257
let mut aliases: HashMap < String , String > = HashMap :: new ( ) ;
258
- let _ = aliases. insert ( "{:app_version :}" . to_string ( ) , "whatever" . to_string ( ) ) ;
258
+ let _ = aliases. insert ( "{:db_version :}" . to_string ( ) , "whatever" . to_string ( ) ) ;
259
259
260
260
assert_eq ! (
261
- "whatever.semver as semver , whatever.application_type as application_type, whatever.updated_at as updated_at"
261
+ "whatever.version as version , whatever.application_type as application_type, whatever.updated_at as updated_at"
262
262
. to_string( ) ,
263
263
projection. expand( aliases)
264
264
) ;
@@ -271,8 +271,8 @@ mod tests {
271
271
272
272
assert_eq ! (
273
273
r#"
274
- select app_version.semver as semver, app_version .application_type as application_type, app_version .updated_at as updated_at
275
- from app_version
274
+ select db_version.version as version, db_version .application_type as application_type, db_version .updated_at as updated_at
275
+ from db_version
276
276
where true
277
277
"# ,
278
278
provider. get_definition( None )
@@ -286,9 +286,9 @@ where true
286
286
287
287
assert_eq ! (
288
288
r#"
289
- insert into app_version (application_type, semver ) values (?, ?)
290
- on conflict (application_type) do update set semver = excluded.semver , updated_at = CURRENT_TIMESTAMP
291
- returning app_version.semver as semver, app_version .application_type as application_type, app_version .updated_at as updated_at
289
+ insert into db_version (application_type, version ) values (?, ?)
290
+ on conflict (application_type) do update set version = excluded.version , updated_at = CURRENT_TIMESTAMP
291
+ returning db_version.version as version, db_version .application_type as application_type, db_version .updated_at as updated_at
292
292
"# ,
293
293
provider. get_definition( None )
294
294
)
0 commit comments