Files
sure/db/migrate/20260219200001_scope_plaid_item_uniqueness.rb
Juan José Mata 8bc4cae728 Add Plaid migration constant alias (#1235)
Restore the original migration constant name as an alias so old references still resolve during production boot and migration loading. Add a regression test covering the legacy alias.
2026-03-21 01:03:19 +01:00

33 lines
1.6 KiB
Ruby

# frozen_string_literal: true
# Scope plaid_accounts uniqueness to plaid_item so the same external account
# can be linked in multiple families. See: https://github.com/we-promise/sure/issues/740
# Class name avoids "Account" to prevent secret-scanner false positive (AWS Access ID pattern)
class ScopePlaidItemUniqueness < ActiveRecord::Migration[7.2]
def up
remove_index :plaid_accounts, name: "index_plaid_accounts_on_plaid_id", if_exists: true
return if index_exists?(:plaid_accounts, [ :plaid_item_id, :plaid_id ], unique: true, name: "index_plaid_accounts_on_item_and_plaid_id")
add_index :plaid_accounts,
[ :plaid_item_id, :plaid_id ],
unique: true,
name: "index_plaid_accounts_on_item_and_plaid_id"
end
def down
if execute("SELECT 1 FROM plaid_accounts WHERE plaid_id IS NOT NULL GROUP BY plaid_id HAVING COUNT(DISTINCT plaid_item_id) > 1 LIMIT 1").any?
raise ActiveRecord::IrreversibleMigration,
"Cannot rollback: cross-item duplicates exist in plaid_accounts. Remove duplicates first."
end
remove_index :plaid_accounts, name: "index_plaid_accounts_on_item_and_plaid_id", if_exists: true
return if index_exists?(:plaid_accounts, :plaid_id, name: "index_plaid_accounts_on_plaid_id")
add_index :plaid_accounts, :plaid_id, name: "index_plaid_accounts_on_plaid_id", unique: true
end
end
# Backwards-compatible alias for environments that may still reference the
# original migration constant derived from the old filename.
ScopePlaidAccountUniquenessToItem = ScopePlaidItemUniqueness unless defined?(ScopePlaidAccountUniquenessToItem)