Skip to content

Commit

Permalink
Remove all select * from the server queries
Browse files Browse the repository at this point in the history
It's not ideal as we should be explicit about what is being queried!

A part one for sorting this all out :)
  • Loading branch information
ellie committed Apr 26, 2022
1 parent 4030de4 commit 2986ca8
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions atuin-server/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,24 +80,26 @@ impl Postgres {
impl Database for Postgres {
#[instrument(skip_all)]
async fn get_session(&self, token: &str) -> Result<Session> {
sqlx::query_as::<_, Session>("select * from sessions where token = $1")
sqlx::query_as::<_, Session>("select id, user_id, token from sessions where token = $1")
.bind(token)
.fetch_one(&self.pool)
.await
}

#[instrument(skip_all)]
async fn get_user(&self, username: &str) -> Result<User> {
sqlx::query_as::<_, User>("select * from users where username = $1")
.bind(username)
.fetch_one(&self.pool)
.await
sqlx::query_as::<_, User>(
"select id, username, email, password from users where username = $1",
)
.bind(username)
.fetch_one(&self.pool)
.await
}

#[instrument(skip_all)]
async fn get_session_user(&self, token: &str) -> Result<User> {
sqlx::query_as::<_, User>(
"select * from users
"select users.id, users.username, user.email, users.password from users
inner join sessions
on users.id = sessions.user_id
and sessions.token = $1",
Expand Down Expand Up @@ -222,7 +224,7 @@ impl Database for Postgres {
host: &str,
) -> Result<Vec<History>> {
let res = sqlx::query_as::<_, History>(
"select * from history
"select id, client_id, user_id, hostname, timestamp, data, created_at from history
where user_id = $1
and hostname != $2
and created_at >= $3
Expand Down Expand Up @@ -311,7 +313,7 @@ impl Database for Postgres {

#[instrument(skip_all)]
async fn get_user_session(&self, u: &User) -> Result<Session> {
sqlx::query_as::<_, Session>("select * from sessions where user_id = $1")
sqlx::query_as::<_, Session>("select id, user_id, token from sessions where user_id = $1")
.bind(u.id)
.fetch_one(&self.pool)
.await
Expand All @@ -320,7 +322,7 @@ impl Database for Postgres {
#[instrument(skip_all)]
async fn oldest_history(&self, user: &User) -> Result<History> {
let res = sqlx::query_as::<_, History>(
"select * from history
"select id, client_id, user_id, hostname, timestamp, data, created_at from history
where user_id = $1
order by timestamp asc
limit 1",
Expand Down

0 comments on commit 2986ca8

Please sign in to comment.