Sindbad~EG File Manager

Current Path : /home/copmadinaarea/.trash/database.1/migrations/
Upload File :
Current File : /home/copmadinaarea/.trash/database.1/migrations/create_events_and_codes_tables.sql

-- Migration: Create events and memberuser_codes tables
-- Date: 2025-10-06
-- Description: Creates events table and memberuser_codes table for tracking member/user codes

-- Events Table
CREATE TABLE IF NOT EXISTS events (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    description TEXT,
    start_date DATETIME,
    end_date DATETIME,
    location_id INT,
    location_type ENUM('area', 'district', 'assembly') DEFAULT 'assembly',
    is_active BOOLEAN DEFAULT TRUE,
    registration_status ENUM('open', 'closed', 'pending') DEFAULT 'pending',
    registration_open_date DATETIME,
    registration_close_date DATETIME,
    registration_message TEXT,
    created_by INT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    FOREIGN KEY (created_by) REFERENCES users(id),
    INDEX idx_location_id (location_id),
    INDEX idx_location_type (location_type),
    INDEX idx_start_date (start_date),
    INDEX idx_is_active (is_active)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Member/User Codes Table
CREATE TABLE IF NOT EXISTS memberuser_codes (
    id INT AUTO_INCREMENT PRIMARY KEY,
    code VARCHAR(50) UNIQUE NOT NULL,
    description TEXT,
    member_id INT NULL,
    user_id INT NULL,
    event_id INT NULL,
    code_type ENUM('member', 'user') NOT NULL,
    tracking_code VARCHAR(100) UNIQUE NOT NULL,
    barcode VARCHAR(255),
    qrcode VARCHAR(255),
    created_by INT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    expires_at DATETIME NULL,
    is_active BOOLEAN DEFAULT TRUE,
    usage_count INT DEFAULT 0,
    max_usage INT DEFAULT NULL,
    FOREIGN KEY (member_id) REFERENCES members(id) ON DELETE CASCADE,
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
    FOREIGN KEY (event_id) REFERENCES events(id) ON DELETE SET NULL,
    FOREIGN KEY (created_by) REFERENCES users(id),
    INDEX idx_member_id (member_id),
    INDEX idx_user_id (user_id),
    INDEX idx_event_id (event_id),
    INDEX idx_code_type (code_type),
    INDEX idx_tracking_code (tracking_code),
    INDEX idx_is_active (is_active),
    CONSTRAINT chk_member_or_user CHECK (
        (member_id IS NOT NULL AND user_id IS NULL) OR 
        (member_id IS NULL AND user_id IS NOT NULL)
    )
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Insert default event types
INSERT IGNORE INTO events (name, description, is_active, created_by) VALUES
('Attendance', 'Regular church attendance tracking', TRUE, 1),
('Program', 'Special church programs and events', TRUE, 1),
('Service', 'Church service participation', TRUE, 1),
('Rally', 'Church rallies and gatherings', TRUE, 1),
('Evangelism', 'Evangelism activities and outreach', TRUE, 1),
('Conference', 'Church conferences and meetings', TRUE, 1),
('Workshop', 'Training workshops and seminars', TRUE, 1),
('Youth Program', 'Youth-specific activities', TRUE, 1),
('Women Fellowship', 'Women fellowship activities', TRUE, 1),
('Men Fellowship', 'Men fellowship activities', TRUE, 1);

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