]> Untitled Git - lemmy.git/blob - src/code_migrations.rs
Merge remote-tracking branch 'yerba/main' into main
[lemmy.git] / src / code_migrations.rs
1 // This is for db migrations that require code
2 use diesel::{
3   sql_types::{Nullable, Text},
4   *,
5 };
6 use lemmy_db::{
7   comment::Comment,
8   community::{Community, CommunityForm},
9   naive_now,
10   post::Post,
11   private_message::PrivateMessage,
12   user::{UserForm, User_},
13   Crud,
14 };
15 use lemmy_utils::{
16   apub::{generate_actor_keypair, get_apub_protocol_string, make_apub_endpoint, EndpointType},
17   settings::Settings,
18   LemmyError,
19 };
20 use log::info;
21
22 pub fn run_advanced_migrations(conn: &PgConnection) -> Result<(), LemmyError> {
23   user_updates_2020_04_02(&conn)?;
24   community_updates_2020_04_02(&conn)?;
25   post_updates_2020_04_03(&conn)?;
26   comment_updates_2020_04_03(&conn)?;
27   private_message_updates_2020_05_05(&conn)?;
28   post_thumbnail_url_updates_2020_07_27(&conn)?;
29
30   Ok(())
31 }
32
33 fn user_updates_2020_04_02(conn: &PgConnection) -> Result<(), LemmyError> {
34   use lemmy_db::schema::user_::dsl::*;
35
36   info!("Running user_updates_2020_04_02");
37
38   // Update the actor_id, private_key, and public_key, last_refreshed_at
39   let incorrect_users = user_
40     .filter(actor_id.like("changeme_%"))
41     .filter(local.eq(true))
42     .load::<User_>(conn)?;
43
44   sql_query("alter table user_ disable trigger refresh_user").execute(conn)?;
45
46   for cuser in &incorrect_users {
47     let keypair = generate_actor_keypair()?;
48
49     let form = UserForm {
50       name: cuser.name.to_owned(),
51       email: Some(cuser.email.to_owned()),
52       matrix_user_id: cuser.matrix_user_id.to_owned(),
53       avatar: Some(cuser.avatar.to_owned()),
54       banner: Some(cuser.banner.to_owned()),
55       password_encrypted: cuser.password_encrypted.to_owned(),
56       preferred_username: cuser.preferred_username.to_owned(),
57       published: Some(cuser.published),
58       updated: None,
59       admin: cuser.admin,
60       banned: cuser.banned,
61       show_nsfw: cuser.show_nsfw,
62       theme: cuser.theme.to_owned(),
63       default_sort_type: cuser.default_sort_type,
64       default_listing_type: cuser.default_listing_type,
65       lang: cuser.lang.to_owned(),
66       show_avatars: cuser.show_avatars,
67       send_notifications_to_email: cuser.send_notifications_to_email,
68       actor_id: Some(make_apub_endpoint(EndpointType::User, &cuser.name).to_string()),
69       bio: cuser.bio.to_owned(),
70       local: cuser.local,
71       private_key: Some(keypair.private_key),
72       public_key: Some(keypair.public_key),
73       last_refreshed_at: Some(naive_now()),
74     };
75
76     User_::update(&conn, cuser.id, &form)?;
77   }
78
79   sql_query("alter table user_ enable trigger refresh_user").execute(conn)?;
80
81   info!("{} user rows updated.", incorrect_users.len());
82
83   Ok(())
84 }
85
86 fn community_updates_2020_04_02(conn: &PgConnection) -> Result<(), LemmyError> {
87   use lemmy_db::schema::community::dsl::*;
88
89   info!("Running community_updates_2020_04_02");
90
91   // Update the actor_id, private_key, and public_key, last_refreshed_at
92   let incorrect_communities = community
93     .filter(actor_id.like("changeme_%"))
94     .filter(local.eq(true))
95     .load::<Community>(conn)?;
96
97   sql_query("alter table community disable trigger refresh_community").execute(conn)?;
98
99   for ccommunity in &incorrect_communities {
100     let keypair = generate_actor_keypair()?;
101
102     let form = CommunityForm {
103       name: ccommunity.name.to_owned(),
104       title: ccommunity.title.to_owned(),
105       description: ccommunity.description.to_owned(),
106       category_id: ccommunity.category_id,
107       creator_id: ccommunity.creator_id,
108       removed: None,
109       deleted: None,
110       nsfw: ccommunity.nsfw,
111       updated: None,
112       actor_id: Some(make_apub_endpoint(EndpointType::Community, &ccommunity.name).to_string()),
113       local: ccommunity.local,
114       private_key: Some(keypair.private_key),
115       public_key: Some(keypair.public_key),
116       last_refreshed_at: Some(naive_now()),
117       published: None,
118       icon: Some(ccommunity.icon.to_owned()),
119       banner: Some(ccommunity.banner.to_owned()),
120     };
121
122     Community::update(&conn, ccommunity.id, &form)?;
123   }
124
125   sql_query("alter table community enable trigger refresh_community").execute(conn)?;
126
127   info!("{} community rows updated.", incorrect_communities.len());
128
129   Ok(())
130 }
131
132 fn post_updates_2020_04_03(conn: &PgConnection) -> Result<(), LemmyError> {
133   use lemmy_db::schema::post::dsl::*;
134
135   info!("Running post_updates_2020_04_03");
136
137   // Update the ap_id
138   let incorrect_posts = post
139     .filter(ap_id.eq("changeme_%"))
140     .filter(local.eq(true))
141     .load::<Post>(conn)?;
142
143   sql_query("alter table post disable trigger refresh_post").execute(conn)?;
144
145   for cpost in &incorrect_posts {
146     let apub_id = make_apub_endpoint(EndpointType::Post, &cpost.id.to_string()).to_string();
147     Post::update_ap_id(&conn, cpost.id, apub_id)?;
148   }
149
150   info!("{} post rows updated.", incorrect_posts.len());
151
152   sql_query("alter table post enable trigger refresh_post").execute(conn)?;
153
154   Ok(())
155 }
156
157 fn comment_updates_2020_04_03(conn: &PgConnection) -> Result<(), LemmyError> {
158   use lemmy_db::schema::comment::dsl::*;
159
160   info!("Running comment_updates_2020_04_03");
161
162   // Update the ap_id
163   let incorrect_comments = comment
164     .filter(ap_id.eq("changeme_%"))
165     .filter(local.eq(true))
166     .load::<Comment>(conn)?;
167
168   sql_query("alter table comment disable trigger refresh_comment").execute(conn)?;
169
170   for ccomment in &incorrect_comments {
171     let apub_id = make_apub_endpoint(EndpointType::Comment, &ccomment.id.to_string()).to_string();
172     Comment::update_ap_id(&conn, ccomment.id, apub_id)?;
173   }
174
175   sql_query("alter table comment enable trigger refresh_comment").execute(conn)?;
176
177   info!("{} comment rows updated.", incorrect_comments.len());
178
179   Ok(())
180 }
181
182 fn private_message_updates_2020_05_05(conn: &PgConnection) -> Result<(), LemmyError> {
183   use lemmy_db::schema::private_message::dsl::*;
184
185   info!("Running private_message_updates_2020_05_05");
186
187   // Update the ap_id
188   let incorrect_pms = private_message
189     .filter(ap_id.eq("changeme_%"))
190     .filter(local.eq(true))
191     .load::<PrivateMessage>(conn)?;
192
193   for cpm in &incorrect_pms {
194     let apub_id = make_apub_endpoint(EndpointType::PrivateMessage, &cpm.id.to_string()).to_string();
195     PrivateMessage::update_ap_id(&conn, cpm.id, apub_id)?;
196   }
197
198   info!("{} private message rows updated.", incorrect_pms.len());
199
200   Ok(())
201 }
202
203 fn post_thumbnail_url_updates_2020_07_27(conn: &PgConnection) -> Result<(), LemmyError> {
204   use lemmy_db::schema::post::dsl::*;
205
206   info!("Running post_thumbnail_url_updates_2020_07_27");
207
208   let domain_prefix = format!(
209     "{}://{}/pictrs/image/",
210     get_apub_protocol_string(),
211     Settings::get().hostname
212   );
213
214   let incorrect_thumbnails = post.filter(thumbnail_url.not_like("http%"));
215
216   // Prepend the rows with the update
217   let res = diesel::update(incorrect_thumbnails)
218     .set(
219       thumbnail_url.eq(
220         domain_prefix
221           .into_sql::<Nullable<Text>>()
222           .concat(thumbnail_url),
223       ),
224     )
225     .get_results::<Post>(conn)?;
226
227   info!("{} Post thumbnail_url rows updated.", res.len());
228
229   Ok(())
230 }