Files
OpenFUT-Core/migrations/0024_club_kit_assignments.sql
T
funman300 f0550e2ae1
CI / Build, lint & test (push) Successful in 2m50s
feat(club): persist active home/away kit assignments
Kits are ownership-backed club items: the owned instance stays in the
generic owned_cards inventory and only the two active roles get their own
table. This mirrors the squad_managers precedent and keeps every
FIFA-specific resourceId/wire concern in the game adapter.

* migration 0024: club_kit_assignments(club_id, slot, owned_card_id) with a
  UNIQUE owned_card_id (one instance cannot hold both roles) and
  ON DELETE CASCADE from owned_cards so a quick-sell clears the role.
* a BEFORE UPDATE OF club_id trigger clears the designation on a market
  transfer, which moves ownership by UPDATE and so is not covered by the
  cascade.
* set_active_club_kits replaces BOTH slots in one transaction, rejects
  home == away, and validates each instance against current club ownership,
  so a half-applied or dangling designation is not representable.
* get_active_club_kits revalidates ownership on read, so a stale row can
  never surface another club's item.
* GET/PUT /club/kits expose the pair.

Tests cover restart persistence, replace/clear without duplicates, atomic
rejection of invalid references, and clearing via delete and transfer.
2026-08-21 03:17:40 +00:00

24 lines
1.1 KiB
SQL

-- Active home/away kits for a club. Ownership remains the generic owned_cards
-- inventory; this table only records which owned instances occupy the two kit
-- roles. FIFA-specific resource ids and wire shapes stay in the FIFA17 adapter.
CREATE TABLE IF NOT EXISTS club_kit_assignments (
club_id TEXT NOT NULL REFERENCES clubs(id) ON DELETE CASCADE,
slot TEXT NOT NULL CHECK (slot IN ('home', 'away')),
owned_card_id TEXT NOT NULL UNIQUE REFERENCES owned_cards(id) ON DELETE CASCADE,
updated_at TEXT NOT NULL,
PRIMARY KEY (club_id, slot)
);
CREATE INDEX IF NOT EXISTS idx_club_kit_assignments_owned
ON club_kit_assignments(owned_card_id);
-- Market transfers change owned_cards.club_id by UPDATE rather than DELETE.
-- Remove any old-club active designation before ownership moves so a stale row
-- cannot hide or block the item for its new owner.
CREATE TRIGGER IF NOT EXISTS clear_club_kit_assignment_before_transfer
BEFORE UPDATE OF club_id ON owned_cards
WHEN OLD.club_id <> NEW.club_id
BEGIN
DELETE FROM club_kit_assignments WHERE owned_card_id = OLD.id;
END;