Skip to content

Commit

Permalink
Split database files and migrations
Browse files Browse the repository at this point in the history
This removes cronback-migration and moves migrations to each service, the approach is pretty dumb, in the follow up PRs, migrations and service bootstrapping will be consolidated in shared traits/types.
  • Loading branch information
AhmedSoliman committed Jul 25, 2023
1 parent 89573ef commit 0751032
Show file tree
Hide file tree
Showing 28 changed files with 118 additions and 332 deletions.
226 changes: 1 addition & 225 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ cronback-api-model = { path = "cronback-api-model", version = "0.1.0" }
cronback-client = { path = "clients/rust", package = "cronback-client", version = "0.1.0" }
dto = { path = "cronback-dto", package = "cronback-dto", version = "0.1.0" }
lib = { path = "cronback-lib", package = "cronback-lib", version = "0.1.0" }
migration = { path = "cronback-migration", package = "cronback-migration", version = "0.1.0" }
proto = { path = "cronback-proto", package = "cronback-proto", version = "0.1.0" }

anyhow = "1.0.69"
Expand Down
1 change: 0 additions & 1 deletion cronback-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ path = "lib.rs"
# Internal Dependencies
proto = { workspace = true }
dto = { workspace = true }
migration = { workspace = true }

# Dependencies from workspace
anyhow = { workspace = true }
Expand Down
17 changes: 3 additions & 14 deletions cronback-lib/database/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,21 @@ mod errors;
mod pagination;

pub use errors::DatabaseError;
use migration::{Migrator, MigratorTrait};
pub use pagination::*;
use sea_orm::TransactionTrait;

#[derive(Clone)]
pub struct Database {
pub orm: sea_orm::DatabaseConnection,
}

impl Database {
pub async fn connect(conn_string: &str) -> Result<Self, sea_orm::DbErr> {
pub async fn connect(conn_string: &str) -> Result<Self, DatabaseError> {
Ok(Self {
orm: sea_orm::Database::connect(conn_string).await?,
})
}

pub async fn in_memory() -> Result<Self, sea_orm::DbErr> {
let conn = Self::connect("sqlite::memory:").await?;
conn.migrate().await?;
Ok(conn)
}

pub async fn migrate(&self) -> Result<(), sea_orm::DbErr> {
let conn = self.orm.begin().await?;
Migrator::up(&conn, None).await?;
conn.commit().await?;
Ok(())
pub async fn in_memory() -> Result<Self, DatabaseError> {
Self::connect("sqlite::memory:").await
}
}
Loading

0 comments on commit 0751032

Please sign in to comment.