startSetup(); $tableAdmins = $installer->getTable('admin/user'); // delete admin username duplicates $duplicatedUsers = $installer->getConnection()->fetchPairs(" SELECT user_id, username FROM {$tableAdmins} GROUP by username HAVING COUNT(user_id) > 1 "); $installer->run("DELETE FROM {$tableAdmins} WHERE username " . $installer->getConnection()->quoteInto('IN (?) ', array_values($duplicatedUsers)) . 'AND user_id ' . $installer->getConnection()->quoteInto('NOT IN (?) ', array_keys($duplicatedUsers)) ); // add unique key to username field $installer->getConnection()->addKey($tableAdmins, 'UNQ_ADMIN_USER_USERNAME', 'username', 'unique'); $installer->endSetup();