From db8a180c000f7729cc08efb89609343ff5218c35 Mon Sep 17 00:00:00 2001 From: mStar aka a person <12024604-mstarongitlab@users.noreply.gitlab.com> Date: Wed, 29 Nov 2023 12:55:56 +0000 Subject: [PATCH] Migrate to diesel for db --- .env | 1 + .gitignore | 1 + Cargo.lock | 75 +++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + diesel.toml | 9 +++++ migrations/.keep | 0 src/storage/mod.rs | 39 +++++++++++++++++++-- src/storage/postgres.rs | 44 ------------------------ src/storage/sqlite.rs | 44 ------------------------ 9 files changed, 123 insertions(+), 91 deletions(-) create mode 100644 .env create mode 100644 diesel.toml create mode 100644 migrations/.keep delete mode 100644 src/storage/postgres.rs delete mode 100644 src/storage/sqlite.rs diff --git a/.env b/.env new file mode 100644 index 0000000..d1c4023 --- /dev/null +++ b/.env @@ -0,0 +1 @@ +DATABASE_URL=./src/storage/db.sqlite \ No newline at end of file diff --git a/.gitignore b/.gitignore index ea8c4bf..359d7c0 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /target +src/storage/db.sqlite \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index 8f24fc1..c9fb174 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -432,6 +432,12 @@ version = "3.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f30e7476521f6f8af1a1c4c0b8cc94f0bee37d91763d0ca2665f299b6cd8aec" +[[package]] +name = "byteorder" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" + [[package]] name = "bytes" version = "1.5.0" @@ -603,6 +609,45 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "diesel" +version = "2.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62c6fcf842f17f8c78ecf7c81d75c5ce84436b41ee07e03f490fbb5f5a8731d8" +dependencies = [ + "bitflags 2.4.1", + "byteorder", + "diesel_derives", + "itoa", + "libsqlite3-sys", + "mysqlclient-sys", + "percent-encoding", + "pq-sys", + "time", + "url", +] + +[[package]] +name = "diesel_derives" +version = "2.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef8337737574f55a468005a83499da720f20c65586241ffea339db9ecdfd2b44" +dependencies = [ + "diesel_table_macro_syntax", + "proc-macro2", + "quote", + "syn 2.0.39", +] + +[[package]] +name = "diesel_table_macro_syntax" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc5557efc453706fed5e4fa85006fe9817c224c3f480a34c7e5959fd700921c5" +dependencies = [ + "syn 2.0.39", +] + [[package]] name = "digest" version = "0.10.7" @@ -1138,12 +1183,23 @@ version = "0.2.150" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "89d92a4743f9a61002fae18374ed11e7973f530cb3a3255fb354818118b2203c" +[[package]] +name = "libsqlite3-sys" +version = "0.27.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf4e226dcd58b4be396f7bd3c20da8fdee2911400705297ba7d2d7cc2c30f716" +dependencies = [ + "pkg-config", + "vcpkg", +] + [[package]] name = "linstrom" version = "0.1.0" dependencies = [ "activitypub_federation", "axum 0.7.1", + "diesel", "tokio", ] @@ -1235,6 +1291,16 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "mysqlclient-sys" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f61b381528ba293005c42a409dd73d034508e273bf90481f17ec2e964a6e969b" +dependencies = [ + "pkg-config", + "vcpkg", +] + [[package]] name = "native-tls" version = "0.2.11" @@ -1416,6 +1482,15 @@ version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" +[[package]] +name = "pq-sys" +version = "0.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "31c0052426df997c0cbd30789eb44ca097e3541717a7b8fa36b1c464ee7edebd" +dependencies = [ + "vcpkg", +] + [[package]] name = "proc-macro2" version = "1.0.70" diff --git a/Cargo.toml b/Cargo.toml index 0b1d8dc..f029edf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,4 +15,5 @@ exclude = ["/.vscode", "/FeatureTargets.md"] [dependencies] activitypub_federation = "0.4.7" axum = "0.7.1" +diesel = { version = "2.1.4", features = ["sqlite", "postgres"] } tokio = { version = "1.34.0", features = ["full"]} diff --git a/diesel.toml b/diesel.toml new file mode 100644 index 0000000..c028f4a --- /dev/null +++ b/diesel.toml @@ -0,0 +1,9 @@ +# For documentation on how to configure this file, +# see https://diesel.rs/guides/configuring-diesel-cli + +[print_schema] +file = "src/schema.rs" +custom_type_derives = ["diesel::query_builder::QueryId"] + +[migrations_directory] +dir = "migrations" diff --git a/migrations/.keep b/migrations/.keep new file mode 100644 index 0000000..e69de29 diff --git a/src/storage/mod.rs b/src/storage/mod.rs index 271a5db..070fec0 100644 --- a/src/storage/mod.rs +++ b/src/storage/mod.rs @@ -8,9 +8,7 @@ // See the Licence for the specific language governing permissions and limitations under the Licence. use tokio::fs::File; - -mod sqlite; -mod postgres; +use diesel:: pub type MessageID = i64; pub type MediaID = i64; @@ -27,3 +25,38 @@ pub trait StorageAccess { fn delete_media(id: &MediaID) -> Result; // No edit media because how? Editing media in a message replaces the media, not edits it } + + +pub struct Storage { + db: +} + +impl StorageAccess for Storage { + fn store_message(user: &str, message: &str) -> Result { + todo!() + } + + fn get_message(user: &str, id: &MessageID) -> Result { + todo!() + } + + fn delete_message(id: &MessageID) -> Result { + todo!() + } + + fn edit_message(id: &MessageID, message: &str) -> Result { + todo!() + } + + fn store_media(f: &File, nsfw: bool, alt_text: Option<&str>) -> Result { + todo!() + } + + fn get_media(id: &MediaID) -> Result<(File, bool, Option), E> { + todo!() + } + + fn delete_media(id: &MediaID) -> Result { + todo!() + } +} \ No newline at end of file diff --git a/src/storage/postgres.rs b/src/storage/postgres.rs deleted file mode 100644 index d9418c6..0000000 --- a/src/storage/postgres.rs +++ /dev/null @@ -1,44 +0,0 @@ -// Copyright (c) 2023 mStar -// -// Licensed under the EUPL, Version 1.2 -// -// You may not use this work except in compliance with the Licence. -// You should have received a copy of the Licence along with this work. If not, see: -// . -// See the Licence for the specific language governing permissions and limitations under the Licence. - -use crate::storage; - -struct PostgressDB { - -} - -impl storage::StorageAccess for PostgressDB { - fn store_message(user: &str, message: &str) -> Result { - todo!() - } - - fn get_message(user: &str, id: &storage::MessageID) -> Result { - todo!() - } - - fn delete_message(id: &storage::MessageID) -> Result { - todo!() - } - - fn edit_message(id: &storage::MessageID, message: &str) -> Result { - todo!() - } - - fn store_media(f: &tokio::fs::File, nsfw: bool, alt_text: Option<&str>) -> Result { - todo!() - } - - fn get_media(id: &storage::MediaID) -> Result<(tokio::fs::File, bool, Option), E> { - todo!() - } - - fn delete_media(id: &storage::MediaID) -> Result { - todo!() - } -} \ No newline at end of file diff --git a/src/storage/sqlite.rs b/src/storage/sqlite.rs deleted file mode 100644 index 0160f19..0000000 --- a/src/storage/sqlite.rs +++ /dev/null @@ -1,44 +0,0 @@ -// Copyright (c) 2023 mStar -// -// Licensed under the EUPL, Version 1.2 -// -// You may not use this work except in compliance with the Licence. -// You should have received a copy of the Licence along with this work. If not, see: -// . -// See the Licence for the specific language governing permissions and limitations under the Licence. - -use crate::storage; - -struct LiteDB { - -} - -impl storage::StorageAccess for LiteDB { - fn store_message(user: &str, message: &str) -> Result { - todo!() - } - - fn get_message(user: &str, id: &storage::MessageID) -> Result { - todo!() - } - - fn delete_message(id: &storage::MessageID) -> Result { - todo!() - } - - fn edit_message(id: &storage::MessageID, message: &str) -> Result { - todo!() - } - - fn store_media(f: &tokio::fs::File, nsfw: bool, alt_text: Option<&str>) -> Result { - todo!() - } - - fn get_media(id: &storage::MediaID) -> Result<(tokio::fs::File, bool, Option), E> { - todo!() - } - - fn delete_media(id: &storage::MediaID) -> Result { - todo!() - } -} \ No newline at end of file