mirror of
https://github.com/we-promise/sure.git
synced 2026-04-19 20:14:08 +00:00
* feat: scope Mercury account uniqueness to mercury_item * feat: extend to all other providers * fix: add uniqueness test * fix: lint * fix: test * fix: coderabbit comment * fix: coderabbit comment * fix: coderabbit comment * fix: update * fix: lint * fix: update * fix: update
30 lines
1.4 KiB
Ruby
30 lines
1.4 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
class ScopeMercuryAccountUniquenessToItem < ActiveRecord::Migration[7.2]
|
|
def up
|
|
# Allow the same Mercury account_id to be linked by different families (different mercury_items).
|
|
# Uniqueness is scoped per mercury_item, mirroring simplefin_accounts.
|
|
remove_index :mercury_accounts, name: "index_mercury_accounts_on_account_id", if_exists: true
|
|
unless index_exists?(:mercury_accounts, [ :mercury_item_id, :account_id ], unique: true, name: "index_mercury_accounts_on_item_and_account_id")
|
|
add_index :mercury_accounts,
|
|
[ :mercury_item_id, :account_id ],
|
|
unique: true,
|
|
name: "index_mercury_accounts_on_item_and_account_id"
|
|
end
|
|
end
|
|
|
|
def down
|
|
if MercuryAccount.group(:account_id).having("COUNT(*) > 1").exists?
|
|
raise ActiveRecord::IrreversibleMigration,
|
|
"Cannot restore global unique index on mercury_accounts.account_id: " \
|
|
"duplicate account_id values exist across mercury_items. " \
|
|
"Remove duplicates first before rolling back."
|
|
end
|
|
|
|
remove_index :mercury_accounts, name: "index_mercury_accounts_on_item_and_account_id", if_exists: true
|
|
unless index_exists?(:mercury_accounts, :account_id, name: "index_mercury_accounts_on_account_id")
|
|
add_index :mercury_accounts, :account_id, name: "index_mercury_accounts_on_account_id", unique: true
|
|
end
|
|
end
|
|
end
|