]> Untitled Git - lemmy.git/blob - server/src/db/code_migrations.rs
Merge branch 'dev' into federation
[lemmy.git] / server / src / db / code_migrations.rs
1 // This is for db migrations that require code
2 use super::comment::Comment;
3 use super::community::{Community, CommunityForm};
4 use super::post::Post;
5 use super::user::{UserForm, User_};
6 use super::*;
7 use crate::apub::{gen_keypair_str, make_apub_endpoint, EndpointType};
8 use crate::naive_now;
9 use log::info;
10
11 pub fn run_advanced_migrations(conn: &PgConnection) -> Result<(), Error> {
12   user_updates_2020_04_02(conn)?;
13   community_updates_2020_04_02(conn)?;
14   post_updates_2020_04_03(conn)?;
15   comment_updates_2020_04_03(conn)?;
16
17   Ok(())
18 }
19
20 fn user_updates_2020_04_02(conn: &PgConnection) -> Result<(), Error> {
21   use crate::schema::user_::dsl::*;
22
23   info!("Running user_updates_2020_04_02");
24
25   // Update the actor_id, private_key, and public_key, last_refreshed_at
26   let incorrect_users = user_
27     .filter(actor_id.eq("changeme"))
28     .filter(local.eq(true))
29     .load::<User_>(conn)?;
30
31   for cuser in &incorrect_users {
32     let (user_public_key, user_private_key) = gen_keypair_str();
33
34     let form = UserForm {
35       name: cuser.name.to_owned(),
36       email: cuser.email.to_owned(),
37       matrix_user_id: cuser.matrix_user_id.to_owned(),
38       avatar: cuser.avatar.to_owned(),
39       password_encrypted: cuser.password_encrypted.to_owned(),
40       preferred_username: cuser.preferred_username.to_owned(),
41       updated: None,
42       admin: cuser.admin,
43       banned: cuser.banned,
44       show_nsfw: cuser.show_nsfw,
45       theme: cuser.theme.to_owned(),
46       default_sort_type: cuser.default_sort_type,
47       default_listing_type: cuser.default_listing_type,
48       lang: cuser.lang.to_owned(),
49       show_avatars: cuser.show_avatars,
50       send_notifications_to_email: cuser.send_notifications_to_email,
51       actor_id: make_apub_endpoint(EndpointType::User, &cuser.name).to_string(),
52       bio: cuser.bio.to_owned(),
53       local: cuser.local,
54       private_key: Some(user_private_key),
55       public_key: Some(user_public_key),
56       last_refreshed_at: Some(naive_now()),
57     };
58
59     User_::update(&conn, cuser.id, &form)?;
60   }
61
62   info!("{} user rows updated.", incorrect_users.len());
63
64   Ok(())
65 }
66
67 fn community_updates_2020_04_02(conn: &PgConnection) -> Result<(), Error> {
68   use crate::schema::community::dsl::*;
69
70   info!("Running community_updates_2020_04_02");
71
72   // Update the actor_id, private_key, and public_key, last_refreshed_at
73   let incorrect_communities = community
74     .filter(actor_id.eq("changeme"))
75     .filter(local.eq(true))
76     .load::<Community>(conn)?;
77
78   for ccommunity in &incorrect_communities {
79     let (community_public_key, community_private_key) = gen_keypair_str();
80
81     let form = CommunityForm {
82       name: ccommunity.name.to_owned(),
83       title: ccommunity.title.to_owned(),
84       description: ccommunity.description.to_owned(),
85       category_id: ccommunity.category_id,
86       creator_id: ccommunity.creator_id,
87       removed: None,
88       deleted: None,
89       nsfw: ccommunity.nsfw,
90       updated: None,
91       actor_id: make_apub_endpoint(EndpointType::Community, &ccommunity.name).to_string(),
92       local: ccommunity.local,
93       private_key: Some(community_private_key),
94       public_key: Some(community_public_key),
95       last_refreshed_at: Some(naive_now()),
96       published: None,
97     };
98
99     Community::update(&conn, ccommunity.id, &form)?;
100   }
101
102   info!("{} community rows updated.", incorrect_communities.len());
103
104   Ok(())
105 }
106
107 fn post_updates_2020_04_03(conn: &PgConnection) -> Result<(), Error> {
108   use crate::schema::post::dsl::*;
109
110   info!("Running post_updates_2020_04_03");
111
112   // Update the ap_id
113   let incorrect_posts = post
114     .filter(ap_id.eq("changeme"))
115     .filter(local.eq(true))
116     .load::<Post>(conn)?;
117
118   for cpost in &incorrect_posts {
119     Post::update_ap_id(&conn, cpost.id)?;
120   }
121
122   info!("{} post rows updated.", incorrect_posts.len());
123
124   Ok(())
125 }
126
127 fn comment_updates_2020_04_03(conn: &PgConnection) -> Result<(), Error> {
128   use crate::schema::comment::dsl::*;
129
130   info!("Running comment_updates_2020_04_03");
131
132   // Update the ap_id
133   let incorrect_comments = comment
134     .filter(ap_id.eq("changeme"))
135     .filter(local.eq(true))
136     .load::<Comment>(conn)?;
137
138   for ccomment in &incorrect_comments {
139     Comment::update_ap_id(&conn, ccomment.id)?;
140   }
141
142   info!("{} comment rows updated.", incorrect_comments.len());
143
144   Ok(())
145 }