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