-- V6: build_id support for multi-build-per-version sourcemap tracking
-- ─────────────────────────────────────────────────────────────────────
-- log_sourcemaps:
--   • Drop old unique key that prevented multiple builds of the same version
--   • Add build_id (nullable for backward compat with pre-V6 uploads)
--   • New unique key includes build_id; MySQL treats NULL as distinct in UNIQUE,
--     so old rows (build_id=NULL) are not affected and remain individually unique
--   • Add fallback index for version-only lookup (for events without buildId)
--
-- log_issue_events:
--   • Add build_id so events can be matched to the exact sourcemap build

ALTER TABLE log_sourcemaps
    ADD COLUMN build_id VARCHAR(32) NULL COMMENT 'Gradle build timestamp yyyyMMddHHmmss; NULL = pre-V6 upload'
    AFTER bundle_name;

ALTER TABLE log_sourcemaps DROP KEY uk_map;

ALTER TABLE log_sourcemaps
    ADD UNIQUE KEY uk_map_build (app_key, platform, app_version, build_id, bundle_name);

ALTER TABLE log_sourcemaps
    ADD INDEX idx_sm_version_latest (app_key, platform, app_version, bundle_name, uploaded_at);

ALTER TABLE log_issue_events
    ADD COLUMN build_id VARCHAR(32) NULL COMMENT 'Matches log_sourcemaps.build_id for exact symbolication'
    AFTER release;
