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 user::{UserForm, User_},
20 apub::{generate_actor_keypair, make_apub_endpoint, EndpointType},
26 pub fn run_advanced_migrations(conn: &PgConnection) -> Result<(), LemmyError> {
27 user_updates_2020_04_02(&conn)?;
28 community_updates_2020_04_02(&conn)?;
29 post_updates_2020_04_03(&conn)?;
30 comment_updates_2020_04_03(&conn)?;
31 private_message_updates_2020_05_05(&conn)?;
32 post_thumbnail_url_updates_2020_07_27(&conn)?;
37 fn user_updates_2020_04_02(conn: &PgConnection) -> Result<(), LemmyError> {
38 use lemmy_db_schema::schema::user_::dsl::*;
40 info!("Running user_updates_2020_04_02");
42 // Update the actor_id, private_key, and public_key, last_refreshed_at
43 let incorrect_users = user_
44 .filter(actor_id.like("changeme_%"))
45 .filter(local.eq(true))
46 .load::<User_>(conn)?;
48 sql_query("alter table user_ disable trigger refresh_user").execute(conn)?;
50 for cuser in &incorrect_users {
51 let keypair = generate_actor_keypair()?;
54 name: cuser.name.to_owned(),
55 email: Some(cuser.email.to_owned()),
56 matrix_user_id: Some(cuser.matrix_user_id.to_owned()),
57 avatar: Some(cuser.avatar.to_owned()),
58 banner: Some(cuser.banner.to_owned()),
59 password_encrypted: cuser.password_encrypted.to_owned(),
60 preferred_username: Some(cuser.preferred_username.to_owned()),
61 published: Some(cuser.published),
64 banned: Some(cuser.banned),
65 show_nsfw: cuser.show_nsfw,
66 theme: cuser.theme.to_owned(),
67 default_sort_type: cuser.default_sort_type,
68 default_listing_type: cuser.default_listing_type,
69 lang: cuser.lang.to_owned(),
70 show_avatars: cuser.show_avatars,
71 send_notifications_to_email: cuser.send_notifications_to_email,
72 actor_id: Some(make_apub_endpoint(EndpointType::User, &cuser.name).to_string()),
73 bio: Some(cuser.bio.to_owned()),
75 private_key: Some(keypair.private_key),
76 public_key: Some(keypair.public_key),
77 last_refreshed_at: Some(naive_now()),
80 User_::update(&conn, cuser.id, &form)?;
83 sql_query("alter table user_ enable trigger refresh_user").execute(conn)?;
85 info!("{} user rows updated.", incorrect_users.len());
90 fn community_updates_2020_04_02(conn: &PgConnection) -> Result<(), LemmyError> {
91 use lemmy_db_schema::schema::community::dsl::*;
93 info!("Running community_updates_2020_04_02");
95 // Update the actor_id, private_key, and public_key, last_refreshed_at
96 let incorrect_communities = community
97 .filter(actor_id.like("changeme_%"))
98 .filter(local.eq(true))
99 .load::<Community>(conn)?;
101 sql_query("alter table community disable trigger refresh_community").execute(conn)?;
103 for ccommunity in &incorrect_communities {
104 let keypair = generate_actor_keypair()?;
106 let form = CommunityForm {
107 name: ccommunity.name.to_owned(),
108 title: ccommunity.title.to_owned(),
109 description: ccommunity.description.to_owned(),
110 category_id: ccommunity.category_id,
111 creator_id: ccommunity.creator_id,
114 nsfw: ccommunity.nsfw,
116 actor_id: Some(make_apub_endpoint(EndpointType::Community, &ccommunity.name).to_string()),
117 local: ccommunity.local,
118 private_key: Some(keypair.private_key),
119 public_key: Some(keypair.public_key),
120 last_refreshed_at: Some(naive_now()),
122 icon: Some(ccommunity.icon.to_owned()),
123 banner: Some(ccommunity.banner.to_owned()),
126 Community::update(&conn, ccommunity.id, &form)?;
129 sql_query("alter table community enable trigger refresh_community").execute(conn)?;
131 info!("{} community rows updated.", incorrect_communities.len());
136 fn post_updates_2020_04_03(conn: &PgConnection) -> Result<(), LemmyError> {
137 use lemmy_db_schema::schema::post::dsl::*;
139 info!("Running post_updates_2020_04_03");
142 let incorrect_posts = post
143 .filter(ap_id.eq("changeme_%"))
144 .filter(local.eq(true))
145 .load::<Post>(conn)?;
147 sql_query("alter table post disable trigger refresh_post").execute(conn)?;
149 for cpost in &incorrect_posts {
150 let apub_id = make_apub_endpoint(EndpointType::Post, &cpost.id.to_string()).to_string();
151 Post::update_ap_id(&conn, cpost.id, apub_id)?;
154 info!("{} post rows updated.", incorrect_posts.len());
156 sql_query("alter table post enable trigger refresh_post").execute(conn)?;
161 fn comment_updates_2020_04_03(conn: &PgConnection) -> Result<(), LemmyError> {
162 use lemmy_db_schema::schema::comment::dsl::*;
164 info!("Running comment_updates_2020_04_03");
167 let incorrect_comments = comment
168 .filter(ap_id.eq("changeme_%"))
169 .filter(local.eq(true))
170 .load::<Comment>(conn)?;
172 sql_query("alter table comment disable trigger refresh_comment").execute(conn)?;
174 for ccomment in &incorrect_comments {
175 let apub_id = make_apub_endpoint(EndpointType::Comment, &ccomment.id.to_string()).to_string();
176 Comment::update_ap_id(&conn, ccomment.id, apub_id)?;
179 sql_query("alter table comment enable trigger refresh_comment").execute(conn)?;
181 info!("{} comment rows updated.", incorrect_comments.len());
186 fn private_message_updates_2020_05_05(conn: &PgConnection) -> Result<(), LemmyError> {
187 use lemmy_db_schema::schema::private_message::dsl::*;
189 info!("Running private_message_updates_2020_05_05");
192 let incorrect_pms = private_message
193 .filter(ap_id.eq("changeme_%"))
194 .filter(local.eq(true))
195 .load::<PrivateMessage>(conn)?;
197 for cpm in &incorrect_pms {
198 let apub_id = make_apub_endpoint(EndpointType::PrivateMessage, &cpm.id.to_string()).to_string();
199 PrivateMessage::update_ap_id(&conn, cpm.id, apub_id)?;
202 info!("{} private message rows updated.", incorrect_pms.len());
207 fn post_thumbnail_url_updates_2020_07_27(conn: &PgConnection) -> Result<(), LemmyError> {
208 use lemmy_db_schema::schema::post::dsl::*;
210 info!("Running post_thumbnail_url_updates_2020_07_27");
212 let domain_prefix = format!(
214 Settings::get().get_protocol_and_hostname(),
217 let incorrect_thumbnails = post.filter(thumbnail_url.not_like("http%"));
219 // Prepend the rows with the update
220 let res = diesel::update(incorrect_thumbnails)
224 .into_sql::<Nullable<Text>>()
225 .concat(thumbnail_url),
228 .get_results::<Post>(conn)?;
230 info!("{} Post thumbnail_url rows updated.", res.len());