1
1
use crate :: error:: Error :: { DatabaseInitializationError , DatabaseStartError , DatabaseStopError } ;
2
2
use crate :: error:: Result ;
3
3
use crate :: settings:: { Settings , BOOTSTRAP_SUPERUSER } ;
4
+ use postgresql_archive:: get_version;
4
5
use postgresql_archive:: { extract, get_archive} ;
5
- use postgresql_archive:: { get_version, Version } ;
6
6
use postgresql_commands:: initdb:: InitDbBuilder ;
7
7
use postgresql_commands:: pg_ctl:: Mode :: { Start , Stop } ;
8
8
use postgresql_commands:: pg_ctl:: PgCtlBuilder ;
@@ -16,26 +16,10 @@ use postgresql_commands::CommandExecutor;
16
16
use std:: fs:: { remove_dir_all, remove_file} ;
17
17
use std:: io:: prelude:: * ;
18
18
use std:: net:: TcpListener ;
19
- #[ cfg( feature = "bundled" ) ]
20
- use std:: str:: FromStr ;
21
19
use tracing:: { debug, instrument} ;
22
20
23
21
use crate :: Error :: { CreateDatabaseError , DatabaseExistsError , DropDatabaseError } ;
24
22
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
-
39
23
const PGDATABASE : & str = "PGDATABASE" ;
40
24
41
25
/// `PostgreSQL` status
@@ -54,21 +38,21 @@ pub enum Status {
54
38
/// `PostgreSQL` server
55
39
#[ derive( Clone , Debug ) ]
56
40
pub struct PostgreSQL {
57
- version : Version ,
58
41
settings : Settings ,
59
42
}
60
43
61
44
/// `PostgreSQL` server methods
62
45
impl PostgreSQL {
63
46
/// Create a new [`PostgreSQL`] instance
64
47
#[ 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 } ;
67
50
68
51
// If the minor and release version are set, append the version to the installation directory
69
52
// to avoid conflicts with other versions. This will also facilitate setting the status
70
53
// of the server to the correct initial value. If the minor and release version are not set,
71
54
// the installation directory will be determined dynamically during the installation process.
55
+ let version = postgresql. settings . version ;
72
56
if version. minor . is_some ( ) && version. release . is_some ( ) {
73
57
let path = & postgresql. settings . installation_dir ;
74
58
let version_string = version. to_string ( ) ;
@@ -82,20 +66,6 @@ impl PostgreSQL {
82
66
postgresql
83
67
}
84
68
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
-
99
69
/// Get the [status](Status) of the PostgreSQL server
100
70
#[ instrument( level = "debug" , skip( self ) ) ]
101
71
pub fn status ( & self ) -> Status {
@@ -110,12 +80,6 @@ impl PostgreSQL {
110
80
}
111
81
}
112
82
113
- /// Get the [version](Version) of the `PostgreSQL` server
114
- #[ must_use]
115
- pub fn version ( & self ) -> & Version {
116
- & self . version
117
- }
118
-
119
83
/// Get the [settings](Settings) of the `PostgreSQL` server
120
84
#[ must_use]
121
85
pub fn settings ( & self ) -> & Settings {
@@ -124,12 +88,13 @@ impl PostgreSQL {
124
88
125
89
/// Check if the `PostgreSQL` server is installed
126
90
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 ( ) {
128
93
return false ;
129
94
}
130
95
131
96
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 ( )
133
98
}
134
99
135
100
/// Check if the `PostgreSQL` server is initialized
@@ -166,18 +131,21 @@ impl PostgreSQL {
166
131
/// returned.
167
132
#[ instrument( skip( self ) ) ]
168
133
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
+ ) ;
170
138
171
139
// If the minor and release version are not set, determine the latest version and update the
172
140
// version and installation directory accordingly. This is an optimization to avoid downloading
173
141
// 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 ? ;
177
145
self . settings . installation_dir = self
178
146
. settings
179
147
. installation_dir
180
- . join ( self . version . to_string ( ) ) ;
148
+ . join ( self . settings . version . to_string ( ) ) ;
181
149
}
182
150
183
151
if self . settings . installation_dir . exists ( ) {
@@ -189,22 +157,26 @@ impl PostgreSQL {
189
157
// If the requested version is the same as the version of the bundled archive, use the bundled
190
158
// archive. This avoids downloading the archive in environments where internet access is
191
159
// restricted or undesirable.
192
- let ( version, bytes) = if * ARCHIVE_VERSION == self . version {
160
+ let ( version, bytes) = if * crate :: settings :: ARCHIVE_VERSION == self . settings . version {
193
161
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
+ )
195
166
} else {
196
- get_archive ( & self . settings . releases_url , & self . version ) . await ?
167
+ get_archive ( & self . settings . releases_url , & self . settings . version ) . await ?
197
168
} ;
198
169
199
170
#[ 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 ? } ;
201
173
202
- self . version = version;
174
+ self . settings . version = version;
203
175
extract ( & bytes, & self . settings . installation_dir ) . await ?;
204
176
205
177
debug ! (
206
178
"Installed PostgreSQL version {} to {}" ,
207
- self . version,
179
+ self . settings . version,
208
180
self . settings. installation_dir. to_string_lossy( )
209
181
) ;
210
182
@@ -433,9 +405,7 @@ impl PostgreSQL {
433
405
/// Default `PostgreSQL` server
434
406
impl Default for PostgreSQL {
435
407
fn default ( ) -> Self {
436
- let version = PostgreSQL :: default_version ( ) ;
437
- let settings = Settings :: default ( ) ;
438
- Self :: new ( version, settings)
408
+ Self :: new ( Settings :: default ( ) )
439
409
}
440
410
}
441
411
@@ -459,12 +429,3 @@ impl Drop for PostgreSQL {
459
429
}
460
430
}
461
431
}
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