-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdbsetup.sql
More file actions
58 lines (55 loc) · 1.44 KB
/
dbsetup.sql
File metadata and controls
58 lines (55 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
# Create animals table with extended fields
1. New Tables
- `animals`
- `id` (uuid, primary key)
- `animal_name` (text)
- `animal_type` (text)
- `breed` (text)
- `color` (text)
- `size` (text)
- `health_status` (text)
- `incident` (text)
- `last_seen` (timestamptz)
- `latitude` (float8)
- `longitude` (float8)
- `created_at` (timestamptz)
- `updated_at` (timestamptz)
- `radius` (float8)
- `color_code` (text)
- `assets` (JSONB)
*/
-- Drop existing trigger if exists
DROP TRIGGER IF EXISTS update_animals_updated_at ON animals;
DROP FUNCTION IF EXISTS update_updated_at();
-- Create or update the table
CREATE TABLE IF NOT EXISTS animals (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
animal_name text,
animal_type text NOT NULL,
breed text,
color text,
size text,
health_status text,
incident text,
last_seen timestamptz DEFAULT now(),
latitude float8 NOT NULL,
longitude float8 NOT NULL,
created_at timestamptz DEFAULT now(),
updated_at timestamptz DEFAULT now(),
radius float8 DEFAULT 1000,
color_code text,
assets JSONB DEFAULT '{}'
);
-- Create updated_at trigger
CREATE OR REPLACE FUNCTION update_updated_at()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at = now();
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER update_animals_updated_at
BEFORE UPDATE ON animals
FOR EACH ROW
EXECUTE FUNCTION update_updated_at();