]> Untitled Git - lemmy.git/blob - migrations/2020-07-18-234519_add_unique_community_user_actor_ids/up.sql
routes.api: fix get_captcha endpoint (#1135)
[lemmy.git] / migrations / 2020-07-18-234519_add_unique_community_user_actor_ids / up.sql
1 -- Following this issue : https://github.com/LemmyNet/lemmy/issues/957
2
3 -- Creating a unique changeme actor_id
4 create or replace function generate_unique_changeme() 
5 returns text language sql 
6 as $$
7   select 'changeme_' || string_agg (substr('abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789', ceil (random() * 62)::integer, 1), '')
8   from generate_series(1, 20)
9 $$;
10
11 -- Need to delete the possible community and user dupes for ones that don't start with the fake one
12 -- A few test inserts, to make sure this removes later dupes
13 -- insert into community (name, title, category_id, creator_id) values ('testcom', 'another testcom', 1, 2);
14 delete from community a using (
15   select min(id) as id, actor_id
16     from community 
17     group by actor_id having count(*) > 1
18 ) b
19 where a.actor_id = b.actor_id 
20 and a.id <> b.id;
21
22 delete from user_ a using (
23   select min(id) as id, actor_id
24     from user_ 
25     group by actor_id having count(*) > 1
26 ) b
27 where a.actor_id = b.actor_id 
28 and a.id <> b.id;
29
30 -- Replacing the current default on the columns, to the unique one
31 update community 
32 set actor_id = generate_unique_changeme()
33 where actor_id = 'http://fake.com';
34
35 update user_ 
36 set actor_id = generate_unique_changeme()
37 where actor_id = 'http://fake.com';
38
39 -- Add the unique indexes
40 alter table community alter column actor_id set not null;
41 alter table community alter column actor_id set default generate_unique_changeme();
42
43 alter table user_ alter column actor_id set not null;
44 alter table user_ alter column actor_id set default generate_unique_changeme();
45
46 -- Add lowercase uniqueness too
47 drop index idx_user_name_lower_actor_id;
48 create unique index idx_user_lower_actor_id on user_ (lower(actor_id));
49
50 create unique index idx_community_lower_actor_id on community (lower(actor_id));