@@ -35,17 +35,17 @@ impl Display for ApplicationNodeType {
35
35
}
36
36
}
37
37
38
- /// Entity related to the `db_version ` database table.
38
+ /// Entity related to the `app_version ` database table.
39
39
#[ derive( Debug , PartialEq , Eq ) ]
40
- pub struct DatabaseVersion {
40
+ pub struct ApplicationVersion {
41
41
/// Semver of the database structure.
42
42
pub database_version : Version ,
43
43
44
44
/// Name of the application.
45
45
pub application_type : ApplicationNodeType ,
46
46
}
47
47
48
- impl SqLiteEntity for DatabaseVersion {
48
+ impl SqLiteEntity for ApplicationVersion {
49
49
fn hydrate ( row : Row ) -> Result < Self , HydrationError > {
50
50
Ok ( Self {
51
51
database_version : Version :: parse ( & row. get :: < String , _ > ( 0 ) )
@@ -56,12 +56,12 @@ impl SqLiteEntity for DatabaseVersion {
56
56
}
57
57
}
58
58
59
- /// Projection dedicated to [DatabaseVersion ] entities.
60
- struct DbVersionProjection {
59
+ /// Projection dedicated to [ApplicationVersion ] entities.
60
+ struct ApplicationVersionProjection {
61
61
fields : Vec < ProjectionField > ,
62
62
}
63
63
64
- impl Projection for DbVersionProjection {
64
+ impl Projection for ApplicationVersionProjection {
65
65
fn set_field ( & mut self , field : ProjectionField ) {
66
66
self . fields . push ( field) ;
67
67
}
@@ -70,36 +70,40 @@ impl Projection for DbVersionProjection {
70
70
& self . fields
71
71
}
72
72
}
73
- impl DbVersionProjection {
73
+ impl ApplicationVersionProjection {
74
74
pub fn new ( ) -> Self {
75
75
let mut projection = Self { fields : Vec :: new ( ) } ;
76
- projection. add_field ( "db_version" , "{:version:}.version" , "text" ) ;
77
- projection. add_field ( "application_type" , "{:version:}.application_type" , "text" ) ;
76
+ projection. add_field ( "version" , "{:app_version:}.version" , "text" ) ;
77
+ projection. add_field (
78
+ "application_type" ,
79
+ "{:app_version:}.application_type" ,
80
+ "text" ,
81
+ ) ;
78
82
79
83
projection
80
84
}
81
85
}
82
86
83
- /// Provider for the [DatabaseVersion ] entities using the [DbVersionProjection] .
87
+ /// Provider for the [ApplicationVersion ] entities using the `ApplicationVersionProjection` .
84
88
pub struct VersionProvider < ' conn > {
85
89
connection : & ' conn Connection ,
86
- projection : DbVersionProjection ,
90
+ projection : ApplicationVersionProjection ,
87
91
}
88
92
89
93
impl < ' conn > VersionProvider < ' conn > {
90
94
/// [VersionProvider] constructor.
91
95
pub fn new ( connection : & ' conn Connection ) -> Self {
92
96
Self {
93
97
connection,
94
- projection : DbVersionProjection :: new ( ) ,
98
+ projection : ApplicationVersionProjection :: new ( ) ,
95
99
}
96
100
}
97
101
98
102
/// Method to create the table at the beginning of the migration procedure.
99
103
/// This code is temporary and should not last.
100
104
pub fn create_table_if_not_exists ( & self ) -> Result < ( ) , Box < dyn Error > > {
101
105
let connection = self . get_connection ( ) ;
102
- let sql = "select exists(select name from sqlite_master where type='table' and name='db_version ') as table_exists" ;
106
+ let sql = "select exists(select name from sqlite_master where type='table' and name='app_version ') as table_exists" ;
103
107
let table_exists = connection
104
108
. prepare ( sql) ?
105
109
. into_cursor ( )
@@ -111,7 +115,7 @@ impl<'conn> VersionProvider<'conn> {
111
115
112
116
if !table_exists {
113
117
let sql = r#"
114
- create table db_version (application_type text not null primary key, version text not null)
118
+ create table app_version (application_type text not null primary key, version text not null)
115
119
"# ;
116
120
connection. execute ( sql) ?;
117
121
}
@@ -120,15 +124,20 @@ create table db_version (application_type text not null primary key, version tex
120
124
}
121
125
122
126
/// Read the database version from the database.
123
- pub fn get_database_version ( & self ) -> Result < Option < DatabaseVersion > , Box < dyn Error > > {
124
- let result = self . find ( None , & [ ] ) ?. next ( ) ;
127
+ pub fn get_database_version (
128
+ & self ,
129
+ application_type : & ApplicationNodeType ,
130
+ ) -> Result < Option < ApplicationVersion > , Box < dyn Error > > {
131
+ let condition = "application_type = ?" ;
132
+ let params = [ Value :: String ( format ! ( "{}" , application_type) ) ] ;
133
+ let result = self . find ( Some ( condition) , & params) ?. next ( ) ;
125
134
126
135
Ok ( result)
127
136
}
128
137
}
129
138
130
139
impl < ' conn > Provider < ' conn > for VersionProvider < ' conn > {
131
- type Entity = DatabaseVersion ;
140
+ type Entity = ApplicationVersion ;
132
141
133
142
fn get_projection ( & self ) -> & dyn Projection {
134
143
& self . projection
@@ -141,37 +150,37 @@ impl<'conn> Provider<'conn> for VersionProvider<'conn> {
141
150
fn get_definition ( & self , condition : Option < & str > ) -> String {
142
151
let where_clause = condition. unwrap_or ( "true" ) ;
143
152
let mut aliases = HashMap :: new ( ) ;
144
- let _ = aliases. insert ( "{:version :}" . to_string ( ) , "db_version " . to_string ( ) ) ;
153
+ let _ = aliases. insert ( "{:app_version :}" . to_string ( ) , "app_version " . to_string ( ) ) ;
145
154
let projection = self . get_projection ( ) . expand ( aliases) ;
146
155
147
156
format ! (
148
157
r#"
149
158
select {projection}
150
- from db_version
159
+ from app_version
151
160
where {where_clause}
152
161
"#
153
162
)
154
163
}
155
164
}
156
165
157
- /// Write [Provider] for the [DatabaseVersion ] entities.
166
+ /// Write [Provider] for the [ApplicationVersion ] entities.
158
167
/// This will perform an UPSERT and return the updated entity.
159
- pub struct VersionUpdatedProvider < ' conn > {
168
+ pub struct VersionUpdaterProvider < ' conn > {
160
169
connection : & ' conn Connection ,
161
- projection : DbVersionProjection ,
170
+ projection : ApplicationVersionProjection ,
162
171
}
163
172
164
- impl < ' conn > VersionUpdatedProvider < ' conn > {
165
- /// [VersionUpdateprovider ] constructor.
173
+ impl < ' conn > VersionUpdaterProvider < ' conn > {
174
+ /// [VersionUpdaterProvider ] constructor.
166
175
pub fn new ( connection : & ' conn Connection ) -> Self {
167
176
Self {
168
177
connection,
169
- projection : DbVersionProjection :: new ( ) ,
178
+ projection : ApplicationVersionProjection :: new ( ) ,
170
179
}
171
180
}
172
181
173
182
/// Persist the given entity and return the projection of the saved entity.
174
- pub fn save ( & self , version : DatabaseVersion ) -> Result < DatabaseVersion , Box < dyn Error > > {
183
+ pub fn save ( & self , version : ApplicationVersion ) -> Result < ApplicationVersion , Box < dyn Error > > {
175
184
let params = [
176
185
Value :: String ( format ! ( "{}" , version. application_type) ) ,
177
186
Value :: String ( version. database_version . to_string ( ) ) ,
@@ -185,8 +194,8 @@ impl<'conn> VersionUpdatedProvider<'conn> {
185
194
}
186
195
}
187
196
188
- impl < ' conn > Provider < ' conn > for VersionUpdatedProvider < ' conn > {
189
- type Entity = DatabaseVersion ;
197
+ impl < ' conn > Provider < ' conn > for VersionUpdaterProvider < ' conn > {
198
+ type Entity = ApplicationVersion ;
190
199
191
200
fn get_projection ( & self ) -> & dyn Projection {
192
201
& self . projection
@@ -199,12 +208,12 @@ impl<'conn> Provider<'conn> for VersionUpdatedProvider<'conn> {
199
208
fn get_definition ( & self , condition : Option < & str > ) -> String {
200
209
let _where_clause = condition. unwrap_or ( "true" ) ;
201
210
let mut aliases = HashMap :: new ( ) ;
202
- let _ = aliases. insert ( "{:version :}" . to_string ( ) , "db_version " . to_string ( ) ) ;
211
+ let _ = aliases. insert ( "{:app_version :}" . to_string ( ) , "app_version " . to_string ( ) ) ;
203
212
let projection = self . get_projection ( ) . expand ( aliases) ;
204
213
205
214
format ! (
206
215
r#"
207
- insert into db_version (application_type, version) values (?, ?)
216
+ insert into app_version (application_type, version) values (?, ?)
208
217
on conflict (application_type) do update set version = excluded.version
209
218
returning {projection}
210
219
"#
@@ -218,12 +227,12 @@ mod tests {
218
227
219
228
#[ test]
220
229
fn test_projection ( ) {
221
- let projection = DbVersionProjection :: new ( ) ;
230
+ let projection = ApplicationVersionProjection :: new ( ) ;
222
231
let mut aliases: HashMap < String , String > = HashMap :: new ( ) ;
223
- let _ = aliases. insert ( "{:version :}" . to_string ( ) , "whatever" . to_string ( ) ) ;
232
+ let _ = aliases. insert ( "{:app_version :}" . to_string ( ) , "whatever" . to_string ( ) ) ;
224
233
225
234
assert_eq ! (
226
- "whatever.version as db_version , whatever.application_type as application_type"
235
+ "whatever.version as version , whatever.application_type as application_type"
227
236
. to_string( ) ,
228
237
projection. expand( aliases)
229
238
) ;
@@ -236,8 +245,8 @@ mod tests {
236
245
237
246
assert_eq ! (
238
247
r#"
239
- select db_version .version as db_version, db_version .application_type as application_type
240
- from db_version
248
+ select app_version .version as version, app_version .application_type as application_type
249
+ from app_version
241
250
where true
242
251
"# ,
243
252
provider. get_definition( None )
@@ -247,13 +256,13 @@ where true
247
256
#[ test]
248
257
fn test_updated_entity ( ) {
249
258
let connection = Connection :: open ( ":memory:" ) . unwrap ( ) ;
250
- let provider = VersionUpdatedProvider :: new ( & connection) ;
259
+ let provider = VersionUpdaterProvider :: new ( & connection) ;
251
260
252
261
assert_eq ! (
253
262
r#"
254
- insert into db_version (application_type, version) values (?, ?)
263
+ insert into app_version (application_type, version) values (?, ?)
255
264
on conflict (application_type) do update set version = excluded.version
256
- returning db_version .version as db_version, db_version .application_type as application_type
265
+ returning app_version .version as version, app_version .application_type as application_type
257
266
"# ,
258
267
provider. get_definition( None )
259
268
)
0 commit comments