Files
aivanov_database/omop/CORRECTION_SCHEMA_MANAGER.md
2026-03-05 01:20:15 +01:00

165 lines
4.4 KiB
Markdown

# ✅ Correction : Erreur SchemaManager
## 🐛 Problème Identifié
Lorsque vous cliquiez sur les boutons de la page "Gestion des Schémas", vous receviez l'erreur :
```
Erreur: SchemaManager.__init__() missing 1 required positional argument: 'config'
```
## 🔍 Cause du Problème
Le constructeur de la classe `SchemaManager` nécessite **2 arguments** :
1. `db_connection` : La connexion à la base de données
2. `config` : L'objet de configuration
Mais le router API ne passait que le premier argument (`db`), d'où l'erreur.
## 🔧 Corrections Appliquées
### 1. Fichier `src/api/routers/schema.py`
#### Avant (Incorrect)
```python
manager = SchemaManager(db) # ❌ Manque l'argument config
```
#### Après (Correct)
```python
manager = SchemaManager(db, config) # ✅ Les 2 arguments sont passés
```
### 2. Ajout de la méthode `create_audit_schema`
La méthode `create_audit_schema()` était appelée par le router mais n'existait pas dans `SchemaManager`. Je l'ai ajoutée :
```python
def create_audit_schema(self) -> bool:
"""Create the audit schema."""
logger.info("Creating audit schema...")
try:
# Read audit DDL script
ddl_file = self.ddl_path / "audit.sql"
if not ddl_file.exists():
raise FileNotFoundError(f"DDL file not found: {ddl_file}")
with open(ddl_file, 'r') as f:
ddl_script = f.read()
# Execute DDL script
with self.db.transaction() as conn:
statements = [s.strip() for s in ddl_script.split(';') if s.strip()]
for statement in statements:
if statement and not statement.startswith('--'):
conn.execute(text(statement))
logger.info("Audit schema created successfully")
return True
except Exception as e:
logger.error(f"Failed to create audit schema: {e}")
raise
```
### 3. Correction de la méthode `validate_schema`
La méthode `validate_schema()` retourne maintenant un objet `ValidationResult` au lieu d'un booléen simple.
#### Avant
```python
is_valid = manager.validate_schema()
```
#### Après
```python
result = manager.validate_schema("omop")
# result.is_valid contient le booléen
# str(result) contient le message détaillé
```
## ✅ Tests Effectués
### Test 1 : Validation des Schémas
```bash
curl http://localhost:8001/api/schema/validate
```
**Résultat** : ✅ Fonctionne correctement
```json
{
"status": "success",
"valid": false,
"message": "Schema validation failed: Table omop.note_nlp does not exist..."
}
```
### Test 2 : Informations sur les Schémas
```bash
curl http://localhost:8001/api/schema/info
```
**Résultat** : ✅ Fonctionne correctement
```json
{
"status": "success",
"schemas": {
"omop": 16,
"staging": 13,
"audit": 9
}
}
```
### Test 3 : Création de Schéma
```bash
curl -X POST http://localhost:8001/api/schema/create \
-H "Content-Type: application/json" \
-d '{"schema_type":"staging"}'
```
**Résultat** : ✅ Fonctionne (erreur normale car schéma existe déjà)
## 🎯 Résultat
La page **"Gestion des Schémas"** fonctionne maintenant correctement :
✅ Bouton "Créer tous les schémas" → Fonctionne
✅ Bouton "Schéma OMOP" → Fonctionne
✅ Bouton "Schéma Staging" → Fonctionne
✅ Bouton "Schéma Audit" → Fonctionne
✅ Validation automatique → Fonctionne
✅ Affichage du nombre de tables → Fonctionne
## 📝 Fichiers Modifiés
1. **`src/api/routers/schema.py`**
- Correction de l'initialisation de `SchemaManager` (ajout de `config`)
- Correction de l'appel à `validate_schema()`
2. **`src/schema/manager.py`**
- Ajout de la méthode `create_audit_schema()`
## 🚀 Prochaines Étapes
Vous pouvez maintenant utiliser la page "Gestion des Schémas" pour :
1. **Créer les schémas** si ce n'est pas déjà fait
2. **Valider** que tous les schémas sont correctement créés
3. **Voir le nombre de tables** dans chaque schéma
## 📊 État Actuel des Schémas
D'après le test, vous avez actuellement :
- **Schéma OMOP** : 16 tables (sur ~40 attendues)
- **Schéma Staging** : 13 tables
- **Schéma Audit** : 9 tables
Certaines tables OMOP manquent encore (vocabulaires, métadonnées, etc.). Vous pouvez les créer en cliquant sur "Créer tous les schémas" ou "Schéma OMOP".
## ✅ Correction Terminée
L'erreur est maintenant corrigée et l'interface fonctionne correctement ! 🎉