SET @has_old_rosters := (
  SELECT COUNT(*) FROM information_schema.TABLES
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'hems_duty_rosters'
);
SET @has_new_rosters := (
  SELECT COUNT(*) FROM information_schema.TABLES
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'duty_rosters'
);
SET @sql_rosters := IF(@has_old_rosters = 1 AND @has_new_rosters = 0,
  'RENAME TABLE hems_duty_rosters TO duty_rosters',
  'SELECT 1'
);
PREPARE stmt_rosters FROM @sql_rosters;
EXECUTE stmt_rosters;
DEALLOCATE PREPARE stmt_rosters;

SET @has_old_crews := (
  SELECT COUNT(*) FROM information_schema.TABLES
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'hems_duty_roster_crews'
);
SET @has_new_crews := (
  SELECT COUNT(*) FROM information_schema.TABLES
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'duty_roster_crews'
);
SET @sql_crews := IF(@has_old_crews = 1 AND @has_new_crews = 0,
  'RENAME TABLE hems_duty_roster_crews TO duty_roster_crews',
  'SELECT 1'
);
PREPARE stmt_crews FROM @sql_crews;
EXECUTE stmt_crews;
DEALLOCATE PREPARE stmt_crews;
