# Generated by Django 3.2.9 on 2025-08-25 09:30

from django.db import migrations


class Migration(migrations.Migration):

    dependencies = [
        ('rog', '0008_auto_20250825_0716'),
    ]

    operations = [
        # GPS Checkin テーブルの安全な作成
        migrations.RunSQL(
            sql="""
            DO $$ 
            BEGIN
                -- rog_gpscheckin テーブルが存在しない場合のみ作成
                IF NOT EXISTS (
                    SELECT FROM information_schema.tables 
                    WHERE table_schema = 'public' 
                    AND table_name = 'rog_gpscheckin'
                ) THEN
                    CREATE TABLE rog_gpscheckin (
                        id SERIAL PRIMARY KEY,
                        event_code VARCHAR(50),
                        zekken VARCHAR(20),
                        serial_number VARCHAR(50),
                        team_name VARCHAR(255),
                        cp_number VARCHAR(20),
                        lat DOUBLE PRECISION,
                        lng DOUBLE PRECISION,
                        checkin_time TIMESTAMP WITH TIME ZONE,
                        record_time TIMESTAMP WITH TIME ZONE,
                        location GEOMETRY(Point, 4326),
                        mobserver_id INTEGER,
                        event_id INTEGER,
                        team_id INTEGER,
                        checkpoint_id INTEGER,
                        goal_time TEXT,
                        late_point INTEGER,
                        buy_flag BOOLEAN DEFAULT FALSE,
                        image_address TEXT,
                        minus_photo_flag BOOLEAN DEFAULT FALSE,
                        create_user TEXT,
                        update_user TEXT,
                        comment TEXT,
                        created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
                        updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
                    );
                    
                    -- インデックスの作成
                    CREATE INDEX IF NOT EXISTS rog_gpschec_event_c_9f1a75_idx ON rog_gpscheckin (event_code);
                    CREATE INDEX IF NOT EXISTS rog_gpschec_zekken_8e7f42_idx ON rog_gpscheckin (zekken);
                    CREATE INDEX IF NOT EXISTS rog_gpschec_serial__1b5e3a_idx ON rog_gpscheckin (serial_number);
                    CREATE INDEX IF NOT EXISTS rog_gpschec_cp_numb_d7c8e5_idx ON rog_gpscheckin (cp_number);
                    CREATE INDEX IF NOT EXISTS rog_gpscheckin_team_id_e85ab512 ON rog_gpscheckin (team_id);
                END IF;
                
                -- rog_gpslog テーブルが存在しない場合のみ作成
                IF NOT EXISTS (
                    SELECT FROM information_schema.tables 
                    WHERE table_schema = 'public' 
                    AND table_name = 'rog_gpslog'
                ) THEN
                    CREATE TABLE rog_gpslog (
                        id SERIAL PRIMARY KEY,
                        event_code VARCHAR(50),
                        zekken VARCHAR(20),
                        serial_number VARCHAR(50),
                        cp_number VARCHAR(20),
                        lat DOUBLE PRECISION,
                        lng DOUBLE PRECISION,
                        checkin_time TIMESTAMP WITH TIME ZONE,
                        record_time TIMESTAMP WITH TIME ZONE,
                        location GEOMETRY(Point, 4326),
                        mobserver_id INTEGER,
                        event_id INTEGER,
                        team_id INTEGER,
                        checkpoint_id INTEGER
                    );
                    
                    -- インデックスの作成
                    CREATE INDEX IF NOT EXISTS rog_gpslog_event_c_8a2b94_idx ON rog_gpslog (event_code);
                    CREATE INDEX IF NOT EXISTS rog_gpslog_zekken_7c5f39_idx ON rog_gpslog (zekken);
                    CREATE INDEX IF NOT EXISTS rog_gpslog_serial__6d4e28_idx ON rog_gpslog (serial_number);
                    CREATE INDEX IF NOT EXISTS rog_gpslog_cp_numb_5b3a17_idx ON rog_gpslog (cp_number);
                END IF;
            END $$;
            """,
            reverse_sql="""
            DROP TABLE IF EXISTS rog_gpscheckin CASCADE;
            DROP TABLE IF EXISTS rog_gpslog CASCADE;
            """
        ),
    ]
