From d0bd02eea0d2813df7401d71af7822d6ecacd318 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Sun, 14 Feb 2021 13:46:16 -0500 Subject: [PATCH] Starting on user_ to person migration. --- crates/db_schema/src/source/user.rs | 2 +- .../down.sql | 59 ++++++++++++ .../2021-02-14-041356_split_user_table/up.sql | 93 +++++++++++++++++++ 3 files changed, 153 insertions(+), 1 deletion(-) create mode 100644 migrations/2021-02-14-041356_split_user_table/down.sql create mode 100644 migrations/2021-02-14-041356_split_user_table/up.sql diff --git a/crates/db_schema/src/source/user.rs b/crates/db_schema/src/source/user.rs index cb7ae87d5..17e8734cc 100644 --- a/crates/db_schema/src/source/user.rs +++ b/crates/db_schema/src/source/user.rs @@ -32,7 +32,7 @@ pub struct User_ { pub public_key: Option, // person pub last_refreshed_at: chrono::NaiveDateTime, // person pub banner: Option, // person - pub deleted: bool, // local_user + pub deleted: bool, // person pub inbox_url: Url, // person pub shared_inbox_url: Option, // person } diff --git a/migrations/2021-02-14-041356_split_user_table/down.sql b/migrations/2021-02-14-041356_split_user_table/down.sql new file mode 100644 index 000000000..218aaec2d --- /dev/null +++ b/migrations/2021-02-14-041356_split_user_table/down.sql @@ -0,0 +1,59 @@ +-- user_ table +-- Drop views +drop view person_alias_1, person_alias_2; + +-- Rename indexes +alter index person__pkey rename to user__pkey; +alter index idx_person_actor_id rename to idx_user_actor_id; +alter index idx_person_inbox_url rename to idx_user_inbox_url; +alter index idx_person_lower_actor_id rename to idx_user_lower_actor_id; +alter index idx_person_published rename to idx_user_published; + +-- Rename triggers +alter trigger site_aggregates_person_delete on person rename to site_aggregates_user_delete; +alter trigger site_aggregates_person_insert on person rename to site_aggregates_user_insert; +alter trigger person_aggregates_person on person rename to user_aggregates_user; + +-- Rename the trigger functions +alter function site_aggregates_person_delete() rename to site_aggregates_user_delete; +alter function site_aggregates_person_insert() rename to site_aggregates_user_insert; +alter function person_aggregates_person() rename to user_aggregates_user; + +-- Rename the table back to user_ +alter table person rename to user_; + +-- Add the columns back in +alter table user_ + add column password_encrypted text not null default 'changeme', + add column email text, + add column admin boolean default false not null, + add column show_nsfw boolean default false not null, + add column theme character varying(20) default 'darkly'::character varying not null, + add column default_sort_type smallint default 0 not null, + add column default_listing_type smallint default 1 not null, + add column lang character varying(20) default 'browser'::character varying not null, + add column show_avatars boolean default true not null, + add column send_notifications_to_email boolean default false not null, + add column matrix_user_id text; + +-- Update the user_ table with the local_user data +update user_ u set + password_encrypted = lu.password_encrypted, + email = lu.email, + admin = lu.admin, + show_nsfw = lu.show_nsfw, + theme = lu.theme, + default_sort_type = lu.default_sort_type, + default_listing_type = lu.default_listing_type, + lang = lu.lang, + show_avatars = lu.show_avatars, + send_notifications_to_email = lu.send_notifications_to_email, + matrix_user_id = lu.matrix_user_id +from local_user lu +where lu.user_id = u.id; + +create view user_alias_1 as select * from user_; +create view user_alias_2 as select * from user_; + +drop table local_user; + diff --git a/migrations/2021-02-14-041356_split_user_table/up.sql b/migrations/2021-02-14-041356_split_user_table/up.sql new file mode 100644 index 000000000..7d673c432 --- /dev/null +++ b/migrations/2021-02-14-041356_split_user_table/up.sql @@ -0,0 +1,93 @@ +-- Drop the 2 views user_alias_1, user_alias_2 +drop view user_alias_1, user_alias_2; + +-- rename the user_ table to person +alter table user_ rename to person; + +-- create a new table local_user +create table local_user ( + id serial primary key, + user_id int references person on update cascade on delete cascade not null, + password_encrypted text not null, + email text, + admin boolean default false not null, + show_nsfw boolean default false not null, + theme character varying(20) default 'darkly'::character varying not null, + default_sort_type smallint default 0 not null, + default_listing_type smallint default 1 not null, + lang character varying(20) default 'browser'::character varying not null, + show_avatars boolean default true not null, + send_notifications_to_email boolean default false not null, + matrix_user_id text, + unique (user_id) +); + +-- Copy the local users over to the new table +insert into local_user +( + user_id, + password_encrypted, + email, + admin, + show_nsfw, + theme, + default_sort_type, + default_listing_type, + lang, + show_avatars, + send_notifications_to_email, + matrix_user_id +) +select + id, + password_encrypted, + email, + admin, + show_nsfw, + theme, + default_sort_type, + default_listing_type, + lang, + show_avatars, + send_notifications_to_email, + matrix_user_id +from person +where local = true; + +-- Drop those columns from person +alter table person + drop column password_encrypted, + drop column email, + drop column admin, + drop column show_nsfw, + drop column theme, + drop column default_sort_type, + drop column default_listing_type, + drop column lang, + drop column show_avatars, + drop column send_notifications_to_email, + drop column matrix_user_id; + +-- Rename indexes +alter index user__pkey rename to person__pkey; +alter index idx_user_actor_id rename to idx_person_actor_id; +alter index idx_user_inbox_url rename to idx_person_inbox_url; +alter index idx_user_lower_actor_id rename to idx_person_lower_actor_id; +alter index idx_user_published rename to idx_person_published; + +-- Rename triggers +alter trigger site_aggregates_user_delete on person rename to site_aggregates_person_delete; +alter trigger site_aggregates_user_insert on person rename to site_aggregates_person_insert; +alter trigger user_aggregates_user on person rename to person_aggregates_person; + +-- Rename the trigger functions +alter function site_aggregates_user_delete() rename to site_aggregates_person_delete; +alter function site_aggregates_user_insert() rename to site_aggregates_person_insert; +alter function user_aggregates_user() rename to person_aggregates_person; + +-- Create views +create view person_alias_1 as select * from person; +create view person_alias_2 as select * from person; + +-- Rename every user_id column to person_id +