-- Tables affected: qt_pricelists
-- recovery.not-started: safe to re-run; ADD COLUMN IF NOT EXISTS
-- recovery.partial: ALTER TABLE qt_pricelists DROP COLUMN source;
-- recovery.completed: no action needed
-- verify: source column in qt_pricelists missing | SELECT 1 FROM information_schema.COLUMNS WHERE table_schema=DATABASE() AND table_name='qt_pricelists' AND column_name='source' HAVING COUNT(*)=0

ALTER TABLE qt_pricelists
	ADD COLUMN IF NOT EXISTS source ENUM('estimate', 'warehouse', 'competitor') NOT NULL DEFAULT 'estimate' AFTER id;

UPDATE qt_pricelists
SET source = 'estimate'
WHERE source IS NULL OR source = '';

ALTER TABLE qt_pricelists
	DROP INDEX IF EXISTS idx_qt_pricelists_version;

CREATE UNIQUE INDEX idx_qt_pricelists_source_version
	ON qt_pricelists(source, version);

CREATE INDEX idx_qt_pricelists_source_created_at
	ON qt_pricelists(source, created_at);
