diff --git a/migrations/0016_remove_legacy_features/up.sql b/migrations/0016_remove_legacy_features/up.sql index 67c6f56..4270040 100644 --- a/migrations/0016_remove_legacy_features/up.sql +++ b/migrations/0016_remove_legacy_features/up.sql @@ -4,24 +4,187 @@ DROP TABLE IF EXISTS ticket_links; DROP TABLE IF EXISTS tickets; DROP TABLE IF EXISTS lot_model_mappings; -ALTER TABLE parts - DROP FOREIGN KEY fk_parts_lot, - DROP INDEX idx_parts_lot, - DROP COLUMN lot_id; +SET @sql = ( + SELECT IF( + EXISTS( + SELECT 1 FROM information_schema.table_constraints + WHERE constraint_schema = DATABASE() + AND table_name = 'parts' + AND constraint_name = 'fk_parts_lot' + ), + 'ALTER TABLE parts DROP FOREIGN KEY fk_parts_lot', + 'SELECT 1' + ) +); +PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt; -ALTER TABLE machines - DROP FOREIGN KEY fk_machines_customer, - DROP FOREIGN KEY fk_machines_location, - DROP INDEX idx_machines_customer, - DROP INDEX idx_machines_location, - DROP COLUMN customer_id, - DROP COLUMN location_id; +SET @sql = ( + SELECT IF( + EXISTS( + SELECT 1 FROM information_schema.statistics + WHERE table_schema = DATABASE() + AND table_name = 'parts' + AND index_name = 'idx_parts_lot' + ), + 'ALTER TABLE parts DROP INDEX idx_parts_lot', + 'SELECT 1' + ) +); +PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt; -ALTER TABLE projects - DROP FOREIGN KEY fk_projects_customer, - DROP INDEX idx_projects_customer, - DROP INDEX uniq_projects_customer_name, - DROP COLUMN customer_id; +SET @sql = ( + SELECT IF( + EXISTS( + SELECT 1 FROM information_schema.columns + WHERE table_schema = DATABASE() + AND table_name = 'parts' + AND column_name = 'lot_id' + ), + 'ALTER TABLE parts DROP COLUMN lot_id', + 'SELECT 1' + ) +); +PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt; + +SET @sql = ( + SELECT IF( + EXISTS( + SELECT 1 FROM information_schema.table_constraints + WHERE constraint_schema = DATABASE() + AND table_name = 'machines' + AND constraint_name = 'fk_machines_customer' + ), + 'ALTER TABLE machines DROP FOREIGN KEY fk_machines_customer', + 'SELECT 1' + ) +); +PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt; + +SET @sql = ( + SELECT IF( + EXISTS( + SELECT 1 FROM information_schema.table_constraints + WHERE constraint_schema = DATABASE() + AND table_name = 'machines' + AND constraint_name = 'fk_machines_location' + ), + 'ALTER TABLE machines DROP FOREIGN KEY fk_machines_location', + 'SELECT 1' + ) +); +PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt; + +SET @sql = ( + SELECT IF( + EXISTS( + SELECT 1 FROM information_schema.statistics + WHERE table_schema = DATABASE() + AND table_name = 'machines' + AND index_name = 'idx_machines_customer' + ), + 'ALTER TABLE machines DROP INDEX idx_machines_customer', + 'SELECT 1' + ) +); +PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt; + +SET @sql = ( + SELECT IF( + EXISTS( + SELECT 1 FROM information_schema.statistics + WHERE table_schema = DATABASE() + AND table_name = 'machines' + AND index_name = 'idx_machines_location' + ), + 'ALTER TABLE machines DROP INDEX idx_machines_location', + 'SELECT 1' + ) +); +PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt; + +SET @sql = ( + SELECT IF( + EXISTS( + SELECT 1 FROM information_schema.columns + WHERE table_schema = DATABASE() + AND table_name = 'machines' + AND column_name = 'customer_id' + ), + 'ALTER TABLE machines DROP COLUMN customer_id', + 'SELECT 1' + ) +); +PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt; + +SET @sql = ( + SELECT IF( + EXISTS( + SELECT 1 FROM information_schema.columns + WHERE table_schema = DATABASE() + AND table_name = 'machines' + AND column_name = 'location_id' + ), + 'ALTER TABLE machines DROP COLUMN location_id', + 'SELECT 1' + ) +); +PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt; + +SET @sql = ( + SELECT IF( + EXISTS( + SELECT 1 FROM information_schema.table_constraints + WHERE constraint_schema = DATABASE() + AND table_name = 'projects' + AND constraint_name = 'fk_projects_customer' + ), + 'ALTER TABLE projects DROP FOREIGN KEY fk_projects_customer', + 'SELECT 1' + ) +); +PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt; + +SET @sql = ( + SELECT IF( + EXISTS( + SELECT 1 FROM information_schema.statistics + WHERE table_schema = DATABASE() + AND table_name = 'projects' + AND index_name = 'idx_projects_customer' + ), + 'ALTER TABLE projects DROP INDEX idx_projects_customer', + 'SELECT 1' + ) +); +PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt; + +SET @sql = ( + SELECT IF( + EXISTS( + SELECT 1 FROM information_schema.statistics + WHERE table_schema = DATABASE() + AND table_name = 'projects' + AND index_name = 'uniq_projects_customer_name' + ), + 'ALTER TABLE projects DROP INDEX uniq_projects_customer_name', + 'SELECT 1' + ) +); +PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt; + +SET @sql = ( + SELECT IF( + EXISTS( + SELECT 1 FROM information_schema.columns + WHERE table_schema = DATABASE() + AND table_name = 'projects' + AND column_name = 'customer_id' + ), + 'ALTER TABLE projects DROP COLUMN customer_id', + 'SELECT 1' + ) +); +PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt; DROP TABLE IF EXISTS lots; DROP TABLE IF EXISTS locations;