move chir.rs to postgresql

This commit is contained in:
Charlotte 🦝 Delenk 2024-11-28 08:45:41 +01:00
parent c5a2787cf9
commit 78f24904d1
Signed by: darkkirb
GPG key ID: AB2BD8DAF2E37122
11 changed files with 135 additions and 120 deletions

23
Cargo.lock generated
View file

@ -797,6 +797,7 @@ dependencies = [
"eyre",
"serde",
"sqlx",
"tokio",
"tracing",
]
@ -809,7 +810,7 @@ dependencies = [
"chir-rs-config",
"chir-rs-db",
"eyre",
"rustls 0.23.18",
"rustls 0.23.19",
"tokio",
"tokio-rustls 0.26.0",
"tracing",
@ -1640,7 +1641,7 @@ dependencies = [
"http 1.1.0",
"hyper 1.5.1",
"hyper-util",
"rustls 0.23.18",
"rustls 0.23.19",
"rustls-pki-types",
"tokio",
"tokio-rustls 0.26.0",
@ -2384,7 +2385,7 @@ dependencies = [
"quinn-proto",
"quinn-udp",
"rustc-hash 2.0.0",
"rustls 0.23.18",
"rustls 0.23.19",
"socket2",
"thiserror 2.0.3",
"tokio",
@ -2402,7 +2403,7 @@ dependencies = [
"rand",
"ring",
"rustc-hash 2.0.0",
"rustls 0.23.18",
"rustls 0.23.19",
"rustls-pki-types",
"slab",
"thiserror 2.0.3",
@ -2557,7 +2558,7 @@ dependencies = [
"percent-encoding",
"pin-project-lite",
"quinn",
"rustls 0.23.18",
"rustls 0.23.19",
"rustls-pemfile 2.2.0",
"rustls-pki-types",
"serde",
@ -2689,9 +2690,9 @@ dependencies = [
[[package]]
name = "rustls"
version = "0.23.18"
version = "0.23.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c9cc1d47e243d655ace55ed38201c19ae02c148ae56412ab8750e8f0166ab7f"
checksum = "934b404430bb06b3fae2cba809eb45a1ab1aecd64491213d7c3301b88393f8d1"
dependencies = [
"aws-lc-rs",
"log",
@ -3599,7 +3600,7 @@ version = "0.26.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0c7bc40d0e5a97695bb96e27995cd3a08538541b0a846f65bba7a359f36700d4"
dependencies = [
"rustls 0.23.18",
"rustls 0.23.19",
"rustls-pki-types",
"tokio",
]
@ -3735,9 +3736,9 @@ checksum = "8df9b6e13f2d32c91b9bd719c00d1958837bc7dec474d94952798cc8e69eeec3"
[[package]]
name = "tracing"
version = "0.1.40"
version = "0.1.41"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3523ab5a71916ccf420eebdf5521fcef02141234bbc0b8a49f2fdc4544364ef"
checksum = "784e0ac535deb450455cbfa28a6f0df145ea1bb7ae51b821cf5e7927fdcfbdd0"
dependencies = [
"log",
"pin-project-lite",
@ -3887,7 +3888,7 @@ dependencies = [
"base64 0.22.1",
"log",
"once_cell",
"rustls 0.23.18",
"rustls 0.23.19",
"rustls-pki-types",
"url",
"webpki-roots",

112
Cargo.nix
View file

@ -30,7 +30,7 @@ args@{
ignoreLockHash,
}:
let
nixifiedLockHash = "f0f3e6730327241337a9223ca44352055eb6bfd223c59d1662b8b7e7a9427251";
nixifiedLockHash = "a24d84ff4e4002670a4a5ee8c354931f6f331f16bb1dec96958b541794e2f4c2";
workspaceSrc = if args.workspaceSrc == null then ./. else args.workspaceSrc;
currentLockHash = builtins.hashFile "sha256" (workspaceSrc + /Cargo.lock);
lockHashIgnored =
@ -400,7 +400,7 @@ else
inherit profileName;
}).out;
tracing =
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.40" {
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.41" {
inherit profileName;
}).out;
url =
@ -634,7 +634,7 @@ else
inherit profileName;
}).out;
tracing =
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.40" {
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.41" {
inherit profileName;
}).out;
uuid =
@ -759,7 +759,7 @@ else
inherit profileName;
}).out;
tracing =
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.40" {
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.41" {
inherit profileName;
}).out;
url =
@ -833,7 +833,7 @@ else
inherit profileName;
}).out;
tracing =
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.40" {
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.41" {
inherit profileName;
}).out;
};
@ -903,7 +903,7 @@ else
inherit profileName;
}).out;
tracing =
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.40" {
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.41" {
inherit profileName;
}).out;
};
@ -977,7 +977,7 @@ else
inherit profileName;
}).out;
tracing =
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.40" {
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.41" {
inherit profileName;
}).out;
};
@ -1080,7 +1080,7 @@ else
inherit profileName;
}).out;
tracing =
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.40" {
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.41" {
inherit profileName;
}).out;
zeroize =
@ -1179,7 +1179,7 @@ else
inherit profileName;
}).out;
tracing =
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.40" {
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.41" {
inherit profileName;
}).out;
};
@ -1276,7 +1276,7 @@ else
inherit profileName;
}).out;
tracing =
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.40" {
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.41" {
inherit profileName;
}).out;
};
@ -1412,7 +1412,7 @@ else
inherit profileName;
}).out;
tracing =
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.40" {
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.41" {
inherit profileName;
}).out;
};
@ -1466,7 +1466,7 @@ else
inherit profileName;
}).out;
tracing =
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.40" {
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.41" {
inherit profileName;
}).out;
zeroize =
@ -1615,7 +1615,7 @@ else
inherit profileName;
}).out;
tracing =
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.40" {
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.41" {
inherit profileName;
}).out;
};
@ -1751,7 +1751,7 @@ else
inherit profileName;
}).out;
tracing =
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.40" {
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.41" {
inherit profileName;
}).out;
};
@ -1820,7 +1820,7 @@ else
inherit profileName;
}).out;
tracing =
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.40" {
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.41" {
inherit profileName;
}).out;
};
@ -2567,8 +2567,12 @@ else
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".sqlx."0.8.2" {
inherit profileName;
}).out;
tokio =
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".tokio."1.41.1" {
inherit profileName;
}).out;
tracing =
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.40" {
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.41" {
inherit profileName;
}).out;
};
@ -2592,7 +2596,7 @@ else
inherit profileName;
}).out;
rustls =
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".rustls."0.23.18" {
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".rustls."0.23.19" {
inherit profileName;
}).out;
tokio =
@ -2604,7 +2608,7 @@ else
inherit profileName;
}).out;
tracing =
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.40" {
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.41" {
inherit profileName;
}).out;
};
@ -2645,7 +2649,7 @@ else
inherit profileName;
}).out;
tracing =
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.40" {
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.41" {
inherit profileName;
}).out;
};
@ -2694,7 +2698,7 @@ else
inherit profileName;
}).out;
tracing =
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.40" {
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.41" {
inherit profileName;
}).out;
};
@ -4221,7 +4225,7 @@ else
inherit profileName;
}).out;
tracing =
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.40" {
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.41" {
inherit profileName;
}).out;
};
@ -4279,7 +4283,7 @@ else
inherit profileName;
}).out;
tracing =
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.40" {
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.41" {
inherit profileName;
}).out;
};
@ -4740,7 +4744,7 @@ else
inherit profileName;
}).out;
tracing =
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.40" {
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.41" {
inherit profileName;
}).out;
want =
@ -4916,7 +4920,7 @@ else
inherit profileName;
}).out;
rustls =
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".rustls."0.23.18" {
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".rustls."0.23.19" {
inherit profileName;
}).out;
pki_types =
@ -5005,7 +5009,7 @@ else
inherit profileName;
}).out;
tracing =
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.40" {
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.41" {
inherit profileName;
}).out;
};
@ -5905,7 +5909,7 @@ else
inherit profileName;
}).out;
tracing =
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.40" {
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.41" {
inherit profileName;
}).out;
};
@ -6911,7 +6915,7 @@ else
inherit profileName;
}).out;
rustls =
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".rustls."0.23.18" {
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".rustls."0.23.19" {
inherit profileName;
}).out;
socket2 =
@ -6927,7 +6931,7 @@ else
inherit profileName;
}).out;
tracing =
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.40" {
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.41" {
inherit profileName;
}).out;
};
@ -6969,7 +6973,7 @@ else
inherit profileName;
}).out;
rustls =
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".rustls."0.23.18" {
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".rustls."0.23.19" {
inherit profileName;
}).out;
${if false then "rustls_pki_types" else null} =
@ -6989,7 +6993,7 @@ else
inherit profileName;
}).out;
tracing =
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.40" {
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.41" {
inherit profileName;
}).out;
${if false then "web_time" else null} =
@ -7026,7 +7030,7 @@ else
inherit profileName;
}).out;
tracing =
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.40" {
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.41" {
inherit profileName;
}).out;
${if hostPlatform.isWindows then "windows_sys" else null} =
@ -7471,7 +7475,7 @@ else
inherit profileName;
}).out;
${if !(hostPlatform.parsed.cpu.name == "wasm32") then "rustls" else null} =
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".rustls."0.23.18" {
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".rustls."0.23.19" {
inherit profileName;
}).out;
${if !(hostPlatform.parsed.cpu.name == "wasm32") then "rustls_pemfile" else null} =
@ -7997,15 +8001,15 @@ else
};
});
"registry+https://github.com/rust-lang/crates.io-index".rustls."0.23.18" =
"registry+https://github.com/rust-lang/crates.io-index".rustls."0.23.19" =
overridableMkRustCrate
(profileName: rec {
name = "rustls";
version = "0.23.18";
version = "0.23.19";
registry = "registry+https://github.com/rust-lang/crates.io-index";
src = fetchCratesIo {
inherit name version;
sha256 = "9c9cc1d47e243d655ace55ed38201c19ae02c148ae56412ab8750e8f0166ab7f";
sha256 = "934b404430bb06b3fae2cba809eb45a1ab1aecd64491213d7c3301b88393f8d1";
};
features = builtins.concatLists [
[ "aws_lc_rs" ]
@ -9387,10 +9391,10 @@ else
[ "json" ]
[ "macros" ]
[ "migrate" ]
[ "postgres" ]
[ "runtime-tokio" ]
[ "sqlite" ]
[ "sqlx-macros" ]
[ "sqlx-sqlite" ]
[ "sqlx-postgres" ]
];
dependencies = {
sqlx_core =
@ -9558,7 +9562,7 @@ else
inherit profileName;
}).out;
tracing =
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.40" {
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.41" {
inherit profileName;
}).out;
url =
@ -9585,7 +9589,7 @@ else
[ "json" ]
[ "macros" ]
[ "migrate" ]
[ "sqlite" ]
[ "postgres" ]
];
dependencies = {
proc_macro2 =
@ -9628,8 +9632,8 @@ else
[ "json" ]
[ "macros" ]
[ "migrate" ]
[ "sqlite" ]
[ "sqlx-sqlite" ]
[ "postgres" ]
[ "sqlx-postgres" ]
[ "tokio" ]
];
dependencies = {
@ -9855,7 +9859,7 @@ else
inherit profileName;
}).out;
tracing =
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.40" {
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.41" {
inherit profileName;
}).out;
whoami =
@ -9995,7 +9999,7 @@ else
inherit profileName;
}).out;
tracing =
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.40" {
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.41" {
inherit profileName;
}).out;
whoami =
@ -10076,7 +10080,7 @@ else
inherit profileName;
}).out;
tracing =
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.40" {
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.41" {
inherit profileName;
}).out;
url =
@ -10684,7 +10688,7 @@ else
];
dependencies = {
rustls =
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".rustls."0.23.18" {
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".rustls."0.23.19" {
inherit profileName;
}).out;
pki_types =
@ -10888,7 +10892,7 @@ else
inherit profileName;
}).out;
tracing =
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.40" {
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.41" {
inherit profileName;
}).out;
};
@ -10946,7 +10950,7 @@ else
inherit profileName;
}).out;
tracing =
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.40" {
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.41" {
inherit profileName;
}).out;
};
@ -11046,7 +11050,7 @@ else
inherit profileName;
}).out;
tracing =
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.40" {
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.41" {
inherit profileName;
}).out;
};
@ -11076,15 +11080,15 @@ else
};
});
"registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.40" =
"registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.41" =
overridableMkRustCrate
(profileName: rec {
name = "tracing";
version = "0.1.40";
version = "0.1.41";
registry = "registry+https://github.com/rust-lang/crates.io-index";
src = fetchCratesIo {
inherit name version;
sha256 = "c3523ab5a71916ccf420eebdf5521fcef02141234bbc0b8a49f2fdc4544364ef";
sha256 = "784e0ac535deb450455cbfa28a6f0df145ea1bb7ae51b821cf5e7927fdcfbdd0";
};
features = builtins.concatLists [
[ "attributes" ]
@ -11183,7 +11187,7 @@ else
];
dependencies = {
tracing =
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.40" {
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.41" {
inherit profileName;
}).out;
tracing_subscriber =
@ -11315,7 +11319,7 @@ else
inherit profileName;
}).out;
tracing =
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.40" {
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".tracing."0.1.41" {
inherit profileName;
}).out;
tracing_core =
@ -11494,7 +11498,7 @@ else
inherit profileName;
}).out;
rustls =
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".rustls."0.23.18" {
(rustPackages."registry+https://github.com/rust-lang/crates.io-index".rustls."0.23.19" {
inherit profileName;
}).out;
rustls_pki_types =

View file

@ -1,12 +0,0 @@
{
"db_name": "SQLite",
"query": "DELETE FROM 'file' WHERE 'id' = ?",
"describe": {
"columns": [],
"parameters": {
"Right": 1
},
"nullable": []
},
"hash": "3b729cf7297b569253249000a3cc0de67106e426a1ae4e7cdac6eb85eeeb3849"
}

View file

@ -0,0 +1,14 @@
{
"db_name": "PostgreSQL",
"query": "DELETE FROM file_map WHERE \"id\" = $1",
"describe": {
"columns": [],
"parameters": {
"Left": [
"Int8"
]
},
"nullable": []
},
"hash": "493543eea67ec3e3ffa4f99e72c3bc730a66fc2c67f4df6327ebd2d83fbea5be"
}

View file

@ -1,12 +0,0 @@
{
"db_name": "SQLite",
"query": "UPDATE 'file' SET 'file_path' = ?, 'mime' = ?, 'b3hash' = ? WHERE 'id' = ?",
"describe": {
"columns": [],
"parameters": {
"Right": 4
},
"nullable": []
},
"hash": "88de2bc8bea8b9b5f1de736af16ed3cc0a2711b37687e595619fa001fa2633bc"
}

View file

@ -0,0 +1,17 @@
{
"db_name": "PostgreSQL",
"query": "UPDATE file_map SET \"file_path\" = $1, \"mime\" = $2, \"b3hash\" = $3 WHERE \"id\" = $4",
"describe": {
"columns": [],
"parameters": {
"Left": [
"Text",
"Text",
"Bytea",
"Int8"
]
},
"nullable": []
},
"hash": "b2fb58d4c75841a1255cc06ca009e874853f418594f89648800b6d4183a68273"
}

View file

@ -6,16 +6,11 @@ edition = "2021"
[dependencies]
bincode = "2.0.0-rc.3"
serde = { version = "1.0.215", features = ["derive"] }
sqlx = { version = "0.8.2", features = [
"runtime-tokio",
"sqlite",
"derive",
"macros",
"migrate",
] }
sqlx = { version = "0.8.2", features = ["runtime-tokio", "derive", "macros", "migrate", "postgres"] }
eyre = "0.6.12"
tracing = "0.1.40"
blake3 = { version = "1.5.4", features = ["serde"] }
tokio = { version = "1.41.1", features = ["fs"] }
[lints.rust]
deprecated-safe = "forbid"

View file

@ -1,10 +1,10 @@
-- Add migration script here
create table 'file' (
id integer primary key not null,
create table file_map (
id bigint generated always as identity,
file_path text not null,
mime text not null,
b3hash blob not null
b3hash bytea not null
);
create unique index file_path_mime on 'file' (file_path, mime);
create index file_path on 'file' (file_path);
create unique index file_path_mime on file_map (file_path, mime);
create index file_path on file_map (file_path);

View file

@ -5,7 +5,7 @@ use blake3::Hash;
use eyre::Context as _;
use eyre::Result;
use serde::{Deserialize, Serialize};
use sqlx::{prelude::FromRow, query_as, sqlite::SqliteRow};
use sqlx::{postgres::PgRow, prelude::FromRow, query_as};
use sqlx::{query, Row as _};
use tracing::instrument;
@ -53,8 +53,8 @@ impl Decode for File {
}
}
impl<'r> FromRow<'r, SqliteRow> for File {
fn from_row(row: &'r SqliteRow) -> std::result::Result<Self, sqlx::Error> {
impl<'r> FromRow<'r, PgRow> for File {
fn from_row(row: &'r PgRow) -> std::result::Result<Self, sqlx::Error> {
let id = u64::try_from(row.try_get::<i64, _>("id")?).unwrap_or_default();
let file_path: String = row.try_get("file_path")?;
let mime: String = row.try_get("mime")?;
@ -86,7 +86,7 @@ impl File {
/// This function returns an error if a database error occurs while loading.
#[instrument(skip(db))]
pub async fn get_by_path_mime(db: &Database, path: &str, mime: &str) -> Result<Option<Self>> {
query_as("SELECT * FROM 'file' WHERE 'path' = ? AND 'mime' = ?")
query_as(r#"SELECT * FROM file_map WHERE "path" = $1 AND "mime" = $2"#)
.bind(path)
.bind(mime)
.fetch_optional(&*db.0)
@ -100,7 +100,7 @@ impl File {
/// This function returns an error if a database error occurs while loading.
#[instrument(skip(db))]
pub async fn get_by_path(db: &Database, path: &str) -> Result<Vec<Self>> {
query_as("SELECT * FROM 'file' WHERE 'path' = ?")
query_as(r#"SELECT * FROM file_map WHERE "path" = $1"#)
.bind(path)
.fetch_all(&*db.0)
.await
@ -114,7 +114,7 @@ impl File {
#[instrument(skip(db))]
pub async fn list(db: &Database, after: i64, limit: usize) -> Result<Vec<Self>> {
let limit: i64 = limit.min(100).try_into().unwrap_or(100); // reasonable limit for pagination size
query_as("SELECT * FROM 'file' WHERE id > ? LIMIT ?")
query_as(r#"SELECT * FROM file_map WHERE "id" > $1 LIMIT $2"#)
.bind(after)
.bind(limit)
.fetch_all(&*db.0)
@ -128,13 +128,15 @@ impl File {
/// This function returns an error if a database error occurs when writing, or there is a conflict
#[instrument(skip(db))]
pub async fn new(db: &Database, path: &str, mime: &str, hash: &Hash) -> Result<()> {
query_as("INSERT INTO 'file' ('file_path', 'mime', 'b3hash') VALUES (?, ?, ?) RETURNING *")
.bind(path)
.bind(mime)
.bind(hash.as_bytes().as_slice())
.fetch_one(&*db.0)
.await
.with_context(|| format!("Inserting new file {path} with mime type {mime}"))
query_as(
r#"INSERT INTO file_map ("file_path", "mime", "b3hash") VALUES ($1, $2, $3) RETURNING *"#,
)
.bind(path)
.bind(mime)
.bind(hash.as_bytes().as_slice())
.fetch_one(&*db.0)
.await
.with_context(|| format!("Inserting new file {path} with mime type {mime}"))
}
/// Deletes a file record from the database. This does not perform any actual file deletion.
@ -144,7 +146,7 @@ impl File {
#[instrument(skip(db))]
pub async fn delete(self, db: &Database) -> Result<()> {
let id: i64 = self.id.try_into()?;
query!("DELETE FROM 'file' WHERE 'id' = ?", id)
query!(r#"DELETE FROM file_map WHERE "id" = $1"#, id)
.execute(&*db.0)
.await
.with_context(|| {
@ -171,7 +173,7 @@ impl File {
let id: i64 = self.id.try_into()?;
let b3hash = self.b3hash.as_bytes().as_slice();
query!(
"UPDATE 'file' SET 'file_path' = ?, 'mime' = ?, 'b3hash' = ? WHERE 'id' = ?",
r#"UPDATE file_map SET "file_path" = $1, "mime" = $2, "b3hash" = $3 WHERE "id" = $4"#,
self.file_path,
self.mime,
b3hash,

View file

@ -3,7 +3,7 @@
use std::sync::Arc;
use eyre::{Context, Result};
use sqlx::{migrate, SqlitePool};
use sqlx::{migrate, PgPool};
use tracing::instrument;
pub mod file;
@ -11,7 +11,7 @@ pub mod file;
/// Opaque database handle
#[derive(Clone, Debug)]
#[repr(transparent)]
pub struct Database(Arc<SqlitePool>);
pub struct Database(Arc<PgPool>);
/// Opens the database
///
@ -19,7 +19,10 @@ pub struct Database(Arc<SqlitePool>);
/// This function returns an error if a connection to the database could not be established
#[instrument]
pub async fn open_database(path: &str) -> Result<Database> {
let pool = SqlitePool::connect(path)
let db_string = tokio::fs::read_to_string(path)
.await
.with_context(|| format!("Reading db connection string at {path}"))?;
let pool = PgPool::connect(&db_string)
.await
.with_context(|| format!("Opening database {path}"))?;
migrate!().run(&pool).await?;

View file

@ -12,9 +12,12 @@ host = "lotte.chir.rs"
private_key = "secrets/server.key"
certificate = "secrets/server.crt"
[database]
path = "secrets/test.db"
# This file would contain something like
# postgresql://darkkirb:blah@localhost:6432/darkkirb
path = "secrets/db_url.txt"
[s3]
endpoint = "https://ams1.vultrobjects.com/"
region = "us-east-1"
access_key_id_file = "secrets/access_key_id.txt"
secret_access_key_file = "secrets/secret_access_key.txt"