]> Untitled Git - lemmy.git/blob - src/code_migrations.rs
Move community to lemmy_db_schema
[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::{comment::Comment_, post::Post_, private_message::PrivateMessage},
8   Crud,
9 };
10 use lemmy_db_schema::{
11   naive_now,
12   source::{
13     comment::Comment,
14     community::{Community, CommunityForm},
15     post::Post,
16     user::{UserForm, User_},
17   },
18 };
19 use lemmy_utils::{
20   apub::{generate_actor_keypair, make_apub_endpoint, EndpointType},
21   settings::Settings,
22   LemmyError,
23 };
24 use log::info;
25
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)?;
33
34   Ok(())
35 }
36
37 fn user_updates_2020_04_02(conn: &PgConnection) -> Result<(), LemmyError> {
38   use lemmy_db_schema::schema::user_::dsl::*;
39
40   info!("Running user_updates_2020_04_02");
41
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)?;
47
48   sql_query("alter table user_ disable trigger refresh_user").execute(conn)?;
49
50   for cuser in &incorrect_users {
51     let keypair = generate_actor_keypair()?;
52
53     let form = UserForm {
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),
62       updated: None,
63       admin: cuser.admin,
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()),
74       local: cuser.local,
75       private_key: Some(keypair.private_key),
76       public_key: Some(keypair.public_key),
77       last_refreshed_at: Some(naive_now()),
78     };
79
80     User_::update(&conn, cuser.id, &form)?;
81   }
82
83   sql_query("alter table user_ enable trigger refresh_user").execute(conn)?;
84
85   info!("{} user rows updated.", incorrect_users.len());
86
87   Ok(())
88 }
89
90 fn community_updates_2020_04_02(conn: &PgConnection) -> Result<(), LemmyError> {
91   use lemmy_db_schema::schema::community::dsl::*;
92
93   info!("Running community_updates_2020_04_02");
94
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)?;
100
101   sql_query("alter table community disable trigger refresh_community").execute(conn)?;
102
103   for ccommunity in &incorrect_communities {
104     let keypair = generate_actor_keypair()?;
105
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,
112       removed: None,
113       deleted: None,
114       nsfw: ccommunity.nsfw,
115       updated: None,
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()),
121       published: None,
122       icon: Some(ccommunity.icon.to_owned()),
123       banner: Some(ccommunity.banner.to_owned()),
124     };
125
126     Community::update(&conn, ccommunity.id, &form)?;
127   }
128
129   sql_query("alter table community enable trigger refresh_community").execute(conn)?;
130
131   info!("{} community rows updated.", incorrect_communities.len());
132
133   Ok(())
134 }
135
136 fn post_updates_2020_04_03(conn: &PgConnection) -> Result<(), LemmyError> {
137   use lemmy_db_schema::schema::post::dsl::*;
138
139   info!("Running post_updates_2020_04_03");
140
141   // Update the ap_id
142   let incorrect_posts = post
143     .filter(ap_id.eq("changeme_%"))
144     .filter(local.eq(true))
145     .load::<Post>(conn)?;
146
147   sql_query("alter table post disable trigger refresh_post").execute(conn)?;
148
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)?;
152   }
153
154   info!("{} post rows updated.", incorrect_posts.len());
155
156   sql_query("alter table post enable trigger refresh_post").execute(conn)?;
157
158   Ok(())
159 }
160
161 fn comment_updates_2020_04_03(conn: &PgConnection) -> Result<(), LemmyError> {
162   use lemmy_db_schema::schema::comment::dsl::*;
163
164   info!("Running comment_updates_2020_04_03");
165
166   // Update the ap_id
167   let incorrect_comments = comment
168     .filter(ap_id.eq("changeme_%"))
169     .filter(local.eq(true))
170     .load::<Comment>(conn)?;
171
172   sql_query("alter table comment disable trigger refresh_comment").execute(conn)?;
173
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)?;
177   }
178
179   sql_query("alter table comment enable trigger refresh_comment").execute(conn)?;
180
181   info!("{} comment rows updated.", incorrect_comments.len());
182
183   Ok(())
184 }
185
186 fn private_message_updates_2020_05_05(conn: &PgConnection) -> Result<(), LemmyError> {
187   use lemmy_db_schema::schema::private_message::dsl::*;
188
189   info!("Running private_message_updates_2020_05_05");
190
191   // Update the ap_id
192   let incorrect_pms = private_message
193     .filter(ap_id.eq("changeme_%"))
194     .filter(local.eq(true))
195     .load::<PrivateMessage>(conn)?;
196
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)?;
200   }
201
202   info!("{} private message rows updated.", incorrect_pms.len());
203
204   Ok(())
205 }
206
207 fn post_thumbnail_url_updates_2020_07_27(conn: &PgConnection) -> Result<(), LemmyError> {
208   use lemmy_db_schema::schema::post::dsl::*;
209
210   info!("Running post_thumbnail_url_updates_2020_07_27");
211
212   let domain_prefix = format!(
213     "{}/pictrs/image/",
214     Settings::get().get_protocol_and_hostname(),
215   );
216
217   let incorrect_thumbnails = post.filter(thumbnail_url.not_like("http%"));
218
219   // Prepend the rows with the update
220   let res = diesel::update(incorrect_thumbnails)
221     .set(
222       thumbnail_url.eq(
223         domain_prefix
224           .into_sql::<Nullable<Text>>()
225           .concat(thumbnail_url),
226       ),
227     )
228     .get_results::<Post>(conn)?;
229
230   info!("{} Post thumbnail_url rows updated.", res.len());
231
232   Ok(())
233 }