bug fix - route probleme
Dieser Commit ist enthalten in:
124
v2_adminpanel/DATABASE_FIELD_INCONSISTENCIES.md
Normale Datei
124
v2_adminpanel/DATABASE_FIELD_INCONSISTENCIES.md
Normale Datei
@@ -0,0 +1,124 @@
|
||||
# Database Field Name Inconsistencies Report
|
||||
|
||||
## Overview
|
||||
This report documents all database field name inconsistencies found between the database schema (init.sql) and Python code usage in the v2_adminpanel application.
|
||||
|
||||
## 1. Sessions Table - Duplicate/Alias Fields
|
||||
|
||||
### Issue
|
||||
The sessions table contains multiple duplicate columns that serve as aliases, causing confusion and inconsistent usage:
|
||||
|
||||
```sql
|
||||
-- Current schema has these duplicate fields:
|
||||
is_active BOOLEAN DEFAULT TRUE,
|
||||
active BOOLEAN DEFAULT TRUE -- Alias for is_active
|
||||
|
||||
started_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
|
||||
login_time TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP, -- Alias for started_at
|
||||
|
||||
last_heartbeat TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
|
||||
last_activity TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP, -- Alias for last_heartbeat
|
||||
|
||||
ended_at TIMESTAMP WITH TIME ZONE,
|
||||
logout_time TIMESTAMP WITH TIME ZONE, -- Alias for ended_at
|
||||
```
|
||||
|
||||
### Code Usage Examples
|
||||
- `routes/session_routes.py`:
|
||||
- Line 32: `WHERE s.is_active = TRUE`
|
||||
- Line 88: `s.active`
|
||||
- Line 192: `WHERE active = true`
|
||||
- Line 201: `SET active = false`
|
||||
|
||||
## 2. Device ID vs Hardware ID Mismatch
|
||||
|
||||
### Issue
|
||||
The database schema uses `hardware_id` but the code references `device_id`:
|
||||
|
||||
```sql
|
||||
-- Database schema:
|
||||
sessions.hardware_id VARCHAR(100)
|
||||
device_registrations.hardware_id TEXT NOT NULL
|
||||
```
|
||||
|
||||
### Code Usage Examples
|
||||
- `routes/session_routes.py`:
|
||||
- Line 84: `s.device_id`
|
||||
- Line 134: `'device_id': row[3]`
|
||||
- Line 190: `SELECT license_key, username, device_id`
|
||||
- Line 326: `COUNT(DISTINCT s.device_id) as unique_devices`
|
||||
|
||||
## 3. Time Field Inconsistencies
|
||||
|
||||
### Issue
|
||||
Mixed usage of time field names:
|
||||
|
||||
### Code Usage Examples
|
||||
- `models.py` line 167: `ORDER BY s.start_time DESC` (but schema has `started_at`)
|
||||
- Session history queries mix `login_time` and `started_at`
|
||||
|
||||
## 4. Field Naming Patterns
|
||||
|
||||
### Consistent Patterns Found
|
||||
- ✅ All tables use `created_at` (not `created`)
|
||||
- ✅ Most tables use `is_active` pattern
|
||||
- ✅ Foreign keys use `_id` suffix consistently
|
||||
|
||||
### Inconsistent Patterns
|
||||
- ❌ Sessions table has both `is_active` and `active`
|
||||
- ❌ Time fields have multiple aliases
|
||||
|
||||
## Migration Scripts
|
||||
|
||||
### Step 1: Fix device_id references in code
|
||||
```sql
|
||||
-- Create view for backward compatibility
|
||||
CREATE OR REPLACE VIEW sessions_compat AS
|
||||
SELECT
|
||||
*,
|
||||
hardware_id as device_id -- Alias for compatibility
|
||||
FROM sessions;
|
||||
```
|
||||
|
||||
### Step 2: Remove duplicate columns (after code update)
|
||||
```sql
|
||||
-- Remove duplicate columns from sessions table
|
||||
ALTER TABLE sessions
|
||||
DROP COLUMN IF EXISTS active,
|
||||
DROP COLUMN IF EXISTS login_time,
|
||||
DROP COLUMN IF EXISTS last_activity,
|
||||
DROP COLUMN IF EXISTS logout_time;
|
||||
```
|
||||
|
||||
### Step 3: Update indexes if needed
|
||||
```sql
|
||||
-- Recreate any indexes that used the dropped columns
|
||||
-- (Check existing indexes first)
|
||||
```
|
||||
|
||||
## Recommended Code Changes
|
||||
|
||||
### 1. Update session_routes.py
|
||||
Replace all occurrences of:
|
||||
- `device_id` → `hardware_id`
|
||||
- `active` → `is_active`
|
||||
- `login_time` → `started_at`
|
||||
- `last_activity` → `last_heartbeat`
|
||||
- `logout_time` → `ended_at`
|
||||
|
||||
### 2. Update models.py
|
||||
- Line 167: Change `start_time` to `started_at`
|
||||
|
||||
### 3. Create database migration script
|
||||
Create a migration that:
|
||||
1. Updates all code references
|
||||
2. Creates compatibility views
|
||||
3. Removes duplicate columns
|
||||
4. Updates any affected indexes
|
||||
|
||||
## Testing Checklist
|
||||
- [ ] All session queries work correctly
|
||||
- [ ] Session history displays properly
|
||||
- [ ] Active session count is accurate
|
||||
- [ ] Device tracking works correctly
|
||||
- [ ] All time-based queries function properly
|
||||
In neuem Issue referenzieren
Einen Benutzer sperren