Tipos de toma backend logic
This commit is contained in:
81
water-api/sql/create_meter_types.sql
Normal file
81
water-api/sql/create_meter_types.sql
Normal file
@@ -0,0 +1,81 @@
|
||||
-- ============================================================================
|
||||
-- Create meter_types table and add relationship to projects
|
||||
-- Meter types: LoRa, LoRaWAN, Grandes Consumidores
|
||||
-- ============================================================================
|
||||
|
||||
-- Create meter_types table
|
||||
CREATE TABLE IF NOT EXISTS meter_types (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
name VARCHAR(50) NOT NULL UNIQUE,
|
||||
code VARCHAR(20) NOT NULL UNIQUE,
|
||||
description TEXT,
|
||||
is_active BOOLEAN DEFAULT true,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
-- Insert default meter types
|
||||
INSERT INTO meter_types (name, code, description) VALUES
|
||||
('LoRa', 'LORA', 'Medidores con tecnología LoRa'),
|
||||
('LoRaWAN', 'LORAWAN', 'Medidores con tecnología LoRaWAN'),
|
||||
('Grandes Consumidores', 'GRANDES', 'Medidores para grandes consumidores')
|
||||
ON CONFLICT (code) DO NOTHING;
|
||||
|
||||
-- Add meter_type_id column to projects table
|
||||
ALTER TABLE projects
|
||||
ADD COLUMN IF NOT EXISTS meter_type_id UUID REFERENCES meter_types(id) ON DELETE SET NULL;
|
||||
|
||||
-- Add index for better query performance
|
||||
CREATE INDEX IF NOT EXISTS idx_projects_meter_type_id ON projects(meter_type_id);
|
||||
|
||||
-- Add comment
|
||||
COMMENT ON TABLE meter_types IS 'Catalog of meter types (LoRa, LoRaWAN, Grandes Consumidores)';
|
||||
COMMENT ON COLUMN projects.meter_type_id IS 'Default meter type for this project';
|
||||
|
||||
-- ============================================================================
|
||||
-- Helper function to get meter type by code
|
||||
-- ============================================================================
|
||||
|
||||
CREATE OR REPLACE FUNCTION get_meter_type_id(type_code VARCHAR)
|
||||
RETURNS UUID AS $$
|
||||
DECLARE
|
||||
type_id UUID;
|
||||
BEGIN
|
||||
SELECT id INTO type_id FROM meter_types WHERE code = type_code AND is_active = true;
|
||||
RETURN type_id;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
|
||||
-- ============================================================================
|
||||
-- Update trigger for updated_at
|
||||
-- ============================================================================
|
||||
|
||||
CREATE OR REPLACE FUNCTION update_meter_types_updated_at()
|
||||
RETURNS TRIGGER AS $$
|
||||
BEGIN
|
||||
NEW.updated_at = CURRENT_TIMESTAMP;
|
||||
RETURN NEW;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
|
||||
CREATE TRIGGER trigger_meter_types_updated_at
|
||||
BEFORE UPDATE ON meter_types
|
||||
FOR EACH ROW
|
||||
EXECUTE FUNCTION update_meter_types_updated_at();
|
||||
|
||||
-- ============================================================================
|
||||
-- Verify the changes
|
||||
-- ============================================================================
|
||||
|
||||
-- Show meter types
|
||||
SELECT id, name, code, description, is_active FROM meter_types ORDER BY code;
|
||||
|
||||
-- Show projects table structure
|
||||
SELECT
|
||||
column_name,
|
||||
data_type,
|
||||
is_nullable,
|
||||
column_default
|
||||
FROM information_schema.columns
|
||||
WHERE table_name = 'projects'
|
||||
AND column_name = 'meter_type_id';
|
||||
Reference in New Issue
Block a user