deleting a project from Gerrit

3,062 views
Skip to first unread message

Pat Notz

unread,
Nov 10, 2011, 11:38:03 AM11/10/11
to Repo and Gerrit Discussion
Hi,

One of our users accidentally pushed some changes to the wrong repository in Gerrit (his git remote pointed to the wrong project in Gerrit). So, Gerrit saw all 3000 commits as new and created a change for each of them. To make matters worse, the repository got corrupted somehow in the process so we can no longer push changes into gerrit or even view the bad changes he submitted. I'd like to wipe out that project from the database entirely (I don't care about the lost reviews). I have other projects in that Gerrit instance so I can't just trash the Gerrit installation.

Does anyone know how I can completely remove that project and it's changes from the database and start fresh? I'm currently running 2.1.7.2 with the H2 database but I could upgrade to a newer version if that'd help.

Cheers ~ Pat

Jay Soffian

unread,
Nov 10, 2011, 3:45:10 PM11/10/11
to Pat Notz, Repo and Gerrit Discussion

I believe this should still work for 2.1.7.2 (unverified):

1) In the gerrit UI, delete all the project's rights.
2) In the gerrit UI, set the project's description to 'DELETE ME'.
3) Via gsql:

select name from projects p where description='DELETE ME' and
not exists (select 1 from projects where p.parent_name = p.name) and
not exists (select 1 from ref_rights rr where rr.project_name = p.name) and
not exists (select 1 from changes c where c.dest_project_name = p.name);

delete from projects p where description='DELETE ME' and
not exists (select 1 from projects where p.parent_name = p.name) and
not exists (select 1 from ref_rights rr where rr.project_name = p.name) and
not exists (select 1 from changes c where c.dest_project_name = p.name);

select project_name from account_project_watches where project_name
not in (select name from projects);

delete from account_project_watches where project_name not in (select
name from projects);

(The selects are obviously there so you can see what you're about to delete.)

4) Remove the repo from on disk

Jay Soffian

unread,
Nov 10, 2011, 3:47:59 PM11/10/11
to Pat Notz, Repo and Gerrit Discussion
>> Does anyone know how I can completely remove that project and it's changes
>> from the database and start fresh? I'm currently running 2.1.7.2 with the H2
>> database but I could upgrade to a newer version if that'd help.
>
> I believe this should still work for 2.1.7.2 (unverified):
>
> 1) In the gerrit UI, delete all the project's rights.
> 2) In the gerrit UI, set the project's description to 'DELETE ME'.
> 3) Via gsql:
>
> select name from projects p where description='DELETE ME' and
> not exists (select 1 from projects where p.parent_name = p.name) and
> not exists (select 1 from ref_rights rr where rr.project_name = p.name) and
> not exists (select 1 from changes c where c.dest_project_name = p.name);
>
> delete from projects p where description='DELETE ME' and
> not exists (select 1 from projects where p.parent_name = p.name) and
> not exists (select 1 from ref_rights rr where rr.project_name = p.name) and
> not exists (select 1 from changes c where c.dest_project_name = p.name);
>
> select project_name from account_project_watches where project_name
> not in (select name from projects);
>
> delete from account_project_watches where project_name not in (select
> name from projects);
>
> (The selects are obviously there so you can see what you're about to delete.)
>
> 4) Remove the repo from on disk

Doh, sorry, that'll only work for deleting a project with no changes.
You're going to have to delete all the changes first from the changes
table and any related tables that reference the changes. :-(

Use '\d' in gsql to explore the schema. It's pretty straight-forward from there.

j.

Pat Notz

unread,
Nov 10, 2011, 6:34:13 PM11/10/11
to Jay Soffian, Repo and Gerrit Discussion
Thanks, will do.

I was considering this approach (we found a similar recipe in one of the issue discussions). My only concern was what will happen with the change numbers (not ChangeIDs). The change numbers are sequential with each submission. Depending on how that's implemented, dropping changes from the change table may/not work. Do you think it's safe to delete changes from the table (assuming I drop other related data from the other tables and the refs/changes/* from the git repo)?

Thanks! ~ Pat

Mihai Rusu

unread,
Nov 10, 2011, 8:14:24 PM11/10/11
to repo-d...@googlegroups.com, Pat Notz

On a Gerrit 2.1.8 instance I'm using the attached script to remove
repositories. They are offered under the Apache License 2.0.

WARNING: THE SCRIPT MAY REMOVE ALL YOUR DATA, KILL KITTENS, ETC. IT'S JUST A
HACK I HAVE DEVELOPED AND SUCCESSFULLY USED ON MY SERVER. THIS WAS NOT
BLESSED/REVIEWED BY ANY GERRIT DEVELOPERS. YOU'VE BEEN WARNED.

Instructions:
- install the "pgdb" Python module (on Ubuntu I had to "apt-get install
python-pygresql")
- run the script as the user that runs Gerrit (and on the machine running
Gerrit). It assumes certain paths, you may have to point it to your
gerrit_site directory
- do a db dump and save the to-be-removed repository files before running the
script, just to be sure

--
Mihai Rusu

gerritlib.py
remove_repo.py

Swindells, Thomas

unread,
Nov 11, 2011, 4:27:04 AM11/11/11
to Pat Notz, Jay Soffian, Repo and Gerrit Discussion

[Swindells, Thomas] With H2 there is a special table used to keep track of the next change Id (and other unique autogenerated values). Even if you delete the rows new changes will carry on from the same previous number.

Thanks! ~ Pat

--
To unsubscribe, email repo-discuss...@googlegroups.com
More info at http://groups.google.com/group/repo-discuss?hl=en




**************************************************************************************
This message is confidential and intended only for the addressee. If you have received this message in error, please immediately notify the postm...@nds.com and delete it from your system as well as any copies. The content of e-mails as well as traffic data may be monitored by NDS for employment and security purposes. To protect the environment please do not print this e-mail unless necessary.

NDS Limited. Registered Office: One London Road, Staines, Middlesex, TW18 4EX, United Kingdom. A company registered in England and Wales. Registered no. 3080780. VAT no. GB 603 8808 40-00
**************************************************************************************

Pat Notz

unread,
Nov 11, 2011, 2:12:32 PM11/11/11
to Mihai Rusu, repo-d...@googlegroups.com

Mihai -

This was a huge help. I'm still using H2 but this had all the SQL goodness I needed. I slapped together a quick and dirty python script (pillaging from yours) to do this over ssh and via the gsql command. Also, I didn't want to delete the repo off the filesystem (I just blasted the refs/changes/ directory by hand) so this only fixes the database. I'm including the script below for anyone else who may find this useful.  Thanks again everyone ~ Pat

#!/usr/bin/env python
import argparse
import subprocess

def delete_project_sql(project):
  # Build the SQL statement.
  sql = """\
DELETE FROM ref_rights WHERE project_name = '%(project)s';
DELETE FROM account_patch_reviews WHERE change_id IN (
    SELECT change_id FROM changes WHERE dest_project_name = '%(project)s');
DELETE FROM change_messages WHERE change_id IN (
    SELECT change_id FROM changes WHERE dest_project_name = '%(project)s');
DELETE FROM patch_comments WHERE change_id IN (
    SELECT change_id FROM changes WHERE dest_project_name = '%(project)s');
DELETE FROM patch_set_ancestors WHERE change_id IN (
    SELECT change_id FROM changes WHERE dest_project_name = '%(project)s');
DELETE FROM patch_set_approvals WHERE change_id IN (
    SELECT change_id FROM changes WHERE dest_project_name = '%(project)s');
DELETE FROM patch_sets WHERE change_id IN (
    SELECT change_id FROM changes WHERE dest_project_name = '%(project)s');
DELETE FROM starred_changes WHERE change_id IN (
    SELECT change_id FROM changes WHERE dest_project_name = '%(project)s');
DELETE FROM tracking_ids WHERE change_id IN (
    SELECT change_id FROM changes WHERE dest_project_name = '%(project)s');
DELETE FROM changes WHERE dest_project_name = '%(project)s';
DELETE FROM account_project_watches WHERE project_name = '%(project)s';

UPDATE projects SET parent_name = (
    SELECT parent_name FROM projects WHERE name = '%(project)s')
  WHERE parent_name = '%(project)s';
DELETE FROM projects WHERE name = '%(project)s'"""
  sql %= {'project': project}
  return sql
   
if __name__ == "__main__":

    defaultServer = "gerrit.sandia.gov"
    defaultPort = 29418

    parser = argparse.ArgumentParser(description='Probe Gerrit.')
    parser.add_argument('-s', '--server', metavar='SERVER', default=defaultServer, help='Gerrit server name')
    parser.add_argument('-p', '--port', metavar='PORT', type=int, default=defaultPort, help='Gerrit SSH port')
    parser.add_argument('project', metavar='PROJECT', nargs=1, help="Gerrit project to delete")
    args = parser.parse_args()

    print "Deleting project %s:%s/%s" % (args.server, args.port, args.project[0])

    sql = delete_project_sql(args.project[0])
   
    cmd = 'gerrit gsql -c "%s"' % sql
    gcmd = ["ssh", "-p", '%s' % args.port, args.server, cmd]
    response = subprocess.call(gcmd)
    print response

Reply all
Reply to author
Forward
0 new messages