Skip to content

Commit

Permalink
Fix odf's sqlx feature
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiimk committed Jul 17, 2024
1 parent dd580ca commit 96d4ef4
Show file tree
Hide file tree
Showing 17 changed files with 133 additions and 92 deletions.
11 changes: 11 additions & 0 deletions deny.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,21 @@ features = [
{ name = "opendatafabric", allow = [
"default",
"sqlx",
"sqlx-mysql",
"sqlx-postgres",
"sqlx-sqlite",
] },
{ name = "kamu", allow = [
"default",
] },
{ name = "kamu-accounts", allow = [
"default",
"sqlx",
] },
{ name = "kamu-datasets", allow = [
"default",
"sqlx",
] },
{ name = "kamu-cli", allow = [
"default",
] },
Expand Down
15 changes: 11 additions & 4 deletions src/domain/accounts/domain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@ workspace = true
doctest = false


[features]
default = []
sqlx = ["dep:sqlx"]


[dependencies]
database-common = { workspace = true }
internal-error = { workspace = true }
opendatafabric = { workspace = true, default-features = true, features = [
"sqlx",
] }
opendatafabric = { workspace = true }
random-names = { workspace = true }

async-trait = { version = "0.1", default-features = false }
Expand All @@ -41,9 +44,13 @@ rand = "0.8"
reusable = "0.1"
serde = "1"
serde_with = { version = "3", default-features = false }
sqlx = { version = "0.7", default-features = false, features = ["macros"] }
thiserror = { version = "1", default-features = false }
tracing = { version = "0.1", default-features = false }
uuid = { version = "1", default-features = false, features = ["v4"] }

# Optional
sqlx = { optional = true, version = "0.7", default-features = false, features = [
"macros",
] }

[dev-dependencies]
2 changes: 2 additions & 0 deletions src/domain/accounts/domain/src/entities/access_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ pub enum DecodeTokenError {

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#[cfg(feature = "sqlx")]
#[derive(Debug, Clone, sqlx::FromRow, PartialEq, Eq)]
pub struct AccessTokenRowModel {
pub id: Uuid,
Expand All @@ -171,6 +172,7 @@ pub struct AccessTokenRowModel {
pub account_id: AccountID,
}

#[cfg(feature = "sqlx")]
impl From<AccessTokenRowModel> for AccessToken {
fn from(value: AccessTokenRowModel) -> Self {
AccessToken {
Expand Down
17 changes: 12 additions & 5 deletions src/domain/accounts/domain/src/entities/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use chrono::{DateTime, TimeZone, Utc};
use lazy_static::lazy_static;
use opendatafabric::{AccountID, AccountName};
use reusable::{reusable, reuse};
use serde::{Deserialize, Serialize};

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -47,8 +46,12 @@ pub struct Account {

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#[derive(Debug, Clone, Copy, sqlx::Type, PartialEq, Eq, Serialize, Deserialize)]
#[sqlx(type_name = "account_type", rename_all = "lowercase")]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(
feature = "sqlx",
derive(sqlx::Type),
sqlx(type_name = "account_type", rename_all = "lowercase")
)]
pub enum AccountType {
User,
Organization,
Expand Down Expand Up @@ -79,7 +82,8 @@ impl Account {

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#[reusable(account_row_model)]
#[cfg(feature = "sqlx")]
#[reusable::reusable(account_row_model)]
#[derive(Debug, Clone, sqlx::FromRow, PartialEq, Eq)]
pub struct AccountRowModel {
pub id: AccountID,
Expand All @@ -94,12 +98,14 @@ pub struct AccountRowModel {
pub provider_identity_key: String,
}

#[reuse(account_row_model)]
#[cfg(feature = "sqlx")]
#[reusable::reuse(account_row_model)]
#[derive(Debug, Clone, sqlx::FromRow, PartialEq, Eq)]
pub struct AccountWithTokenRowModel {
pub token_hash: Vec<u8>,
}

#[cfg(feature = "sqlx")]
impl From<AccountRowModel> for Account {
fn from(value: AccountRowModel) -> Self {
Account {
Expand All @@ -117,6 +123,7 @@ impl From<AccountRowModel> for Account {
}
}

#[cfg(feature = "sqlx")]
impl From<AccountWithTokenRowModel> for Account {
fn from(value: AccountWithTokenRowModel) -> Self {
Account {
Expand Down
20 changes: 11 additions & 9 deletions src/domain/datasets/domain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,26 @@ workspace = true
doctest = false


[features]
default = []
sqlx = ["dep:sqlx"]


[dependencies]
database-common = { workspace = true }
internal-error = { workspace = true }
opendatafabric = { workspace = true, default-features = true, features = [
"sqlx",
] }
opendatafabric = { workspace = true }

aes-gcm = { version = "0.10.3" }
async-trait = { version = "0.1", default-features = false }
chrono = { version = "0.4", default-features = false }
secrecy = "0.8"
sqlx = { version = "0.7", default-features = false, features = [
"macros",
"mysql",
"sqlite",
"postgres",
] }
thiserror = { version = "1", default-features = false }
uuid = { version = "1", default-features = false, features = ["v4"] }

# Optional
sqlx = { optional = true, version = "0.7", default-features = false, features = [
"macros",
] }

[dev-dependencies]
2 changes: 2 additions & 0 deletions src/domain/datasets/domain/src/entities/dataset_env_var.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ impl DatasetEnvVar {

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#[cfg(feature = "sqlx")]
#[derive(Debug, Clone, sqlx::FromRow, PartialEq, Eq)]
pub struct DatasetEnvVarRowModel {
pub id: Uuid,
Expand All @@ -145,6 +146,7 @@ pub struct DatasetEnvVarRowModel {
pub dataset_id: DatasetID,
}

#[cfg(feature = "sqlx")]
impl From<DatasetEnvVarRowModel> for DatasetEnvVar {
fn from(value: DatasetEnvVarRowModel) -> Self {
DatasetEnvVar {
Expand Down
3 changes: 3 additions & 0 deletions src/domain/opendatafabric/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ doctest = false
default = ["arrow"]
arrow = ["dep:arrow"]
sqlx = ["dep:sqlx"]
sqlx-mysql = ["sqlx", "sqlx/mysql"]
sqlx-postgres = ["sqlx", "sqlx/postgres"]
sqlx-sqlite = ["sqlx", "sqlx/sqlite"]


[dependencies]
Expand Down
3 changes: 1 addition & 2 deletions src/domain/opendatafabric/src/identity/account_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use ed25519_dalek::SigningKey;

use crate::formats::*;
use crate::impl_sqlx;

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Expand Down Expand Up @@ -184,6 +183,6 @@ impl<'de> serde::de::Visitor<'de> for AccountIDSerdeVisitor {
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#[cfg(feature = "sqlx")]
impl_sqlx!(AccountID);
super::sqlx::impl_sqlx!(AccountID);

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
3 changes: 1 addition & 2 deletions src/domain/opendatafabric/src/identity/dataset_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use ed25519_dalek::SigningKey;

use super::{DatasetRef, DatasetRefAny, DatasetRefRemote};
use crate::formats::*;
use crate::impl_sqlx;

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Expand Down Expand Up @@ -209,6 +208,6 @@ impl<'de> serde::de::Visitor<'de> for DatasetIDSerdeVisitor {
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#[cfg(feature = "sqlx")]
impl_sqlx!(DatasetID);
super::sqlx::impl_sqlx!(DatasetID);

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
54 changes: 0 additions & 54 deletions src/domain/opendatafabric/src/identity/dataset_identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,57 +495,3 @@ impl_parse_error!(DatasetAliasRemote);
impl_parse_error!(DatasetAliasPattern);

impl_serde!(DatasetAliasRemote, DatasetAliasRemoteSerdeVisitor);

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// SQLX
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#[cfg(feature = "sqlx")]
macro_rules! impl_sqlx {
($typ:ident) => {
impl<'r, DB: sqlx::Database> sqlx::Decode<'r, DB> for $typ
where
&'r str: sqlx::Decode<'r, DB>,
{
fn decode(
value: <DB as sqlx::database::HasValueRef<'r>>::ValueRef,
) -> Result<Self, sqlx::error::BoxDynError> {
let value = <&str as sqlx::Decode<DB>>::decode(value)?;
let account_id = $typ::from_did_str(value)?;
Ok(account_id)
}
}

impl sqlx::Type<sqlx::Postgres> for $typ {
fn type_info() -> <sqlx::Postgres as sqlx::Database>::TypeInfo {
<&str as sqlx::Type<sqlx::Postgres>>::type_info()
}

fn compatible(ty: &<sqlx::Postgres as sqlx::Database>::TypeInfo) -> bool {
<&str as sqlx::Type<sqlx::Postgres>>::compatible(ty)
}
}

impl sqlx::Type<sqlx::MySql> for $typ {
fn type_info() -> <sqlx::MySql as sqlx::Database>::TypeInfo {
<&str as sqlx::Type<sqlx::MySql>>::type_info()
}

fn compatible(ty: &<sqlx::MySql as sqlx::Database>::TypeInfo) -> bool {
<&str as sqlx::Type<sqlx::MySql>>::compatible(ty)
}
}

impl sqlx::Type<sqlx::Sqlite> for $typ {
fn type_info() -> <sqlx::Sqlite as sqlx::Database>::TypeInfo {
<&str as sqlx::Type<sqlx::Sqlite>>::type_info()
}

fn compatible(ty: &<sqlx::Sqlite as sqlx::Database>::TypeInfo) -> bool {
<&str as sqlx::Type<sqlx::Sqlite>>::compatible(ty)
}
}
};
}

pub(crate) use impl_sqlx;
3 changes: 3 additions & 0 deletions src/domain/opendatafabric/src/identity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ pub use dataset_id::*;
pub use dataset_identity::*;
pub use dataset_refs::*;
pub use grammar::*;

#[cfg(feature = "sqlx")]
mod sqlx;
60 changes: 60 additions & 0 deletions src/domain/opendatafabric/src/identity/sqlx.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright Kamu Data, Inc. and contributors. All rights reserved.
//
// Use of this software is governed by the Business Source License
// included in the LICENSE file.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0.

macro_rules! impl_sqlx {
($typ:ident) => {
impl<'r, DB: sqlx::Database> sqlx::Decode<'r, DB> for $typ
where
&'r str: sqlx::Decode<'r, DB>,
{
fn decode(
value: <DB as sqlx::database::HasValueRef<'r>>::ValueRef,
) -> Result<Self, sqlx::error::BoxDynError> {
let value = <&str as sqlx::Decode<DB>>::decode(value)?;
let value = $typ::from_did_str(value)?;
Ok(value)
}
}

#[cfg(feature = "sqlx-postgres")]
impl sqlx::Type<sqlx::Postgres> for $typ {
fn type_info() -> <sqlx::Postgres as sqlx::Database>::TypeInfo {
<&str as sqlx::Type<sqlx::Postgres>>::type_info()
}

fn compatible(ty: &<sqlx::Postgres as sqlx::Database>::TypeInfo) -> bool {
<&str as sqlx::Type<sqlx::Postgres>>::compatible(ty)
}
}

#[cfg(feature = "sqlx-mysql")]
impl sqlx::Type<sqlx::MySql> for $typ {
fn type_info() -> <sqlx::MySql as sqlx::Database>::TypeInfo {
<&str as sqlx::Type<sqlx::MySql>>::type_info()
}

fn compatible(ty: &<sqlx::MySql as sqlx::Database>::TypeInfo) -> bool {
<&str as sqlx::Type<sqlx::MySql>>::compatible(ty)
}
}

#[cfg(feature = "sqlx-sqlite")]
impl sqlx::Type<sqlx::Sqlite> for $typ {
fn type_info() -> <sqlx::Sqlite as sqlx::Database>::TypeInfo {
<&str as sqlx::Type<sqlx::Sqlite>>::type_info()
}

fn compatible(ty: &<sqlx::Sqlite as sqlx::Database>::TypeInfo) -> bool {
<&str as sqlx::Type<sqlx::Sqlite>>::compatible(ty)
}
}
};
}

pub(crate) use impl_sqlx;
8 changes: 4 additions & 4 deletions src/infra/accounts/mysql/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ doctest = false

[dependencies]
database-common = { workspace = true }
kamu-accounts = { workspace = true }
opendatafabric = { workspace = true }
internal-error = { workspace = true }
kamu-accounts = { workspace = true, features = ["sqlx"] }
opendatafabric = { workspace = true, features = ["sqlx-mysql"] }

async-trait = { version = "0.1", default-features = false }
chrono = { version = "0.4", default-features = false }
Expand All @@ -34,8 +34,8 @@ sqlx = { version = "0.7", default-features = false, features = [
"runtime-tokio-rustls",
"macros",
"mysql",
"chrono"
]}
"chrono",
] }
thiserror = { version = "1", default-features = false }
tracing = { version = "0.1", default-features = false }
uuid = "1"
Expand Down
6 changes: 3 additions & 3 deletions src/infra/accounts/postgres/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ doctest = false

[dependencies]
database-common = { workspace = true }
kamu-accounts = { workspace = true }
internal-error = { workspace = true }
opendatafabric = { workspace = true }
kamu-accounts = { workspace = true, features = ["sqlx"] }
opendatafabric = { workspace = true, features = ["sqlx-postgres"] }

async-trait = { version = "0.1", default-features = false }
chrono = { version = "0.4", default-features = false }
Expand All @@ -34,7 +34,7 @@ sqlx = { version = "0.7", default-features = false, features = [
"runtime-tokio-rustls",
"macros",
"postgres",
"chrono"
"chrono",
] }
thiserror = { version = "1", default-features = false }
tracing = { version = "0.1", default-features = false }
Expand Down
Loading

0 comments on commit 96d4ef4

Please sign in to comment.