Updated gerrit account merge script

Posted on 25.11.2016

Since 2014, there were a number of changes to gerrit’s database schema, which required an update to the script we use to merge accounts. Publishing here because why not?

#!/bin/sh
# Copyright (C) 2016 Patrick Georgi <patrick@georgi-clan.de>
# licensing: ISC license terms
#
# usage: $0 id-to-keep id-to-merge
HOST=review.coreboot.org
PORT=29418

if [ $# -ne 2 ]; then
    echo "usage: $0 id-to-keep id-to-merge"
    exit 1
fi

KEEP=$1
MERGE=$2
if [ `expr $KEEP + 0` -eq 0 ]; then
    echo "$KEEP is not an id"
    exit 1
fi

if [ `expr $MERGE + 0` -eq 0 ]; then
    echo "$MERGE is not an id"
    exit 1
fi

ssh -p $PORT $HOST gerrit gsql << _EOF
-- BEGIN TRANSACTION;
UPDATE account_external_ids SET account_id=$KEEP WHERE account_id=$MERGE;
UPDATE account_group_by_id_aud SET added_by=$KEEP WHERE added_by=$MERGE;
UPDATE account_group_by_id_aud SET removed_by=$KEEP WHERE removed_by=$MERGE;
UPDATE account_group_members SET account_id=$KEEP WHERE account_id=$MERGE;
UPDATE account_group_members_audit SET added_by=$KEEP WHERE added_by=$MERGE;
UPDATE account_group_members_audit SET removed_by=$KEEP WHERE removed_by=$MERGE;
UPDATE account_group_members_audit SET account_id=$KEEP WHERE account_id=$MERGE;
UPDATE account_project_watches SET account_id=$KEEP WHERE account_id=$MERGE;
UPDATE change_messages SET author_id=$KEEP WHERE author_id=$MERGE;
UPDATE changes SET owner_account_id=$KEEP WHERE owner_account_id=$MERGE;
UPDATE patch_comments SET author_id=$KEEP WHERE author_id=$MERGE;
UPDATE patch_sets SET uploader_account_id=$KEEP WHERE uploader_account_id=$MERGE;
-- there may be unique constraint violations in these, so transfer what's possible
-- and remove the others. being duplicates, they won't be too important anyway.
UPDATE IGNORE patch_set_approvals SET account_id=$KEEP WHERE account_id=$MERGE;
DELETE FROM patch_set_approvals WHERE account_id=$MERGE;
DELETE FROM accounts WHERE account_id=$MERGE;
-- COMMIT;
_EOF

ssh -p $PORT $HOST gerrit flush-caches