fix: postgres feature

This commit is contained in:
trisua 2025-04-03 13:52:29 -04:00
parent a11a70d3e7
commit dcd5f359c6
13 changed files with 79 additions and 105 deletions

View file

@ -13,7 +13,7 @@
Everything Tetratto needs will be built into the main binary. You can build Tetratto with the following command:
```bash
cargo build -r --no-default-features features=redis,sqlite
cargo build -r --no-default-features --features=redis,sqlite
```
You can replace `sqlite` in the above command with `postgres`, if you'd like. It's also acceptable to remove the `redis` part if you don't want to use a cache. <sup>I wouldn't recomment removing cache, though</sup>

View file

@ -1,8 +1,6 @@
use super::*;
use crate::cache::Cache;
use crate::model::{
Error, Result, auth::User, moderation::AuditLogEntry, permissions::FinePermission,
};
use crate::model::{Error, Result, auth::User, moderation::AuditLogEntry, permissions::FinePermission};
use crate::{auto_method, execute, get, query_row, query_rows};
#[cfg(feature = "sqlite")]
@ -18,9 +16,9 @@ impl DataManager {
#[cfg(feature = "postgres")] x: &Row,
) -> AuditLogEntry {
AuditLogEntry {
id: get!(x->0(isize)) as usize,
created: get!(x->1(isize)) as usize,
moderator: get!(x->2(isize)) as usize,
id: get!(x->0(i64)) as usize,
created: get!(x->1(i64)) as usize,
moderator: get!(x->2(i64)) as usize,
content: get!(x->3(String)),
}
}
@ -45,7 +43,7 @@ impl DataManager {
let res = query_rows!(
&conn,
"SELECT * FROM audit_log ORDER BY created DESC LIMIT $1 OFFSET $2",
&[&(batch as isize), &((page * batch) as isize)],
&[&(batch as i64), &((page * batch) as i64)],
|x| { Self::get_audit_log_entry_from_row(x) }
);

View file

@ -25,8 +25,8 @@ impl DataManager {
#[cfg(feature = "postgres")] x: &Row,
) -> User {
User {
id: get!(x->0(isize)) as usize,
created: get!(x->1(isize)) as usize,
id: get!(x->0(i64)) as usize,
created: get!(x->1(i64)) as usize,
username: get!(x->2(String)),
password: get!(x->3(String)),
salt: get!(x->4(String)),
@ -34,10 +34,10 @@ impl DataManager {
tokens: serde_json::from_str(&get!(x->6(String)).to_string()).unwrap(),
permissions: FinePermission::from_bits(get!(x->7(u32))).unwrap(),
is_verified: if get!(x->8(i8)) == 1 { true } else { false },
notification_count: get!(x->9(isize)) as usize,
follower_count: get!(x->10(isize)) as usize,
following_count: get!(x->11(isize)) as usize,
last_seen: get!(x->12(isize)) as usize,
notification_count: get!(x->9(i64)) as usize,
follower_count: get!(x->10(i64)) as usize,
following_count: get!(x->11(i64)) as usize,
last_seen: get!(x->12(i64)) as usize,
}
}
@ -148,7 +148,7 @@ impl DataManager {
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
};
let res = execute!(&conn, "DELETE FROM users WHERE id = $1", &[&(id as isize)]);
let res = execute!(&conn, "DELETE FROM users WHERE id = $1", &[&(id as i64)]);
if let Err(e) = res {
return Err(Error::DatabaseError(e.to_string()));
@ -160,7 +160,7 @@ impl DataManager {
let res = execute!(
&conn,
"DELETE FROM communities WHERE owner = $1",
&[&(id as isize)]
&[&(id as i64)]
);
if let Err(e) = res {
@ -172,7 +172,7 @@ impl DataManager {
let res = execute!(
&conn,
"DELETE FROM memberships WHERE owner = $1",
&[&(id as isize)]
&[&(id as i64)]
);
if let Err(e) = res {
@ -183,7 +183,7 @@ impl DataManager {
let res = execute!(
&conn,
"DELETE FROM notifications WHERE owner = $1",
&[&(id as isize)]
&[&(id as i64)]
);
if let Err(e) = res {
@ -195,7 +195,7 @@ impl DataManager {
let res = execute!(
&conn,
"DELETE FROM reactions WHERE owner = $1",
&[&(id as isize)]
&[&(id as i64)]
);
if let Err(e) = res {
@ -203,11 +203,7 @@ impl DataManager {
}
// delete posts
let res = execute!(
&conn,
"DELETE FROM posts WHERE owner = $1",
&[&(id as isize)]
);
let res = execute!(&conn, "DELETE FROM posts WHERE owner = $1", &[&(id as i64)]);
if let Err(e) = res {
return Err(Error::DatabaseError(e.to_string()));

View file

@ -38,7 +38,7 @@ macro_rules! auto_method {
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
};
let res = query_row!(&conn, $query, &[&(id as isize)], |x| {
let res = query_row!(&conn, $query, &[&(id as i64)], |x| {
Ok(Self::$select_fn(x))
});
@ -61,7 +61,7 @@ macro_rules! auto_method {
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
};
let res = query_row!(&conn, $query, &[&(id as isize)], |x| {
let res = query_row!(&conn, $query, &[&(id as i64)], |x| {
Ok(Self::$select_fn(x))
});

View file

@ -26,19 +26,19 @@ impl DataManager {
#[cfg(feature = "postgres")] x: &Row,
) -> Community {
Community {
id: get!(x->0(isize)) as usize,
created: get!(x->1(isize)) as usize,
id: get!(x->0(i64)) as usize,
created: get!(x->1(i64)) as usize,
title: get!(x->2(String)),
context: serde_json::from_str(&get!(x->3(String))).unwrap(),
owner: get!(x->4(isize)) as usize,
owner: get!(x->4(i64)) as usize,
read_access: serde_json::from_str(&get!(x->5(String))).unwrap(),
write_access: serde_json::from_str(&get!(x->6(String))).unwrap(),
join_access: serde_json::from_str(&get!(x->7(String))).unwrap(),
// likes
likes: get!(x->8(isize)) as isize,
dislikes: get!(x->9(isize)) as isize,
likes: get!(x->8(i64)) as isize,
dislikes: get!(x->9(i64)) as isize,
// counts
member_count: get!(x->10(isize)) as usize,
member_count: get!(x->10(i64)) as usize,
}
}
@ -59,7 +59,7 @@ impl DataManager {
let res = query_row!(
&conn,
"SELECT * FROM communities WHERE id = $1",
&[&(id as isize)],
&[&(id as i64)],
|x| { Ok(Self::get_community_from_row(x)) }
);
@ -96,7 +96,7 @@ impl DataManager {
let res = query_row!(
&conn,
"SELECT * FROM communities WHERE title = $1",
&[id],
&[&id],
|x| { Ok(Self::get_community_from_row(x)) }
);

View file

@ -18,9 +18,9 @@ impl DataManager {
) -> IpBan {
IpBan {
ip: get!(x->0(String)),
created: get!(x->1(isize)) as usize,
created: get!(x->1(i64)) as usize,
reason: get!(x->2(String)),
moderator: get!(x->3(isize)) as usize,
moderator: get!(x->3(i64)) as usize,
}
}
@ -40,7 +40,7 @@ impl DataManager {
let res = query_rows!(
&conn,
"SELECT * FROM ipbans ORDER BY created DESC LIMIT $1 OFFSET $2",
&[&(batch as isize), &((page * batch) as isize)],
&[&(batch as i64), &((page * batch) as i64)],
|x| { Self::get_ipban_from_row(x) }
);
@ -105,7 +105,7 @@ impl DataManager {
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
};
let res = execute!(&conn, "DELETE FROM ipbans WHERE ip = $1", &[ip]);
let res = execute!(&conn, "DELETE FROM ipbans WHERE ip = $1", &[&ip]);
if let Err(e) = res {
return Err(Error::DatabaseError(e.to_string()));

View file

@ -24,10 +24,10 @@ impl DataManager {
#[cfg(feature = "postgres")] x: &Row,
) -> CommunityMembership {
CommunityMembership {
id: get!(x->0(isize)) as usize,
created: get!(x->1(isize)) as usize,
owner: get!(x->2(isize)) as usize,
community: get!(x->3(isize)) as usize,
id: get!(x->0(i64)) as usize,
created: get!(x->1(i64)) as usize,
owner: get!(x->2(i64)) as usize,
community: get!(x->3(i64)) as usize,
role: CommunityPermission::from_bits(get!(x->4(u32))).unwrap(),
}
}
@ -57,7 +57,7 @@ impl DataManager {
let res = query_row!(
&conn,
"SELECT * FROM memberships WHERE owner = $1 AND community = $2",
&[&(owner as isize), &(community as isize)],
&[&(owner as i64), &(community as i64)],
|x| { Ok(Self::get_membership_from_row(x)) }
);
@ -79,7 +79,7 @@ impl DataManager {
&conn,
// 33 = banned, 65 = pending membership
"SELECT * FROM memberships WHERE owner = $1 AND role IS NOT 33 AND role IS NOT 65 ORDER BY created DESC",
&[&(owner as isize)],
&[&(owner as i64)],
|x| { Self::get_membership_from_row(x) }
);

View file

@ -16,11 +16,11 @@ impl DataManager {
#[cfg(feature = "postgres")] x: &Row,
) -> Notification {
Notification {
id: get!(x->0(isize)) as usize,
created: get!(x->1(isize)) as usize,
id: get!(x->0(i64)) as usize,
created: get!(x->1(i64)) as usize,
title: get!(x->2(String)),
content: get!(x->3(String)),
owner: get!(x->4(isize)) as usize,
owner: get!(x->4(i64)) as usize,
read: if get!(x->5(i8)) == 1 { true } else { false },
}
}
@ -37,7 +37,7 @@ impl DataManager {
let res = query_rows!(
&conn,
"SELECT * FROM notifications WHERE owner = $1 ORDER BY created DESC",
&[&(owner as isize)],
&[&(owner as i64)],
|x| { Self::get_notification_from_row(x) }
);

View file

@ -26,11 +26,11 @@ impl DataManager {
#[cfg(feature = "postgres")] x: &Row,
) -> Post {
Post {
id: get!(x->0(isize)) as usize,
created: get!(x->1(isize)) as usize,
id: get!(x->0(i64)) as usize,
created: get!(x->1(i64)) as usize,
content: get!(x->2(String)),
owner: get!(x->3(isize)) as usize,
community: get!(x->4(isize)) as usize,
owner: get!(x->3(i64)) as usize,
community: get!(x->4(i64)) as usize,
context: serde_json::from_str(&get!(x->5(String))).unwrap(),
replying_to: if let Some(id) = get!(x->6(Option<i64>)) {
Some(id as usize)
@ -38,10 +38,10 @@ impl DataManager {
None
},
// likes
likes: get!(x->7(isize)) as isize,
dislikes: get!(x->8(isize)) as isize,
likes: get!(x->7(i64)) as isize,
dislikes: get!(x->8(i64)) as isize,
// other counts
comment_count: get!(x->9(isize)) as usize,
comment_count: get!(x->9(i64)) as usize,
}
}
@ -67,11 +67,7 @@ impl DataManager {
let res = query_rows!(
&conn,
"SELECT * FROM posts WHERE replying_to = $1 ORDER BY created DESC LIMIT $2 OFFSET $3",
&[
&(id as isize),
&(batch as isize),
&((page * batch) as isize)
],
&[&(id as i64), &(batch as i64), &((page * batch) as i64)],
|x| { Self::get_post_from_row(x) }
);
@ -148,11 +144,7 @@ impl DataManager {
let res = query_rows!(
&conn,
"SELECT * FROM posts WHERE owner = $1 ORDER BY created DESC LIMIT $2 OFFSET $3",
&[
&(id as isize),
&(batch as isize),
&((page * batch) as isize)
],
&[&(id as i64), &(batch as i64), &((page * batch) as i64)],
|x| { Self::get_post_from_row(x) }
);
@ -183,11 +175,7 @@ impl DataManager {
let res = query_rows!(
&conn,
"SELECT * FROM posts WHERE community = $1 AND replying_to IS NULL ORDER BY created DESC LIMIT $2 OFFSET $3",
&[
&(id as isize),
&(batch as isize),
&((page * batch) as isize)
],
&[&(id as i64), &(batch as i64), &((page * batch) as i64)],
|x| { Self::get_post_from_row(x) }
);
@ -211,7 +199,7 @@ impl DataManager {
let res = query_rows!(
&conn,
"SELECT * FROM posts WHERE community = $1 AND context LIKE '%\"is_pinned\":true%' ORDER BY created DESC",
&[&(id as isize),],
&[&(id as i64),],
|x| { Self::get_post_from_row(x) }
);
@ -236,7 +224,7 @@ impl DataManager {
let res = query_rows!(
&conn,
"SELECT * FROM posts ORDER BY likes DESC, created ASC LIMIT $2 OFFSET $3",
&[&(batch as isize), &((page * batch) as isize)],
&[&(batch as i64), &((page * batch) as i64)],
|x| { Self::get_post_from_row(x) }
);
@ -283,7 +271,7 @@ impl DataManager {
"SELECT * FROM posts WHERE community = {} {query_string} ORDER BY created DESC LIMIT $1 OFFSET $2",
first.community
),
&[&(batch as isize), &((page * batch) as isize)],
&[&(batch as i64), &((page * batch) as i64)],
|x| { Self::get_post_from_row(x) }
);

View file

@ -21,10 +21,10 @@ impl DataManager {
#[cfg(feature = "postgres")] x: &Row,
) -> Reaction {
Reaction {
id: get!(x->0(isize)) as usize,
created: get!(x->1(isize)) as usize,
owner: get!(x->2(isize)) as usize,
asset: get!(x->3(isize)) as usize,
id: get!(x->0(i64)) as usize,
created: get!(x->1(i64)) as usize,
owner: get!(x->2(i64)) as usize,
asset: get!(x->3(i64)) as usize,
asset_type: serde_json::from_str(&get!(x->4(String))).unwrap(),
is_like: if get!(x->5(i8)) == 1 { true } else { false },
}
@ -46,7 +46,7 @@ impl DataManager {
let res = query_row!(
&conn,
"SELECT * FROM reactions WHERE owner = $1 AND asset = $2",
&[&(owner as isize), &(asset as isize)],
&[&(owner as i64), &(asset as i64)],
|x| { Ok(Self::get_reaction_from_row(x)) }
);

View file

@ -17,11 +17,11 @@ impl DataManager {
#[cfg(feature = "postgres")] x: &Row,
) -> Report {
Report {
id: get!(x->0(isize)) as usize,
created: get!(x->1(isize)) as usize,
owner: get!(x->2(isize)) as usize,
id: get!(x->0(i64)) as usize,
created: get!(x->1(i64)) as usize,
owner: get!(x->2(i64)) as usize,
content: get!(x->3(String)),
asset: get!(x->4(isize)) as usize,
asset: get!(x->4(i64)) as usize,
asset_type: serde_json::from_str(&get!(x->5(String))).unwrap(),
}
}
@ -42,7 +42,7 @@ impl DataManager {
let res = query_rows!(
&conn,
"SELECT * FROM reports ORDER BY created DESC LIMIT $1 OFFSET $2",
&[&(batch as isize), &((page * batch) as isize)],
&[&(batch as i64), &((page * batch) as i64)],
|x| { Self::get_report_from_row(x) }
);

View file

@ -16,10 +16,10 @@ impl DataManager {
#[cfg(feature = "postgres")] x: &Row,
) -> UserBlock {
UserBlock {
id: get!(x->0(isize)) as usize,
created: get!(x->1(isize)) as usize,
initiator: get!(x->2(isize)) as usize,
receiver: get!(x->3(isize)) as usize,
id: get!(x->0(i64)) as usize,
created: get!(x->1(i64)) as usize,
initiator: get!(x->2(i64)) as usize,
receiver: get!(x->3(i64)) as usize,
}
}
@ -39,7 +39,7 @@ impl DataManager {
let res = query_row!(
&conn,
"SELECT * FROM userblocks WHERE initiator = $1 AND receiver = $2",
&[&(initiator as isize), &(receiver as isize)],
&[&(initiator as i64), &(receiver as i64)],
|x| { Ok(Self::get_userblock_from_row(x)) }
);
@ -64,7 +64,7 @@ impl DataManager {
let res = query_row!(
&conn,
"SELECT * FROM userblocks WHERE receiver = $1 AND initiator = $2",
&[&(receiver as isize), &(initiator as isize)],
&[&(receiver as i64), &(initiator as i64)],
|x| { Ok(Self::get_userblock_from_row(x)) }
);

View file

@ -16,10 +16,10 @@ impl DataManager {
#[cfg(feature = "postgres")] x: &Row,
) -> UserFollow {
UserFollow {
id: get!(x->0(isize)) as usize,
created: get!(x->1(isize)) as usize,
initiator: get!(x->2(isize)) as usize,
receiver: get!(x->3(isize)) as usize,
id: get!(x->0(i64)) as usize,
created: get!(x->1(i64)) as usize,
initiator: get!(x->2(i64)) as usize,
receiver: get!(x->3(i64)) as usize,
}
}
@ -39,7 +39,7 @@ impl DataManager {
let res = query_row!(
&conn,
"SELECT * FROM userfollows WHERE initiator = $1 AND receiver = $2",
&[&(initiator as isize), &(receiver as isize)],
&[&(initiator as i64), &(receiver as i64)],
|x| { Ok(Self::get_userfollow_from_row(x)) }
);
@ -64,7 +64,7 @@ impl DataManager {
let res = query_row!(
&conn,
"SELECT * FROM userfollows WHERE receiver = $1 AND initiator = $2",
&[&(receiver as isize), &(initiator as isize)],
&[&(receiver as i64), &(initiator as i64)],
|x| { Ok(Self::get_userfollow_from_row(x)) }
);
@ -95,11 +95,7 @@ impl DataManager {
let res = query_rows!(
&conn,
"SELECT * FROM userfollows WHERE initiator = $1 ORDER BY created DESC LIMIT $2 OFFSET $3",
&[
&(id as isize),
&(batch as isize),
&((page * batch) as isize)
],
&[&(id as i64), &(batch as i64), &((page * batch) as i64)],
|x| { Self::get_userfollow_from_row(x) }
);
@ -130,11 +126,7 @@ impl DataManager {
let res = query_rows!(
&conn,
"SELECT * FROM userfollows WHERE receiver = $1 ORDER BY created DESC LIMIT $2 OFFSET $3",
&[
&(id as isize),
&(batch as isize),
&((page * batch) as isize)
],
&[&(id as i64), &(batch as i64), &((page * batch) as i64)],
|x| { Self::get_userfollow_from_row(x) }
);