Skip to content

feat/add-popular-tasks - #1708

Open
floscodes wants to merge 22 commits into
loco-rs:feat/add-popular-tasksfrom
floscodes:feat/add-popular-tasks
Open

feat/add-popular-tasks#1708
floscodes wants to merge 22 commits into
loco-rs:feat/add-popular-tasksfrom
floscodes:feat/add-popular-tasks

Conversation

@floscodes

Copy link
Copy Markdown

This PR adds a user deletion functionality to popular tasks.

Comment thread loco-new/base_template/src/tasks/user_delete.rs Outdated
Comment thread loco-new/base_template/src/tasks/user_delete.rs Outdated
Comment thread loco-new/base_template/tests/tasks/user_delete.rs.t Outdated
Comment thread loco-new/base_template/src/tasks/user_create.rs
@floscodes

floscodes commented Dec 22, 2025

Copy link
Copy Markdown
Author

Hi @kaplanelad,

I refactored the code and fixed the remaining issues.

Comment thread loco-new/base_template/src/tasks/user_delete.rs Outdated
Comment thread loco-new/base_template/src/tasks/user_delete.rs Outdated
Comment thread loco-new/base_template/src/tasks/user_delete.rs Outdated
@floscodes

Copy link
Copy Markdown
Author

Hi @kaplanelad,

is it ok like this?

Comment thread loco-new/base_template/src/tasks/user_delete.rs Outdated
Comment thread loco-new/base_template/src/tasks/user_delete.rs Outdated
Comment thread loco-new/base_template/src/tasks/user_delete.rs Outdated
@kaplanelad

Copy link
Copy Markdown
Contributor

Hi @kaplanelad,

is it ok like this?

looks better. thanks.
can you check the ci?

@floscodes
floscodes requested a review from kaplanelad January 2, 2026 09:33
@floscodes

Copy link
Copy Markdown
Author

Hi @kaplanelad,

just a quick heads-up: the CI is now passing and all tests are running successfully.

Thank you!

@kaplanelad

Copy link
Copy Markdown
Contributor

How group related to delete user task?

@floscodes

floscodes commented Jan 8, 2026

Copy link
Copy Markdown
Author

The delete-method from the ActiveModelTrait requires the groups table to exist:

let _deleted_user = user_to_delete
            .into_active_model()
            .delete(&app_context.db)
            .await
            .map_err(|err| {
                tracing::error!(message = err.to_string(), "could not delete user");
                Error::string(&format!("Failed to delete user. err: {err}",))
            })?;

otherwise the test fails with the following error:

Execution Error: error returned from database:
(code: 1) no such table: main.groups

That's why I had to add the groups migration file.

this seems to be caused by the following test:

tester.run_generate_migration(&vec![
    "CreateJoinTableUsersAndGroups",
    "count:int",
]);

@kaplanelad

Copy link
Copy Markdown
Contributor

From where the group came from?

@floscodes

Copy link
Copy Markdown
Author

From where the group came from?

That also caught my attention 🙂

I didn’t intentionally introduce groups on my side.

From what I could tell, the groups table is implicitly required by the ActiveModel::delete implementation. When the table is missing, the delete call fails at runtime, which is what caused the test to break.

I traced this back to the CreateJoinTableUsersAndGroups migration generated in the test. That seems to be where the dependency comes from, even though groups itself isn’t created explicitly beforehand.

The migration was added purely to make the test pass and avoid the runtime error — happy to change this if there’s a more intended approach 👍

@kaplanelad

Copy link
Copy Markdown
Contributor

Please clarify where the groups migration comes from specifically, explain that ActiveModelTrait::delete requires all related tables to exist. this is not clear to me

Also, please remove the commented-out test code in new.rs not sure if why it commended

@floscodes

Copy link
Copy Markdown
Author

Hi @kaplanelad,

sorry for the delay.

It looks like the groups table may originate from one of the wizard tests. In loco/loco-new/tests/wizard/new.rs, the function test_combination calls:

tester.run_generate_migration(&vec!["CreateJoinTableUsersAndGroups", "count:int"]);

Inside run_generate_migration, the base command is constructed with:

let base_command = vec!["loco", "generate", "migration"];

and then extended with CreateJoinTableUsersAndGroups. The migration is executed via self.run_migrate, which during the test run creates a join table between users and groups.

From what I can see, creating a user in the wizard task works without issues. However, when the user is deleted, the presence of the groups table seems to become relevant. My assumption is that the wizard cannot find the corresponding migration in the base template, which may be why the deletion step fails. As a result, the wizard test appears to implicitly require the groups table to exist when deleting a user.

One possible explanation could also be related to how the ORM handles relations. Creating a user only inserts into the users table, so the join table might be not required at that stage. However, when deleting a user, the ORM may attempt to handle related records (for example in a users_groups join table). If those related tables do not exist, the deletion step could fail even though the insert worked correctly.

Please let me know if I might be misunderstanding something here.

Best regards!

jondot added a commit that referenced this pull request Jul 29, 2026
Verified 4 triage PRs + onboarding-cluster issues against the branch: 5
fixed-in-1.0.0 (#1758/#1749, #1770/#1759, #1768, #1729), #1755 was a real bug
(fixed 47d0391), #1708 defer, #1771 partial-adopt (11ce376). Finished reply
drafts in RELEASE-1.0.0-verification-replies.md. Green gate updated to run
loco-gen --all-features (default features masked stale int→i64 snapshots).
@jondot

jondot commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

Hi @floscodes — thanks for sticking with this. user:delete alongside user:create is a genuinely nice addition and I'd like to land it, but not as-is for 1.0: adding a whole groups table to every generated app's schema to work around a test failure — without knowing why the test needed it — isn't something we can commit to at a 1.0 stability boundary. I think the real fix is on the wizard-test side (the CreateJoinTableUsersAndGroups step shouldn't create an implicit runtime dependency for an unrelated task test), not in the generated app. Could you check whether ActiveModelTrait::delete on users::Model actually touches a groups relation, or if the test is just bleeding state from a prior wizard step? Also, run_test()'s assertion got commented out in favor of a println! — that masks the failure, so let's not merge that. I'm going to defer this past 1.0 rather than adopt it now, but I'd love to bring user:delete in right after, once the groups mystery is actually resolved. Appreciate the persistence!

@jondot jondot added this to the 1.1.0 milestone Jul 29, 2026
The generated app never needed a `groups` table: users::Model has no
relation to it, and the user:delete task test doesn't reference it
either. The real dependency came from the pre-existing wizard-test
step generating a CreateJoinTableUsersAndGroups migration, whose
FK reference to a non-existent `groups` table failed at migrate time
- unrelated to user:delete. Point that step at `movies` instead, which
already exists by then, and revert the groups migration added to
base_template as a workaround.

Also restore the run_test() assertion after project generation instead
of the println! that was silently swallowing failures.
@floscodes

Copy link
Copy Markdown
Author

Hi, @jondot, thanks for the detailed feedback — you were right to push back, and I found the actual root cause.

ActiveModelTrait::delete on users::Model doesn't touch a groups relation at all (the generated entity has an empty Relation enum), and the user:delete task test doesn't reference groups either. The dependency was coming from somewhere else entirely: the wizard test already had a CreateJoinTableUsersAndGroups migration-generator step (predates this PR), which produces a migration with FK references to both users and groups. Since no app in this flow ever has a real groups table, cargo loco db migrate failed on that unrelated step — that's the error I was working around, not anything caused by user:delete.

I've pushed a fix that:

  • Reverts the groups migration/table I'd added to base_template as a workaround (removed from migration/src/lib.rs.t, setup.rhai, and deleted m20220101_000002_groups.rs)
  • Points that wizard-test step at CreateJoinTableUsersAndMovies instead, since movies already exists by that point in the flow (generated and auto-migrated a few steps earlier) — keeps the same coverage for the join-table naming inference without an implicit, unrelated dependency
  • Restores the run_test().expect(...) assertion after project generation instead of the println! that was masking failures

So user:delete no longer touches the generated app's schema at all. Let me know if you'd like me to split this into a separate PR against the wizard test first, or if it's fine bundled here with user:delete/user:create.

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.

3 participants