]> Untitled Git - lemmy.git/blob - src/code_migrations.rs
Fixing integration tests.
[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   naive_now,
8   source::{
9     comment::Comment,
10     community::{Community, CommunityForm},
11     post::Post,
12     private_message::PrivateMessage,
13     user::{UserForm, User_},
14   },
15   Crud,
16 };
17 use lemmy_utils::{
18   apub::{generate_actor_keypair, make_apub_endpoint, EndpointType},
19   settings::Settings,
20   LemmyError,
21 };
22 use log::info;
23
24 pub fn run_advanced_migrations(conn: &PgConnection) -> Result<(), LemmyError> {
25   user_updates_2020_04_02(&conn)?;
26   community_updates_2020_04_02(&conn)?;
27   post_updates_2020_04_03(&conn)?;
28   comment_updates_2020_04_03(&conn)?;
29   private_message_updates_2020_05_05(&conn)?;
30   post_thumbnail_url_updates_2020_07_27(&conn)?;
31
32   Ok(())
33 }
34
35 fn user_updates_2020_04_02(conn: &PgConnection) -> Result<(), LemmyError> {
36   use lemmy_db::schema::user_::dsl::*;
37
38   info!("Running user_updates_2020_04_02");
39
40   // Update the actor_id, private_key, and public_key, last_refreshed_at
41   let incorrect_users = user_
42     .filter(actor_id.like("changeme_%"))
43     .filter(local.eq(true))
44     .load::<User_>(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: Some(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: Some(cuser.preferred_username.to_owned()),
57       published: Some(cuser.published),
58       updated: None,
59       admin: cuser.admin,
60       banned: Some(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: Some(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   info!("{} user rows updated.", incorrect_users.len());
80
81   Ok(())
82 }
83
84 fn community_updates_2020_04_02(conn: &PgConnection) -> Result<(), LemmyError> {
85   use lemmy_db::schema::community::dsl::*;
86
87   info!("Running community_updates_2020_04_02");
88
89   // Update the actor_id, private_key, and public_key, last_refreshed_at
90   let incorrect_communities = community
91     .filter(actor_id.like("changeme_%"))
92     .filter(local.eq(true))
93     .load::<Community>(conn)?;
94
95   for ccommunity in &incorrect_communities {
96     let keypair = generate_actor_keypair()?;
97
98     let form = CommunityForm {
99       name: ccommunity.name.to_owned(),
100       title: ccommunity.title.to_owned(),
101       description: ccommunity.description.to_owned(),
102       category_id: ccommunity.category_id,
103       creator_id: ccommunity.creator_id,
104       removed: None,
105       deleted: None,
106       nsfw: ccommunity.nsfw,
107       updated: None,
108       actor_id: Some(make_apub_endpoint(EndpointType::Community, &ccommunity.name).to_string()),
109       local: ccommunity.local,
110       private_key: Some(keypair.private_key),
111       public_key: Some(keypair.public_key),
112       last_refreshed_at: Some(naive_now()),
113       published: None,
114       icon: Some(ccommunity.icon.to_owned()),
115       banner: Some(ccommunity.banner.to_owned()),
116     };
117
118     Community::update(&conn, ccommunity.id, &form)?;
119   }
120
121   info!("{} community rows updated.", incorrect_communities.len());
122
123   Ok(())
124 }
125
126 fn post_updates_2020_04_03(conn: &PgConnection) -> Result<(), LemmyError> {
127   use lemmy_db::schema::post::dsl::*;
128
129   info!("Running post_updates_2020_04_03");
130
131   // Update the ap_id
132   let incorrect_posts = post
133     .filter(ap_id.eq("changeme_%"))
134     .filter(local.eq(true))
135     .load::<Post>(conn)?;
136
137   for cpost in &incorrect_posts {
138     let apub_id = make_apub_endpoint(EndpointType::Post, &cpost.id.to_string()).to_string();
139     Post::update_ap_id(&conn, cpost.id, apub_id)?;
140   }
141
142   info!("{} post rows updated.", incorrect_posts.len());
143
144   Ok(())
145 }
146
147 fn comment_updates_2020_04_03(conn: &PgConnection) -> Result<(), LemmyError> {
148   use lemmy_db::schema::comment::dsl::*;
149
150   info!("Running comment_updates_2020_04_03");
151
152   // Update the ap_id
153   let incorrect_comments = comment
154     .filter(ap_id.eq("changeme_%"))
155     .filter(local.eq(true))
156     .load::<Comment>(conn)?;
157
158   for ccomment in &incorrect_comments {
159     let apub_id = make_apub_endpoint(EndpointType::Comment, &ccomment.id.to_string()).to_string();
160     Comment::update_ap_id(&conn, ccomment.id, apub_id)?;
161   }
162
163   info!("{} comment rows updated.", incorrect_comments.len());
164
165   Ok(())
166 }
167
168 fn private_message_updates_2020_05_05(conn: &PgConnection) -> Result<(), LemmyError> {
169   use lemmy_db::schema::private_message::dsl::*;
170
171   info!("Running private_message_updates_2020_05_05");
172
173   // Update the ap_id
174   let incorrect_pms = private_message
175     .filter(ap_id.eq("changeme_%"))
176     .filter(local.eq(true))
177     .load::<PrivateMessage>(conn)?;
178
179   for cpm in &incorrect_pms {
180     let apub_id = make_apub_endpoint(EndpointType::PrivateMessage, &cpm.id.to_string()).to_string();
181     PrivateMessage::update_ap_id(&conn, cpm.id, apub_id)?;
182   }
183
184   info!("{} private message rows updated.", incorrect_pms.len());
185
186   Ok(())
187 }
188
189 fn post_thumbnail_url_updates_2020_07_27(conn: &PgConnection) -> Result<(), LemmyError> {
190   use lemmy_db::schema::post::dsl::*;
191
192   info!("Running post_thumbnail_url_updates_2020_07_27");
193
194   let domain_prefix = format!(
195     "{}/pictrs/image/",
196     Settings::get().get_protocol_and_hostname(),
197   );
198
199   let incorrect_thumbnails = post.filter(thumbnail_url.not_like("http%"));
200
201   // Prepend the rows with the update
202   let res = diesel::update(incorrect_thumbnails)
203     .set(
204       thumbnail_url.eq(
205         domain_prefix
206           .into_sql::<Nullable<Text>>()
207           .concat(thumbnail_url),
208       ),
209     )
210     .get_results::<Post>(conn)?;
211
212   info!("{} Post thumbnail_url rows updated.", res.len());
213
214   Ok(())
215 }