Skip to content

Commit

Permalink
Shield SeaORM: Fix MySQL and PostgreSQL support
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielleHuisman committed Dec 14, 2024
1 parent b5b93fb commit 5c8c30e
Show file tree
Hide file tree
Showing 12 changed files with 197 additions and 67 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -162,5 +162,5 @@ target/
# mdBook
book/book/

# Tailwind CSS ouput
tailwind.output.css
# SeaORM
packages/storage/shield-seaorm/src/entities_template/
19 changes: 19 additions & 0 deletions compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
services:
mysql:
image: mysql
ports:
- 13306:3306
environment:
MYSQL_ROOT_PASSWORD: shield
MYSQL_USER: shield
MYSQL_PASSWORD: shield
MYSQL_DATABASE: shield

postgresql:
image: postgres
ports:
- 15432:5432
environment:
POSTGRES_USER: shield
POSTGRES_PASSWORD: shield
POSTGRES_DB: shield
8 changes: 6 additions & 2 deletions examples/seaorm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@ async-std = { workspace = true, features = ["attributes", "tokio1"] }
sea-orm = { workspace = true, features = [
"macros",
"runtime-tokio-native-tls",
"sqlx-sqlite",
"sqlx-mysql",
"sqlx-postgres",
"sqlx-sqlite"
] }
sea-orm-migration = { workspace = true, features = [
"runtime-tokio-native-tls",
"sqlx-sqlite",
"sqlx-mysql",
"sqlx-postgres",
"sqlx-sqlite"
] }
shield-seaorm = { path = "../../packages/storage/shield-seaorm", features = [
"all-providers",
Expand Down
8 changes: 7 additions & 1 deletion packages/storage/shield-seaorm/development.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
```
sea-orm-cli migrate generate -d packages/storage/shield-seaorm/src/migrations/<directory> <name>
docker compose up -d
sea-orm-cli migrate fresh -u "mysql://shield:shield@localhost:13306/shield" -d ./examples/seaorm
sea-orm-cli migrate fresh -u "postgres://shield:shield@localhost:15432/shield" -d ./examples/seaorm
sea-orm-cli migrate fresh -u "sqlite:///tmp/shield-seaorm.sqlite?mode=rwc" -d ./examples/seaorm
sea-orm-cli generate entity -u "sqlite:///tmp/shield-seaorm.sqlite?mode=rwc" -o packages/storage/shield-seaorm/src/entities_template --with-serde both
sea-orm-cli generate entity -u "mysql://shield:shield@localhost:13306/shield" -o packages/storage/shield-seaorm/src/entities_template/mysql --with-serde both
sea-orm-cli generate entity -u "postgres://shield:shield@localhost:15432/shield" -o packages/storage/shield-seaorm/src/entities_template/postgresql --with-serde both
sea-orm-cli generate entity -u "sqlite:///tmp/shield-seaorm.sqlite?mode=rwc" -o packages/storage/shield-seaorm/src/entities_template/sqlite --with-serde both
```
3 changes: 1 addition & 2 deletions packages/storage/shield-seaorm/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ impl BaseTable {
.not_null()
.primary_key()
.default(match manager.get_database_backend() {
DatabaseBackend::MySql => Expr::cust("uuid()"),
DatabaseBackend::MySql | DatabaseBackend::Sqlite => Expr::cust("(uuid())"),
DatabaseBackend::Postgres => PgFunc::gen_random_uuid().into(),
DatabaseBackend::Sqlite => Expr::cust("(uuid())"),
}),
)
.col(
Expand Down
48 changes: 42 additions & 6 deletions packages/storage/shield-seaorm/src/entities/oauth_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,45 @@
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, PartialEq, Eq, EnumIter, DeriveActiveEnum, Serialize, Deserialize)]
#[sea_orm(
rs_type = "String",
db_type = "Enum",
enum_name = "oauth_provider_pkce_code_challenge"
)]
pub enum OauthProviderPkceCodeChallenge {
#[sea_orm(string_value = "none")]
None,
#[sea_orm(string_value = "plain")]
Plain,
#[sea_orm(string_value = "s256")]
S256,
}

#[derive(Debug, Clone, PartialEq, Eq, EnumIter, DeriveActiveEnum, Serialize, Deserialize)]
#[sea_orm(
rs_type = "String",
db_type = "Enum",
enum_name = "oauth_provider_type"
)]
pub enum OauthProviderType {
#[sea_orm(string_value = "custom")]
Custom,
}

#[derive(Debug, Clone, PartialEq, Eq, EnumIter, DeriveActiveEnum, Serialize, Deserialize)]
#[sea_orm(
rs_type = "String",
db_type = "Enum",
enum_name = "oauth_provider_visibility"
)]
pub enum OauthProviderVisibility {
#[sea_orm(string_value = "private")]
Private,
#[sea_orm(string_value = "public")]
Public,
}

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
#[sea_orm(table_name = "oauth_provider")]
pub struct Model {
Expand All @@ -12,10 +51,8 @@ pub struct Model {
pub updated_at: DateTimeUtc,
pub name: String,
pub slug: Option<String>,
#[sea_orm(column_type = "custom(\"enum_text\")")]
pub r#type: String,
#[sea_orm(column_type = "custom(\"enum_text\")")]
pub visibility: String,
pub r#type: OauthProviderType,
pub visibility: OauthProviderVisibility,
#[sea_orm(column_type = "Text")]
pub client_id: String,
#[sea_orm(column_type = "Text", nullable)]
Expand All @@ -40,8 +77,7 @@ pub struct Model {
pub revocation_url: Option<String>,
#[sea_orm(column_type = "Text", nullable)]
pub revocation_url_params: Option<String>,
#[sea_orm(column_type = "custom(\"enum_text\")")]
pub pcke_code_challenge: String,
pub pkce_code_challenge: OauthProviderPkceCodeChallenge,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ pub struct Model {
pub id: Uuid,
pub created_at: DateTimeUtc,
pub updated_at: DateTimeUtc,
#[sea_orm(column_type = "Text")]
pub identifier: String,
#[sea_orm(column_type = "Text")]
pub token_type: String,
Expand Down
44 changes: 38 additions & 6 deletions packages/storage/shield-seaorm/src/entities/oidc_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,41 @@
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, PartialEq, Eq, EnumIter, DeriveActiveEnum, Serialize, Deserialize)]
#[sea_orm(
rs_type = "String",
db_type = "Enum",
enum_name = "oidc_provider_pkce_code_challenge"
)]
pub enum OidcProviderPkceCodeChallenge {
#[sea_orm(string_value = "none")]
None,
#[sea_orm(string_value = "plain")]
Plain,
#[sea_orm(string_value = "s256")]
S256,
}

#[derive(Debug, Clone, PartialEq, Eq, EnumIter, DeriveActiveEnum, Serialize, Deserialize)]
#[sea_orm(rs_type = "String", db_type = "Enum", enum_name = "oidc_provider_type")]
pub enum OidcProviderType {
#[sea_orm(string_value = "custom")]
Custom,
}

#[derive(Debug, Clone, PartialEq, Eq, EnumIter, DeriveActiveEnum, Serialize, Deserialize)]
#[sea_orm(
rs_type = "String",
db_type = "Enum",
enum_name = "oidc_provider_visibility"
)]
pub enum OidcProviderVisibility {
#[sea_orm(string_value = "private")]
Private,
#[sea_orm(string_value = "public")]
Public,
}

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
#[sea_orm(table_name = "oidc_provider")]
pub struct Model {
Expand All @@ -12,10 +47,8 @@ pub struct Model {
pub updated_at: DateTimeUtc,
pub name: String,
pub slug: Option<String>,
#[sea_orm(column_type = "custom(\"enum_text\")")]
pub r#type: String,
#[sea_orm(column_type = "custom(\"enum_text\")")]
pub visibility: String,
pub r#type: OidcProviderType,
pub visibility: OidcProviderVisibility,
#[sea_orm(column_type = "Text")]
pub client_id: String,
#[sea_orm(column_type = "Text", nullable)]
Expand Down Expand Up @@ -48,8 +81,7 @@ pub struct Model {
pub json_web_key_set_url: Option<String>,
#[sea_orm(column_type = "JsonBinary", nullable)]
pub json_web_key_set: Option<Json>,
#[sea_orm(column_type = "custom(\"enum_text\")")]
pub pcke_code_challenge: String,
pub pkce_code_challenge: OidcProviderPkceCodeChallenge,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ pub struct Model {
pub id: Uuid,
pub created_at: DateTimeUtc,
pub updated_at: DateTimeUtc,
#[sea_orm(column_type = "Text")]
pub identifier: String,
#[sea_orm(column_type = "Text")]
pub token_type: String,
Expand Down
9 changes: 7 additions & 2 deletions packages/storage/shield-seaorm/src/entities/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@ pub use super::entity::Entity;
pub use super::email_auth_token::Entity as EmailAuthToken;

#[cfg(feature = "provider-oauth")]
pub use super::oauth_provider::Entity as OauthProvider;
pub use super::oauth_provider::{
Entity as OauthProvider, OauthProviderPkceCodeChallenge, OauthProviderType,
OauthProviderVisibility,
};
#[cfg(feature = "provider-oauth")]
pub use super::oauth_provider_connection::Entity as OauthProviderConnection;

#[cfg(feature = "provider-oidc")]
pub use super::oidc_provider::Entity as OidcProvider;
pub use super::oidc_provider::{
Entity as OidcProvider, OidcProviderPkceCodeChallenge, OidcProviderType, OidcProviderVisibility,
};
#[cfg(feature = "provider-oidc")]
pub use super::oidc_provider_connection::Entity as OidcProviderConnection;
Loading

0 comments on commit 5c8c30e

Please sign in to comment.