Sindbad~EG File Manager

Current Path : /home/copmadinaarea/thecopmadinaarea.org/attendance/database/
Upload File :
Current File : /home/copmadinaarea/thecopmadinaarea.org/attendance/database/migrate_locations.sql

-- Migration to add district_id and assembly_id columns to attendance_records table
-- Run this script to update existing database

-- Add new columns to attendance_records table
ALTER TABLE attendance_records 
ADD COLUMN district_id INT NULL AFTER program_id,
ADD COLUMN assembly_id INT NULL AFTER district_id;

-- Add foreign key constraints
ALTER TABLE attendance_records 
ADD CONSTRAINT fk_attendance_district 
    FOREIGN KEY (district_id) REFERENCES locations(id) ON DELETE SET NULL,
ADD CONSTRAINT fk_attendance_assembly 
    FOREIGN KEY (assembly_id) REFERENCES locations(id) ON DELETE SET NULL;

-- Add indexes for better performance
CREATE INDEX idx_attendance_district ON attendance_records(district_id);
CREATE INDEX idx_attendance_assembly ON attendance_records(assembly_id);

-- Update existing records to populate district_id and assembly_id based on district_name and assembly_name
UPDATE attendance_records ar 
SET district_id = (
    SELECT l.id FROM locations l 
    WHERE l.name = ar.district_name AND l.type = 'district' 
    LIMIT 1
)
WHERE ar.district_name IS NOT NULL AND ar.district_name != '';

UPDATE attendance_records ar 
SET assembly_id = (
    SELECT l.id FROM locations l 
    WHERE l.name = ar.assembly_name AND l.type = 'assembly' 
    LIMIT 1
)
WHERE ar.assembly_name IS NOT NULL AND ar.assembly_name != '';

-- Verify the migration
SELECT 
    COUNT(*) as total_records,
    COUNT(district_id) as records_with_district_id,
    COUNT(assembly_id) as records_with_assembly_id
FROM attendance_records;

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists