-- Motorboy BusOps V1.5.1 — Cameroon operations refinements
-- Buses are operator-wide fleet assets, not branch-owned.
SET @fk_exists := (
 SELECT COUNT(*) FROM information_schema.TABLE_CONSTRAINTS
 WHERE CONSTRAINT_SCHEMA=DATABASE() AND TABLE_NAME='buses'
 AND CONSTRAINT_NAME='fk_bus_branch' AND CONSTRAINT_TYPE='FOREIGN KEY'
);
SET @sql := IF(@fk_exists>0,'ALTER TABLE buses DROP FOREIGN KEY fk_bus_branch','SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SET @col_exists := (
 SELECT COUNT(*) FROM information_schema.COLUMNS
 WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='buses' AND COLUMN_NAME='branch_id'
);
SET @sql := IF(@col_exists>0,'ALTER TABLE buses DROP COLUMN branch_id','SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

INSERT INTO system_settings(setting_key,setting_value)
VALUES('installed_version','1.5.1')
ON DUPLICATE KEY UPDATE setting_value=VALUES(setting_value);
