broken migration 20150306023106_fix_namespace_duplication
Created by: thechile
While upgrading out Gitlab server from 6.9 to 7.14, a migrate script introduced in 7.8 called db/migrate/20150306023106_fix_namespace_duplication.rb
caused one of our namespaces to be wiped out.
This was caused because we have both a user AND a namespace with the exact same name.
So the SELECT MAX(id) statement which assumed the namespace with highest ID was the good one, proceeded to consolidate all other namespace of the same name into the users namespace and then deleted the group namespace.
Below is info on the script being run manually. The namespace id=849 was wrongly selected as the one to keep and the other projects were then assigned to it. Where in fact it's type is NULL which i guess is a user namespace.
Queries and result
SELECT MAX(id) max, COUNT(id) cnt, name FROM namespaces GROUP BY name HAVING COUNT(id) > 1;
+------+-----+------+
| max | cnt | name |
+------+-----+------+
| 849 | 2 | PUP |
+------+-----+------+
SELECT id,name FROM namespaces WHERE name = 'pup';
+-----+------+
| id | name |
+-----+------+
| 483 | PUP | <--- This is the Group
| 849 | pup | <--- This is the User
+-----+------+
select count(*) from projects where namespace_id=483;
+----------+
| count(*) |
+----------+
| 16 |
+----------+
select count(*) from projects where namespace_id=849;
+----------+
| count(*) |
+----------+
| 0 |
+----------+
SELECT * FROM namespaces WHERE name = 'pup'\G
*************************** 1. row ***************************
id: 483
name: PUP
path: tts-go-ps
owner_id: NULL
created_at: 2014-09-19 13:15:00
updated_at: 2015-08-20 15:18:05
type: Group
description:
avatar: go96.png
*************************** 2. row ***************************
id: 849
name: pup
path: pup
owner_id: 739
created_at: 2015-05-11 14:18:37
updated_at: 2015-05-11 14:18:37
type: NULL
description:
avatar: NULL