1 // This is for db migrations that require code
3 sql_types::{Nullable, Text},
7 source::{comment::Comment_, post::Post_, private_message::PrivateMessage_},
10 use lemmy_db_schema::{
14 community::{Community, CommunityForm},
16 private_message::PrivateMessage,
17 user::{UserForm, User_},
21 apub::{generate_actor_keypair, make_apub_endpoint, EndpointType},
27 pub fn run_advanced_migrations(conn: &PgConnection) -> Result<(), LemmyError> {
28 user_updates_2020_04_02(&conn)?;
29 community_updates_2020_04_02(&conn)?;
30 post_updates_2020_04_03(&conn)?;
31 comment_updates_2020_04_03(&conn)?;
32 private_message_updates_2020_05_05(&conn)?;
33 post_thumbnail_url_updates_2020_07_27(&conn)?;
38 fn user_updates_2020_04_02(conn: &PgConnection) -> Result<(), LemmyError> {
39 use lemmy_db_schema::schema::user_::dsl::*;
41 info!("Running user_updates_2020_04_02");
43 // Update the actor_id, private_key, and public_key, last_refreshed_at
44 let incorrect_users = user_
45 .filter(actor_id.like("changeme_%"))
46 .filter(local.eq(true))
47 .load::<User_>(conn)?;
49 sql_query("alter table user_ disable trigger refresh_user").execute(conn)?;
51 for cuser in &incorrect_users {
52 let keypair = generate_actor_keypair()?;
55 name: cuser.name.to_owned(),
56 email: Some(cuser.email.to_owned()),
57 matrix_user_id: Some(cuser.matrix_user_id.to_owned()),
58 avatar: Some(cuser.avatar.to_owned()),
59 banner: Some(cuser.banner.to_owned()),
60 password_encrypted: cuser.password_encrypted.to_owned(),
61 preferred_username: Some(cuser.preferred_username.to_owned()),
62 published: Some(cuser.published),
65 banned: Some(cuser.banned),
66 show_nsfw: cuser.show_nsfw,
67 theme: cuser.theme.to_owned(),
68 default_sort_type: cuser.default_sort_type,
69 default_listing_type: cuser.default_listing_type,
70 lang: cuser.lang.to_owned(),
71 show_avatars: cuser.show_avatars,
72 send_notifications_to_email: cuser.send_notifications_to_email,
73 actor_id: Some(make_apub_endpoint(EndpointType::User, &cuser.name).to_string()),
74 bio: Some(cuser.bio.to_owned()),
76 private_key: Some(keypair.private_key),
77 public_key: Some(keypair.public_key),
78 last_refreshed_at: Some(naive_now()),
81 User_::update(&conn, cuser.id, &form)?;
84 sql_query("alter table user_ enable trigger refresh_user").execute(conn)?;
86 info!("{} user rows updated.", incorrect_users.len());
91 fn community_updates_2020_04_02(conn: &PgConnection) -> Result<(), LemmyError> {
92 use lemmy_db_schema::schema::community::dsl::*;
94 info!("Running community_updates_2020_04_02");
96 // Update the actor_id, private_key, and public_key, last_refreshed_at
97 let incorrect_communities = community
98 .filter(actor_id.like("changeme_%"))
99 .filter(local.eq(true))
100 .load::<Community>(conn)?;
102 sql_query("alter table community disable trigger refresh_community").execute(conn)?;
104 for ccommunity in &incorrect_communities {
105 let keypair = generate_actor_keypair()?;
107 let form = CommunityForm {
108 name: ccommunity.name.to_owned(),
109 title: ccommunity.title.to_owned(),
110 description: ccommunity.description.to_owned(),
111 category_id: ccommunity.category_id,
112 creator_id: ccommunity.creator_id,
115 nsfw: ccommunity.nsfw,
117 actor_id: Some(make_apub_endpoint(EndpointType::Community, &ccommunity.name).to_string()),
118 local: ccommunity.local,
119 private_key: Some(keypair.private_key),
120 public_key: Some(keypair.public_key),
121 last_refreshed_at: Some(naive_now()),
123 icon: Some(ccommunity.icon.to_owned()),
124 banner: Some(ccommunity.banner.to_owned()),
127 Community::update(&conn, ccommunity.id, &form)?;
130 sql_query("alter table community enable trigger refresh_community").execute(conn)?;
132 info!("{} community rows updated.", incorrect_communities.len());
137 fn post_updates_2020_04_03(conn: &PgConnection) -> Result<(), LemmyError> {
138 use lemmy_db_schema::schema::post::dsl::*;
140 info!("Running post_updates_2020_04_03");
143 let incorrect_posts = post
144 .filter(ap_id.eq("changeme_%"))
145 .filter(local.eq(true))
146 .load::<Post>(conn)?;
148 sql_query("alter table post disable trigger refresh_post").execute(conn)?;
150 for cpost in &incorrect_posts {
151 let apub_id = make_apub_endpoint(EndpointType::Post, &cpost.id.to_string()).to_string();
152 Post::update_ap_id(&conn, cpost.id, apub_id)?;
155 info!("{} post rows updated.", incorrect_posts.len());
157 sql_query("alter table post enable trigger refresh_post").execute(conn)?;
162 fn comment_updates_2020_04_03(conn: &PgConnection) -> Result<(), LemmyError> {
163 use lemmy_db_schema::schema::comment::dsl::*;
165 info!("Running comment_updates_2020_04_03");
168 let incorrect_comments = comment
169 .filter(ap_id.eq("changeme_%"))
170 .filter(local.eq(true))
171 .load::<Comment>(conn)?;
173 sql_query("alter table comment disable trigger refresh_comment").execute(conn)?;
175 for ccomment in &incorrect_comments {
176 let apub_id = make_apub_endpoint(EndpointType::Comment, &ccomment.id.to_string()).to_string();
177 Comment::update_ap_id(&conn, ccomment.id, apub_id)?;
180 sql_query("alter table comment enable trigger refresh_comment").execute(conn)?;
182 info!("{} comment rows updated.", incorrect_comments.len());
187 fn private_message_updates_2020_05_05(conn: &PgConnection) -> Result<(), LemmyError> {
188 use lemmy_db_schema::schema::private_message::dsl::*;
190 info!("Running private_message_updates_2020_05_05");
193 let incorrect_pms = private_message
194 .filter(ap_id.eq("changeme_%"))
195 .filter(local.eq(true))
196 .load::<PrivateMessage>(conn)?;
198 for cpm in &incorrect_pms {
199 let apub_id = make_apub_endpoint(EndpointType::PrivateMessage, &cpm.id.to_string()).to_string();
200 PrivateMessage::update_ap_id(&conn, cpm.id, apub_id)?;
203 info!("{} private message rows updated.", incorrect_pms.len());
208 fn post_thumbnail_url_updates_2020_07_27(conn: &PgConnection) -> Result<(), LemmyError> {
209 use lemmy_db_schema::schema::post::dsl::*;
211 info!("Running post_thumbnail_url_updates_2020_07_27");
213 let domain_prefix = format!(
215 Settings::get().get_protocol_and_hostname(),
218 let incorrect_thumbnails = post.filter(thumbnail_url.not_like("http%"));
220 // Prepend the rows with the update
221 let res = diesel::update(incorrect_thumbnails)
225 .into_sql::<Nullable<Text>>()
226 .concat(thumbnail_url),
229 .get_results::<Post>(conn)?;
231 info!("{} Post thumbnail_url rows updated.", res.len());