Globinours/migrations/046_structured_animal_intake.sql

16 lines
1.3 KiB
SQL
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

ALTER TABLE animals ADD COLUMN intake_type TEXT NOT NULL DEFAULT 'unknown'
CHECK (intake_type IN ('unknown','found','owner_surrender','transfer','born_in_care','return','seizure','other'));
ALTER TABLE animals ADD COLUMN intake_reason TEXT NOT NULL DEFAULT 'unknown'
CHECK (intake_reason IN ('unknown','stray_found','unwanted_litter','owner_health','owner_death','allergy','housing','financial','behavior','abandonment','danger','transfer','birth','other'));
ALTER TABLE animals ADD COLUMN depositor_contact_id INTEGER REFERENCES directory_contacts(id) ON DELETE SET NULL;
ALTER TABLE animals ADD COLUMN intake_circumstances TEXT;
CREATE INDEX idx_animals_intake_type ON animals(intake_type);
CREATE INDEX idx_animals_intake_reason ON animals(intake_reason);
CREATE INDEX idx_animals_depositor ON animals(depositor_contact_id);
INSERT INTO animal_movements(animal_id,kind,place,lieu,note,created_at)
SELECT a.id,'entry',COALESCE(NULLIF(trim(a.rescue_location_name),''),NULLIF(trim(a.rescue_address),'')),
'Entrée historique','Informations dentrée à compléter dans la fiche animale',
COALESCE(NULLIF(a.intake_date,'')||' 12:00:00',a.created_at,datetime('now'))
FROM animals a
WHERE NOT EXISTS(SELECT 1 FROM animal_movements m WHERE m.animal_id=a.id AND m.kind='entry');