Skip to content

Commit

Permalink
Added embedded postgres to unit test postgres implementation of DB la…
Browse files Browse the repository at this point in the history
…yer (#203)

* Added embedded postgres to unit test postgres implementation of DB layer
  • Loading branch information
zajko authored Oct 4, 2023
1 parent da3e3ac commit 38e7247
Show file tree
Hide file tree
Showing 12 changed files with 1,839 additions and 742 deletions.
1,185 changes: 842 additions & 343 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[workspace]
resolver = "1"
members = [
"sidecar",
"listener",
Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[toolchain]
channel = "1.68.1"
channel = "1.72.1"
components = [ "rustfmt", "clippy" ]
targets = [ "wasm32-unknown-unknown" ]
profile = "minimal"
1 change: 1 addition & 0 deletions sidecar/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ tabled = { version = "0.10.0", features = ["derive", "color"] }
tempfile = "3"
once_cell = "1.17.1"
tokio-util = "0.7.8"
pg-embed = { git = "https://github.com/faokunega/pg-embed", tag = "v0.8.0" }

[package.metadata.deb]
revision = "0"
Expand Down
2 changes: 2 additions & 0 deletions sidecar/src/database/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ pub mod errors;
pub mod migration_manager;
pub mod postgresql_database;
pub mod sqlite_database;
#[cfg(test)]
pub mod tests;
pub mod types;
14 changes: 13 additions & 1 deletion sidecar/src/database/postgresql_database.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod reader;
#[cfg(test)]
mod tests;
mod writer;

use anyhow::Error;
use sea_query::PostgresQueryBuilder;
use sqlx::{
Expand All @@ -22,6 +23,17 @@ pub struct PostgreSqlDatabase {
}

impl PostgreSqlDatabase {
#[cfg(test)]
pub async fn new_from_postgres_uri(uri: String) -> Result<PostgreSqlDatabase, Error> {
let connection_pool = PgPoolOptions::new()
.max_connections(30)
.connect(uri.as_str())
.await?;
let db = PostgreSqlDatabase { connection_pool };
MigrationManager::apply_all_migrations(db.clone()).await?;
Ok(db)
}

pub async fn new(config: PostgresqlConfig) -> Result<PostgreSqlDatabase, Error> {
let host = config.host;
let database_name = config.database_name;
Expand Down
Loading

0 comments on commit 38e7247

Please sign in to comment.