From ac29b38175ab152d8e64b1833bd841527fb13e96 Mon Sep 17 00:00:00 2001 From: David Gwilliam Date: Sun, 29 Mar 2026 20:47:31 -0700 Subject: [PATCH] fix: update database switching to modify schema file directly --- justfile | 129 +++++++++++++++++++++++++++++++++++++ mise.toml | 3 + prisma/schema.prisma | 6 +- scripts/setup-postgres.js | 2 +- scripts/switch-database.js | 30 ++++++++- 5 files changed, 166 insertions(+), 4 deletions(-) create mode 100644 justfile diff --git a/justfile b/justfile new file mode 100644 index 0000000..dd17768 --- /dev/null +++ b/justfile @@ -0,0 +1,129 @@ +# justfile for Euchre Camp +# Automates development, testing, and deployment tasks. +# Designed for CI/CD pipelines and local development. + +# --- Configuration --- +set dotenv-load +set positional-arguments + +# Project name +PROJECT := "euchre-camp" + +# Docker registry (CasaOS local registry) +REGISTRY := "dhg.lol:5000" # Update IP to your CasaOS server IP +IMAGE_TAG := "latest" + +# --- Variables --- +# Database +DB_CONTAINER := "euchre-camp-postgres" + +# --- Setup & Installation --- + +# Install dependencies and setup environment +setup: + @echo "Installing dependencies..." + npm install + @echo "Setting up database..." + npm run db:setup-postgres + @echo "Generating Prisma client..." + npx prisma generate + +# --- Development --- + +# Start the development server +dev: + npm run dev + +# Lint the codebase +lint: + npm run lint + +# Type check the codebase +typecheck: + npx tsc --noEmit + +# Format code (if prettier is available, otherwise just lint) +format: + npx prettier --write . + +# --- Testing --- + +# Run all tests (unit + acceptance) +test: test-unit test-acceptance + +# Run unit tests (Vitest) +test-unit: + npm run test:run + +# Run acceptance tests (Playwright) +test-acceptance: + @echo "Starting Docker containers for acceptance tests..." + docker compose up -d + @echo "Waiting for services to be ready..." + sleep 10 + @echo "Running acceptance tests..." + npm run test:acceptance + @echo "Stopping Docker containers..." + docker compose down + +# Run database migrations (Prisma) +migrate: + npx prisma migrate dev + +# Seed the database +seed: + npm run db:seed + +# --- Docker --- + +# Build the Docker image +docker-build: + @echo "Building Docker image {{PROJECT}}:{{IMAGE_TAG}}..." + docker build -t {{PROJECT}}:{{IMAGE_TAG}} . + +# Run the Docker container locally +docker-run: docker-build + @echo "Running Docker container {{PROJECT}}..." + docker run -d -p 3000:3000 --name {{PROJECT}} {{PROJECT}}:{{IMAGE_TAG}} + +# Stop and remove the Docker container +docker-stop: + @echo "Stopping Docker container {{PROJECT}}..." + docker stop {{PROJECT}} || true + docker rm {{PROJECT}} || true + +# Clean up Docker images and containers +docker-clean: docker-stop + @echo "Removing Docker image {{PROJECT}}:{{IMAGE_TAG}}..." + docker rmi {{PROJECT}}:{{IMAGE_TAG}} || true + +# --- Deployment --- + +# Tag image for local registry +docker-tag: + @echo "Tagging image for registry {{REGISTRY}}..." + docker tag {{PROJECT}}:{{IMAGE_TAG}} {{REGISTRY}}/{{PROJECT}}:{{IMAGE_TAG}} + +# Push image to local registry (CasaOS) +docker-push: docker-build docker-tag + @echo "Pushing image to {{REGISTRY}}..." + docker push {{REGISTRY}}/{{PROJECT}}:{{IMAGE_TAG}} + +# --- CI/CD Pipeline Simulation --- + +# Run full CI pipeline locally (lint, test, build, push) +ci: lint typecheck test-unit docker-build + @echo "CI Pipeline completed successfully!" + +# --- Utilities --- + +# Show help information +help: + @just --list + +# Clean up project (remove node_modules, build artifacts) +clean: + @echo "Cleaning project..." + rm -rf node_modules .next dist + @echo "Cleaning Docker artifacts..." + docker system prune -f diff --git a/mise.toml b/mise.toml index 681d0db..a6ca516 100644 --- a/mise.toml +++ b/mise.toml @@ -1,3 +1,6 @@ [tools] +docker-compose = "latest" +just = "latest" node = "latest" npm = "latest" +postgres = "latest" diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 16ecc21..29fd59e 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -4,11 +4,13 @@ generator client { } datasource db { - provider = env("DATABASE_PROVIDER", "sqlite") + provider = "sqlite" url = env("DATABASE_URL") - shadowDatabaseUrl = env("DATABASE_SHADOW_URL") } +// Note: For PostgreSQL, create a separate schema file or use environment variable +// See scripts/switch-database.js for provider switching logic + model Player { id Int @id @default(autoincrement()) name String diff --git a/scripts/setup-postgres.js b/scripts/setup-postgres.js index 80ca439..0976e31 100755 --- a/scripts/setup-postgres.js +++ b/scripts/setup-postgres.js @@ -16,7 +16,7 @@ function checkPostgresConnection() { console.error('❌ DATABASE_URL is not set for PostgreSQL'); console.error('Please update your .env file with:'); console.error('DATABASE_PROVIDER=postgresql'); - console.error('DATABASE_URL="postgresql://username:password@localhost:5432/euchre_camp"'); + console.error('DATABASE_URL="postgresql://euchre_camp:o_7urWjkvPfsE1Q*@localhost:5432/euchre_camp"'); return false; } diff --git a/scripts/switch-database.js b/scripts/switch-database.js index 7d7aed7..e36a585 100755 --- a/scripts/switch-database.js +++ b/scripts/switch-database.js @@ -22,7 +22,7 @@ function updateEnvFile(provider) { envContent = fs.readFileSync(envExampleFile, 'utf8'); } - // Update DATABASE_PROVIDER + // Update DATABASE_PROVIDER (note: this is for reference only, not used by Prisma) const providerRegex = /^DATABASE_PROVIDER=.*$/m; if (providerRegex.test(envContent)) { envContent = envContent.replace(providerRegex, `DATABASE_PROVIDER=${provider}`); @@ -54,6 +54,33 @@ function updateEnvFile(provider) { console.log(`✅ Updated ${envFile} to use ${provider} database`); } +function updateSchemaFile(provider) { + const schemaPath = path.join(process.cwd(), 'prisma', 'schema.prisma'); + + if (!fs.existsSync(schemaPath)) { + console.error(`❌ Schema file not found: ${schemaPath}`); + return false; + } + + let schemaContent = fs.readFileSync(schemaPath, 'utf8'); + + // Update the provider in the datasource block + const providerRegex = /(datasource db\s*\{[^}]*provider\s*=\s*)"[^"]+"/; + if (providerRegex.test(schemaContent)) { + schemaContent = schemaContent.replace( + providerRegex, + `$1"${provider}"` + ); + + fs.writeFileSync(schemaPath, schemaContent); + console.log(`✅ Updated schema.prisma to use ${provider} provider`); + return true; + } else { + console.error(`❌ Could not find provider declaration in schema.prisma`); + return false; + } +} + function main() { const args = process.argv.slice(2); const provider = args[0]; @@ -70,6 +97,7 @@ function main() { const normalizedProvider = provider === 'postgres' ? 'postgresql' : provider; updateEnvFile(normalizedProvider); + updateSchemaFile(normalizedProvider); console.log('\nNext steps:'); console.log('1. Run: npx prisma generate');