๐Ÿ—„๏ธ Azure SQL Database Setup Guide

This folder contains all SQL scripts required to set up the Azure SQL Database for the Resource Management Dashboard.


๐Ÿ“‹ Files in This Folder

File Purpose
01_create_tables.sql Creates all database tables
02_insert_default_data.sql Inserts default theme configuration
03_create_admin_user.sql Creates default admin user
04_sample_resources.sql Inserts sample resources (optional)
backup_and_restore.sql Backup and restore commands

๐Ÿš€ Quick Setup (Step-by-Step)

Step 1: Create Azure SQL Database

  1. Login to Azure Portal: https://portal.azure.com
  2. Search for "SQL databases"
  3. Click "+ Create"
  4. Fill in details:
    • Resource Group: Create new or select existing
    • Database Name: resource-dashboard-db (CHANGE ACCORDINGLY)
    • Server: Create new
      • Server name: your-unique-server-name (CHANGE ACCORDINGLY)
      • Location: Southeast Asia (or your preferred region)
      • Authentication: SQL authentication
      • Admin login: dbadmin (CHANGE ACCORDINGLY)
      • Password: Strong password (CHANGE ACCORDINGLY and SAVE IT!)
    • Compute + Storage: Basic or Standard tier
  5. Networking:
    • โœ… Allow Azure services
    • โœ… Add current client IP
  6. Click "Review + Create" > "Create"

โฑ Wait: 5-10 minutes for deployment


Step 2: Get Connection String

  1. Go to your database resource
  2. Click "Connection strings" (left menu)
  3. Copy the ADO.NET connection string
  4. Note it down - you'll need it for .env configuration

Format for PyMSSQL (Backend .env):

mssql+pymssql://USERNAME:PASSWORD@SERVER_NAME.database.windows.net:1433/DATABASE_NAME

Example (CHANGE ACCORDINGLY):

mssql+pymssql://dbadmin:MySecure@Pass123@resource-server.database.windows.net:1433/resource-dashboard-db

Step 3: Run SQL Scripts

Option A: Using Azure Portal Query Editor (Easiest)

  1. Go to your database in Azure Portal
  2. Click "Query editor" (left menu)
  3. Login with:
    • Authentication type: SQL authentication
    • Login: dbadmin (or your admin username)
    • Password: Your database password
  4. PLEASE NOTE THAT, IN YOUR SQL SERVER FIREWALL, CLIENT IP AND BACKEND IP SHOULD BE ALLOWED, FOR PRACTISE PURPOSE ONLY
  5. Run scripts in order:
    • Copy content of 01_create_tables.sql โ†’ Paste โ†’ Click "Run"
    • Copy content of 02_insert_default_data.sql โ†’ Paste โ†’ Click "Run"
    • Copy content of 03_create_admin_user.sql โ†’ Paste โ†’ Click "Run"
    • (Optional) Copy content of 04_sample_resources.sql โ†’ Paste โ†’ Click "Run"

โœ… Done! Database is ready.

Option B: Using SQL Server Management Studio (SSMS)

  1. Download SSMS: https://aka.ms/ssmsfullsetup
  2. Connect to Azure SQL:
    • Server name: your-server.database.windows.net
    • Authentication: SQL Server Authentication
    • Login: Your admin username
    • Password: Your admin password
  3. Open each .sql file and execute in order

Option C: Using Python Script (From Backend)

bash
# Make sure backend .env is configured with DATABASE_URL
cd backend
source venv/bin/activate  # or venv\Scripts\activate on Windows

# Run migration
python -c "from app.db.database import engine; from app.models.user import Base; Base.metadata.create_all(engine)"

Step 4: Verify Tables

Run this query in Query Editor:

sql
-- Check all tables
SELECT TABLE_NAME 
FROM INFORMATION_SCHEMA.TABLES 
WHERE TABLE_TYPE = 'BASE TABLE';

Expected Output:


๐Ÿ” Default Admin User

After running 03_create_admin_user.sql, you'll have:

โš ๏ธ IMPORTANT: Change these credentials in production!


๐Ÿ”„ Backup and Restore

Backup Database

Azure Portal:

  1. Go to SQL database
  2. Click "Export"
  3. Choose storage account
  4. Enter admin credentials
  5. Click "OK"

Automated Backups:
Azure SQL automatically backs up databases. You can restore to any point in last 7-35 days.

Restore Database

  1. Go to SQL database
  2. Click "Restore"
  3. Select restore point
  4. Enter new database name
  5. Click "OK"

๐Ÿ” Troubleshooting

Cannot connect to database

Check:

Add IP to Firewall:

  1. SQL Database > Networking
  2. Add your current IP address
  3. Save

Query timeout

Solution:

Permission denied

Check:


๐Ÿ“Š Database Schema

Users Table

Resources Table

Theme Config Table


๐Ÿ”— Connection String Examples

Development (Local):

env
DATABASE_URL=sqlite:///./resource_dashboard.db

Production (Azure SQL):

env
# CHANGE ACCORDINGLY with your actual credentials
DATABASE_URL=mssql+pymssql://dbadmin:YourPassword@your-server.database.windows.net:1433/resource-dashboard-db

๐Ÿ’ก Architecture Benefits

โœ… All About Our Complete Project

  1. Frontend - React + Vite (Static build ready)
  2. Backend - Python FastAPI with JWT authentication
  3. Database - Azure SQL Database

๐Ÿ“ Project Structure

project/
โ”œโ”€โ”€ backend/                    # Python FastAPI Backend
โ”‚   โ”œโ”€โ”€ app/
โ”‚   โ”‚   โ”œโ”€โ”€ api/               # API endpoints (auth, users, theme)
โ”‚   โ”‚   โ”œโ”€โ”€ core/              # Config & security
โ”‚   โ”‚   โ”œโ”€โ”€ db/                # Database setup
โ”‚   โ”‚   โ”œโ”€โ”€ models/            # SQLAlchemy models
โ”‚   โ”‚   โ”œโ”€โ”€ schemas/           # Pydantic schemas
โ”‚   โ”‚   โ””โ”€โ”€ main.py           # FastAPI application
โ”‚   โ”œโ”€โ”€ requirements.txt       # Python dependencies
โ”‚   โ”œโ”€โ”€ azure_sql_schema.sql  # Database schema
โ”‚   โ””โ”€โ”€ README.md             # Backend documentation
โ”‚
โ”œโ”€โ”€ client/                    # React Frontend
โ”‚   โ”œโ”€โ”€ src/
โ”‚   โ”‚   โ”œโ”€โ”€ components/       # UI components
โ”‚   โ”‚   โ”œโ”€โ”€ pages/           # Page components
โ”‚   โ”‚   โ”œโ”€โ”€ contexts/        # Auth context (updated for Python backend)
โ”‚   โ”‚   โ”œโ”€โ”€ config/          # API configuration
โ”‚   โ”‚   โ””โ”€โ”€ lib/             # API client (updated)
โ”‚   โ””โ”€โ”€ .env.example         # Frontend env vars
โ”‚
โ””โ”€โ”€ DEPLOYMENT_GUIDE.md      # Complete deployment instructions
๐Ÿ”ง What Included

Backend

  • โœ… Python FastAPI framework instead of Node.js
  • โœ… Azure SQL Database instead of Supabase Postgres
  • โœ… JWT Authentication with python-jose
  • โœ… SQLAlchemy ORM for database operations
  • โœ… Pydantic for request/response validation

Frontend

  • โœ… Auth Context to use Python backend API
  • โœ… API Client to call FastAPI endpoints
  • โœ… API Configuration for backend URL

Database

  • โœ… Azure SQL Schema created
  • โœ… Users table with email, password, roles
  • โœ… Theme config table for customization
  • โœ… Default theme values inserted
๐Ÿš€ OUR MAIN IMPORTANT STEPS

1. Setup Azure SQL Database

bash
# Azure Portal mein:
1. Create SQL Database
2. Note: server name, database name, username, password
3. Run: backend/azure_sql_schema.sql
4. Configure firewall rules for VM IPs

2. Deploy Backend (VM 2)

bash
# Backend VM mein:
cd backend
pip install -r requirements.txt

# .env file configure karo:
AZURE_SQL_SERVER=your-server.database.windows.net
AZURE_SQL_DATABASE=your-database
AZURE_SQL_USERNAME=your-username
AZURE_SQL_PASSWORD=your-password
SECRET_KEY=your-secret-key

# Run backend:
python run.py
# Production: uvicorn app.main:app --host 0.0.0.0 --port 8000

3. Deploy Frontend (VM 1)

bash
# Frontend VM mein:
cd client

# Update API URL in .env:
VITE_API_URL=http://backend-vm-ip:8000

# Build frontend:
npm install
npm run build

# Deploy to nginx:
sudo cp -r dist/public/* /var/www/html/
๐Ÿ” Security Notes

1. Environment Variables

  • Backend: Use .env file (see backend/.env.example)
  • Frontend: Use client/.env (see client/.env.example)

2. Production Checklist

  • Change SECRET_KEY to random secure value
  • Enable HTTPS with SSL certificates
  • Configure Azure SQL firewall rules
  • Set up Azure Key Vault for secrets
  • Enable CORS only for your frontend domain
  • Use strong passwords for database
๐Ÿ“‹ Features Implemented

Authentication

  • โœ… User signup with email/password
  • โœ… User login with JWT tokens
  • โœ… Role-based access control (admin/user)
  • โœ… Protected routes

User Management

  • โœ… Get current user profile
  • โœ… Update user profile (name, bio, avatar)
  • โœ… Admin: View all users
  • โœ… Admin: View user by ID

Theme Configuration

  • โœ… Get theme configuration
  • โœ… Admin: Update theme colors
  • โœ… Default theme values
๐Ÿงช Testing

Test Backend API

bash
# Health check
curl http://localhost:8000/health

# Get theme config (public)
curl http://localhost:8000/api/theme

# Signup
curl -X POST http://localhost:8000/api/auth/signup \
  -H "Content-Type: application/json" \
  -d '{"email":"test@example.com","password":"password123"}'

# Login
curl -X POST http://localhost:8000/api/auth/login \
  -d "email=test@example.com&password=password123"

Test Frontend

bash
# Local development:
npm run dev
# Open: http://localhost:5000

# Production build:
npm run build
# Serve dist/public/ with nginx

Resource Management Dashboard โ€ข Azure SQL Database Setup Guide
Last Updated: November 26, 2025 โ€ข Built with โค๏ธ by Ritesh Sharma