Add initial DB schema and connection pool
This commit is contained in:
parent
3693a19411
commit
1bcbd9d2ff
2 changed files with 29 additions and 0 deletions
22
src/db/migrations.sql
Normal file
22
src/db/migrations.sql
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
-- Migration 001: initial schema
|
||||
CREATE EXTENSION IF NOT EXISTS "pgcrypto";
|
||||
|
||||
CREATE TABLE users (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
username VARCHAR(64) UNIQUE NOT NULL,
|
||||
email VARCHAR(255) UNIQUE NOT NULL,
|
||||
password_hash TEXT NOT NULL,
|
||||
created_at TIMESTAMPTZ DEFAULT NOW(),
|
||||
updated_at TIMESTAMPTZ DEFAULT NOW()
|
||||
);
|
||||
|
||||
CREATE TABLE sessions (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
token_hash TEXT NOT NULL,
|
||||
created_at TIMESTAMPTZ DEFAULT NOW(),
|
||||
expires_at TIMESTAMPTZ NOT NULL
|
||||
);
|
||||
|
||||
CREATE INDEX idx_sessions_user_id ON sessions(user_id);
|
||||
CREATE INDEX idx_sessions_expires_at ON sessions(expires_at);
|
||||
7
src/db/pool.rs
Normal file
7
src/db/pool.rs
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
use sqlx::PgPool;
|
||||
use std::env;
|
||||
|
||||
pub async fn create_pool() -> Result<PgPool, sqlx::Error> {
|
||||
let url = env::var("DATABASE_URL").expect("DATABASE_URL must be set");
|
||||
PgPool::connect(&url).await
|
||||
}
|
||||
Loading…
Reference in a new issue