-- Allow placeholder mappings (partnumber without bound lot) and store import description.
ALTER TABLE lot_partnumbers
    ADD COLUMN IF NOT EXISTS description VARCHAR(10000) NULL AFTER lot_name;

ALTER TABLE lot_partnumbers
    MODIFY COLUMN lot_name VARCHAR(255) NOT NULL DEFAULT '';

-- Drop FK on lot_name if it exists to allow unresolved placeholders.
SET @lp_fk_name := (
    SELECT kcu.CONSTRAINT_NAME
    FROM information_schema.KEY_COLUMN_USAGE kcu
    WHERE kcu.TABLE_SCHEMA = DATABASE()
      AND kcu.TABLE_NAME = 'lot_partnumbers'
      AND kcu.COLUMN_NAME = 'lot_name'
      AND kcu.REFERENCED_TABLE_NAME IS NOT NULL
    LIMIT 1
);
SET @lp_drop_fk_sql := IF(
    @lp_fk_name IS NULL,
    'SELECT 1',
    CONCAT('ALTER TABLE lot_partnumbers DROP FOREIGN KEY `', @lp_fk_name, '`')
);
PREPARE lp_stmt FROM @lp_drop_fk_sql;
EXECUTE lp_stmt;
DEALLOCATE PREPARE lp_stmt;
