]> Untitled Git - lemmy.git/blob - src/code_migrations.rs
Move user to lemmy_db_schema, create traits for impls
[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   source::{
8     comment::Comment_,
9     community::{Community, CommunityForm},
10     post::Post_,
11     private_message::PrivateMessage,
12   },
13   Crud,
14 };
15 use lemmy_db_schema::{
16   naive_now,
17   source::{
18     comment::Comment,
19     post::Post,
20     user::{UserForm, User_},
21   },
22 };
23 use lemmy_utils::{
24   apub::{generate_actor_keypair, make_apub_endpoint, EndpointType},
25   settings::Settings,
26   LemmyError,
27 };
28 use log::info;
29
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)?;
37
38   Ok(())
39 }
40
41 fn user_updates_2020_04_02(conn: &PgConnection) -> Result<(), LemmyError> {
42   use lemmy_db_schema::schema::user_::dsl::*;
43
44   info!("Running user_updates_2020_04_02");
45
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)?;
51
52   sql_query("alter table user_ disable trigger refresh_user").execute(conn)?;
53
54   for cuser in &incorrect_users {
55     let keypair = generate_actor_keypair()?;
56
57     let form = UserForm {
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),
66       updated: None,
67       admin: cuser.admin,
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()),
78       local: cuser.local,
79       private_key: Some(keypair.private_key),
80       public_key: Some(keypair.public_key),
81       last_refreshed_at: Some(naive_now()),
82     };
83
84     User_::update(&conn, cuser.id, &form)?;
85   }
86
87   sql_query("alter table user_ enable trigger refresh_user").execute(conn)?;
88
89   info!("{} user rows updated.", incorrect_users.len());
90
91   Ok(())
92 }
93
94 fn community_updates_2020_04_02(conn: &PgConnection) -> Result<(), LemmyError> {
95   use lemmy_db_schema::schema::community::dsl::*;
96
97   info!("Running community_updates_2020_04_02");
98
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)?;
104
105   sql_query("alter table community disable trigger refresh_community").execute(conn)?;
106
107   for ccommunity in &incorrect_communities {
108     let keypair = generate_actor_keypair()?;
109
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,
116       removed: None,
117       deleted: None,
118       nsfw: ccommunity.nsfw,
119       updated: None,
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()),
125       published: None,
126       icon: Some(ccommunity.icon.to_owned()),
127       banner: Some(ccommunity.banner.to_owned()),
128     };
129
130     Community::update(&conn, ccommunity.id, &form)?;
131   }
132
133   sql_query("alter table community enable trigger refresh_community").execute(conn)?;
134
135   info!("{} community rows updated.", incorrect_communities.len());
136
137   Ok(())
138 }
139
140 fn post_updates_2020_04_03(conn: &PgConnection) -> Result<(), LemmyError> {
141   use lemmy_db_schema::schema::post::dsl::*;
142
143   info!("Running post_updates_2020_04_03");
144
145   // Update the ap_id
146   let incorrect_posts = post
147     .filter(ap_id.eq("changeme_%"))
148     .filter(local.eq(true))
149     .load::<Post>(conn)?;
150
151   sql_query("alter table post disable trigger refresh_post").execute(conn)?;
152
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)?;
156   }
157
158   info!("{} post rows updated.", incorrect_posts.len());
159
160   sql_query("alter table post enable trigger refresh_post").execute(conn)?;
161
162   Ok(())
163 }
164
165 fn comment_updates_2020_04_03(conn: &PgConnection) -> Result<(), LemmyError> {
166   use lemmy_db_schema::schema::comment::dsl::*;
167
168   info!("Running comment_updates_2020_04_03");
169
170   // Update the ap_id
171   let incorrect_comments = comment
172     .filter(ap_id.eq("changeme_%"))
173     .filter(local.eq(true))
174     .load::<Comment>(conn)?;
175
176   sql_query("alter table comment disable trigger refresh_comment").execute(conn)?;
177
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)?;
181   }
182
183   sql_query("alter table comment enable trigger refresh_comment").execute(conn)?;
184
185   info!("{} comment rows updated.", incorrect_comments.len());
186
187   Ok(())
188 }
189
190 fn private_message_updates_2020_05_05(conn: &PgConnection) -> Result<(), LemmyError> {
191   use lemmy_db_schema::schema::private_message::dsl::*;
192
193   info!("Running private_message_updates_2020_05_05");
194
195   // Update the ap_id
196   let incorrect_pms = private_message
197     .filter(ap_id.eq("changeme_%"))
198     .filter(local.eq(true))
199     .load::<PrivateMessage>(conn)?;
200
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)?;
204   }
205
206   info!("{} private message rows updated.", incorrect_pms.len());
207
208   Ok(())
209 }
210
211 fn post_thumbnail_url_updates_2020_07_27(conn: &PgConnection) -> Result<(), LemmyError> {
212   use lemmy_db_schema::schema::post::dsl::*;
213
214   info!("Running post_thumbnail_url_updates_2020_07_27");
215
216   let domain_prefix = format!(
217     "{}/pictrs/image/",
218     Settings::get().get_protocol_and_hostname(),
219   );
220
221   let incorrect_thumbnails = post.filter(thumbnail_url.not_like("http%"));
222
223   // Prepend the rows with the update
224   let res = diesel::update(incorrect_thumbnails)
225     .set(
226       thumbnail_url.eq(
227         domain_prefix
228           .into_sql::<Nullable<Text>>()
229           .concat(thumbnail_url),
230       ),
231     )
232     .get_results::<Post>(conn)?;
233
234   info!("{} Post thumbnail_url rows updated.", res.len());
235
236   Ok(())
237 }