Skip to content

Instantly share code, notes, and snippets.

@simonswine
Created June 7, 2013 11:10
Show Gist options
  • Save simonswine/5728575 to your computer and use it in GitHub Desktop.
Save simonswine/5728575 to your computer and use it in GitHub Desktop.
Upgrade deploy key Gitlab 5.1 -> 5.2
#!/usr/bin/env python
import MySQLdb
import MySQLdb.cursors
sql = MySQLdb.connect(host="xyz", user="abc",passwd="def",db="f03_gitlab_production",cursorclass=MySQLdb.cursors.DictCursor)
c = sql.cursor()
c.execute("SELECT `key` FROM `keys` GROUP BY `key` HAVING COUNT(*) > 1")
keys_dup = c.fetchall()
# Loop over duplicate keys
for key in keys_dup:
print key['key'].split(' ')[-1]
# Get ids of dups
c.execute("SELECT `id` FROM `keys` WHERE `key`=%s",key['key'])
dups = c.fetchall()
# Get ids
dups_ids = [i['id'] for i in dups]
# Use highest id
new_id = max(dups_ids)
# Remove highest id from list
dups_ids.remove(new_id)
for id in dups_ids:
# Remove old ids
c.execute("DELETE from `keys` WHERE `id`=%d" % (id))
# Get relation with this id
c.execute("UPDATE `deploy_keys_projects` SET `deploy_key_id`=%d WHERE `deploy_key_id` = %d" % (new_id,id))
c.fetchall()
print new_id, dups_ids
# Test results, then comment next line
sql.rollback()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment