- Added customer_id column to fleet_vehicles with FK to customers. - Enforced customer_id on create_vehicle endpoints (workshop and fleet). - New vehicle modal now requires a selected customer and stores customer_id. - Migration v4.13 applied to all tenants.
21 lines
564 B
SQL
21 lines
564 B
SQL
-- v4.13_fleet_vehicle_customer.sql
|
|
-- Link fleet vehicles to a customer.
|
|
|
|
ALTER TABLE fleet_vehicles
|
|
ADD COLUMN IF NOT EXISTS customer_id INTEGER;
|
|
|
|
DO $$
|
|
BEGIN
|
|
IF NOT EXISTS (
|
|
SELECT 1 FROM pg_constraint
|
|
WHERE conname = 'fleet_vehicles_customer_id_fkey'
|
|
) THEN
|
|
ALTER TABLE fleet_vehicles
|
|
ADD CONSTRAINT fleet_vehicles_customer_id_fkey
|
|
FOREIGN KEY (customer_id) REFERENCES customers(id);
|
|
END IF;
|
|
END $$;
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_fleet_vehicles_customer_id
|
|
ON fleet_vehicles (customer_id);
|