From f09238d21dffe81898422ec89a7d3801438dd19e Mon Sep 17 00:00:00 2001 From: Kristofers Solo Date: Sat, 25 Jan 2025 14:44:50 +0200 Subject: [PATCH] feat(db): create initial db --- .gitignore | 3 ++ justfile | 49 +++++++++++++++++++------ migrations/20250125123853_init.down.sql | 11 ++++++ migrations/20250125123853_init.up.sql | 25 +++++++++++++ 4 files changed, 76 insertions(+), 12 deletions(-) create mode 100644 migrations/20250125123853_init.down.sql create mode 100644 migrations/20250125123853_init.up.sql diff --git a/.gitignore b/.gitignore index b15bd75..c8b9aff 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,6 @@ test-results/ end2end/playwright-report/ playwright/.cache/ bruno/ + +.env +**/*.db diff --git a/justfile b/justfile index 501da5c..f5b6dad 100644 --- a/justfile +++ b/justfile @@ -1,10 +1,14 @@ +set dotenv-load + export RUSTC_WRAPPER:="sccache" -# Li st all available commands + +# List all available commands default: @just --list # Install required tools and dependencies setup: + just db-setup rustup toolchain install nightly rustup default nightly rustup target add wasm32-unknown-unknown @@ -15,8 +19,8 @@ setup: # Development Commands # Start development server with hot reload -dev: kill-server - RUST_BACKTRACE=full cargo leptos watch | bunyan +dev: kill-server db-setup db-migrate + cargo leptos watch | bunyan # Run cargo check on both native and wasm targets check: @@ -61,16 +65,9 @@ build-wasm: build-server: cargo leptos build-only-server -# Database Commands (add these when you set up your database) -db-setup: - echo "Add your database setup commands here" - -db-migrate: - echo "Add your database migration commands here" - -# Deployment Commands (customize based on your deployment strategy) +# Deployment Commands deploy: - echo "Add your deployment commands here" + echo "Add deployment commands here" # Combined commands check-all: fmt lint check test @@ -84,3 +81,31 @@ kill-server: pkill -f "target/debug/server" || true pkill -f "cargo-leptos" || true + +# Database Commands + +# Setup the database +db-setup: + sqlite3 ${DATABASE_URL#sqlite:} ".databases" + +alias migrate:=db-migrate +alias m:=db-migrate +# Migrate +db-migrate: + sqlx migrate run + +# Generate sqlx prepare check files +db-prepare: + sqlx prepare + +# Reset database +db-reset: + rm -f ${DATABASE_URL#sqlite:} + just db-setup + just db-migrate + +alias migrations:=db-new-migration +# Create new migration +db-new-migration name: + sqlx migrate add -r {{name}} + diff --git a/migrations/20250125123853_init.down.sql b/migrations/20250125123853_init.down.sql new file mode 100644 index 0000000..679391c --- /dev/null +++ b/migrations/20250125123853_init.down.sql @@ -0,0 +1,11 @@ +-- Add down migration script here +-- Drop indexes first +DROP INDEX IF EXISTS idx_scores_user_score; + +DROP INDEX IF EXISTS idx_users_login; + +-- Drop tables in reverse order of creation +DROP TABLE IF EXISTS scores; + +DROP TABLE IF EXISTS users; + diff --git a/migrations/20250125123853_init.up.sql b/migrations/20250125123853_init.up.sql new file mode 100644 index 0000000..5cd9541 --- /dev/null +++ b/migrations/20250125123853_init.up.sql @@ -0,0 +1,25 @@ +-- Add up migration script here +-- Users table with login codes +CREATE TABLE IF NOT EXISTS users ( + id integer PRIMARY KEY AUTOINCREMENT, + username text NOT NULL UNIQUE, + code text NOT NULL UNIQUE, + created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP +); + +-- Scores table with detailed game stats +CREATE TABLE IF NOT EXISTS scores ( + id integer PRIMARY KEY AUTOINCREMENT, + user_id integer NOT NULL, + score integer NOT NULL, + floor_reached integer NOT NULL, + play_time_seconds integer NOT NULL, + created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (user_id) REFERENCES users (id) +); + +-- Indexes for performance +CREATE INDEX idx_users_login ON users (code); + +CREATE INDEX idx_scores_user_score ON scores (user_id, score DESC); +