diff --git a/.cargo/config.toml b/.cargo/config.toml index 2a9d7bf..0a40fa3 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -2,4 +2,4 @@ POSTGRES_USER = "postgres" POSTGRES_PASSWORD = "DB_PASS_HERE" POSTGRES_HOST = "localhost" -POSTGRES_DBNAME = "test" \ No newline at end of file +POSTGRES_DBNAME = "questionanswer" \ No newline at end of file diff --git a/README.md b/README.md index e95f315..62c2bc5 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/migrations/0_init_schema.sql b/migrations/0_init_schema.sql index 9594926..d358fbc 100644 --- a/migrations/0_init_schema.sql +++ b/migrations/0_init_schema.sql @@ -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, -} \ No newline at end of file +-- Create answers table +CREATE TABLE IF NOT EXISTS answers ( + id SERIAL PRIMARY KEY, + question_id INT REFERENCES questions(id), + content TEXT NOT NULL +); diff --git a/src/main.rs b/src/main.rs index b947c0d..0f5e22a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -56,10 +56,11 @@ async fn serve_file(uri: Uri) -> impl IntoResponse { async fn db_connection() -> Result, 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 = db_connection().await.unwrap(); + let db: Pool = db_connection().await.unwrap(); + sqlx::migrate!().run(&db).await.unwrap(); axum::serve(listener, app).await.unwrap(); }