11use crate :: error:: Error :: { DatabaseInitializationError , DatabaseStartError , DatabaseStopError } ;
22use crate :: error:: Result ;
33use crate :: settings:: { Settings , BOOTSTRAP_SUPERUSER } ;
4+ use postgresql_archive:: get_version;
45use postgresql_archive:: { extract, get_archive} ;
5- use postgresql_archive:: { get_version, Version } ;
66use postgresql_commands:: initdb:: InitDbBuilder ;
77use postgresql_commands:: pg_ctl:: Mode :: { Start , Stop } ;
88use postgresql_commands:: pg_ctl:: PgCtlBuilder ;
@@ -16,26 +16,10 @@ use postgresql_commands::CommandExecutor;
1616use std:: fs:: { remove_dir_all, remove_file} ;
1717use std:: io:: prelude:: * ;
1818use std:: net:: TcpListener ;
19- #[ cfg( feature = "bundled" ) ]
20- use std:: str:: FromStr ;
2119use tracing:: { debug, instrument} ;
2220
2321use crate :: Error :: { CreateDatabaseError , DatabaseExistsError , DropDatabaseError } ;
2422
25- #[ cfg( feature = "bundled" ) ]
26- lazy_static:: lazy_static! {
27- #[ allow( clippy:: unwrap_used) ]
28- pub ( crate ) static ref ARCHIVE_VERSION : Version = {
29- let version_string = include_str!( concat!( std:: env!( "OUT_DIR" ) , "/postgresql.version" ) ) ;
30- let version = Version :: from_str( version_string) . unwrap( ) ;
31- debug!( "Bundled installation archive version {version}" ) ;
32- version
33- } ;
34- }
35-
36- #[ cfg( feature = "bundled" ) ]
37- pub ( crate ) const ARCHIVE : & [ u8 ] = include_bytes ! ( concat!( env!( "OUT_DIR" ) , "/postgresql.tar.gz" ) ) ;
38-
3923const PGDATABASE : & str = "PGDATABASE" ;
4024
4125/// `PostgreSQL` status
@@ -54,21 +38,21 @@ pub enum Status {
5438/// `PostgreSQL` server
5539#[ derive( Clone , Debug ) ]
5640pub struct PostgreSQL {
57- version : Version ,
5841 settings : Settings ,
5942}
6043
6144/// `PostgreSQL` server methods
6245impl PostgreSQL {
6346 /// Create a new [`PostgreSQL`] instance
6447 #[ must_use]
65- pub fn new ( version : Version , settings : Settings ) -> Self {
66- let mut postgresql = PostgreSQL { version , settings } ;
48+ pub fn new ( settings : Settings ) -> Self {
49+ let mut postgresql = PostgreSQL { settings } ;
6750
6851 // If the minor and release version are set, append the version to the installation directory
6952 // to avoid conflicts with other versions. This will also facilitate setting the status
7053 // of the server to the correct initial value. If the minor and release version are not set,
7154 // the installation directory will be determined dynamically during the installation process.
55+ let version = postgresql. settings . version ;
7256 if version. minor . is_some ( ) && version. release . is_some ( ) {
7357 let path = & postgresql. settings . installation_dir ;
7458 let version_string = version. to_string ( ) ;
@@ -82,20 +66,6 @@ impl PostgreSQL {
8266 postgresql
8367 }
8468
85- /// Get the default version used if not otherwise specified
86- #[ must_use]
87- pub fn default_version ( ) -> Version {
88- #[ cfg( feature = "bundled" ) ]
89- {
90- * ARCHIVE_VERSION
91- }
92-
93- #[ cfg( not( feature = "bundled" ) ) ]
94- {
95- postgresql_archive:: LATEST
96- }
97- }
98-
9969 /// Get the [status](Status) of the PostgreSQL server
10070 #[ instrument( level = "debug" , skip( self ) ) ]
10171 pub fn status ( & self ) -> Status {
@@ -110,12 +80,6 @@ impl PostgreSQL {
11080 }
11181 }
11282
113- /// Get the [version](Version) of the `PostgreSQL` server
114- #[ must_use]
115- pub fn version ( & self ) -> & Version {
116- & self . version
117- }
118-
11983 /// Get the [settings](Settings) of the `PostgreSQL` server
12084 #[ must_use]
12185 pub fn settings ( & self ) -> & Settings {
@@ -124,12 +88,13 @@ impl PostgreSQL {
12488
12589 /// Check if the `PostgreSQL` server is installed
12690 fn is_installed ( & self ) -> bool {
127- if self . version . minor . is_none ( ) || self . version . release . is_none ( ) {
91+ let version = self . settings . version ;
92+ if version. minor . is_none ( ) || version. release . is_none ( ) {
12893 return false ;
12994 }
13095
13196 let path = & self . settings . installation_dir ;
132- path. ends_with ( self . version . to_string ( ) ) && path. exists ( )
97+ path. ends_with ( version. to_string ( ) ) && path. exists ( )
13398 }
13499
135100 /// Check if the `PostgreSQL` server is initialized
@@ -166,18 +131,21 @@ impl PostgreSQL {
166131 /// returned.
167132 #[ instrument( skip( self ) ) ]
168133 async fn install ( & mut self ) -> Result < ( ) > {
169- debug ! ( "Starting installation process for version {}" , self . version) ;
134+ debug ! (
135+ "Starting installation process for version {}" ,
136+ self . settings. version
137+ ) ;
170138
171139 // If the minor and release version are not set, determine the latest version and update the
172140 // version and installation directory accordingly. This is an optimization to avoid downloading
173141 // the archive if the latest version is already installed.
174- if self . version . minor . is_none ( ) || self . version . release . is_none ( ) {
175- let version = get_version ( & self . settings . releases_url , & self . version ) . await ? ;
176- self . version = version;
142+ if self . settings . version . minor . is_none ( ) || self . settings . version . release . is_none ( ) {
143+ self . settings . version =
144+ get_version ( & self . settings . releases_url , & self . settings . version ) . await ? ;
177145 self . settings . installation_dir = self
178146 . settings
179147 . installation_dir
180- . join ( self . version . to_string ( ) ) ;
148+ . join ( self . settings . version . to_string ( ) ) ;
181149 }
182150
183151 if self . settings . installation_dir . exists ( ) {
@@ -189,22 +157,26 @@ impl PostgreSQL {
189157 // If the requested version is the same as the version of the bundled archive, use the bundled
190158 // archive. This avoids downloading the archive in environments where internet access is
191159 // restricted or undesirable.
192- let ( version, bytes) = if * ARCHIVE_VERSION == self . version {
160+ let ( version, bytes) = if * crate :: settings :: ARCHIVE_VERSION == self . settings . version {
193161 debug ! ( "Using bundled installation archive" ) ;
194- ( self . version , bytes:: Bytes :: copy_from_slice ( ARCHIVE ) )
162+ (
163+ self . settings . version ,
164+ bytes:: Bytes :: copy_from_slice ( crate :: settings:: ARCHIVE ) ,
165+ )
195166 } else {
196- get_archive ( & self . settings . releases_url , & self . version ) . await ?
167+ get_archive ( & self . settings . releases_url , & self . settings . version ) . await ?
197168 } ;
198169
199170 #[ cfg( not( feature = "bundled" ) ) ]
200- let ( version, bytes) = { get_archive ( & self . settings . releases_url , & self . version ) . await ? } ;
171+ let ( version, bytes) =
172+ { get_archive ( & self . settings . releases_url , & self . settings . version ) . await ? } ;
201173
202- self . version = version;
174+ self . settings . version = version;
203175 extract ( & bytes, & self . settings . installation_dir ) . await ?;
204176
205177 debug ! (
206178 "Installed PostgreSQL version {} to {}" ,
207- self . version,
179+ self . settings . version,
208180 self . settings. installation_dir. to_string_lossy( )
209181 ) ;
210182
@@ -433,9 +405,7 @@ impl PostgreSQL {
433405/// Default `PostgreSQL` server
434406impl Default for PostgreSQL {
435407 fn default ( ) -> Self {
436- let version = PostgreSQL :: default_version ( ) ;
437- let settings = Settings :: default ( ) ;
438- Self :: new ( version, settings)
408+ Self :: new ( Settings :: default ( ) )
439409 }
440410}
441411
@@ -459,12 +429,3 @@ impl Drop for PostgreSQL {
459429 }
460430 }
461431}
462-
463- #[ cfg( test) ]
464- mod tests {
465- #[ test]
466- #[ cfg( feature = "bundled" ) ]
467- fn test_archive_version ( ) {
468- assert ! ( !super :: ARCHIVE_VERSION . to_string( ) . is_empty( ) ) ;
469- }
470- }
0 commit comments