database setup
This commit is contained in:
parent
3918445f52
commit
6b8371936e
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,2 +1,4 @@
|
|||||||
/target
|
/target
|
||||||
result*
|
result*
|
||||||
|
/temp
|
||||||
|
/.direnv
|
||||||
|
122
Cargo.lock
generated
122
Cargo.lock
generated
@ -3,5 +3,125 @@
|
|||||||
version = 3
|
version = 3
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "rust-example"
|
name = "bitflags"
|
||||||
|
version = "2.4.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ed570934406eb16438a4e976b1b4500774099c13b8cb96eec99f620f05090ddf"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "byteorder"
|
||||||
|
version = "1.5.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "db"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"diesel",
|
||||||
|
"dotenvy",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "diesel"
|
||||||
|
version = "2.1.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "62c6fcf842f17f8c78ecf7c81d75c5ce84436b41ee07e03f490fbb5f5a8731d8"
|
||||||
|
dependencies = [
|
||||||
|
"bitflags",
|
||||||
|
"byteorder",
|
||||||
|
"diesel_derives",
|
||||||
|
"itoa",
|
||||||
|
"pq-sys",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[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",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "diesel_table_macro_syntax"
|
||||||
|
version = "0.1.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "fc5557efc453706fed5e4fa85006fe9817c224c3f480a34c7e5959fd700921c5"
|
||||||
|
dependencies = [
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "dotenvy"
|
||||||
|
version = "0.15.7"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1aaf95b3e5c8f23aa320147307562d361db0ae0d51242340f558153b4eb2439b"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "elnafo"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"db",
|
||||||
|
"diesel",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "itoa"
|
||||||
|
version = "1.0.10"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b1a46d1a171d865aa5f83f92695765caa047a9b4cbae2cbf37dbd613a793fd4c"
|
||||||
|
|
||||||
|
[[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.78"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e2422ad645d89c99f8f3e6b88a9fdeca7fabeac836b1002371c4367c8f984aae"
|
||||||
|
dependencies = [
|
||||||
|
"unicode-ident",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "quote"
|
||||||
|
version = "1.0.35"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "syn"
|
||||||
|
version = "2.0.52"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b699d15b36d1f02c3e7c69f8ffef53de37aefae075d8488d4ba1a7788d574a07"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"unicode-ident",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "unicode-ident"
|
||||||
|
version = "1.0.12"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "vcpkg"
|
||||||
|
version = "0.2.15"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
|
||||||
|
@ -1,7 +1,3 @@
|
|||||||
[package]
|
|
||||||
name = "elnafo"
|
|
||||||
version = "0.1.0"
|
|
||||||
edition = "2021"
|
|
||||||
|
|
||||||
[workspace]
|
[workspace]
|
||||||
members = ["crates/server"]
|
resolver = "2"
|
||||||
|
members = [ "crates/db","crates/server"]
|
||||||
|
@ -1,8 +1 @@
|
|||||||
# Basic Rust Example
|
# elnafo
|
||||||
|
|
||||||
* Creating/updating `Cargo.lock`
|
|
||||||
```shell
|
|
||||||
touch Cargo.lock
|
|
||||||
nix develop
|
|
||||||
cargo check
|
|
||||||
```
|
|
||||||
|
1
crates/db/.env
Normal file
1
crates/db/.env
Normal file
@ -0,0 +1 @@
|
|||||||
|
DATABASE_URL=postgres://elnafo:test@localhost/elnafo
|
8
crates/db/Cargo.toml
Normal file
8
crates/db/Cargo.toml
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
[package]
|
||||||
|
name = "db"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
diesel = { version = "2.1.4", features = ["postgres"] }
|
||||||
|
dotenvy = "0.15.7"
|
9
crates/db/diesel.toml
Normal file
9
crates/db/diesel.toml
Normal file
@ -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"
|
0
crates/db/migrations/.keep
Normal file
0
crates/db/migrations/.keep
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
-- This file was automatically created by Diesel to setup helper functions
|
||||||
|
-- and other internal bookkeeping. This file is safe to edit, any future
|
||||||
|
-- changes will be added to existing projects as new migrations.
|
||||||
|
|
||||||
|
DROP FUNCTION IF EXISTS diesel_manage_updated_at(_tbl regclass);
|
||||||
|
DROP FUNCTION IF EXISTS diesel_set_updated_at();
|
||||||
|
|
@ -0,0 +1,38 @@
|
|||||||
|
-- This file was automatically created by Diesel to setup helper functions
|
||||||
|
-- and other internal bookkeeping. This file is safe to edit, any future
|
||||||
|
-- changes will be added to existing projects as new migrations.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
-- Sets up a trigger for the given table to automatically set a column called
|
||||||
|
-- `updated_at` whenever the row is modified (unless `updated_at` was included
|
||||||
|
-- in the modified columns)
|
||||||
|
--
|
||||||
|
-- # Example
|
||||||
|
--
|
||||||
|
-- ```sql
|
||||||
|
-- CREATE TABLE users (id SERIAL PRIMARY KEY, updated_at TIMESTAMP NOT NULL DEFAULT NOW());
|
||||||
|
--
|
||||||
|
-- SELECT diesel_manage_updated_at('users');
|
||||||
|
-- ```
|
||||||
|
CREATE OR REPLACE FUNCTION diesel_manage_updated_at(_tbl regclass) RETURNS VOID AS $$
|
||||||
|
BEGIN
|
||||||
|
EXECUTE format('CREATE TRIGGER set_updated_at BEFORE UPDATE ON %s
|
||||||
|
FOR EACH ROW EXECUTE PROCEDURE diesel_set_updated_at()', _tbl);
|
||||||
|
END;
|
||||||
|
$$ LANGUAGE plpgsql;
|
||||||
|
|
||||||
|
CREATE OR REPLACE FUNCTION diesel_set_updated_at() RETURNS trigger AS $$
|
||||||
|
BEGIN
|
||||||
|
IF (
|
||||||
|
NEW IS DISTINCT FROM OLD AND
|
||||||
|
NEW.updated_at IS NOT DISTINCT FROM OLD.updated_at
|
||||||
|
) THEN
|
||||||
|
NEW.updated_at := current_timestamp;
|
||||||
|
END IF;
|
||||||
|
RETURN NEW;
|
||||||
|
END;
|
||||||
|
$$ LANGUAGE plpgsql;
|
||||||
|
|
||||||
|
|
@ -0,0 +1,2 @@
|
|||||||
|
-- This file should undo anything in `up.sql`
|
||||||
|
DROP TABLE IF EXISTS "users";
|
@ -0,0 +1,9 @@
|
|||||||
|
-- Your SQL goes here
|
||||||
|
CREATE TABLE "users"(
|
||||||
|
"id" SERIAL NOT NULL PRIMARY KEY,
|
||||||
|
"login" TEXT NOT NULL,
|
||||||
|
"hashed_password" TEXT NOT NULL,
|
||||||
|
"name" TEXT NOT NULL,
|
||||||
|
"email" TEXT NOT NULL,
|
||||||
|
"is_admin" BOOL NOT NULL
|
||||||
|
);
|
51
crates/db/src/lib.rs
Normal file
51
crates/db/src/lib.rs
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
pub mod models;
|
||||||
|
pub mod schema;
|
||||||
|
|
||||||
|
use diesel::prelude::*;
|
||||||
|
use dotenvy::dotenv;
|
||||||
|
use std::env;
|
||||||
|
|
||||||
|
use crate::models::{NewUser, User};
|
||||||
|
|
||||||
|
pub fn establish_connection() -> PgConnection {
|
||||||
|
dotenv().ok();
|
||||||
|
|
||||||
|
let db_url = env::var("DATABASE_URL").expect("Missed DATABASE_URL");
|
||||||
|
|
||||||
|
PgConnection::establish(&db_url).unwrap_or_else(|_| panic!("Error connecting to {}", db_url))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn create_user(
|
||||||
|
connection: &mut PgConnection,
|
||||||
|
login: &str,
|
||||||
|
hashed_password: &str,
|
||||||
|
name: &str,
|
||||||
|
email: &str,
|
||||||
|
is_admin: bool,
|
||||||
|
) -> User {
|
||||||
|
use crate::schema::users;
|
||||||
|
|
||||||
|
let new_user = NewUser {
|
||||||
|
login,
|
||||||
|
hashed_password,
|
||||||
|
name,
|
||||||
|
email,
|
||||||
|
is_admin,
|
||||||
|
};
|
||||||
|
|
||||||
|
diesel::insert_into(users::table)
|
||||||
|
.values(&new_user)
|
||||||
|
.returning(User::as_returning())
|
||||||
|
.get_result(connection)
|
||||||
|
.expect("Error creating new user")
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn it_works() {
|
||||||
|
unimplemented!();
|
||||||
|
}
|
||||||
|
}
|
24
crates/db/src/models.rs
Normal file
24
crates/db/src/models.rs
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
use crate::schema;
|
||||||
|
use diesel::prelude::*;
|
||||||
|
|
||||||
|
#[derive(Queryable, Selectable)]
|
||||||
|
#[diesel(table_name = schema::users)]
|
||||||
|
#[diesel(check_for_backend(diesel::pg::Pg))]
|
||||||
|
pub struct User {
|
||||||
|
pub id: i32,
|
||||||
|
pub login: String,
|
||||||
|
pub hashed_password: String,
|
||||||
|
pub name: String,
|
||||||
|
pub email: String,
|
||||||
|
pub is_admin: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Insertable)]
|
||||||
|
#[diesel(table_name = schema::users)]
|
||||||
|
pub struct NewUser<'a> {
|
||||||
|
pub login: &'a str,
|
||||||
|
pub hashed_password: &'a str,
|
||||||
|
pub name: &'a str,
|
||||||
|
pub email: &'a str,
|
||||||
|
pub is_admin: bool,
|
||||||
|
}
|
12
crates/db/src/schema.rs
Normal file
12
crates/db/src/schema.rs
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
// @generated automatically by Diesel CLI.
|
||||||
|
|
||||||
|
diesel::table! {
|
||||||
|
users (id) {
|
||||||
|
id -> Int4,
|
||||||
|
login -> Text,
|
||||||
|
hashed_password -> Text,
|
||||||
|
name -> Text,
|
||||||
|
email -> Text,
|
||||||
|
is_admin -> Bool,
|
||||||
|
}
|
||||||
|
}
|
@ -1,10 +1,13 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "server"
|
name = "elnafo"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
authors = ["L-Nafaryus <l.nafaryus@elnafo.ru"]
|
||||||
|
|
||||||
[[bin]]
|
[[bin]]
|
||||||
name = "elnafo"
|
name = "elnafo"
|
||||||
path = "src/main.rs"
|
path = "src/main.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
diesel = { version = "2.1.4", features = ["postgres"] }
|
||||||
|
db = { path = "../db" }
|
||||||
|
@ -1,3 +1,24 @@
|
|||||||
|
use db::{create_user, establish_connection, models::User};
|
||||||
|
use diesel::prelude::*;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("Hello, world!");
|
use db::schema::users::dsl::*;
|
||||||
|
|
||||||
|
let connection = &mut establish_connection();
|
||||||
|
|
||||||
|
create_user(
|
||||||
|
connection,
|
||||||
|
"L-Nafaryus",
|
||||||
|
"asdasd",
|
||||||
|
"L-Nafaryus",
|
||||||
|
"l.nafaryus@elnafo.ru",
|
||||||
|
true,
|
||||||
|
);
|
||||||
|
|
||||||
|
let results = users
|
||||||
|
.select(User::as_select())
|
||||||
|
.load(connection)
|
||||||
|
.expect("Error loading users");
|
||||||
|
|
||||||
|
println!("Found {} users", results.len());
|
||||||
}
|
}
|
||||||
|
22
flake.nix
22
flake.nix
@ -49,10 +49,28 @@
|
|||||||
});
|
});
|
||||||
|
|
||||||
devShells = forAllSystems (system: {
|
devShells = forAllSystems (system: {
|
||||||
default = let pkgs = nixpkgsFor.${system}; in pkgs.mkShell {
|
default = let
|
||||||
nativeBuildInputs = [
|
pkgs = nixpkgsFor.${system};
|
||||||
|
#db_host = "";
|
||||||
|
db_name = "elnafo";
|
||||||
|
db_user = "elnafo";
|
||||||
|
db_password = "test";
|
||||||
|
db_path = "temp/elnafo";
|
||||||
|
in pkgs.mkShell {
|
||||||
|
buildInputs = [
|
||||||
fenix.packages.${system}.complete.toolchain
|
fenix.packages.${system}.complete.toolchain
|
||||||
|
pkgs.ripgrep
|
||||||
|
pkgs.postgresql
|
||||||
|
pkgs.diesel-cli
|
||||||
];
|
];
|
||||||
|
|
||||||
|
shellHook = ''
|
||||||
|
trap "pg_ctl -D ${db_path} stop" EXIT
|
||||||
|
|
||||||
|
[ ! -d $(pwd)/${db_path} ] && initdb -D $(pwd)/${db_path} -U ${db_user}
|
||||||
|
pg_ctl -D $(pwd)/${db_path} -l $(pwd)/${db_path}/db.log -o "--unix_socket_directories=$(pwd)/${db_path}" start
|
||||||
|
#[ ! "$(psql -h $(pwd)/${db_path} -U ${db_user} -l | rg '^ ${db_name}')" ] && createdb -h $(pwd)/${db_path} -U ${db_user} ${db_name}
|
||||||
|
'';
|
||||||
};
|
};
|
||||||
elnafo = crane.lib.${system}.devShell {
|
elnafo = crane.lib.${system}.devShell {
|
||||||
checks = self.checks.${system};
|
checks = self.checks.${system};
|
||||||
|
Loading…
Reference in New Issue
Block a user