97 lines
3.7 KiB
PL/PgSQL
97 lines
3.7 KiB
PL/PgSQL
-- Location: supabase/migrations/20251031165808_enhance_taxi_directory.sql
|
|
-- Schema Analysis: Existing taxis table with district column, needs shift column and terminology update
|
|
-- Integration Type: PARTIAL_EXISTS - Extending existing taxi module
|
|
-- Dependencies: Existing taxis and favorite_taxis tables
|
|
|
|
-- 1. Create shift enum type for taxi shifts
|
|
CREATE TYPE public.taxi_shift AS ENUM ('day', 'evening', 'night');
|
|
|
|
-- 2. Add shift column to existing taxis table
|
|
ALTER TABLE public.taxis
|
|
ADD COLUMN shift public.taxi_shift DEFAULT 'day'::public.taxi_shift;
|
|
|
|
-- 3. Rename district column to corregimiento for terminology consistency
|
|
ALTER TABLE public.taxis
|
|
RENAME COLUMN district TO corregimiento;
|
|
|
|
-- 4. Update existing indexes to match new column names
|
|
DROP INDEX IF EXISTS idx_taxis_district;
|
|
CREATE INDEX idx_taxis_corregimiento ON public.taxis(corregimiento);
|
|
CREATE INDEX idx_taxis_shift ON public.taxis(shift);
|
|
|
|
-- 5. Add updated sample data with new structure
|
|
DO $$
|
|
DECLARE
|
|
taxi1_id UUID := gen_random_uuid();
|
|
taxi2_id UUID := gen_random_uuid();
|
|
taxi3_id UUID := gen_random_uuid();
|
|
taxi4_id UUID := gen_random_uuid();
|
|
taxi5_id UUID := gen_random_uuid();
|
|
taxi6_id UUID := gen_random_uuid();
|
|
BEGIN
|
|
-- Remove existing sample data first
|
|
DELETE FROM public.taxis WHERE phone IN ('+507 720-1234', '+507 775-5678');
|
|
|
|
-- Insert comprehensive sample data with corregimiento and shift
|
|
INSERT INTO public.taxis (id, name, phone, corregimiento, shift, is_active, created_at, updated_at) VALUES
|
|
(taxi1_id, 'Taxi Central Boquete', '+507 720-1234', 'Boquete', 'day'::public.taxi_shift, true, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
|
|
(taxi2_id, 'Taxi Flores David', '+507 775-5678', 'David', 'evening'::public.taxi_shift, true, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
|
|
(taxi3_id, 'Taxi Noctuno David', '+507 776-9999', 'David', 'night'::public.taxi_shift, true, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
|
|
(taxi4_id, 'Taxi Diurno Chiriquí', '+507 721-4567', 'Chiriquí', 'day'::public.taxi_shift, true, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
|
|
(taxi5_id, 'Taxi Tarde Boquete', '+507 722-8890', 'Boquete', 'evening'::public.taxi_shift, true, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
|
|
(taxi6_id, 'Taxi Madrugada Chiriquí', '+507 723-1122', 'Chiriquí', 'night'::public.taxi_shift, true, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP);
|
|
|
|
EXCEPTION
|
|
WHEN unique_violation THEN
|
|
RAISE NOTICE 'Some taxi data already exists, skipping duplicates';
|
|
WHEN OTHERS THEN
|
|
RAISE NOTICE 'Error inserting taxi data: %', SQLERRM;
|
|
END $$;
|
|
|
|
-- 6. Create helper function for getting distinct corregimientos
|
|
CREATE OR REPLACE FUNCTION public.get_distinct_corregimientos()
|
|
RETURNS TABLE(corregimiento TEXT)
|
|
LANGUAGE sql
|
|
STABLE
|
|
SECURITY DEFINER
|
|
AS $$
|
|
SELECT DISTINCT t.corregimiento::TEXT
|
|
FROM public.taxis t
|
|
WHERE t.is_active = true
|
|
ORDER BY t.corregimiento::TEXT;
|
|
$$;
|
|
|
|
-- 7. Create helper function for filtering taxis by corregimiento and shift
|
|
CREATE OR REPLACE FUNCTION public.filter_taxis_by_criteria(
|
|
selected_corregimiento TEXT DEFAULT NULL,
|
|
selected_shift TEXT DEFAULT NULL
|
|
)
|
|
RETURNS TABLE(
|
|
id UUID,
|
|
name TEXT,
|
|
phone TEXT,
|
|
corregimiento TEXT,
|
|
shift TEXT,
|
|
is_active BOOLEAN,
|
|
created_at TIMESTAMPTZ,
|
|
updated_at TIMESTAMPTZ
|
|
)
|
|
LANGUAGE sql
|
|
STABLE
|
|
SECURITY DEFINER
|
|
AS $$
|
|
SELECT
|
|
t.id,
|
|
t.name,
|
|
t.phone,
|
|
t.corregimiento::TEXT,
|
|
t.shift::TEXT,
|
|
t.is_active,
|
|
t.created_at,
|
|
t.updated_at
|
|
FROM public.taxis t
|
|
WHERE t.is_active = true
|
|
AND (selected_corregimiento IS NULL OR t.corregimiento = selected_corregimiento)
|
|
AND (selected_shift IS NULL OR t.shift::TEXT = selected_shift)
|
|
ORDER BY t.name ASC;
|
|
$$; |