Skip to content

Commit

Permalink
Unifies SQLite and Postgres backend into a single docker image (#397)
Browse files Browse the repository at this point in the history
* Unified postgres and sqlite connection in one container.

* Improved logging.

* Added macro invocation to prevent code doubling.

* Added default features sqlite and postgresql.

* Fixed clippy.

* Removed unnecessary macros.

* Install git.

* Fixed build rs.

* Fixed linter.
  • Loading branch information
SamTV12345 authored Nov 8, 2023
1 parent 3ceec62 commit d5e1607
Show file tree
Hide file tree
Showing 31 changed files with 152 additions and 183 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ jobs:
name: Rust lint
runs-on: ubuntu-latest
steps:
- run: sudo apt install git
name: Install git
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ build = "build.rs"
built = {version="0.7.1", features=["chrono", "semver","cargo-lock"]}

[features]
default = ["sqlite"]
default = ["sqlite", "postgresql"]
mysql = ["diesel/mysql", "diesel_migrations/mysql", "diesel/mysql_backend"]
postgresql = ["diesel/postgres", "diesel_migrations/postgres", "diesel/chrono", "r2d2_postgres",
"diesel/postgres_backend"]
Expand Down
30 changes: 15 additions & 15 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,31 @@ use std::process::Command;


fn main() {
#[cfg(feature = "sqlite")]
println!("cargo:rustc-cfg=sqlite");
#[cfg(feature = "mysql")]
println!("cargo:rustc-cfg=mysql");
#[cfg(feature = "postgresql")]
println!("cargo:rustc-cfg=postgresql");
let maybe_vaultwarden_version =
env::var("VW_VERSION").or_else(|_| env::var("BWRS_VERSION")).or_else(|_| version_from_git_info());

#[cfg(feature = "postgresql")]
version_from_git_info().expect("Error retrieving git information");

create_git_sqlite();
if let Ok(version) = maybe_vaultwarden_version {
let result_of_git = version_from_git_info();
match result_of_git{
Ok(version) => {
println!("cargo:rustc-env=VW_VERSION={version}");
println!("cargo:rustc-env=CARGO_PKG_VERSION={version}");
println!("cargo:rustc-env=GIT_EXACT_TAG={version}");
}
else{
},
Err(_) => {
println!("cargo:rustc-env=VW_VERSION=unknown");
println!("cargo:rustc-env=CARGO_PKG_VERSION=unknown");
println!("cargo:rustc-env=GIT_EXACT_TAG=unknown");
println!("cargo:rustc-env=GIT_LAST_TAG=unknown");
println!("cargo:rustc-env=GIT_BRANCH=unknown");
println!("cargo:rustc-env=GIT_REV=unknown");
}
}

if let Ok(version) = version_from_git_info() {
println!("cargo:rustc-env=VW_VERSION={version}");
println!("cargo:rustc-env=CARGO_PKG_VERSION={version}");
println!("cargo:rustc-env=GIT_EXACT_TAG={version}");
}
else{
create_git_sqlite();
}
}

Expand Down
37 changes: 13 additions & 24 deletions src/config/dbconfig.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use diesel::prelude::*;
use std::env;
use std::process::exit;
use std::time::Duration;
use crate::constants::inner_constants::{DATABASE_URL, DATABASE_URL_DEFAULT_SQLITE};
#[cfg(sqlite)]
use crate::DbConnection;
use crate::dbconfig::DBType;
use crate::DBType as DbConnection;

#[derive(Debug)]
pub struct ConnectionOptions {
Expand All @@ -12,7 +13,6 @@ pub struct ConnectionOptions {
pub busy_timeout: Option<Duration>,
}

#[cfg(sqlite)]
impl r2d2::CustomizeConnection<DbConnection, diesel::r2d2::Error>
for ConnectionOptions
{
Expand All @@ -34,30 +34,19 @@ for ConnectionOptions
}
}

#[cfg(sqlite)]
pub fn establish_connection() -> DbConnection {
pub fn establish_connection() -> DBType {
let database_url = &get_database_url();
DbConnection::establish(database_url)
.unwrap_or_else(|_| panic!("Error connecting to {}", database_url))
}


#[cfg(postgresql)]
pub fn establish_connection()->PgConnection{
let database_url = &get_database_url();
println!("Connecting to {}", database_url);
PgConnection::establish(database_url)
.unwrap_or_else(|e| panic!("Error connecting to {} with error {}", database_url,e))
}

#[cfg(mysql)]
pub fn establish_connection()->MysqlConnection{
let database_url = &get_database_url();
MysqlConnection::establish(database_url)
.unwrap_or_else(|_| panic!("Error connecting to {}", database_url))
DBType::establish(database_url)
.unwrap_or_else(|e| {
log::error!("Error connecting to {} with reason {}", database_url, e);
exit(1)
})
}


pub fn get_database_url()->String{
env::var(DATABASE_URL).unwrap_or(DATABASE_URL_DEFAULT_SQLITE.to_string())

let url = env::var(DATABASE_URL).unwrap_or(DATABASE_URL_DEFAULT_SQLITE.to_string());
log::info!("Database url is set to {}", url);
url
}
3 changes: 2 additions & 1 deletion src/controllers/podcast_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::service::environment_service::EnvironmentService;
use crate::service::mapping_service::MappingService;
use crate::service::podcast_episode_service::PodcastEpisodeService;
use crate::service::rust_service::PodcastService;
use crate::{DbConnection, DbPool, get_default_image, unwrap_string};
use crate::{DbPool, get_default_image, unwrap_string};
use actix::Addr;
use actix_web::web::{Data, Path};
use actix_web::{get, post, put, delete, HttpRequest, error, Error};
Expand Down Expand Up @@ -44,6 +44,7 @@ use tokio_stream::wrappers::UnboundedReceiverStream;
use crate::models::podcast_episode::PodcastEpisode;
use crate::models::podcast_history_item::PodcastHistoryItem;
use crate::utils::append_to_header::add_basic_auth_headers_conditionally;
use crate::DBType as DbConnection;

#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
Expand Down
5 changes: 3 additions & 2 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use diesel::prelude::*;
use diesel::{RunQueryDsl};
use diesel::dsl::max;
use crate::controllers::podcast_episode_controller::TimelineQueryParams;
use crate::{DbConnection};
use crate::dbconfig::DBType;
use crate::models::favorites::Favorite;
use crate::models::filter::Filter;
use crate::models::podcast_history_item::PodcastHistoryItem;
Expand All @@ -18,7 +18,8 @@ pub struct TimelineItem {
}

impl TimelineItem {
pub fn get_timeline(username_to_search: String, conn: &mut DbConnection, favored_only: TimelineQueryParams)
pub fn get_timeline(username_to_search: String, conn: &mut DBType, favored_only:
TimelineQueryParams)
-> Result<TimelineItem, CustomError> {
use crate::dbconfig::schema::podcast_episodes::dsl::*;
use crate::dbconfig::schema::podcasts::dsl::*;
Expand Down
68 changes: 20 additions & 48 deletions src/dbconfig/mod.rs
Original file line number Diff line number Diff line change
@@ -1,60 +1,32 @@
#[cfg(sqlite)]
#[path = "schemas/sqlite/schema.rs"]
pub mod schema;

#[cfg(mysql)]
#[path = "schemas/mysql/schema.rs"]
pub mod schema;

#[cfg(postgresql)]
#[path = "schemas/postgresql/schema.rs"]
pub mod schema;


#[macro_export]
#[cfg(sqlite)]
macro_rules! import_database_connections {
() => {
use diesel::SqliteConnection;
};
}

#[macro_export]
#[cfg(postgresql)]
macro_rules! import_database_connections {
() => {
use diesel::PgConnection;
};
#[derive(diesel::MultiConnection)]
pub enum DBType {
Postgresql(diesel::PgConnection),
Sqlite(diesel::SqliteConnection),
}

#[macro_export]
#[cfg(mysql)]
macro_rules! import_database_connections {
() => {
use diesel::MysqlConnection;
};
}


#[macro_export]
macro_rules! import_database_config{
()=>{
#[cfg(sqlite)]
type DbConnection = SqliteConnection;
#[cfg(sqlite)]
pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!("./migrations/sqlite");


#[cfg(postgresql)]
pub type DbConnection = PgConnection;
pub const SQLITE_MIGRATIONS: EmbeddedMigrations = embed_migrations!("./migrations/sqlite");


#[cfg(postgresql)]
pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!("./migrations/postgres");

#[cfg(mysql)]
type DbConnection = MysqlConnection;
#[cfg(mysql)]
pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!("./migrations/mysql");
pub const POSTGRES_MIGRATIONS: EmbeddedMigrations = embed_migrations!("./migrations/postgres");
}
}

#[macro_export]
macro_rules! execute_with_conn {
($conn:expr, $diesel_func:expr) => {
match $conn {
DbConnection::Sqlite(conn) => {
return $diesel_func(conn)
},
DbConnection::Postgresql(conn) => {
return $diesel_func(conn)
},
}
};
}
Loading

0 comments on commit d5e1607

Please sign in to comment.