@@ -39,26 +39,17 @@ pub(crate) const ARCHIVE: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/post
39
39
pub enum Status {
40
40
/// Archive not installed
41
41
NotInstalled ,
42
- /// Installing software from archive
43
- Installing ,
44
42
/// Installation complete; not initialized
45
43
Installed ,
46
- /// Initialization running
47
- Initializing ,
48
- /// Server starting
49
- Starting ,
50
44
/// Server started
51
45
Started ,
52
- /// Server stopping
53
- Stopping ,
54
46
/// Server initialized and stopped
55
47
Stopped ,
56
48
}
57
49
58
50
/// PostgreSQL server
59
51
#[ derive( Clone , Debug ) ]
60
52
pub struct PostgreSQL {
61
- status : Status ,
62
53
version : Version ,
63
54
settings : Settings ,
64
55
}
@@ -67,11 +58,7 @@ pub struct PostgreSQL {
67
58
impl PostgreSQL {
68
59
/// Create a new [`PostgreSQL`] instance
69
60
pub fn new ( version : Version , settings : Settings ) -> Self {
70
- let mut postgresql = PostgreSQL {
71
- status : Status :: NotInstalled ,
72
- version,
73
- settings,
74
- } ;
61
+ let mut postgresql = PostgreSQL { version, settings } ;
75
62
76
63
// If the minor and release version are set, append the version to the installation directory
77
64
// to avoid conflicts with other versions. This will also facilitate setting the status
@@ -85,8 +72,6 @@ impl PostgreSQL {
85
72
postgresql. settings . installation_dir =
86
73
postgresql. settings . installation_dir . join ( version_string) ;
87
74
}
88
-
89
- postgresql. update_status ( ) ;
90
75
}
91
76
92
77
postgresql
@@ -101,23 +86,17 @@ impl PostgreSQL {
101
86
}
102
87
}
103
88
104
- /// Determine the status of the PostgreSQL server based on the settings
105
- fn update_status ( & mut self ) {
106
- let is_installed = self . is_installed ( ) ;
107
- let is_initialized = self . is_initialized ( ) ;
108
-
109
- if is_installed && is_initialized {
110
- self . status = Status :: Stopped
111
- } else if is_installed {
112
- self . status = Status :: Installed
113
- } else {
114
- self . status = Status :: NotInstalled
115
- }
116
- }
117
-
118
89
/// Get the [status](Status) of the PostgreSQL server
119
90
pub fn status ( & self ) -> Status {
120
- self . status
91
+ if self . is_running ( ) {
92
+ Status :: Started
93
+ } else if self . is_initialized ( ) {
94
+ Status :: Stopped
95
+ } else if self . is_installed ( ) {
96
+ Status :: Installed
97
+ } else {
98
+ Status :: NotInstalled
99
+ }
121
100
}
122
101
123
102
/// Get the [version](Version) of the PostgreSQL server
@@ -145,6 +124,12 @@ impl PostgreSQL {
145
124
self . settings . data_dir . join ( "postgresql.conf" ) . exists ( )
146
125
}
147
126
127
+ /// Check if the PostgreSQL server is running
128
+ fn is_running ( & self ) -> bool {
129
+ let pid_file = self . settings . data_dir . join ( "postmaster.pid" ) ;
130
+ pid_file. exists ( )
131
+ }
132
+
148
133
/// Set up the database by extracting the archive and initializing the database.
149
134
/// If the installation directory already exists, the archive will not be extracted.
150
135
/// If the data directory already exists, the database will not be initialized.
@@ -182,7 +167,6 @@ impl PostgreSQL {
182
167
183
168
if self . settings . installation_dir . exists ( ) {
184
169
debug ! ( "Installation directory already exists" ) ;
185
- self . update_status ( ) ;
186
170
return Ok ( ( ) ) ;
187
171
}
188
172
@@ -201,10 +185,8 @@ impl PostgreSQL {
201
185
let ( version, bytes) = { get_archive ( & self . version ) . await ? } ;
202
186
203
187
self . version = version;
204
- self . status = Status :: Installing ;
205
188
create_dir_all ( & self . settings . installation_dir ) ?;
206
189
extract ( & bytes, & self . settings . installation_dir ) . await ?;
207
- self . status = Status :: Installed ;
208
190
209
191
debug ! (
210
192
"Installed PostgreSQL version {} to {}" ,
@@ -236,20 +218,15 @@ impl PostgreSQL {
236
218
. username ( & self . settings . username )
237
219
. encoding ( "UTF8" ) ;
238
220
239
- self . status = Status :: Initializing ;
240
221
match self . execute_command ( initdb) . await {
241
222
Ok ( ( _stdout, _stderr) ) => {
242
- self . status = Status :: Stopped ;
243
223
debug ! (
244
224
"Initialized database {}" ,
245
225
self . settings. data_dir. to_string_lossy( )
246
226
) ;
247
227
Ok ( ( ) )
248
228
}
249
- Err ( error) => {
250
- self . update_status ( ) ;
251
- Err ( DatabaseInitializationError ( error. into ( ) ) )
252
- }
229
+ Err ( error) => Err ( DatabaseInitializationError ( error. into ( ) ) ) ,
253
230
}
254
231
}
255
232
@@ -276,26 +253,21 @@ impl PostgreSQL {
276
253
. options ( options)
277
254
. wait ( ) ;
278
255
279
- self . status = Status :: Starting ;
280
256
match self . execute_command ( pg_ctl) . await {
281
257
Ok ( ( _stdout, _stderr) ) => {
282
- self . status = Status :: Started ;
283
258
debug ! (
284
259
"Started database {} on port {}" ,
285
260
self . settings. data_dir. to_string_lossy( ) ,
286
261
self . settings. port
287
262
) ;
288
263
Ok ( ( ) )
289
264
}
290
- Err ( error) => {
291
- self . update_status ( ) ;
292
- Err ( DatabaseStartError ( error. into ( ) ) )
293
- }
265
+ Err ( error) => Err ( DatabaseStartError ( error. into ( ) ) ) ,
294
266
}
295
267
}
296
268
297
269
/// Stop the database gracefully (smart mode) and wait for the shutdown to complete.
298
- pub async fn stop ( & mut self ) -> Result < ( ) > {
270
+ pub async fn stop ( & self ) -> Result < ( ) > {
299
271
debug ! (
300
272
"Stopping database {}" ,
301
273
self . settings. data_dir. to_string_lossy( )
@@ -307,25 +279,20 @@ impl PostgreSQL {
307
279
. shutdown_mode ( Fast )
308
280
. wait ( ) ;
309
281
310
- self . status = Status :: Stopping ;
311
282
match self . execute_command ( pg_ctl) . await {
312
283
Ok ( ( _stdout, _stderr) ) => {
313
- self . status = Status :: Stopped ;
314
284
debug ! (
315
285
"Stopped database {}" ,
316
286
self . settings. data_dir. to_string_lossy( )
317
287
) ;
318
288
Ok ( ( ) )
319
289
}
320
- Err ( error) => {
321
- self . update_status ( ) ;
322
- Err ( DatabaseStopError ( error. into ( ) ) )
323
- }
290
+ Err ( error) => Err ( DatabaseStopError ( error. into ( ) ) ) ,
324
291
}
325
292
}
326
293
327
294
/// Create a new database with the given name.
328
- pub async fn create_database < S : AsRef < str > > ( & mut self , database_name : S ) -> Result < ( ) > {
295
+ pub async fn create_database < S : AsRef < str > > ( & self , database_name : S ) -> Result < ( ) > {
329
296
debug ! (
330
297
"Creating database {} for {}:{}" ,
331
298
database_name. as_ref( ) ,
@@ -358,7 +325,7 @@ impl PostgreSQL {
358
325
}
359
326
360
327
/// Check if a database with the given name exists.
361
- pub async fn database_exists < S : AsRef < str > > ( & mut self , database_name : S ) -> Result < bool > {
328
+ pub async fn database_exists < S : AsRef < str > > ( & self , database_name : S ) -> Result < bool > {
362
329
debug ! (
363
330
"Checking if database {} exists for {}:{}" ,
364
331
database_name. as_ref( ) ,
@@ -389,7 +356,7 @@ impl PostgreSQL {
389
356
}
390
357
391
358
/// Drop a database with the given name.
392
- pub async fn drop_database < S : AsRef < str > > ( & mut self , database_name : S ) -> Result < ( ) > {
359
+ pub async fn drop_database < S : AsRef < str > > ( & self , database_name : S ) -> Result < ( ) > {
393
360
debug ! (
394
361
"Dropping database {} for {}:{}" ,
395
362
database_name. as_ref( ) ,
@@ -457,7 +424,7 @@ impl Default for PostgreSQL {
457
424
/// Stop the PostgreSQL server and remove the data directory if it is marked as temporary.
458
425
impl Drop for PostgreSQL {
459
426
fn drop ( & mut self ) {
460
- if self . status == Status :: Starting || self . status == Status :: Started {
427
+ if self . status ( ) == Status :: Started {
461
428
let mut pg_ctl = PgCtlBuilder :: new ( )
462
429
. program_dir ( self . settings . binary_dir ( ) )
463
430
. mode ( Stop )
0 commit comments