Skip to content

Commit feeeeb6

Browse files
committed
refactor: simplified installation logic and improved code coverage
1 parent b85bca1 commit feeeeb6

File tree

2 files changed

+25
-43
lines changed

2 files changed

+25
-43
lines changed

postgresql_embedded/src/error.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ pub enum Error {
99
/// Error when PostgreSQL archive operations fail
1010
#[error(transparent)]
1111
ArchiveError(postgresql_archive::Error),
12-
/// Error when the archive is not found for a specific version
13-
#[error("Archive not found for version [{0}]")]
14-
ArchiveNotFound(String),
1512
/// Error when a command fails
1613
#[error("Command error: stdout={stdout}; stderr={stderr}")]
1714
CommandError { stdout: String, stderr: String },

postgresql_embedded/src/postgresql.rs

Lines changed: 25 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ use crate::command::pg_ctl::Mode::{Start, Stop};
33
use crate::command::pg_ctl::PgCtlBuilder;
44
use crate::command::pg_ctl::ShutdownMode::Fast;
55
use crate::command::traits::{CommandBuilder, CommandExecutor};
6-
use crate::error::Error::{
7-
ArchiveNotFound, DatabaseInitializationError, DatabaseStartError, DatabaseStopError,
8-
};
6+
use crate::error::Error::{DatabaseInitializationError, DatabaseStartError, DatabaseStopError};
97
use crate::error::Result;
108
use crate::settings::Settings;
119
use bytes::Bytes;
@@ -94,7 +92,7 @@ impl PostgreSQL {
9492
postgresql
9593
}
9694

97-
/// Get the default version used by if not otherwise specified
95+
/// Get the default version used if not otherwise specified
9896
pub fn default_version() -> Version {
9997
if cfg!(feature = "bundled") {
10098
*ARCHIVE_VERSION
@@ -168,19 +166,8 @@ impl PostgreSQL {
168166
/// already exists, the archive will not be extracted. If the archive is not found, an error will be
169167
/// returned.
170168
async fn install(&mut self) -> Result<()> {
171-
let mut archive_bytes: Option<Bytes> = None;
172-
173169
debug!("Starting installation process for version {}", self.version);
174170

175-
#[cfg(feature = "bundled")]
176-
// If the requested version is the same as the version of the bundled archive, use the bundled
177-
// archive. This avoids downloading the archive in environments where internet access is
178-
// restricted or undesirable.
179-
if ARCHIVE_VERSION.deref() == &self.version {
180-
debug!("Using bundled installation archive");
181-
archive_bytes = Some(Bytes::copy_from_slice(ARCHIVE));
182-
}
183-
184171
// If the minor and release version are not set, determine the latest version and update the
185172
// version and installation directory accordingly. This is an optimization to avoid downloading
186173
// the archive if the latest version is already installed.
@@ -191,35 +178,33 @@ impl PostgreSQL {
191178
.settings
192179
.installation_dir
193180
.join(self.version.to_string());
194-
195-
if self.settings.installation_dir.exists() {
196-
debug!("Installation directory already exists");
197-
self.update_status();
198-
return Ok(());
199-
}
200181
}
201182

202-
if archive_bytes.is_none() {
203-
let (version, bytes) = get_archive(&self.version).await?;
204-
self.version = version;
205-
archive_bytes = Some(bytes);
183+
if self.settings.installation_dir.exists() {
184+
debug!("Installation directory already exists");
185+
self.update_status();
186+
return Ok(());
206187
}
207188

208-
if !self.settings.installation_dir.exists() {
209-
self.status = Status::Installing;
210-
create_dir_all(&self.settings.installation_dir)?;
211-
212-
match archive_bytes {
213-
Some(bytes) => {
214-
extract(&bytes, &self.settings.installation_dir).await?;
215-
self.status = Status::Installed;
216-
}
217-
None => {
218-
self.update_status();
219-
return Err(ArchiveNotFound(self.version.to_string()));
220-
}
221-
}
222-
}
189+
#[cfg(feature = "bundled")]
190+
// If the requested version is the same as the version of the bundled archive, use the bundled
191+
// archive. This avoids downloading the archive in environments where internet access is
192+
// restricted or undesirable.
193+
let (version, bytes) = if ARCHIVE_VERSION.deref() == &self.version {
194+
debug!("Using bundled installation archive");
195+
(self.version, Bytes::copy_from_slice(ARCHIVE))
196+
} else {
197+
get_archive(&self.version).await?
198+
};
199+
200+
#[cfg(not(feature = "bundled"))]
201+
let (version, bytes) = { get_archive(&self.version).await? };
202+
203+
self.version = version;
204+
self.status = Status::Installing;
205+
create_dir_all(&self.settings.installation_dir)?;
206+
extract(&bytes, &self.settings.installation_dir).await?;
207+
self.status = Status::Installed;
223208

224209
debug!(
225210
"Installed PostgreSQL version {} to {}",

0 commit comments

Comments
 (0)