Skip to content

fix(db): reject application models in migrations - #625

Open
Guflly wants to merge 2 commits into
cot-rs:masterfrom
Guflly:fix/migration-model-guard
Open

fix(db): reject application models in migrations#625
Guflly wants to merge 2 commits into
cot-rs:masterfrom
Guflly:fix/migration-model-guard

Conversation

@Guflly

@Guflly Guflly commented Jul 27, 2026

Copy link
Copy Markdown

Fixes #623. Custom migrations now reject application models before they can access the database. Migration models and raw SQL keep working. Tested with the full cot suite, the macro suite, and clippy.

@github-actions github-actions Bot added C-lib Crate: cot (main library crate) C-macros Crate: cot-macros labels Jul 27, 2026
@Guflly
Guflly marked this pull request as ready for review July 27, 2026 18:40
Comment thread cot/src/db.rs Outdated
#[derive(Debug, Clone)]
pub struct Database {
inner: Arc<DatabaseImpl>,
migration_context: bool,

@m4tx m4tx Jul 27, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Let's use enum here instead of bool. If we have something like enum DatabaseContext { Default, InMigration }, a match will be a bit easier to understand in the places where we use this field, and will also allow us to extend the set of possible contexts, if ever needed.

Comment thread cot/src/db.rs Outdated
const COLUMNS: &'static [Column];

#[doc(hidden)]
const IS_APPLICATION_MODEL: bool = true;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't think this should be #[doc(hidden)]; it's fine to expose this information. Instead of a bool, however, I think we should define a new public enum, mirroring the one we currently have in cot-codegen:

#[non_exhaustive]
pub enum ModelType {
    Application,
    Migration,
    Internal,
}

@codecov

codecov Bot commented Jul 27, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 83.87097% with 10 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
cot/src/db.rs 75.00% 0 Missing and 7 partials ⚠️
cot/src/db/migrations.rs 90.62% 1 Missing and 2 partials ⚠️
Flag Coverage Δ
rust 90.28% <83.87%> (-0.02%) ⬇️

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

Files with missing lines Coverage Δ
cot-macros/src/model.rs 96.42% <100.00%> (+0.06%) ⬆️
cot/src/db/migrations.rs 83.87% <90.62%> (+0.40%) ⬆️
cot/src/db.rs 87.70% <75.00%> (-0.50%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Guflly

Guflly commented Jul 27, 2026

Copy link
Copy Markdown
Author

Updated both. Database now uses DatabaseContext, and models expose a public non-exhaustive ModelType. The Cot and macro test suites and clippy pass.

Comment thread cot/src/db.rs
}

fn ensure_model_allowed<T: Model>(&self) -> Result<()> {
match (self.context, T::MODEL_TYPE) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Shouldn't we handle (DatabaseContext::Default, ModelType::Migration) here as well? I can't come up with a case where it makes sense 🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C-lib Crate: cot (main library crate) C-macros Crate: cot-macros

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Usage of regular models in custom migrations should not be allowed

3 participants