-- liquibase formatted sql
-- changeset 2fi:20260505_01_truck_lane_version_drop_store_id
-- preconditions onFail:MARK_RAN
-- precondition-sql-check expectedResult:1 SELECT COUNT(*) FROM information_schema.columns WHERE table_schema = DATABASE() AND table_name = 'truck_lane_version' AND column_name = 'storeId'

SET @idx_tlv := (
    SELECT COUNT(*)
    FROM information_schema.statistics
    WHERE table_schema = DATABASE()
      AND table_name = 'truck_lane_version'
      AND index_name = 'idx_tlv_lane'
);
SET @sql_drop_idx := IF(
    @idx_tlv > 0,
    'DROP INDEX idx_tlv_lane ON `truck_lane_version`',
    'SELECT 1'
);
PREPARE stmt_drop_idx FROM @sql_drop_idx;
EXECUTE stmt_drop_idx;
DEALLOCATE PREPARE stmt_drop_idx;

SET @col_tlv_sid := (
    SELECT COUNT(*)
    FROM information_schema.columns
    WHERE table_schema = DATABASE()
      AND table_name = 'truck_lane_version'
      AND column_name = 'storeId'
);
SET @sql_drop_col := IF(
    @col_tlv_sid > 0,
    'ALTER TABLE `truck_lane_version` DROP COLUMN `storeId`',
    'SELECT 1'
);
PREPARE stmt_drop_col FROM @sql_drop_col;
EXECUTE stmt_drop_col;
DEALLOCATE PREPARE stmt_drop_col;

SET @idx_new := (
    SELECT COUNT(*)
    FROM information_schema.statistics
    WHERE table_schema = DATABASE()
      AND table_name = 'truck_lane_version'
      AND index_name = 'idx_tlv_lane_created'
);
SET @sql_new_idx := IF(
    @idx_new = 0,
    'CREATE INDEX idx_tlv_lane_created ON `truck_lane_version` (`truckLanceCode`, `created`)',
    'SELECT 1'
);
PREPARE stmt_new_idx FROM @sql_new_idx;
EXECUTE stmt_new_idx;
DEALLOCATE PREPARE stmt_new_idx;
