Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: Isolate dynamodb to feature flag only #612

Merged
merged 3 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions autopush-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ name = "autopush_common"
version.workspace = true
authors.workspace = true
edition.workspace = true
build = "build.rs"

[lib]
name = "autopush_common"
Expand Down
5 changes: 5 additions & 0 deletions autopush-common/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub fn main() {
if !(cfg!(feature = "dynamodb") || cfg!(feature = "bigtable")) {
panic!("No database defined! Please compile with either `features=dynamodb` or `features=bigtable`");
}
}
10 changes: 10 additions & 0 deletions autopush-common/src/db/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use backtrace::Backtrace;
#[cfg(feature = "dynamodb")]
use rusoto_core::RusotoError;
#[cfg(feature = "dynamodb")]
use rusoto_dynamodb::{
BatchWriteItemError, DeleteItemError, DescribeTableError, GetItemError, PutItemError,
QueryError, UpdateItemError,
Expand All @@ -14,27 +16,35 @@ pub type DbResult<T> = Result<T, DbError>;

#[derive(Debug, Error)]
pub enum DbError {
#[cfg(feature = "dynamodb")]
#[error("Database error while performing GetItem")]
DdbGetItem(#[from] RusotoError<GetItemError>),

#[cfg(feature = "dynamodb")]
#[error("Database error while performing UpdateItem")]
DdbUpdateItem(#[from] RusotoError<UpdateItemError>),

#[cfg(feature = "dynamodb")]
#[error("Database error while performing PutItem")]
DdbPutItem(#[from] RusotoError<PutItemError>),

#[cfg(feature = "dynamodb")]
#[error("Database error while performing DeleteItem")]
DdbDeleteItem(#[from] RusotoError<DeleteItemError>),

#[cfg(feature = "dynamodb")]
#[error("Database error while performing BatchWriteItem")]
DdbBatchWriteItem(#[from] RusotoError<BatchWriteItemError>),

#[cfg(feature = "dynamodb")]
#[error("Database error while performing DescribeTable")]
DdbDescribeTable(#[from] RusotoError<DescribeTableError>),

#[cfg(feature = "dynamodb")]
#[error("Database error while performing Query")]
DdbQuery(#[from] RusotoError<QueryError>),

#[cfg(feature = "dynamodb")]
#[error("Error while performing DynamoDB (de)serialization: {0}")]
DdbSerialization(#[from] serde_dynamodb::Error),

Expand Down
24 changes: 21 additions & 3 deletions autopush-common/src/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ use serde::Serializer;
use serde_derive::{Deserialize, Serialize};
use uuid::Uuid;

use crate::db::{dynamodb::has_connected_this_month, util::generate_last_connect};
#[cfg(feature = "dynamodb")]
use crate::db::dynamodb::has_connected_this_month;
use util::generate_last_connect;

#[cfg(feature = "bigtable")]
pub mod bigtable;
Expand Down Expand Up @@ -59,6 +61,20 @@ pub enum StorageType {
// Postgres,
}

impl From<&str> for StorageType {
fn from(name: &str) -> Self {
match name.to_lowercase().as_str() {
#[cfg(feature = "bigtable")]
"bigtable" => Self::BigTable,
#[cfg(feature = "dual")]
"dual" => Self::Dual,
#[cfg(feature = "dynamodb")]
"dynamodb" => Self::DynamoDb,
_ => Self::INVALID,
}
}
}

/// The type of storage to use.
#[allow(clippy::vec_init_then_push)] // Because we are only pushing on feature flags.
impl StorageType {
Expand All @@ -78,8 +94,9 @@ impl StorageType {
debug!("Supported data types: {:?}", StorageType::available());
debug!("Checking DSN: {:?}", &dsn);
if dsn.is_none() {
info!("No DSN specified, failing over to old default dsn");
return Self::DynamoDb;
let default = Self::available()[0];
info!("No DSN specified, failing over to old default dsn: {default}");
return Self::from(default);
}
let dsn = dsn
.clone()
Expand Down Expand Up @@ -207,6 +224,7 @@ impl Default for User {
}

impl User {
#[cfg(feature = "dynamodb")]
pub fn set_last_connect(&mut self) {
self.last_connect = if has_connected_this_month(self) {
None
Expand Down