Skip to content

Commit

Permalink
Add system ID field to db
Browse files Browse the repository at this point in the history
  • Loading branch information
xSke committed Feb 14, 2024
1 parent edc20e2 commit 5fb901c
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 3 deletions.
14 changes: 14 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ rust-s3 = { version = "0.33.0", default-features = false, features = ["tokio-rus
serde = { version = "1.0.196", features = ["derive"] }
serde_json = "1.0.113"
sha2 = "0.10.8"
sqlx = { version = "0.7.3", features = ["runtime-tokio", "postgres", "time"] }
sqlx = { version = "0.7.3", features = ["runtime-tokio", "postgres", "time", "uuid"] }
thiserror = "1.0.56"
time = "0.3.34"
tokio = { version = "1.36.0", features = ["full"] }
tracing = "0.1.40"
tracing-subscriber = "0.3.18"
uuid = { version = "1.7.0", features = ["serde"] }
webp = "0.2.6"
5 changes: 4 additions & 1 deletion src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::ImageKind;
use s3::creds::time::OffsetDateTime;
use serde::Serialize;
use sqlx::{Executor, FromRow, PgPool, Postgres, Transaction};
use uuid::Uuid;

#[derive(FromRow)]
pub struct ImageMeta {
Expand All @@ -18,6 +19,7 @@ pub struct ImageMeta {
pub original_file_size: Option<i32>,
pub original_type: Option<String>,
pub uploaded_by_account: Option<i64>,
pub uploaded_by_system: Option<Uuid>,
}

#[derive(FromRow, Serialize)]
Expand Down Expand Up @@ -90,7 +92,7 @@ pub async fn add_image(pool: &PgPool, meta: ImageMeta) -> anyhow::Result<bool> {
ImageKind::Banner => "banner",
};

let res = sqlx::query("insert into images (id, url, original_url, file_size, width, height, original_file_size, original_type, original_attachment_id, kind, uploaded_by_account, uploaded_at) values ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, (now() at time zone 'utc')) on conflict (id) do nothing")
let res = sqlx::query("insert into images (id, url, original_url, file_size, width, height, original_file_size, original_type, original_attachment_id, kind, uploaded_by_account, uploaded_by_system, uploaded_at) values ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, (now() at time zone 'utc')) on conflict (id) do nothing")
.bind(meta.id)
.bind(meta.url)
.bind(meta.original_url)
Expand All @@ -102,6 +104,7 @@ pub async fn add_image(pool: &PgPool, meta: ImageMeta) -> anyhow::Result<bool> {
.bind(meta.original_attachment_id)
.bind(kind_str)
.bind(meta.uploaded_by_account)
.bind(meta.uploaded_by_system)
.execute(pool).await?;
Ok(res.rows_affected() > 0)
}
4 changes: 3 additions & 1 deletion src/init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ create index on images (original_url);
create index on images (original_attachment_id);
create index on images (uploaded_by_account);

create table if not exists image_queue (itemid serial primary key, url text not null, kind text not null);
create table if not exists image_queue (itemid serial primary key, url text not null, kind text not null);

alter table images add column if not exists uploaded_by_system uuid;
3 changes: 3 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use sqlx::PgPool;
use std::sync::Arc;
use thiserror::Error;
use tracing::{error, info};
use uuid::Uuid;

#[derive(Error, Debug)]
pub enum PKAvatarError {
Expand Down Expand Up @@ -83,6 +84,7 @@ pub struct PullRequest {
url: String,
kind: ImageKind,
uploaded_by: Option<u64>, // should be String? serde makes this hard :/
system_id: Option<Uuid>,

#[serde(default)]
force: bool,
Expand Down Expand Up @@ -134,6 +136,7 @@ async fn pull(
kind: req.kind,
uploaded_at: None,
uploaded_by_account: req.uploaded_by.map(|x| x as i64),
uploaded_by_system: req.system_id,
},
)
.await?;
Expand Down
1 change: 1 addition & 0 deletions src/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub async fn handle_item_inner(
kind: item.kind,
uploaded_at: None,
uploaded_by_account: None,
uploaded_by_system: None,
},
)
.await?;
Expand Down

0 comments on commit 5fb901c

Please sign in to comment.