From 4847a13c0887a3ab1014452333db42d9cac146be Mon Sep 17 00:00:00 2001 From: Xander Bil Date: Sat, 8 Jun 2024 19:48:40 +0200 Subject: [PATCH] Convert from sqlite to postgres database --- Cargo.lock | 77 ++++--------------- Cargo.toml | 2 +- .../2024-03-03-220459_create_records/up.sql | 2 +- src/db/lib.rs | 5 +- src/db/models.rs | 6 +- src/schema.rs | 10 +-- 6 files changed, 28 insertions(+), 74 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5d0d18d..1b740cd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -68,6 +68,12 @@ version = "3.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" +[[package]] +name = "byteorder" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" + [[package]] name = "bytes" version = "1.6.0" @@ -105,24 +111,17 @@ version = "0.8.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06ea2b9bc92be3c2baa9334a323ebca2d6f074ff852cd1d7b11064035cd3868f" -[[package]] -name = "deranged" -version = "0.3.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b42b6fa04a440b495c8b04d0e71b707c585f83cb9cb28cf8cd0d976c315e31b4" -dependencies = [ - "powerfmt", -] - [[package]] name = "diesel" version = "2.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "62c6fcf842f17f8c78ecf7c81d75c5ce84436b41ee07e03f490fbb5f5a8731d8" dependencies = [ + "bitflags 2.5.0", + "byteorder", "diesel_derives", - "libsqlite3-sys", - "time", + "itoa", + "pq-sys", ] [[package]] @@ -443,16 +442,6 @@ version = "0.2.153" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd" -[[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 = "linux-raw-sys" version = "0.4.14" @@ -514,12 +503,6 @@ dependencies = [ "tempfile", ] -[[package]] -name = "num-conv" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "51d515d32fb182ee37cda2ccdcb92950d6a3c2893aa280e540671c2cd0f3b1d9" - [[package]] name = "num_cpus" version = "1.16.0" @@ -634,10 +617,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d231b230927b5e4ad203db57bbcbee2802f6bce620b1e4a9024a07d94e2907ec" [[package]] -name = "powerfmt" -version = "0.2.0" +name = "pq-sys" +version = "0.4.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391" +checksum = "31c0052426df997c0cbd30789eb44ca097e3541717a7b8fa36b1c464ee7edebd" +dependencies = [ + "vcpkg", +] [[package]] name = "proc-macro2" @@ -911,37 +897,6 @@ dependencies = [ "windows-sys 0.52.0", ] -[[package]] -name = "time" -version = "0.3.34" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c8248b6521bb14bc45b4067159b9b6ad792e2d6d754d6c41fb50e29fefe38749" -dependencies = [ - "deranged", - "itoa", - "num-conv", - "powerfmt", - "serde", - "time-core", - "time-macros", -] - -[[package]] -name = "time-core" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ef927ca75afb808a4d64dd374f00a2adf8d0fcff8e7b184af886c3c87ec4a3f3" - -[[package]] -name = "time-macros" -version = "0.2.17" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ba3a3ef41e6672a2f0f001392bb5dcd3ff0a9992d618ca761a11c3121547774" -dependencies = [ - "num-conv", - "time-core", -] - [[package]] name = "tinyvec" version = "1.6.0" diff --git a/Cargo.toml b/Cargo.toml index 880c7d3..a1497dd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,7 @@ edition = "2021" [dependencies] -diesel = { version = "2.1.4", features = ["sqlite"] } +diesel = { version = "2.1.4", features = ["postgres"] } dotenvy = "0.15" tokio = {version = "1.36.0", features = ["macros","rt-multi-thread","net"], default-features = false} ring = "0.17.8" diff --git a/migrations/2024-03-03-220459_create_records/up.sql b/migrations/2024-03-03-220459_create_records/up.sql index 67e9c48..0fb7ed2 100644 --- a/migrations/2024-03-03-220459_create_records/up.sql +++ b/migrations/2024-03-03-220459_create_records/up.sql @@ -5,7 +5,7 @@ CREATE TABLE records ( class INT NOT NULL, ttl INT NOT NULL, rdlength INT NOT NULL, - rdata BLOB NOT NULL, + rdata BYTEA NOT NULL, PRIMARY KEY (name,type,class,rdlength,rdata) ) diff --git a/src/db/lib.rs b/src/db/lib.rs index c27c753..ce68bc1 100644 --- a/src/db/lib.rs +++ b/src/db/lib.rs @@ -1,10 +1,9 @@ use diesel::prelude::*; -use diesel::sqlite::SqliteConnection; use crate::config::Config; -pub fn establish_connection() -> SqliteConnection { +pub fn establish_connection() -> PgConnection { let database_url = Config::get().db_uri.clone(); - SqliteConnection::establish(&database_url) + PgConnection::establish(&database_url) .unwrap_or_else(|_| panic!("Error connecting to {}", Config::get().db_uri)) } diff --git a/src/db/models.rs b/src/db/models.rs index b034775..ec3ddd3 100644 --- a/src/db/models.rs +++ b/src/db/models.rs @@ -35,7 +35,7 @@ struct Record { impl Record { pub fn get( - db: &mut SqliteConnection, + db: &mut PgConnection, name: String, _type: i32, class: i32, @@ -50,7 +50,7 @@ impl Record { } pub fn create( - db: &mut SqliteConnection, + db: &mut PgConnection, new_record: Record, ) -> Result { diesel::insert_into(records::table) @@ -59,7 +59,7 @@ impl Record { } pub fn delete( - db: &mut SqliteConnection, + db: &mut PgConnection, name: String, _type: Option, class: i32, diff --git a/src/schema.rs b/src/schema.rs index d03d571..e833484 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -4,10 +4,10 @@ diesel::table! { records (name, type_, class, rdlength, rdata) { name -> Text, #[sql_name = "type"] - type_ -> Integer, - class -> Integer, - ttl -> Integer, - rdlength -> Integer, - rdata -> Binary, + type_ -> Int4, + class -> Int4, + ttl -> Int4, + rdlength -> Int4, + rdata -> Bytea, } }