From e4024d05f9758b3faf0cb998ef9194b072a1ea83 Mon Sep 17 00:00:00 2001 From: Martin Berg Alstad Date: Wed, 5 Mar 2025 21:56:41 +0100 Subject: [PATCH] :sparkles: Nix shell, pg docker, fix paths, replaced make with just --- .env | 5 ++++- .idea/dataSources.xml | 2 +- Cargo.lock | 8 ++++---- Cargo.toml | 4 ++-- Makefile.toml | 27 -------------------------- diesel.toml | 2 +- docker-compose.yml | 9 +++++++++ flake.lock | 44 +++++++++++++++++++++++++++++++++++++++++++ flake.nix | 38 +++++++++++++++++++++++++++++++++++++ justfile | 23 ++++++++++++++++++++++ src/database.rs | 5 +---- 11 files changed, 127 insertions(+), 40 deletions(-) delete mode 100644 Makefile.toml create mode 100644 docker-compose.yml create mode 100644 flake.lock create mode 100644 flake.nix create mode 100644 justfile diff --git a/.env b/.env index fe143f5..d7e76f7 100644 --- a/.env +++ b/.env @@ -1 +1,4 @@ -DATABASE_URL="postgres://postgres:postgres@localhost:32784/postgres" \ No newline at end of file +DATABASE_URL="postgres://postgres:postgres@localhost:5432/postgres" +POSTGRES_DB=postgres +POSTGRES_USER=postgres +POSTGRES_PASSWORD=postgres \ No newline at end of file diff --git a/.idea/dataSources.xml b/.idea/dataSources.xml index 75f69bb..3362652 100644 --- a/.idea/dataSources.xml +++ b/.idea/dataSources.xml @@ -5,7 +5,7 @@ postgresql true org.postgresql.Driver - jdbc:postgresql://localhost:32769/postgres + jdbc:postgresql://localhost:5432/postgres $ProjectFileDir$ diff --git a/Cargo.lock b/Cargo.lock index ea4671d..141e886 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "addr2line" @@ -1512,7 +1512,6 @@ version = "1.4.3" dependencies = [ "async-trait", "axum", - "bon", "chrono", "deadpool-diesel", "derive_more", @@ -2847,14 +2846,15 @@ dependencies = [ [[package]] name = "tower-http" -version = "0.6.1" +version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8437150ab6bbc8c5f0f519e3d5ed4aa883a83dd4cdd3d1b21f9482936046cb97" +checksum = "1e9cd434a998747dd2c4276bc96ee2e0c7a2eadf3cae88e52be55a05fa9053f5" dependencies = [ "bitflags 2.6.0", "bytes", "http", "http-body", + "http-body-util", "pin-project-lite", "tower-layer", "tower-service", diff --git a/Cargo.toml b/Cargo.toml index a51d48f..8de70b5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -41,11 +41,11 @@ bon = "2.0.0" validator = { version = "0.18.1", features = ["derive"] } axum-valid = { version = "0.20.0", features = ["validator"] } -lib = { path = "../lib", features = ["axum", "serde", "derive", "diesel", "time"] } +lib = { path = "../rust-lib", features = ["axum", "serde", "derive", "diesel", "time"] } #lib = { git = "https://github.com/emberal/rust-lib", tag = "1.4.3", features = ["axum", "serde", "derive"] } [dev-dependencies] -lib = { path = "../lib", features = ["test"] } +lib = { path = "../rust-lib", features = ["test"] } rstest = "0.22.0" testcontainers-modules = { version = "0.10.0", features = ["postgres"] } async-std = { version = "1.12.0", features = ["attributes"] } diff --git a/Makefile.toml b/Makefile.toml deleted file mode 100644 index 580f542..0000000 --- a/Makefile.toml +++ /dev/null @@ -1,27 +0,0 @@ -[tasks.fmt] -command = "cargo" -args = ["fmt"] - -[tasks.lint] -command = "cargo" -args = ["clippy"] - -[tasks.release] -command = "cargo" -args = ["build", "--release"] - -[tasks.run_migrations] -command = "diesel" -args = ["migration", "run"] - -[tasks.redo_migrations] -command = "diesel" -args = ["migration", "redo"] - -[tasks.test] -command = "cargo" -args = ["test", "--all-features"] - -[tasks.coverage] -command = "cargo" -args = ["llvm-cov"] diff --git a/diesel.toml b/diesel.toml index ed486ba..83d15a9 100644 --- a/diesel.toml +++ b/diesel.toml @@ -6,4 +6,4 @@ file = "src/schema.rs" custom_type_derives = ["diesel::query_builder::QueryId", "Clone"] [migrations_directory] -dir = "/home/martin/git/rust/hotel_service/migrations" +dir = "./migrations" diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..ab7fb6f --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,9 @@ +services: + database: + image: postgres:latest + ports: + - "5432:5432" + env_file: + - .env + volumes: + - ${PWD}/db-data/:/var/lib/postgresql/data/ \ No newline at end of file diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..c3ae6a3 --- /dev/null +++ b/flake.lock @@ -0,0 +1,44 @@ +{ + "nodes": { + "nixpkgs": { + "locked": { + "lastModified": 1741048562, + "narHash": "sha256-W4YZ3fvWZiFYYyd900kh8P8wU6DHSiwaH0j4+fai1Sk=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "6af28b834daca767a7ef99f8a7defa957d0ade6f", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-24.11", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs-unstable": { + "locked": { + "lastModified": 1741010256, + "narHash": "sha256-WZNlK/KX7Sni0RyqLSqLPbK8k08Kq7H7RijPJbq9KHM=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "ba487dbc9d04e0634c64e3b1f0d25839a0a68246", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "nixpkgs": "nixpkgs", + "nixpkgs-unstable": "nixpkgs-unstable" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..dca4fc3 --- /dev/null +++ b/flake.nix @@ -0,0 +1,38 @@ +{ + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.11"; + nixpkgs-unstable.url = "github:NixOS/nixpkgs/nixos-unstable"; + }; + + outputs = { nixpkgs, nixpkgs-unstable, ... }: + let + system = "x86_64-linux"; + in + { + devShells.${system}.default = + let + pkgs = import nixpkgs { + inherit system; + }; + unstable = import nixpkgs-unstable { + inherit system; + }; + in + pkgs.mkShell { + packages = with pkgs; [ + git + just + ] ++ [ + # Rust + gcc + cargo + ] ++ [ + # Diesel + diesel-cli + unstable.libpq + ]; + + shellHook = "fish"; + }; + }; +} diff --git a/justfile b/justfile new file mode 100644 index 0000000..b56301a --- /dev/null +++ b/justfile @@ -0,0 +1,23 @@ +run: + cargo run + +fmt: + cargo fmt + +lint: + cargo clippy + +release: + cargo build --release + +run_migrations: + diesel migration run + +redo_migrations: + diesel migration redo + +test: + cargo test --all-features + +coverage: + cargo llvm-cov diff --git a/src/database.rs b/src/database.rs index f48ef8e..fc790dc 100644 --- a/src/database.rs +++ b/src/database.rs @@ -3,8 +3,5 @@ use deadpool_diesel::postgres::BuildError; use lib::diesel::pool::PgPool; pub(crate) fn create_pool() -> Result { - lib::diesel::pool::create_pool() - .url(config::DATABASE_URL) - .size(config::POOL_SIZE) - .call() + lib::diesel::pool::create_pool_from_url_with_size(config::DATABASE_URL, config::POOL_SIZE) }