Skip to content

Commit

Permalink
Refactor dbclient so that we can run different sets of migrations (an…
Browse files Browse the repository at this point in the history
…d therefore, support multiple schemas)
  • Loading branch information
ofux authored and AnthonyBuisset committed Sep 29, 2023
1 parent f91e8fb commit 8fb48b8
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 9 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ diesel = { version = "2.1.0", default-features = false, features = [
"uuid",
"chrono",
] }
diesel_migrations = "2.1.0"
diesel-derive-newtype = "2.1.0"
diesel_json = "0.2.0"
uuid = { version = "1.3.3", default_features = false, features = [
Expand Down
5 changes: 4 additions & 1 deletion api/src/presentation/http/bootstrap.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::sync::Arc;

use anyhow::Result;
use diesel_migrations::{embed_migrations, EmbeddedMigrations};
use domain::{AggregateRepository, CompositePublisher, EventPublisher};
use infrastructure::{amqp, dbclient, event_bus::EXCHANGE_NAME, github};
use rocket::{Build, Rocket};
Expand All @@ -12,12 +13,14 @@ use crate::{
Config,
};

pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!("../migrations");

pub async fn bootstrap(config: Config) -> Result<Rocket<Build>> {
info!("Bootstrapping api http server");
let database = Arc::new(dbclient::Client::new(dbclient::init_pool(
config.database.clone(),
)?));
database.run_migrations()?;
database.run_migrations(MIGRATIONS)?;

let github_api_client: Arc<github::Client> =
github::RoundRobinClient::new(config.github_api_client.clone())?.into();
Expand Down
10 changes: 4 additions & 6 deletions common/infrastructure/src/dbclient/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@ use crate::diesel_migrations::MigrationHarness;
pub type Pool = r2d2::Pool<ConnectionManager<PgConnection>>;
type PooledConnection = r2d2::PooledConnection<ConnectionManager<PgConnection>>;

pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!();

pub fn run_migrations(pool: &Pool) {
pub fn run_migrations(pool: &Pool, migrations: EmbeddedMigrations) {
let mut connection = pool.get().expect("Unable to get connection from pool");
connection.run_pending_migrations(MIGRATIONS).expect("diesel migration failure");
connection.run_pending_migrations(migrations).expect("diesel migration failure");
}

pub struct Client {
Expand All @@ -48,9 +46,9 @@ impl Client {
})
}

pub fn run_migrations(&self) -> Result<()> {
pub fn run_migrations(&self, migrations: EmbeddedMigrations) -> Result<()> {
let mut connection = self.connection()?;
connection.run_pending_migrations(MIGRATIONS).map_err(|e| {
connection.run_pending_migrations(migrations).map_err(|e| {
error!(error = e.to_field(), "Failed to run migrations");
DatabaseError::Migration(anyhow!(e))
})?;
Expand Down
1 change: 0 additions & 1 deletion common/infrastructure/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ pub mod http;
pub mod tracing;
pub mod web3;

#[macro_use]
extern crate diesel_migrations;
#[macro_use]
extern crate lazy_static;
1 change: 1 addition & 0 deletions common/testing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ project-root = "0.2.2"
serde_json = "1.0.81"
serde = { version = "1.0.137", features = ["derive"] }
reqwest = { version = "0.11" }
diesel_migrations = "2.1.0"

# Local dependecies
domain = { path = "../domain" }
Expand Down
5 changes: 4 additions & 1 deletion common/testing/src/context/database.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use std::sync::Arc;

use anyhow::{anyhow, Result};
use diesel_migrations::{embed_migrations, EmbeddedMigrations};
use infrastructure::dbclient;
use testcontainers::{
clients::Cli, core::WaitFor, images::generic::GenericImage, Container, RunnableImage,
};

pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!("../../migrations");

static USER: &str = "postgres";
static PASSWORD: &str = "Passw0rd";
static DATABASE: &str = "marketplace_db";
Expand All @@ -31,7 +34,7 @@ impl<'docker> Context<'docker> {

let client = dbclient::Client::new(dbclient::init_pool(config.clone())?);

client.run_migrations()?;
client.run_migrations(MIGRATIONS)?;

Ok(Self {
_container: container,
Expand Down

0 comments on commit 8fb48b8

Please sign in to comment.