1#!/bin/bash
2
3# Setup script for SQLx CLI and database migrations
4# This script installs sqlx-cli and sets up the database for development
5
6set -e
7
8echo "🔧 Setting up SQLx CLI and database..."
9
10# Check if cargo is installed
11if ! command -v cargo &> /dev/null; then
12 echo "❌ Cargo is not installed. Please install Rust first."
13 exit 1
14fi
15
16# Install sqlx-cli if not already installed
17if ! command -v sqlx &> /dev/null; then
18 echo "📦 Installing sqlx-cli..."
19 cargo install sqlx-cli --features postgres
20else
21 echo "✅ sqlx-cli is already installed"
22fi
23
24# Check if DATABASE_URL is set
25if [ -z "$DATABASE_URL" ]; then
26 echo "⚠️ DATABASE_URL not set. Using default: postgres://localhost/teal"
27 export DATABASE_URL="postgres://localhost/teal"
28fi
29
30echo "🗄️ Database URL: $DATABASE_URL"
31
32# Navigate to services directory for sqlx commands
33cd services
34
35# Check if database exists, create if it doesn't
36echo "🏗️ Creating database if it doesn't exist..."
37sqlx database create 2>/dev/null || echo "Database already exists or created successfully"
38
39# Run migrations
40echo "🚀 Running database migrations..."
41sqlx migrate run
42
43# Prepare queries for compile-time verification
44echo "🔍 Preparing queries for compile-time verification..."
45sqlx prepare --check 2>/dev/null || {
46 echo "📝 Generating query metadata..."
47 sqlx prepare
48}
49
50cd ..
51
52echo "✅ SQLx setup complete!"
53echo ""
54echo "Available commands:"
55echo " pnpm db:migrate - Run migrations"
56echo " pnpm db:migrate:revert - Revert last migration"
57echo " pnpm db:create - Create database"
58echo " pnpm db:drop - Drop database"
59echo " pnpm db:reset - Drop, create, and migrate database"
60echo " pnpm db:prepare - Prepare queries for compile-time verification"
61echo ""
62echo "🎉 Your database is ready for development!"