get sql migrations working; update readme
This commit is contained in:
parent
20158271ad
commit
2d05b0be07
@ -2,4 +2,4 @@
|
|||||||
POSTGRES_USER = "postgres"
|
POSTGRES_USER = "postgres"
|
||||||
POSTGRES_PASSWORD = "DB_PASS_HERE"
|
POSTGRES_PASSWORD = "DB_PASS_HERE"
|
||||||
POSTGRES_HOST = "localhost"
|
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
|
#### Rustup
|
||||||
First install Rust. I suggest using the [rustup toolchain](https://rustup.rs/)
|
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
|
#### Build and Run
|
||||||
```
|
```
|
||||||
cargo run
|
cargo run
|
||||||
|
@ -1,21 +1,26 @@
|
|||||||
CREATE TABLE IF NOT EXISTS questions {
|
-- Create questions table
|
||||||
id PRIMARY KEY,
|
CREATE TABLE IF NOT EXISTS questions (
|
||||||
|
id SERIAL PRIMARY KEY,
|
||||||
title TEXT NOT NULL,
|
title TEXT NOT NULL,
|
||||||
content TEXT NOT NULL,
|
content TEXT NOT NULL
|
||||||
}
|
);
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS tags {
|
-- Create tags table
|
||||||
id PRIMARY KEY,
|
CREATE TABLE IF NOT EXISTS tags (
|
||||||
label TEXT NOT NULL,
|
id SERIAL PRIMARY KEY,
|
||||||
}
|
label TEXT NOT NULL
|
||||||
|
);
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS question_tag {
|
-- Create question_tag table
|
||||||
question_id REFERENCES questions(id),
|
CREATE TABLE IF NOT EXISTS question_tag (
|
||||||
tag_id REFERENCES tags(id),
|
question_id INT REFERENCES questions(id),
|
||||||
}
|
tag_id INT REFERENCES tags(id),
|
||||||
|
PRIMARY KEY (question_id, tag_id)
|
||||||
|
);
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS answers {
|
-- Create answers table
|
||||||
id PRIMARY KEY,
|
CREATE TABLE IF NOT EXISTS answers (
|
||||||
question_id REFERENCES questions(id),
|
id SERIAL PRIMARY KEY,
|
||||||
content TEXT NOT NULL,
|
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> {
|
async fn db_connection() -> Result<Pool<Postgres>, sqlx::Error> {
|
||||||
let url: String = format!(
|
let url: String = format!(
|
||||||
"postgres://{}:{}@{}:5432",
|
"postgres://{}:{}@{}:5432/{}",
|
||||||
var("POSTGRES_USER").unwrap(),
|
var("POSTGRES_USER").unwrap(),
|
||||||
var("POSTGRES_PASSWORD").unwrap(),
|
var("POSTGRES_PASSWORD").unwrap(),
|
||||||
var("POSTGRES_HOST").unwrap(),
|
var("POSTGRES_HOST").unwrap(),
|
||||||
|
var("POSTGRES_DBNAME").unwrap()
|
||||||
);
|
);
|
||||||
PgPool::connect(&url).await
|
PgPool::connect(&url).await
|
||||||
}
|
}
|
||||||
@ -82,6 +83,7 @@ async fn main() {
|
|||||||
.route("/", get(serve_file))
|
.route("/", get(serve_file))
|
||||||
.route("/*path", get(serve_file))
|
.route("/*path", get(serve_file))
|
||||||
.fallback(handler_404);
|
.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();
|
axum::serve(listener, app).await.unwrap();
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user