1 // This is for db migrations that require code
3 sql_types::{Nullable, Text},
9 community::{Community, CommunityForm},
11 private_message::PrivateMessage,
15 use lemmy_db_schema::{
20 user::{UserForm, User_},
24 apub::{generate_actor_keypair, make_apub_endpoint, EndpointType},
30 pub fn run_advanced_migrations(conn: &PgConnection) -> Result<(), LemmyError> {
31 user_updates_2020_04_02(&conn)?;
32 community_updates_2020_04_02(&conn)?;
33 post_updates_2020_04_03(&conn)?;
34 comment_updates_2020_04_03(&conn)?;
35 private_message_updates_2020_05_05(&conn)?;
36 post_thumbnail_url_updates_2020_07_27(&conn)?;
41 fn user_updates_2020_04_02(conn: &PgConnection) -> Result<(), LemmyError> {
42 use lemmy_db_schema::schema::user_::dsl::*;
44 info!("Running user_updates_2020_04_02");
46 // Update the actor_id, private_key, and public_key, last_refreshed_at
47 let incorrect_users = user_
48 .filter(actor_id.like("changeme_%"))
49 .filter(local.eq(true))
50 .load::<User_>(conn)?;
52 sql_query("alter table user_ disable trigger refresh_user").execute(conn)?;
54 for cuser in &incorrect_users {
55 let keypair = generate_actor_keypair()?;
58 name: cuser.name.to_owned(),
59 email: Some(cuser.email.to_owned()),
60 matrix_user_id: Some(cuser.matrix_user_id.to_owned()),
61 avatar: Some(cuser.avatar.to_owned()),
62 banner: Some(cuser.banner.to_owned()),
63 password_encrypted: cuser.password_encrypted.to_owned(),
64 preferred_username: Some(cuser.preferred_username.to_owned()),
65 published: Some(cuser.published),
68 banned: Some(cuser.banned),
69 show_nsfw: cuser.show_nsfw,
70 theme: cuser.theme.to_owned(),
71 default_sort_type: cuser.default_sort_type,
72 default_listing_type: cuser.default_listing_type,
73 lang: cuser.lang.to_owned(),
74 show_avatars: cuser.show_avatars,
75 send_notifications_to_email: cuser.send_notifications_to_email,
76 actor_id: Some(make_apub_endpoint(EndpointType::User, &cuser.name).to_string()),
77 bio: Some(cuser.bio.to_owned()),
79 private_key: Some(keypair.private_key),
80 public_key: Some(keypair.public_key),
81 last_refreshed_at: Some(naive_now()),
84 User_::update(&conn, cuser.id, &form)?;
87 sql_query("alter table user_ enable trigger refresh_user").execute(conn)?;
89 info!("{} user rows updated.", incorrect_users.len());
94 fn community_updates_2020_04_02(conn: &PgConnection) -> Result<(), LemmyError> {
95 use lemmy_db_schema::schema::community::dsl::*;
97 info!("Running community_updates_2020_04_02");
99 // Update the actor_id, private_key, and public_key, last_refreshed_at
100 let incorrect_communities = community
101 .filter(actor_id.like("changeme_%"))
102 .filter(local.eq(true))
103 .load::<Community>(conn)?;
105 sql_query("alter table community disable trigger refresh_community").execute(conn)?;
107 for ccommunity in &incorrect_communities {
108 let keypair = generate_actor_keypair()?;
110 let form = CommunityForm {
111 name: ccommunity.name.to_owned(),
112 title: ccommunity.title.to_owned(),
113 description: ccommunity.description.to_owned(),
114 category_id: ccommunity.category_id,
115 creator_id: ccommunity.creator_id,
118 nsfw: ccommunity.nsfw,
120 actor_id: Some(make_apub_endpoint(EndpointType::Community, &ccommunity.name).to_string()),
121 local: ccommunity.local,
122 private_key: Some(keypair.private_key),
123 public_key: Some(keypair.public_key),
124 last_refreshed_at: Some(naive_now()),
126 icon: Some(ccommunity.icon.to_owned()),
127 banner: Some(ccommunity.banner.to_owned()),
130 Community::update(&conn, ccommunity.id, &form)?;
133 sql_query("alter table community enable trigger refresh_community").execute(conn)?;
135 info!("{} community rows updated.", incorrect_communities.len());
140 fn post_updates_2020_04_03(conn: &PgConnection) -> Result<(), LemmyError> {
141 use lemmy_db_schema::schema::post::dsl::*;
143 info!("Running post_updates_2020_04_03");
146 let incorrect_posts = post
147 .filter(ap_id.eq("changeme_%"))
148 .filter(local.eq(true))
149 .load::<Post>(conn)?;
151 sql_query("alter table post disable trigger refresh_post").execute(conn)?;
153 for cpost in &incorrect_posts {
154 let apub_id = make_apub_endpoint(EndpointType::Post, &cpost.id.to_string()).to_string();
155 Post::update_ap_id(&conn, cpost.id, apub_id)?;
158 info!("{} post rows updated.", incorrect_posts.len());
160 sql_query("alter table post enable trigger refresh_post").execute(conn)?;
165 fn comment_updates_2020_04_03(conn: &PgConnection) -> Result<(), LemmyError> {
166 use lemmy_db_schema::schema::comment::dsl::*;
168 info!("Running comment_updates_2020_04_03");
171 let incorrect_comments = comment
172 .filter(ap_id.eq("changeme_%"))
173 .filter(local.eq(true))
174 .load::<Comment>(conn)?;
176 sql_query("alter table comment disable trigger refresh_comment").execute(conn)?;
178 for ccomment in &incorrect_comments {
179 let apub_id = make_apub_endpoint(EndpointType::Comment, &ccomment.id.to_string()).to_string();
180 Comment::update_ap_id(&conn, ccomment.id, apub_id)?;
183 sql_query("alter table comment enable trigger refresh_comment").execute(conn)?;
185 info!("{} comment rows updated.", incorrect_comments.len());
190 fn private_message_updates_2020_05_05(conn: &PgConnection) -> Result<(), LemmyError> {
191 use lemmy_db_schema::schema::private_message::dsl::*;
193 info!("Running private_message_updates_2020_05_05");
196 let incorrect_pms = private_message
197 .filter(ap_id.eq("changeme_%"))
198 .filter(local.eq(true))
199 .load::<PrivateMessage>(conn)?;
201 for cpm in &incorrect_pms {
202 let apub_id = make_apub_endpoint(EndpointType::PrivateMessage, &cpm.id.to_string()).to_string();
203 PrivateMessage::update_ap_id(&conn, cpm.id, apub_id)?;
206 info!("{} private message rows updated.", incorrect_pms.len());
211 fn post_thumbnail_url_updates_2020_07_27(conn: &PgConnection) -> Result<(), LemmyError> {
212 use lemmy_db_schema::schema::post::dsl::*;
214 info!("Running post_thumbnail_url_updates_2020_07_27");
216 let domain_prefix = format!(
218 Settings::get().get_protocol_and_hostname(),
221 let incorrect_thumbnails = post.filter(thumbnail_url.not_like("http%"));
223 // Prepend the rows with the update
224 let res = diesel::update(incorrect_thumbnails)
228 .into_sql::<Nullable<Text>>()
229 .concat(thumbnail_url),
232 .get_results::<Post>(conn)?;
234 info!("{} Post thumbnail_url rows updated.", res.len());