Skip to content

Commit

Permalink
team name change on deletion (linkedin#188)
Browse files Browse the repository at this point in the history
* on delete change team name to uuid to avoid future conflicts

* string formating

* requested changes

* close connection
  • Loading branch information
diegocepedaw authored and dwang159 committed Jul 30, 2018
1 parent 566801f commit 5ab4685
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
16 changes: 16 additions & 0 deletions db/schema.v0.sql
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,22 @@ CREATE TABLE IF NOT EXISTS `team` (
PRIMARY KEY (`id`),
UNIQUE INDEX `name_unique` (`name` ASC));

-- -----------------------------------------------------
-- Table `deleted_team`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `deleted_team` (
`team_id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
`new_name` VARCHAR(255) NOT NULL,
`old_name` VARCHAR(255) NOT NULL,
`deletion_date` BIGINT(20) NOT NULL,
PRIMARY KEY (`team_id`),
CONSTRAINT `deleted_team_team_fk`
FOREIGN KEY (`team_id`)
REFERENCES `team` (`id`)
ON DELETE CASCADE
ON UPDATE CASCADE,
UNIQUE INDEX `new_name_unique` (`new_name` ASC));

-- -----------------------------------------------------
-- Table `user`
-- -----------------------------------------------------
Expand Down
21 changes: 17 additions & 4 deletions src/oncall/api/v0/team.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright (c) LinkedIn Corporation. All rights reserved. Licensed under the BSD-2 Clause license.
# See LICENSE in the project root for license information.

import uuid
import time
from urllib import unquote
from falcon import HTTPNotFound, HTTPBadRequest, HTTPError
from ujson import dumps as json_dumps
Expand Down Expand Up @@ -244,6 +245,8 @@ def on_delete(req, resp, team):
:statuscode 404: Team not found
'''
team = unquote(team)
new_team = str(uuid.uuid4())
deletion_date = time.time()
check_team_auth(team, req)
connection = db.connect()
cursor = connection.cursor()
Expand All @@ -253,9 +256,19 @@ def on_delete(req, resp, team):
'AND `start` > UNIX_TIMESTAMP()', team)
create_audit({}, team, TEAM_DELETED, req, cursor)
deleted = cursor.rowcount
connection.commit()
cursor.close()
connection.close()

if deleted == 0:
connection.commit()
cursor.close()
connection.close()
raise HTTPNotFound()

cursor.execute('SELECT `id` FROM `team` WHERE `name`=%s', team)
team_id = cursor.fetchone()

# create entry in deleted_teams and then change name in team to preserve a clean namespace
cursor.execute('UPDATE `team` SET `name` = %s WHERE `name`= %s', (new_team, team))
cursor.execute('INSERT INTO `deleted_team` (team_id, new_name, old_name, deletion_date) VALUES (%s, %s, %s, %s)', (team_id, new_team, team, deletion_date))
connection.commit()
cursor.close()
connection.close()

0 comments on commit 5ab4685

Please sign in to comment.