The tables that have a foreign key to osversion.

Table				Column
distro				osversion_id
exclude_osversion		osversion_id
osversion_arch_map		osversion_id
provision_update_family		osversion_id

# See if we have anything pointing at our duplicate records.
SELECT id FROM distro WHERE osversion_id IN (SELECT id FROM osversion WHERE id NOT IN (SELECT id FROM osversion GROUP BY osmajor_id, osminor));

SELECT id FROM exclude_osversion WHERE osversion_id IN (SELECT id FROM osversion WHERE id NOT IN (SELECT id FROM osversion GROUP BY osmajor_id, osminor));

SELECT id FROM osversion_arch_map WHERE osversion_id IN (SELECT id FROM osversion WHERE id NOT IN (SELECT id FROM osversion GROUP BY osmajor_id, osminor));

SELECT id FROM provision_update_family WHERE osversion_id IN (SELECT id FROM osversion WHERE id NOT IN (SELECT id FROM osversion GROUP BY osmajor_id, osminor));

# Fix the records pointing to the duplicates
UPDATE distro SET osversion_id = (SELECT id FROM osversion WHERE osmajor_id = (SELECT osmajor_id FROM osversion WHERE osversion.id = distro.osversion_id) AND osminor = (SELECT osminor FROM osversion WHERE osversion.id = distro.osversion_id) ORDER BY id ASC limit 1);

UPDATE exclude_osversion SET osversion_id = (SELECT id FROM osversion WHERE osmajor_id = (SELECT osmajor_id FROM osversion WHERE osversion.id = exclude_osversion.osversion_id) AND osminor = (SELECT osminor FROM osversion WHERE osversion.id = exclude_osversion.osversion_id) ORDER BY id ASC limit 1);

DELETE FROM osversion_arch_map WHERE osversion_id NOT IN (SELECT id FROM osversion GROUP BY osmajor_id, osminor);

UPDATE provision_update_family SET osversion_id = (SELECT id FROM osversion WHERE osmajor_id = (SELECT osmajor_id FROM osversion WHERE osversion.id = provision_update_family.osversion_id) AND osminor = (SELECT osminor FROM osversion WHERE osversion.id = provision_update_family.osversion_id) ORDER BY id ASC limit 1);


# Remove the duplicate records
CREATE TEMPORARY TABLE tmposversion AS SELECT * FROM osversion;
DELETE FROM osversion WHERE id NOT IN (SELECT id FROM tmposversion GROUP BY osmajor_id, osminor);
DROP TABLE tmposversion;

# Create the Unique constraint
ALTER TABLE osversion ADD UNIQUE KEY osversion_uix_1 (osmajor_id, osminor);
