get sql migrations working; update readme
This commit is contained in:
parent
20158271ad
commit
2d05b0be07
@ -2,4 +2,4 @@
|
||||
POSTGRES_USER = "postgres"
|
||||
POSTGRES_PASSWORD = "DB_PASS_HERE"
|
||||
POSTGRES_HOST = "localhost"
|
||||
POSTGRES_DBNAME = "test"
|
||||
POSTGRES_DBNAME = "questionanswer"
|
@ -18,6 +18,9 @@ I'll do my best to keep these contents up-to-date as changes are made to the sou
|
||||
#### Rustup
|
||||
First install Rust. I suggest using the [rustup toolchain](https://rustup.rs/)
|
||||
|
||||
#### Database
|
||||
A postgres database is required. At this time, the database must be setup and run manually (using docker is recommended). Create a database within postgres called `questionanswer` and choose a password. Set this password in `.cargo/config.toml`
|
||||
|
||||
#### Build and Run
|
||||
```
|
||||
cargo run
|
||||
|
@ -1,21 +1,26 @@
|
||||
CREATE TABLE IF NOT EXISTS questions {
|
||||
id PRIMARY KEY,
|
||||
-- Create questions table
|
||||
CREATE TABLE IF NOT EXISTS questions (
|
||||
id SERIAL PRIMARY KEY,
|
||||
title TEXT NOT NULL,
|
||||
content TEXT NOT NULL,
|
||||
}
|
||||
content TEXT NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS tags {
|
||||
id PRIMARY KEY,
|
||||
label TEXT NOT NULL,
|
||||
}
|
||||
-- Create tags table
|
||||
CREATE TABLE IF NOT EXISTS tags (
|
||||
id SERIAL PRIMARY KEY,
|
||||
label TEXT NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS question_tag {
|
||||
question_id REFERENCES questions(id),
|
||||
tag_id REFERENCES tags(id),
|
||||
}
|
||||
-- Create question_tag table
|
||||
CREATE TABLE IF NOT EXISTS question_tag (
|
||||
question_id INT REFERENCES questions(id),
|
||||
tag_id INT REFERENCES tags(id),
|
||||
PRIMARY KEY (question_id, tag_id)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS answers {
|
||||
id PRIMARY KEY,
|
||||
question_id REFERENCES questions(id),
|
||||
content TEXT NOT NULL,
|
||||
}
|
||||
-- Create answers table
|
||||
CREATE TABLE IF NOT EXISTS answers (
|
||||
id SERIAL PRIMARY KEY,
|
||||
question_id INT REFERENCES questions(id),
|
||||
content TEXT NOT NULL
|
||||
);
|
||||
|
@ -56,10 +56,11 @@ async fn serve_file(uri: Uri) -> impl IntoResponse {
|
||||
|
||||
async fn db_connection() -> Result<Pool<Postgres>, sqlx::Error> {
|
||||
let url: String = format!(
|
||||
"postgres://{}:{}@{}:5432",
|
||||
"postgres://{}:{}@{}:5432/{}",
|
||||
var("POSTGRES_USER").unwrap(),
|
||||
var("POSTGRES_PASSWORD").unwrap(),
|
||||
var("POSTGRES_HOST").unwrap(),
|
||||
var("POSTGRES_DBNAME").unwrap()
|
||||
);
|
||||
PgPool::connect(&url).await
|
||||
}
|
||||
@ -82,6 +83,7 @@ async fn main() {
|
||||
.route("/", get(serve_file))
|
||||
.route("/*path", get(serve_file))
|
||||
.fallback(handler_404);
|
||||
let _db: Pool<Postgres> = db_connection().await.unwrap();
|
||||
let db: Pool<Postgres> = db_connection().await.unwrap();
|
||||
sqlx::migrate!().run(&db).await.unwrap();
|
||||
axum::serve(listener, app).await.unwrap();
|
||||
}
|
||||
|
Reference in New Issue
Block a user