Fix collections allowing multiple occurrences of the same user (#38636)
Some checks are pending
Bundler Audit / security (push) Waiting to run
Check i18n / check-i18n (push) Waiting to run
Chromatic / Check for relevant changes (push) Waiting to run
Chromatic / Run Chromatic (push) Blocked by required conditions
CodeQL / Analyze (actions) (push) Waiting to run
CodeQL / Analyze (javascript) (push) Waiting to run
CodeQL / Analyze (ruby) (push) Waiting to run
Check formatting / lint (push) Waiting to run
CSS Linting / lint (push) Waiting to run
Haml Linting / lint (push) Waiting to run
JavaScript Linting / lint (push) Waiting to run
Ruby Linting / lint (push) Waiting to run
JavaScript Testing / test (push) Waiting to run
Historical data migration test / test (14-alpine) (push) Waiting to run
Historical data migration test / test (15-alpine) (push) Waiting to run
Historical data migration test / test (16-alpine) (push) Waiting to run
Historical data migration test / test (17-alpine) (push) Waiting to run
Ruby Testing / build (production) (push) Waiting to run
Ruby Testing / build (test) (push) Waiting to run
Ruby Testing / test (.ruby-version) (push) Blocked by required conditions
Ruby Testing / test (3.3) (push) Blocked by required conditions
Ruby Testing / test (3.4) (push) Blocked by required conditions
Ruby Testing / End to End testing (.ruby-version) (push) Blocked by required conditions
Ruby Testing / End to End testing (3.3) (push) Blocked by required conditions
Ruby Testing / End to End testing (3.4) (push) Blocked by required conditions
Ruby Testing / Elastic Search integration testing (.ruby-version, docker.elastic.co/elasticsearch/elasticsearch:7.17.29) (push) Blocked by required conditions
Ruby Testing / Elastic Search integration testing (.ruby-version, docker.elastic.co/elasticsearch/elasticsearch:8.19.2) (push) Blocked by required conditions
Ruby Testing / Elastic Search integration testing (.ruby-version, opensearchproject/opensearch:2) (push) Blocked by required conditions
Ruby Testing / Elastic Search integration testing (3.3, docker.elastic.co/elasticsearch/elasticsearch:7.17.29) (push) Blocked by required conditions
Ruby Testing / Elastic Search integration testing (3.4, docker.elastic.co/elasticsearch/elasticsearch:7.17.29) (push) Blocked by required conditions

This commit is contained in:
Claire
2026-04-13 12:28:54 +02:00
committed by GitHub
parent 7b343c9567
commit 06a8379dce
3 changed files with 44 additions and 2 deletions

View File

@@ -27,6 +27,7 @@ class CollectionItem < ApplicationRecord
delegate :local?, :remote?, to: :collection
validates :account_id, uniqueness: { scope: :collection_id }
validates :position, numericality: { only_integer: true, greater_than: 0 }
validates :activity_uri, presence: true, if: :local_item_with_remote_account?
validates :approval_uri, presence: true, unless: -> { local? || account&.local? || !accepted? }

View File

@@ -0,0 +1,41 @@
# frozen_string_literal: true
class AddIndexToCollectionItemsAccountIdCollectionId < ActiveRecord::Migration[8.1]
disable_ddl_transaction!
def up
add_index_to_table
remove_index :collection_items, [:account_id]
end
def down
add_index :collection_items, [:account_id]
remove_index_from_table
end
private
def add_index_to_table
add_index :collection_items, [:account_id, :collection_id], unique: true, algorithm: :concurrently
rescue ActiveRecord::RecordNotUnique
remove_index_from_table
deduplicate_records
retry
end
def remove_index_from_table
remove_index :collection_items, [:account_id, :collection_id]
end
def deduplicate_records
safety_assured do
execute <<~SQL.squish
DELETE FROM collection_items
WHERE id NOT IN (
SELECT DISTINCT ON(account_id, collection_id) id FROM collection_items
ORDER BY account_id, collection_id, id ASC
)
SQL
end
end
end

View File

@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[8.1].define(version: 2026_03_26_112324) do
ActiveRecord::Schema[8.1].define(version: 2026_04_10_083500) do
# These are extensions that must be enabled in order to support this database
enable_extension "pg_catalog.plpgsql"
@@ -370,7 +370,7 @@ ActiveRecord::Schema[8.1].define(version: 2026_03_26_112324) do
t.integer "state", default: 0, null: false
t.datetime "updated_at", null: false
t.string "uri"
t.index ["account_id"], name: "index_collection_items_on_account_id"
t.index ["account_id", "collection_id"], name: "index_collection_items_on_account_id_and_collection_id", unique: true
t.index ["approval_uri"], name: "index_collection_items_on_approval_uri", unique: true, where: "(approval_uri IS NOT NULL)"
t.index ["collection_id"], name: "index_collection_items_on_collection_id"
t.index ["uri"], name: "index_collection_items_on_uri", unique: true, where: "(uri IS NOT NULL)"