@@ -39,26 +39,17 @@ pub(crate) const ARCHIVE: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/post
3939pub enum Status {
4040 /// Archive not installed
4141 NotInstalled ,
42- /// Installing software from archive
43- Installing ,
4442 /// Installation complete; not initialized
4543 Installed ,
46- /// Initialization running
47- Initializing ,
48- /// Server starting
49- Starting ,
5044 /// Server started
5145 Started ,
52- /// Server stopping
53- Stopping ,
5446 /// Server initialized and stopped
5547 Stopped ,
5648}
5749
5850/// PostgreSQL server
5951#[ derive( Clone , Debug ) ]
6052pub struct PostgreSQL {
61- status : Status ,
6253 version : Version ,
6354 settings : Settings ,
6455}
@@ -68,7 +59,6 @@ impl PostgreSQL {
6859 /// Create a new [`PostgreSQL`] instance
6960 pub fn new ( version : Version , settings : Settings ) -> Self {
7061 let mut postgresql = PostgreSQL {
71- status : Status :: NotInstalled ,
7262 version,
7363 settings,
7464 } ;
@@ -85,8 +75,6 @@ impl PostgreSQL {
8575 postgresql. settings . installation_dir =
8676 postgresql. settings . installation_dir . join ( version_string) ;
8777 }
88-
89- postgresql. update_status ( ) ;
9078 }
9179
9280 postgresql
@@ -101,23 +89,17 @@ impl PostgreSQL {
10189 }
10290 }
10391
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-
11892 /// Get the [status](Status) of the PostgreSQL server
11993 pub fn status ( & self ) -> Status {
120- self . status
94+ if self . is_running ( ) {
95+ Status :: Started
96+ } else if self . is_initialized ( ) {
97+ Status :: Stopped
98+ } else if self . is_installed ( ) {
99+ Status :: Installed
100+ } else {
101+ Status :: NotInstalled
102+ }
121103 }
122104
123105 /// Get the [version](Version) of the PostgreSQL server
@@ -145,6 +127,12 @@ impl PostgreSQL {
145127 self . settings . data_dir . join ( "postgresql.conf" ) . exists ( )
146128 }
147129
130+ /// Check if the PostgreSQL server is running
131+ fn is_running ( & self ) -> bool {
132+ let pid_file = self . settings . data_dir . join ( "postmaster.pid" ) ;
133+ pid_file. exists ( )
134+ }
135+
148136 /// Set up the database by extracting the archive and initializing the database.
149137 /// If the installation directory already exists, the archive will not be extracted.
150138 /// If the data directory already exists, the database will not be initialized.
@@ -182,7 +170,6 @@ impl PostgreSQL {
182170
183171 if self . settings . installation_dir . exists ( ) {
184172 debug ! ( "Installation directory already exists" ) ;
185- self . update_status ( ) ;
186173 return Ok ( ( ) ) ;
187174 }
188175
@@ -201,10 +188,8 @@ impl PostgreSQL {
201188 let ( version, bytes) = { get_archive ( & self . version ) . await ? } ;
202189
203190 self . version = version;
204- self . status = Status :: Installing ;
205191 create_dir_all ( & self . settings . installation_dir ) ?;
206192 extract ( & bytes, & self . settings . installation_dir ) . await ?;
207- self . status = Status :: Installed ;
208193
209194 debug ! (
210195 "Installed PostgreSQL version {} to {}" ,
@@ -236,18 +221,15 @@ impl PostgreSQL {
236221 . username ( & self . settings . username )
237222 . encoding ( "UTF8" ) ;
238223
239- self . status = Status :: Initializing ;
240224 match self . execute_command ( initdb) . await {
241225 Ok ( ( _stdout, _stderr) ) => {
242- self . status = Status :: Stopped ;
243226 debug ! (
244227 "Initialized database {}" ,
245228 self . settings. data_dir. to_string_lossy( )
246229 ) ;
247230 Ok ( ( ) )
248231 }
249232 Err ( error) => {
250- self . update_status ( ) ;
251233 Err ( DatabaseInitializationError ( error. into ( ) ) )
252234 }
253235 }
@@ -276,10 +258,8 @@ impl PostgreSQL {
276258 . options ( options)
277259 . wait ( ) ;
278260
279- self . status = Status :: Starting ;
280261 match self . execute_command ( pg_ctl) . await {
281262 Ok ( ( _stdout, _stderr) ) => {
282- self . status = Status :: Started ;
283263 debug ! (
284264 "Started database {} on port {}" ,
285265 self . settings. data_dir. to_string_lossy( ) ,
@@ -288,14 +268,13 @@ impl PostgreSQL {
288268 Ok ( ( ) )
289269 }
290270 Err ( error) => {
291- self . update_status ( ) ;
292271 Err ( DatabaseStartError ( error. into ( ) ) )
293272 }
294273 }
295274 }
296275
297276 /// Stop the database gracefully (smart mode) and wait for the shutdown to complete.
298- pub async fn stop ( & mut self ) -> Result < ( ) > {
277+ pub async fn stop ( & self ) -> Result < ( ) > {
299278 debug ! (
300279 "Stopping database {}" ,
301280 self . settings. data_dir. to_string_lossy( )
@@ -307,25 +286,22 @@ impl PostgreSQL {
307286 . shutdown_mode ( Fast )
308287 . wait ( ) ;
309288
310- self . status = Status :: Stopping ;
311289 match self . execute_command ( pg_ctl) . await {
312290 Ok ( ( _stdout, _stderr) ) => {
313- self . status = Status :: Stopped ;
314291 debug ! (
315292 "Stopped database {}" ,
316293 self . settings. data_dir. to_string_lossy( )
317294 ) ;
318295 Ok ( ( ) )
319296 }
320297 Err ( error) => {
321- self . update_status ( ) ;
322298 Err ( DatabaseStopError ( error. into ( ) ) )
323299 }
324300 }
325301 }
326302
327303 /// Create a new database with the given name.
328- pub async fn create_database < S : AsRef < str > > ( & mut self , database_name : S ) -> Result < ( ) > {
304+ pub async fn create_database < S : AsRef < str > > ( & self , database_name : S ) -> Result < ( ) > {
329305 debug ! (
330306 "Creating database {} for {}:{}" ,
331307 database_name. as_ref( ) ,
@@ -358,7 +334,7 @@ impl PostgreSQL {
358334 }
359335
360336 /// 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 > {
337+ pub async fn database_exists < S : AsRef < str > > ( & self , database_name : S ) -> Result < bool > {
362338 debug ! (
363339 "Checking if database {} exists for {}:{}" ,
364340 database_name. as_ref( ) ,
@@ -389,7 +365,7 @@ impl PostgreSQL {
389365 }
390366
391367 /// Drop a database with the given name.
392- pub async fn drop_database < S : AsRef < str > > ( & mut self , database_name : S ) -> Result < ( ) > {
368+ pub async fn drop_database < S : AsRef < str > > ( & self , database_name : S ) -> Result < ( ) > {
393369 debug ! (
394370 "Dropping database {} for {}:{}" ,
395371 database_name. as_ref( ) ,
@@ -457,7 +433,7 @@ impl Default for PostgreSQL {
457433/// Stop the PostgreSQL server and remove the data directory if it is marked as temporary.
458434impl Drop for PostgreSQL {
459435 fn drop ( & mut self ) {
460- if self . status == Status :: Starting || self . status == Status :: Started {
436+ if self . status ( ) == Status :: Started {
461437 let mut pg_ctl = PgCtlBuilder :: new ( )
462438 . program_dir ( self . settings . binary_dir ( ) )
463439 . mode ( Stop )
0 commit comments