Sindbad~EG File Manager

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

-- Migration script to add locations table and update users table
-- Run this script to update your existing database

USE cop_news_portal;

-- Create locations table if it doesn't exist
CREATE TABLE IF NOT EXISTS locations (
    id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(100) NOT NULL,
    type ENUM('area', 'district', 'assembly') NOT NULL,
    parent_id INT NULL,
    address TEXT,
    contact_person VARCHAR(100),
    contact_phone VARCHAR(20),
    contact_email VARCHAR(100),
    description TEXT,
    status ENUM('active', 'inactive') DEFAULT 'active',
    created_by INT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    FOREIGN KEY (parent_id) REFERENCES locations(id) ON DELETE SET NULL,
    UNIQUE KEY unique_location (name, type)
);

-- Insert sample locations if they don't exist
INSERT IGNORE INTO locations (id, name, type, parent_id, address, contact_person, contact_phone, contact_email, description, created_by) VALUES
(1, 'Greater Accra Area', 'area', NULL, 'Accra, Ghana', 'Elder John Mensah', '+233244123456', 'accra.area@cop.org', 'Greater Accra Area Office', 1),
(2, 'Ashanti Area', 'area', NULL, 'Kumasi, Ghana', 'Elder Grace Owusu', '+233244123457', 'ashanti.area@cop.org', 'Ashanti Area Office', 1),
(3, 'Northern Area', 'area', NULL, 'Tamale, Ghana', 'Elder Isaac Alhassan', '+233244123458', 'northern.area@cop.org', 'Northern Area Office', 1),
(4, 'Tema District', 'district', 1, 'Tema, Ghana', 'Pastor Samuel Osei', '+233244234567', 'tema.district@cop.org', 'Tema District Office', 1),
(5, 'Accra Central District', 'district', 1, 'Accra Central, Ghana', 'Pastor Rebecca Adjei', '+233244234568', 'accra.central@cop.org', 'Accra Central District Office', 1),
(6, 'Kumasi District', 'district', 2, 'Kumasi, Ghana', 'Pastor Emmanuel Boateng', '+233244234569', 'kumasi.district@cop.org', 'Kumasi District Office', 1),
(7, 'Community 1 Assembly', 'assembly', 4, 'Community 1, Tema', 'Elder Mary Asante', '+233244345678', 'comm1.assembly@cop.org', 'Community 1 Local Assembly', 1),
(8, 'Community 9 Assembly', 'assembly', 4, 'Community 9, Tema', 'Elder Joseph Tetteh', '+233244345679', 'comm9.assembly@cop.org', 'Community 9 Local Assembly', 1),
(9, 'Osu Assembly', 'assembly', 5, 'Osu, Accra', 'Elder Sarah Mensah', '+233244345680', 'osu.assembly@cop.org', 'Osu Local Assembly', 1),
(10, 'Adum Assembly', 'assembly', 6, 'Adum, Kumasi', 'Elder Peter Asante', '+233244345681', 'adum.assembly@cop.org', 'Adum Local Assembly', 1);

-- Add location_id column to users table if it doesn't exist
SET @column_exists = (
    SELECT COUNT(*)
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_SCHEMA = 'cop_news_portal'
    AND TABLE_NAME = 'users'
    AND COLUMN_NAME = 'location_id'
);

SET @sql = IF(@column_exists = 0,
    'ALTER TABLE users ADD COLUMN location_id INT NULL AFTER user_picture',
    'SELECT "Column location_id already exists" as message'
);

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- Add foreign key constraint if it doesn't exist
SET @fk_exists = (
    SELECT COUNT(*)
    FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
    WHERE TABLE_SCHEMA = 'cop_news_portal'
    AND TABLE_NAME = 'users'
    AND CONSTRAINT_NAME = 'users_ibfk_1'
);

SET @sql = IF(@fk_exists = 0,
    'ALTER TABLE users ADD CONSTRAINT users_ibfk_1 FOREIGN KEY (location_id) REFERENCES locations(id) ON DELETE SET NULL',
    'SELECT "Foreign key constraint already exists" as message'
);

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- Migrate existing location data to location_id
-- Map existing location_type and location_name to location_id
UPDATE users u 
SET location_id = (
    SELECT l.id 
    FROM locations l 
    WHERE l.type = u.location_type 
    AND l.name LIKE CONCAT('%', SUBSTRING_INDEX(u.location_name, ' ', 1), '%')
    LIMIT 1
)
WHERE u.location_id IS NULL 
AND u.location_type IS NOT NULL 
AND u.location_name IS NOT NULL;

-- Set default location for users without a match
UPDATE users 
SET location_id = 1 
WHERE location_id IS NULL;

SELECT 'Migration completed successfully!' as message;

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