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

feat(orm): add foreign key support #88

Merged
merged 9 commits into from
Jan 10, 2025
Merged

feat(orm): add foreign key support #88

merged 9 commits into from
Jan 10, 2025

Conversation

m4tx
Copy link
Member

@m4tx m4tx commented Dec 20, 2024

This also contains a general ORM refactor:

  • Auto enum was introduced instead of implying if a field is autoincrement based on its name
  • save() now actually modifies the saved model with newly assigned autoincrement primary keys (if any)
  • A separate SELECT query is performed after INSERT for MySQL to obtain the auto-generated values because MySQL doesn't support the RETURNING clause.

@m4tx m4tx requested a review from seqre December 20, 2024 21:14
Copy link
Member

@seqre seqre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like a good start

flareon-cli/tests/migration_generator.rs Outdated Show resolved Hide resolved
flareon-codegen/src/model.rs Show resolved Hide resolved
flareon-codegen/src/model.rs Outdated Show resolved Hide resolved
flareon-codegen/src/model.rs Show resolved Hide resolved
todo.md Outdated Show resolved Hide resolved
flareon/src/db.rs Show resolved Hide resolved
flareon/tests/db.rs Show resolved Hide resolved
@m4tx
Copy link
Member Author

m4tx commented Jan 7, 2025

I'm investigating the build failure, it builds successfully in current Rust beta and nightly... Wondering if rust-lang/rust#130654 might be the reason it's fixed.

If I won't get anywhere, I would consider waiting two more days for Rust 1.84 to hit stable and just merge this as-is. We don't really care about this project working on old stable compiler versions anyways... (yet!)

@m4tx
Copy link
Member Author

m4tx commented Jan 7, 2025

Ah yes, seems like indeed what I've linked is the fix for the issue. There's a blogpost about this: https://blog.rust-lang.org/inside-rust/2023/12/22/trait-system-refactor-initiative.html

I think these trait impls are problematic:

impl<T: DatabaseField> ToDbFieldValue for Auto<T> {}
impl<T: DatabaseField> ToDbFieldValue for Option<Auto<T>> {}
impl<T: Model + Send + Sync> ToDbFieldValue for ForeignKey<T> {}
impl<T: Model + Send + Sync> ToDbFieldValue for Option<ForeignKey<T>> {}

(or some subset of them)

I don't really want to remove them though, since they make implementing new DatabaseField types much easier (and it's nice to have shiny new features if we're writing a shiny new framework anyway — modern codebase will be one of our advantages over other similar projects). Like I said in my previous comment, I'd just wait for Rust 1.84 to hit stable (9 Jan, so we're almost there) and just run CI on beta until then. @seqre what do you think?

@m4tx m4tx force-pushed the foreign-keys branch 2 times, most recently from c19c3c1 to 4f30fbe Compare January 7, 2025 22:25
@m4tx m4tx marked this pull request as ready for review January 7, 2025 22:35
Copy link

codecov bot commented Jan 7, 2025

Codecov Report

Attention: Patch coverage is 94.17040% with 117 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
flareon-cli/src/migration_generator.rs 92.93% 32 Missing ⚠️
flareon-macros/src/query.rs 9.52% 19 Missing ⚠️
flareon-codegen/src/model.rs 93.18% 15 Missing ⚠️
flareon/src/db.rs 89.14% 14 Missing ⚠️
flareon-codegen/src/expr.rs 93.93% 10 Missing ⚠️
flareon/src/db/query.rs 76.00% 6 Missing ⚠️
flareon-codegen/src/symbol_resolver.rs 98.87% 4 Missing ⚠️
flareon/src/db/migrations.rs 92.59% 4 Missing ⚠️
flareon/src/db/migrations/sorter.rs 73.33% 4 Missing ⚠️
flareon/src/db/relations.rs 95.45% 4 Missing ⚠️
... and 2 more
Flag Coverage Δ
rust 86.90% <94.17%> (+2.91%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
flareon-macros/src/model.rs 95.76% <100.00%> (+1.53%) ⬆️
flareon-macros/tests/compile_tests.rs 100.00% <100.00%> (ø)
flareon/src/auth.rs 94.04% <100.00%> (ø)
flareon/src/db/impl_mysql.rs 100.00% <100.00%> (ø)
flareon/src/db/impl_postgres.rs 100.00% <100.00%> (ø)
flareon/src/db/impl_sqlite.rs 100.00% <100.00%> (ø)
flareon/src/db/sea_query_db.rs 100.00% <100.00%> (ø)
flareon/src/lib.rs 57.57% <ø> (ø)
flareon/src/utils/graph.rs 100.00% <100.00%> (ø)
flareon/tests/db.rs 99.40% <100.00%> (+0.74%) ⬆️
... and 12 more

@m4tx m4tx force-pushed the foreign-keys branch 2 times, most recently from d082bb1 to a6cdd82 Compare January 7, 2025 22:50
@m4tx
Copy link
Member Author

m4tx commented Jan 9, 2025

If I won't get anywhere, I would consider waiting two more days for Rust 1.84 to hit stable and just merge this as-is. We don't really care about this project working on old stable compiler versions anyways... (yet!)

1.84 has been released today, so we're back on stable.

@seqre
Copy link
Member

seqre commented Jan 10, 2025

I don't really want to remove them though, since they make implementing new DatabaseField types much easier (and it's nice to have shiny new features if we're writing a shiny new framework anyway — modern codebase will be one of our advantages over other similar projects). (...) @seqre what do you think?

I agree, since this is a new framework with no existing users, I see no point in supporting older compiler versions. Once it gains popularity, we can think about supporting N-X versions.

Copy link
Member

@seqre seqre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM overall, some notes for possible improvements and nitpicks

flareon-cli/src/migration_generator.rs Outdated Show resolved Hide resolved
flareon-cli/src/migration_generator.rs Outdated Show resolved Hide resolved
flareon-cli/src/migration_generator.rs Outdated Show resolved Hide resolved
flareon-cli/src/migration_generator.rs Show resolved Hide resolved
flareon-codegen/src/expr.rs Show resolved Hide resolved
flareon-codegen/src/expr.rs Show resolved Hide resolved
flareon/src/auth/db/migrations/m_0001_initial.rs Outdated Show resolved Hide resolved
flareon/src/db.rs Outdated Show resolved Hide resolved
flareon/src/utils/graph.rs Show resolved Hide resolved
flareon/tests/db.rs Show resolved Hide resolved
Copy link
Member

@seqre seqre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work!

@m4tx m4tx merged commit b228d0b into master Jan 10, 2025
25 checks passed
@m4tx m4tx deleted the foreign-keys branch January 10, 2025 15:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants