-
-
Notifications
You must be signed in to change notification settings - Fork 934
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add polymorphic owner to ApiKey (#4212)
In preparation for ApiKeys belonging to trusted publishers in addition to users Additionally, add a backfill so all ApiKeys will have an owner, and we can move to only read from that column, eventually deleting the user_id column
- Loading branch information
Showing
9 changed files
with
72 additions
and
12 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,12 @@ | ||
# frozen_string_literal: true | ||
|
||
class Maintenance::BackfillApiKeyOwnersTask < MaintenanceTasks::Task | ||
def collection | ||
ApiKey.where(owner_id: nil).or(ApiKey.where(owner_type: nil)) | ||
end | ||
|
||
def process(api_key) | ||
api_key.owner ||= api_key.user | ||
api_key.save!(validate: false) | ||
end | ||
end |
This file contains 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,7 @@ | ||
class AddOwnerToApiKeys < ActiveRecord::Migration[7.0] | ||
disable_ddl_transaction! | ||
|
||
def change | ||
add_reference :api_keys, :owner, polymorphic: true, null: true, index: {algorithm: :concurrently} | ||
end | ||
end |
This file contains 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 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
25 changes: 25 additions & 0 deletions
25
test/tasks/maintenance/backfill_api_key_owners_task_test.rb
This file contains 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,25 @@ | ||
# frozen_string_literal: true | ||
|
||
require "test_helper" | ||
|
||
class Maintenance::BackfillApiKeyOwnersTaskTest < ActiveSupport::TestCase | ||
test "#process performs a task iteration" do | ||
element = create(:api_key) | ||
element.update_columns(owner_id: nil, owner_type: nil) | ||
|
||
assert_nil element.reload.owner | ||
|
||
Maintenance::BackfillApiKeyOwnersTask.process(element) | ||
|
||
assert_equal element.reload.user, element.owner | ||
end | ||
|
||
test "#collection returns the elements to process" do | ||
create(:api_key) | ||
nil_owner = create(:api_key, key: "other") | ||
nil_owner.update_columns(owner_id: nil, owner_type: nil) | ||
|
||
assert_nil nil_owner.reload.owner | ||
assert_same_elements [nil_owner], Maintenance::BackfillApiKeyOwnersTask.collection | ||
end | ||
end |
This file contains 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