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