Skip to content

Commit

Permalink
chore: more fixes, more docs
Browse files Browse the repository at this point in the history
  • Loading branch information
m4tx committed Jan 8, 2025
1 parent 0d81872 commit 4081a59
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 25 deletions.
26 changes: 7 additions & 19 deletions flareon-cli/src/migration_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ impl MigrationGenerator {
migration.toposort_operations();
migration
.dependencies
.extend(migration.get_foreign_key_dependencies(&self.crate_name));
.extend(migration.get_foreign_key_dependencies());

Ok(Some(migration))
}
Expand Down Expand Up @@ -527,7 +527,7 @@ pub struct SourceFile {

impl SourceFile {
#[must_use]
pub fn new(path: PathBuf, content: syn::File) -> Self {
fn new(path: PathBuf, content: syn::File) -> Self {
assert!(
path.is_relative(),
"path must be relative to the src directory"
Expand Down Expand Up @@ -666,7 +666,7 @@ pub struct GeneratedMigration {
}

impl GeneratedMigration {
fn get_foreign_key_dependencies(&self, crate_name: &str) -> Vec<DynDependency> {
fn get_foreign_key_dependencies(&self) -> Vec<DynDependency> {
let create_ops = self.get_create_ops_map();
let ops_adding_foreign_keys = self.get_ops_adding_foreign_keys();

Expand Down Expand Up @@ -977,20 +977,8 @@ impl Repr for DynOperation {
fn repr(&self) -> TokenStream {
match self {
Self::CreateModel {
table_name,
model_ty,
fields,
..
table_name, fields, ..
} => {
let model_name = match model_ty {
syn::Type::Path(syn::TypePath { path, .. }) => path
.segments
.last()
.expect("TypePath must have at least one segment")
.ident
.to_string(),
_ => unreachable!("model_ty is expected to be a TypePath"),
};
let fields = fields.iter().map(Repr::repr).collect::<Vec<_>>();
quote! {
::flareon::db::migrations::Operation::create_model()
Expand Down Expand Up @@ -1265,7 +1253,7 @@ mod tests {
}],
};

let external_dependencies = migration.get_foreign_key_dependencies("my_crate");
let external_dependencies = migration.get_foreign_key_dependencies();
assert!(external_dependencies.is_empty());
}

Expand All @@ -1292,7 +1280,7 @@ mod tests {
}],
};

let external_dependencies = migration.get_foreign_key_dependencies("my_crate");
let external_dependencies = migration.get_foreign_key_dependencies();
assert_eq!(external_dependencies.len(), 1);
assert_eq!(
external_dependencies[0],
Expand Down Expand Up @@ -1342,7 +1330,7 @@ mod tests {
],
};

let external_dependencies = migration.get_foreign_key_dependencies("my_crate");
let external_dependencies = migration.get_foreign_key_dependencies();
assert_eq!(external_dependencies.len(), 2);
assert!(external_dependencies.contains(&DynDependency::Model {
model_type: parse_quote!(my_crate::Table2),
Expand Down
3 changes: 1 addition & 2 deletions flareon/src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ use subtle::ConstantTimeEq;
use thiserror::Error;

use crate::config::SecretKey;
use crate::db::DbValue;
#[cfg(feature = "db")]
use crate::db::{ColumnType, DatabaseField, FromDbValue, SqlxValueRef, ToDbValue};
use crate::db::{ColumnType, DatabaseField, DbValue, FromDbValue, SqlxValueRef, ToDbValue};
use crate::request::{Request, RequestExt};

#[derive(Debug, Error)]
Expand Down
16 changes: 13 additions & 3 deletions flareon/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ impl DatabaseError {
}
}

/// An alias for [`Result`] that uses [`DatabaseError`] as the error type.
pub type Result<T> = std::result::Result<T, DatabaseError>;

/// A model trait for database models.
Expand Down Expand Up @@ -211,6 +212,8 @@ impl Column {
}
}

/// A marker trait that denotes that a type can be used as a primary key in a
/// database.
pub trait PrimaryKey: DatabaseField + Clone {}

/// A row structure that holds the data of a single row retrieved from the
Expand Down Expand Up @@ -749,7 +752,11 @@ impl Database {

fn supports_returning(&self) -> bool {
match self.inner {
DatabaseImpl::Sqlite(_) | DatabaseImpl::Postgres(_) => true,
#[cfg(feature = "sqlite")]
DatabaseImpl::Sqlite(_) => true,
#[cfg(feature = "postgres")]
DatabaseImpl::Postgres(_) => true,
#[cfg(feature = "mysql")]
DatabaseImpl::MySql(_) => false,
}
}
Expand Down Expand Up @@ -913,7 +920,7 @@ pub struct RowsNum(pub u64);
/// ```
/// use flareon::db::{model, Auto, Model};
/// # use flareon::db::migrations::{Field, Operation};
/// # use flareon::db::{Database, Identifier};
/// # use flareon::db::{Database, Identifier, DatabaseField};
/// # use flareon::Result;
///
/// #[model]
Expand All @@ -925,7 +932,7 @@ pub struct RowsNum(pub u64);
/// # async fn main() -> Result<()> {
///
/// # const OPERATION: Operation = Operation::create_model()
/// # .table_name(Identifier::new("todoapp__my_model"))
/// # .table_name(Identifier::new("my_model"))
/// # .fields(&[
/// # Field::new(Identifier::new("id"), <i32 as DatabaseField>::TYPE)
/// # .primary_key()
Expand Down Expand Up @@ -955,6 +962,7 @@ impl<T> Auto<T> {
/// Creates a new `Auto` instance that is automatically generated by the
/// database.
#[must_use]
#[allow(clippy::self_named_constructors)]
pub const fn auto() -> Self {
Self::Auto
}
Expand Down Expand Up @@ -1025,6 +1033,8 @@ impl<const LIMIT: u32> PartialEq<LimitedString<LIMIT>> for String {
}
}

/// An error returned by [`LimitedString::new`] when the string is longer than
/// the specified limit.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Error)]
#[error("string is too long ({length} > {LIMIT})")]
pub struct NewLimitedStringError<const LIMIT: u32> {
Expand Down
6 changes: 6 additions & 0 deletions flareon/src/db/fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,20 +288,23 @@ impl<T: DatabaseField> DatabaseField for Auto<T> {
}

impl<T: DatabaseField> FromDbValue for Auto<T> {
#[cfg(feature = "sqlite")]
fn from_sqlite(value: SqliteValueRef) -> Result<Self>
where
Self: Sized,
{
Ok(Self::fixed(T::from_sqlite(value)?))
}

#[cfg(feature = "postgres")]
fn from_postgres(value: PostgresValueRef) -> Result<Self>
where
Self: Sized,
{
Ok(Self::fixed(T::from_postgres(value)?))
}

#[cfg(feature = "mysql")]
fn from_mysql(value: MySqlValueRef) -> Result<Self>
where
Self: Sized,
Expand All @@ -323,20 +326,23 @@ impl<T: DatabaseField> FromDbValue for Option<Auto<T>>
where
Option<T>: FromDbValue,
{
#[cfg(feature = "sqlite")]
fn from_sqlite(value: SqliteValueRef) -> Result<Self>
where
Self: Sized,
{
<Option<T>>::from_sqlite(value).map(|value| value.map(Auto::fixed))
}

#[cfg(feature = "postgres")]
fn from_postgres(value: PostgresValueRef) -> Result<Self>
where
Self: Sized,
{
<Option<T>>::from_postgres(value).map(|value| value.map(Auto::fixed))
}

#[cfg(feature = "mysql")]
fn from_mysql(value: MySqlValueRef) -> Result<Self>
where
Self: Sized,
Expand Down
2 changes: 1 addition & 1 deletion flareon/src/db/relations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ impl From<ForeignKeyOnDeletePolicy> for sea_query::ForeignKeyAction {
}
}

/// A foreign key on delete constraint.
/// A foreign key on update constraint.
///
/// This is used to define the behavior of a foreign key when the referenced row
/// is updated.
Expand Down

0 comments on commit 4081a59

Please sign in to comment.