1 -- Following this issue : https://github.com/LemmyNet/lemmy/issues/957
3 -- Creating a unique changeme actor_id
4 create or replace function generate_unique_changeme()
5 returns text language sql
7 select 'changeme_' || string_agg (substr('abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789', ceil (random() * 62)::integer, 1), '')
8 from generate_series(1, 20)
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
17 group by actor_id having count(*) > 1
19 where a.actor_id = b.actor_id
22 delete from user_ a using (
23 select min(id) as id, actor_id
25 group by actor_id having count(*) > 1
27 where a.actor_id = b.actor_id
30 -- Replacing the current default on the columns, to the unique one
32 set actor_id = generate_unique_changeme()
33 where actor_id = 'http://fake.com';
36 set actor_id = generate_unique_changeme()
37 where actor_id = 'http://fake.com';
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();
43 alter table user_ alter column actor_id set not null;
44 alter table user_ alter column actor_id set default generate_unique_changeme();
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));
50 create unique index idx_community_lower_actor_id on community (lower(actor_id));