-
Notifications
You must be signed in to change notification settings - Fork 7
Store user_id as comment author_id for local_agent comments (COPLAN-33) #128
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
db/migrate/20260609185102_backfill_local_agent_comment_author_ids.co_plan.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| # This migration comes from co_plan (originally 20260609000000) | ||
| class BackfillLocalAgentCommentAuthorIds < ActiveRecord::Migration[8.1] | ||
| # For local_agent comments whose author_id still points at a | ||
| # coplan_api_tokens.id, rewrite it to that token's user_id. This | ||
| # pairs with the model change in engine/app/models/coplan/comment.rb: | ||
| # Comment#author now resolves via a direct find_by(id:) for both | ||
| # human and local_agent comments instead of joining coplan_api_tokens. | ||
| # | ||
| # Idempotent: once author_id holds a user UUID it won't match any | ||
| # coplan_api_tokens.id, so re-runs skip those rows. | ||
| def up | ||
| CoPlan::Comment.where(author_type: "local_agent").find_each do |comment| | ||
| token = CoPlan::ApiToken.find_by(id: comment.author_id) | ||
| next unless token | ||
|
|
||
| comment.update_columns(author_id: token.user_id) # rubocop:disable Rails/SkipsModelValidations | ||
| end | ||
| end | ||
|
|
||
| def down | ||
| # no-op (not reversible) | ||
| end | ||
| end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
engine/db/migrate/20260609000000_backfill_local_agent_comment_author_ids.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| class BackfillLocalAgentCommentAuthorIds < ActiveRecord::Migration[8.1] | ||
| # For local_agent comments whose author_id still points at a | ||
| # coplan_api_tokens.id, rewrite it to that token's user_id. This | ||
| # pairs with the model change in engine/app/models/coplan/comment.rb: | ||
| # Comment#author now resolves via a direct find_by(id:) for both | ||
| # human and local_agent comments instead of joining coplan_api_tokens. | ||
| # | ||
| # Idempotent: once author_id holds a user UUID it won't match any | ||
| # coplan_api_tokens.id, so re-runs skip those rows. | ||
| def up | ||
| CoPlan::Comment.where(author_type: "local_agent").find_each do |comment| | ||
| token = CoPlan::ApiToken.find_by(id: comment.author_id) | ||
| next unless token | ||
|
|
||
| comment.update_columns(author_id: token.user_id) # rubocop:disable Rails/SkipsModelValidations | ||
| end | ||
| end | ||
|
|
||
| def down | ||
| # no-op (not reversible) | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| require "rails_helper" | ||
|
|
||
| RSpec.describe CoPlan::CommentsHelper, type: :helper do | ||
| describe "#comment_author_name" do | ||
| it "renders 'Agent (via User)' for a local_agent comment" do | ||
| user = create(:coplan_user, name: "Alice") | ||
| comment = create(:comment, author_type: "local_agent", agent_name: "Amp", author_id: user.id) | ||
|
|
||
| expect(helper.comment_author_name(comment)).to eq("Amp (via Alice)") | ||
| end | ||
|
|
||
| it "renders just the user name for a human comment" do | ||
| user = create(:coplan_user, name: "Bob") | ||
| comment = create(:comment, author_type: "human", author_id: user.id) | ||
|
|
||
| expect(helper.comment_author_name(comment)).to eq("Bob") | ||
| end | ||
| end | ||
| end |
48 changes: 48 additions & 0 deletions
48
spec/migrations/backfill_local_agent_comment_author_ids_spec.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| require "rails_helper" | ||
| require CoPlan::Engine.root.join("db/migrate/20260609000000_backfill_local_agent_comment_author_ids.rb") | ||
|
|
||
| RSpec.describe BackfillLocalAgentCommentAuthorIds do | ||
| subject(:migration) { described_class.new } | ||
|
|
||
| before { migration.verbose = false } | ||
|
|
||
| it "rewrites local_agent author_id from a token id to the token's user_id" do | ||
| user = create(:coplan_user) | ||
| token = create(:api_token, user: user) | ||
| comment = create(:comment, author_type: "local_agent", agent_name: "Amp", author_id: token.id) | ||
|
|
||
| migration.up | ||
|
|
||
| expect(comment.reload.author_id).to eq(user.id) | ||
| expect(comment.author).to eq(user) | ||
| end | ||
|
|
||
| it "leaves human comments untouched" do | ||
| user = create(:coplan_user) | ||
| comment = create(:comment, author_type: "human", author_id: user.id) | ||
|
|
||
| migration.up | ||
|
|
||
| expect(comment.reload.author_id).to eq(user.id) | ||
| end | ||
|
|
||
| it "leaves local_agent comments whose author_id is already a user_id untouched" do | ||
| user = create(:coplan_user) | ||
| comment = create(:comment, author_type: "local_agent", agent_name: "Amp", author_id: user.id) | ||
|
|
||
| migration.up | ||
|
|
||
| expect(comment.reload.author_id).to eq(user.id) | ||
| end | ||
|
|
||
| it "is idempotent across repeated runs" do | ||
| user = create(:coplan_user) | ||
| token = create(:api_token, user: user) | ||
| comment = create(:comment, author_type: "local_agent", agent_name: "Amp", author_id: token.id) | ||
|
|
||
| migration.up | ||
| migration.up | ||
|
|
||
| expect(comment.reload.author_id).to eq(user.id) | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This backfill is added only under the host app's
db/migrate, but the code it supports is in the packaged engine. I checkedengine/coplan.gemspec, and the gem only packages files under the engine directory ({app,config,db,lib,prompts}), so upgrading a non-repo host withcoplan-enginewill get the newComment#authorbehavior without receiving this migration; existinglocal_agentcomments whoseauthor_idstill points atcoplan_api_tokens.idwill stop resolving to a user. Please put the migration inengine/db/migrate(and copy it into the host as usual) so engine consumers receive the backfill.Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch — fixed in c870c1d. Moved the backfill to
engine/db/migrate/(mirroring the existingbackfill_structured_tagsprecedent) and copied it into the host viaco_plan:install:migrations(db/migrate/...backfill_local_agent_comment_author_ids.co_plan.rb), so gem consumers now receive the backfill alongside the engineComment#authorchange.