WellNuo/backend/migrations/000_initial_schema.sql
Sergei ec63a2c1e2 Add admin panel, optimized API, OTP auth, migrations
Admin Panel (Next.js):
- Dashboard with stats
- Users list with relationships (watches/watched_by)
- User detail pages
- Deployments list and detail pages
- Devices, Orders, Subscriptions pages
- OTP-based admin authentication

Backend Optimizations:
- Fixed N+1 query problem in admin APIs
- Added pagination support
- Added .range() and count support to Supabase wrapper
- Optimized batch queries with lookup maps

Database:
- Added migrations for schema evolution
- New tables: push_tokens, notification_settings
- Updated access model

iOS Build Scripts:
- build-ios.sh, clear-apple-cache.sh
- EAS configuration updates

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-20 11:05:39 -08:00

152 lines
6.0 KiB
SQL

-- ============================================================
-- WellNuo Database Initial Schema
-- Database: PostgreSQL on eluxnetworks.net:5432/wellnuo_app
-- Date: 2025-12-19
-- Description: Snapshot of existing database structure
-- ============================================================
-- This file documents the existing schema. DO NOT RUN - it's for reference only.
-- The tables already exist in the database.
-- ============================================================
-- TABLE: person_details (Users & Authentication)
-- ============================================================
-- CREATE TABLE person_details (
-- user_id INTEGER PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
-- role_ids TEXT, -- "1,2" = roles (1=beneficiary, 2=caretaker, 3=admin)
-- access_to_deployments TEXT, -- "21,38,29" = comma-separated deployment IDs
-- email TEXT,
-- user_name TEXT, -- login username
-- first_name TEXT,
-- last_name TEXT,
-- address_street TEXT,
-- address_city TEXT,
-- address_zip TEXT,
-- address_state TEXT,
-- address_country TEXT,
-- time_edit REAL, -- unix timestamp of last edit
-- user_edit INTEGER, -- user who made last edit
-- phone_number TEXT,
-- picture TEXT, -- URL to avatar
-- key TEXT -- PASSWORD IN PLAIN TEXT! (needs migration)
-- );
-- ============================================================
-- TABLE: deployments (Monitored Locations)
-- ============================================================
-- CREATE TABLE deployments (
-- deployment_id INTEGER PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
-- time_edit REAL,
-- user_edit INTEGER,
-- time_zone_s TEXT, -- "America/Los_Angeles"
-- persons INTEGER, -- number of people
-- gender INTEGER, -- 0-2
-- race INTEGER,
-- born INTEGER, -- birth year
-- pets INTEGER,
-- context TEXT, -- notes about beneficiary
-- alarm_details TEXT -- JSON with alarm configuration
-- );
-- ============================================================
-- TABLE: deployment_details (Extended Deployment Info)
-- ============================================================
-- CREATE TABLE deployment_details (
-- deployment_id INTEGER PRIMARY KEY,
-- beneficiary_id INTEGER, -- FK to person_details
-- caretaker_id INTEGER, -- FK to person_details
-- owner_id INTEGER,
-- installer_id INTEGER,
-- devices TEXT, -- JSON array of device MACs
-- address_street TEXT,
-- address_city TEXT,
-- address_zip TEXT,
-- address_state TEXT,
-- address_country TEXT,
-- wifis TEXT,
-- lat REAL,
-- lng REAL,
-- gps_age INTEGER,
-- note TEXT,
-- floor_plan TEXT, -- JSON layout
-- overlapps TEXT
-- );
-- ============================================================
-- TABLE: devices (IoT Sensors)
-- ============================================================
-- CREATE TABLE devices (
-- device_id INTEGER PRIMARY KEY,
-- device_mac TEXT NOT NULL,
-- well_id INTEGER, -- FK to deployment
-- description TEXT,
-- location INTEGER, -- room type
-- close_to TEXT,
-- radar_threshold TEXT,
-- fw_version TEXT,
-- hw_version TEXT,
-- ble_scan_period INTEGER,
-- ble_scan_duration INTEGER,
-- temperature_calib TEXT,
-- humidity_calib TEXT,
-- reporting_period_s INTEGER,
-- reboot_time REAL,
-- led_schema TEXT,
-- alert_details TEXT,
-- other TEXT,
-- group_id INTEGER,
-- UNIQUE(well_id, device_mac)
-- );
-- ============================================================
-- TABLE: disclaimers (User Agreements)
-- ============================================================
-- CREATE TABLE disclaimers (
-- id INTEGER PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
-- first_name TEXT,
-- last_name TEXT,
-- user_name TEXT,
-- email TEXT,
-- devices TEXT,
-- date TIMESTAMPTZ,
-- policy_version TEXT
-- );
-- ============================================================
-- TABLE: deployment_history (Audit Log)
-- ============================================================
-- Already exists - tracks changes to deployments
-- ============================================================
-- TABLE: _migrations (Migration Tracking)
-- ============================================================
-- Created by this setup
-- CREATE TABLE _migrations (
-- id SERIAL PRIMARY KEY,
-- name VARCHAR(255) NOT NULL UNIQUE,
-- applied_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
-- applied_by VARCHAR(100) DEFAULT 'claude',
-- description TEXT
-- );
-- ============================================================
-- STATISTICS (as of 2025-12-19)
-- ============================================================
-- person_details: 8 rows
-- deployments: 45 rows
-- deployment_details: 23 rows (subset of deployments)
-- devices: 455 rows
-- disclaimers: ? rows
-- ============================================================
-- KNOWN ISSUES
-- ============================================================
-- 1. person_details.key stores passwords in PLAIN TEXT
-- → Need migration 002_add_password_hash.sql
--
-- 2. access_to_deployments is TEXT with comma-separated IDs
-- → Should be junction table, but works for now
--
-- 3. No push_tokens table yet
-- → Need migration 003_create_push_tokens.sql