db fixes and patches: phase data tables and unified phase executions migrations, database entities doc

This commit is contained in:
salirezav
2026-02-02 11:27:53 -05:00
parent 49ddcfd002
commit df6c849ca4
5 changed files with 459 additions and 0 deletions

View File

@@ -70,6 +70,10 @@ CREATE TABLE IF NOT EXISTS public.shelling (
scheduled_start_time TIMESTAMP WITH TIME ZONE NOT NULL,
actual_start_time TIMESTAMP WITH TIME ZONE,
actual_end_time TIMESTAMP WITH TIME ZONE,
-- The space (in inches) between the sheller's rings
ring_gap_inches NUMERIC(6,2) CHECK (ring_gap_inches > 0),
-- The revolutions per minute for the sheller drum
drum_rpm INTEGER CHECK (drum_rpm > 0),
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
created_by UUID NOT NULL REFERENCES public.user_profiles(id),

View File

@@ -56,6 +56,80 @@ CREATE INDEX IF NOT EXISTS idx_phase_executions_machine_type_id
CREATE INDEX IF NOT EXISTS idx_phase_executions_created_by
ON public.experiment_phase_executions(created_by);
-- =============================================
-- 2.5. CREATE CONDUCTOR ASSIGNMENTS TABLE
-- =============================================
-- Table to store conductor assignments to phase executions
-- This allows multiple conductors to be assigned to each phase execution
CREATE TABLE IF NOT EXISTS public.experiment_phase_assignments (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
phase_execution_id UUID NOT NULL REFERENCES public.experiment_phase_executions(id) ON DELETE CASCADE,
conductor_id UUID NOT NULL REFERENCES public.user_profiles(id) ON DELETE CASCADE,
-- Scheduled times for this assignment (should match phase_execution times, but stored for clarity)
scheduled_start_time TIMESTAMP WITH TIME ZONE NOT NULL,
scheduled_end_time TIMESTAMP WITH TIME ZONE,
-- Status tracking
status TEXT NOT NULL DEFAULT 'scheduled'
CHECK (status IN ('scheduled', 'in_progress', 'completed', 'cancelled')),
-- Optional notes about the assignment
notes TEXT,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
created_by UUID NOT NULL REFERENCES public.user_profiles(id),
-- Ensure scheduled_end_time is after scheduled_start_time
CONSTRAINT valid_scheduled_time_range CHECK (scheduled_end_time IS NULL OR scheduled_end_time > scheduled_start_time),
-- Ensure unique assignment per conductor per phase execution
CONSTRAINT unique_conductor_phase_execution UNIQUE (phase_execution_id, conductor_id)
);
-- Indexes for conductor assignments
CREATE INDEX IF NOT EXISTS idx_phase_assignments_phase_execution_id
ON public.experiment_phase_assignments(phase_execution_id);
CREATE INDEX IF NOT EXISTS idx_phase_assignments_conductor_id
ON public.experiment_phase_assignments(conductor_id);
CREATE INDEX IF NOT EXISTS idx_phase_assignments_status
ON public.experiment_phase_assignments(status);
CREATE INDEX IF NOT EXISTS idx_phase_assignments_scheduled_start_time
ON public.experiment_phase_assignments(scheduled_start_time);
CREATE INDEX IF NOT EXISTS idx_phase_assignments_created_by
ON public.experiment_phase_assignments(created_by);
-- Trigger for updated_at on conductor assignments
CREATE TRIGGER set_updated_at_phase_assignments
BEFORE UPDATE ON public.experiment_phase_assignments
FOR EACH ROW
EXECUTE FUNCTION public.handle_updated_at();
-- Grant permissions
GRANT ALL ON public.experiment_phase_assignments TO authenticated;
-- Enable Row Level Security
ALTER TABLE public.experiment_phase_assignments ENABLE ROW LEVEL SECURITY;
-- RLS Policies for conductor assignments
CREATE POLICY "Phase assignments are viewable by authenticated users"
ON public.experiment_phase_assignments
FOR SELECT USING (auth.role() = 'authenticated');
CREATE POLICY "Phase assignments are insertable by authenticated users"
ON public.experiment_phase_assignments
FOR INSERT WITH CHECK (auth.role() = 'authenticated');
CREATE POLICY "Phase assignments are updatable by authenticated users"
ON public.experiment_phase_assignments
FOR UPDATE USING (auth.role() = 'authenticated');
CREATE POLICY "Phase assignments are deletable by authenticated users"
ON public.experiment_phase_assignments
FOR DELETE USING (auth.role() = 'authenticated');
-- =============================================
-- 3. FUNCTION: Calculate Sequential Phase Start Times
-- =============================================