# CloudVault 2.0 – Database Schema Specification

This document details the complete 10-table relational schema powering the CloudVault 2.0 Centralized Backend.

The architecture supports both **PostgreSQL** in production and **SQLite** in local development, with zero data loss or migration discrepancies.

---

## 📊 Entity Relationship Diagram

```
+--------------------+        +-------------------------+
|       users        |        |   connected_accounts    |
+--------------------+        +-------------------------+
| id (PK)            | 1    * | id (PK)                 |
| name               |--------| user_id (FK -> users)   |
| email (Unique)     |        | provider                |
| password_hash      |        | provider_account_id     |
| account_status     |        | provider_email          |
| created_at         |        | encrypted_refresh_token |
| updated_at         |        | connection_status       |
+--------------------+        | created_at              |
          | 1                 | last_synced_at          |
          |                   +-------------------------+
          |                                | 1
          | 1                              |
          |--------+                       | 1
          |        |                       |
          | *      | *                     | *
+--------------------+        +-------------------------+
|      devices       |        |      backup_files       |
+--------------------+        +-------------------------+
| id (PK)            |        | id (PK)                 |
| user_id (FK)       |        | user_id (FK -> users)   |
| device_name        |        | connected_account_id(FK)|
| device_type        |        | google_drive_file_id    |
| last_active_at     |        | google_drive_folder_id  |
| device_status      |        | filename                |
+--------------------+        | mime_type               |
          | 1                 | file_size               |
          |                   | checksum                |
          | *                 | backup_status           |
+--------------------+        | uploaded_at             |
|   user_sessions    |        +-------------------------+
+--------------------+                     | 1
| id (PK)            |                     |
| user_id (FK)       |                     | *
| device_id (FK)     |        +-------------------------+
| session_status     |        |     backup_history      |
| expires_at         |        +-------------------------+
| session_token      |        | id (PK)                 |
| refresh_token      |        | user_id (FK)            |
+--------------------+        | file_id (FK -> backup)  |
                              | action                  |
                              | status                  |
                              | source_device           |
                              | created_at              |
                              +-------------------------+
```

---

## 🗄️ Detailed Table Specifications

### 1. `users`
Authoritative identity registry for all CloudVault users.

| Column | Type | Constraints | Description |
|---|---|---|---|
| `id` | VARCHAR(64) | PRIMARY KEY | Deterministic UUIDv5 based on email |
| `name` | VARCHAR(128) | NOT NULL | User's full display name |
| `email` | VARCHAR(128) | NOT NULL, UNIQUE, INDEX | User's unique email address (lowercase) |
| `password_hash` | VARCHAR(256) | NOT NULL | SHA-256 password hash |
| `account_status` | VARCHAR(32) | NOT NULL, DEFAULT 'active' | Status: `active`, `suspended`, `pending` |
| `salt` | VARCHAR(128) | NULLABLE | Deterministic per-user cryptographic salt |
| `created_at` | TIMESTAMP | DEFAULT CURRENT_TIMESTAMP | Account creation timestamp |
| `updated_at` | TIMESTAMP | DEFAULT CURRENT_TIMESTAMP | Last profile modification timestamp |

---

### 2. `connected_accounts`
Google Drive accounts linked for cloud backup storage.

| Column | Type | Constraints | Description |
|---|---|---|---|
| `id` | VARCHAR(64) | PRIMARY KEY | Unique account identifier |
| `user_id` | VARCHAR(64) | NOT NULL, FK -> `users.id` (CASCADE) | Owner user ID |
| `provider` | VARCHAR(64) | NOT NULL, DEFAULT 'google' | Storage provider type |
| `provider_account_id` | VARCHAR(128) | NULLABLE | Provider's unique account ID |
| `provider_email` | VARCHAR(128) | NOT NULL, INDEX | Google Account email address |
| `encrypted_refresh_token` | TEXT | NULLABLE | Encrypted OAuth refresh token |
| `connection_status` | VARCHAR(32) | NOT NULL, DEFAULT 'active' | Status: `active`, `unlinked`, `expired` |
| `created_at` | TIMESTAMP | DEFAULT CURRENT_TIMESTAMP | Account connection timestamp |
| `last_synced_at` | TIMESTAMP | DEFAULT CURRENT_TIMESTAMP | Last remote Drive synchronization |
| `display_name` | VARCHAR(128) | NULLABLE | Custom user label for the account |
| `photo_url` | VARCHAR(512) | NULLABLE | Avatar URL from Google profile |
| `is_active_backup` | BOOLEAN | DEFAULT TRUE | Whether this account is the active backup target |

---

### 3. `devices`
Physical Android devices, tablets, and web browsers connected to the user's account.

| Column | Type | Constraints | Description |
|---|---|---|---|
| `id` | VARCHAR(64) | PRIMARY KEY | Hardware UUID or web client ID |
| `user_id` | VARCHAR(64) | NOT NULL, FK -> `users.id` (CASCADE) | Owner user ID |
| `device_name` | VARCHAR(128) | NOT NULL | e.g. "Xiaomi Redmi Note 13", "Chrome Laptop" |
| `device_type` | VARCHAR(64) | NOT NULL, DEFAULT 'android' | Type: `android`, `web`, `desktop`, `ios` |
| `last_active_at` | TIMESTAMP | DEFAULT CURRENT_TIMESTAMP | Timestamp of most recent activity |
| `device_status` | VARCHAR(32) | NOT NULL, DEFAULT 'active' | Status: `active`, `revoked` |

---

### 4. `backup_files`
Authoritative metadata catalog of backed-up files stored in Google Drive.

| Column | Type | Constraints | Description |
|---|---|---|---|
| `id` | VARCHAR(64) | PRIMARY KEY | Unique backup file identifier |
| `user_id` | VARCHAR(64) | NOT NULL, FK -> `users.id` (CASCADE) | Owner user ID |
| `connected_account_id`| VARCHAR(64) | NULLABLE, FK -> `connected_accounts.id` | Target Google Drive account |
| `google_drive_file_id`| VARCHAR(128) | NULLABLE, INDEX | Authoritative Google Drive File ID |
| `google_drive_folder_id`| VARCHAR(128)| NULLABLE | Drive Folder ID (`Photos`, `Videos`, `Documents`) |
| `filename` | VARCHAR(256) | NOT NULL, INDEX | File name with extension |
| `mime_type` | VARCHAR(128) | NOT NULL | MIME content type |
| `file_size` | BIGINT | NOT NULL, DEFAULT 0 | File size in bytes |
| `checksum` | VARCHAR(128) | NULLABLE, INDEX | SHA-256 deduplication hash |
| `backup_status` | VARCHAR(32) | NOT NULL, DEFAULT 'completed' | Status: `completed`, `pending`, `failed` |
| `uploaded_at` | TIMESTAMP | DEFAULT CURRENT_TIMESTAMP | Date and time uploaded to Google Drive |
| `local_path` | VARCHAR(512) | NULLABLE | Source path on device (empty for remote items) |
| `file_type` | VARCHAR(32) | DEFAULT 'photo' | Category: `photo`, `video`, `document`, `other` |
| `cloud_web_url` | VARCHAR(512) | NULLABLE | Google Drive web preview URL |

---

### 5. `backup_history`
Audit log of all backup, upload, download, and synchronization activities.

| Column | Type | Constraints | Description |
|---|---|---|---|
| `id` | VARCHAR(64) | PRIMARY KEY | Unique history event ID |
| `user_id` | VARCHAR(64) | NOT NULL, FK -> `users.id` (CASCADE) | Owner user ID |
| `file_id` | VARCHAR(64) | NULLABLE, FK -> `backup_files.id` | Target file ID |
| `action` | VARCHAR(64) | NOT NULL, DEFAULT 'upload' | Action: `upload`, `download`, `delete`, `sync` |
| `status` | VARCHAR(32) | NOT NULL, DEFAULT 'success' | Status: `success`, `failed`, `in_progress` |
| `source_device` | VARCHAR(128) | NOT NULL | Device name where operation originated |
| `created_at` | TIMESTAMP | DEFAULT CURRENT_TIMESTAMP | Timestamp of event occurrence |

---

### 6. `storage_usage`
Live storage quota monitoring per connected account.

| Column | Type | Constraints | Description |
|---|---|---|---|
| `id` | VARCHAR(64) | PRIMARY KEY | Unique record ID |
| `connected_account_id`| VARCHAR(64) | NOT NULL, UNIQUE, FK -> `connected_accounts.id` | Target account |
| `actual_quota` | BIGINT | NOT NULL, DEFAULT 16106127360 | Google Drive total quota (15 GB standard) |
| `total_usage` | BIGINT | NOT NULL, DEFAULT 2147483648 | Total storage used on Google Drive |
| `available_storage` | BIGINT | NOT NULL, DEFAULT 13958643712 | Remaining storage available |
| `cloudvault_usage` | BIGINT | NOT NULL, DEFAULT 0 | Storage used specifically by CloudVault backups |
| `last_updated` | TIMESTAMP | DEFAULT CURRENT_TIMESTAMP | Last quota audit timestamp |

---

### 7. `storage_targets`
User-defined storage limits and reserve buffers.

| Column | Type | Constraints | Description |
|---|---|---|---|
| `id` | VARCHAR(64) | PRIMARY KEY | Unique record ID |
| `connected_account_id`| VARCHAR(64) | NOT NULL, UNIQUE, FK -> `connected_accounts.id` | Target account |
| `target_storage` | BIGINT | NOT NULL, DEFAULT 10737418240 | User target limit (e.g. 10 GB) |
| `reserved_storage` | BIGINT | NOT NULL, DEFAULT 2147483648 | Safety buffer to avoid 100% full lock (2 GB) |
| `updated_at` | TIMESTAMP | DEFAULT CURRENT_TIMESTAMP | Target configuration timestamp |

---

### 8. `upload_queue`
Orchestration queue for pending and retrying uploads.

| Column | Type | Constraints | Description |
|---|---|---|---|
| `id` | VARCHAR(64) | PRIMARY KEY | Unique queue task ID |
| `user_id` | VARCHAR(64) | NOT NULL, FK -> `users.id` (CASCADE) | Owner user ID |
| `connected_account_id`| VARCHAR(64) | NULLABLE, FK -> `connected_accounts.id` | Assigned account |
| `file_metadata` | TEXT | NOT NULL | JSON metadata (filename, size, target path) |
| `upload_status` | VARCHAR(32) | NOT NULL, DEFAULT 'queued' | Status: `queued`, `uploading`, `completed`, `failed` |
| `retry_count` | INTEGER | DEFAULT 0 | Number of retry attempts |
| `created_at` | TIMESTAMP | DEFAULT CURRENT_TIMESTAMP | Enqueue timestamp |

---

### 9. `user_settings`
User preferences for automation and network safety.

| Column | Type | Constraints | Description |
|---|---|---|---|
| `id` | VARCHAR(64) | PRIMARY KEY | Unique settings ID |
| `user_id` | VARCHAR(64) | NOT NULL, UNIQUE, FK -> `users.id` (CASCADE) | Owner user ID |
| `auto_backup` | BOOLEAN | NOT NULL, DEFAULT TRUE | Automated background backups enabled |
| `wifi_only` | BOOLEAN | NOT NULL, DEFAULT TRUE | Only upload over Wi-Fi networks |
| `photo_backup` | BOOLEAN | NOT NULL, DEFAULT TRUE | Automatically back up photos |
| `video_backup` | BOOLEAN | NOT NULL, DEFAULT TRUE | Automatically back up videos |
| `document_backup` | BOOLEAN | NOT NULL, DEFAULT TRUE | Automatically back up documents |
| `backup_quality` | VARCHAR(32) | DEFAULT 'Original' | Quality: `Original`, `High Quality` |
| `storage_threshold_percent`| INTEGER | DEFAULT 90 | Fullness alert percentage |
| `notifications_enabled`| BOOLEAN | DEFAULT TRUE | In-app and system alerts |
| `schedule_interval_hours`| INTEGER | DEFAULT 4 | Periodic sync interval |
| `updated_at` | TIMESTAMP | DEFAULT CURRENT_TIMESTAMP | Last settings update |

---

### 10. `user_sessions`
Active JWT session tracking and device authorization.

| Column | Type | Constraints | Description |
|---|---|---|---|
| `id` | VARCHAR(64) | PRIMARY KEY | Unique session ID |
| `user_id` | VARCHAR(64) | NOT NULL, FK -> `users.id` (CASCADE) | Owner user ID |
| `device_id` | VARCHAR(64) | NULLABLE, FK -> `devices.id` (CASCADE) | Bound hardware device |
| `session_status` | VARCHAR(32) | NOT NULL, DEFAULT 'active' | Status: `active`, `expired`, `revoked` |
| `expires_at` | TIMESTAMP | NOT NULL | Session expiry timestamp |
| `session_token` | TEXT | NULLABLE | JWT access token |
| `refresh_token` | TEXT | NULLABLE | Refresh token for renewal |
| `created_at` | TIMESTAMP | DEFAULT CURRENT_TIMESTAMP | Session initialization timestamp |
