# Instructions de modification des scripts de migration

## Modifications à effectuer

### 1. migrate_from_backup.php

#### A. Remplacer les lignes 31-50 (configuration DB)

**ANCIEN** :
```php
    private $sourceDbName;
    private $targetDbName;
    private $sourceDb;
    private $targetDb;
    private $mode;
    private $entityId;
    private $logFile;
    private $deleteBefore;

    // Configuration MariaDB (maria4 sur IN4)
    // pra-geo se connecte à maria4 via l'IP du container
    private const DB_HOST = '13.23.33.4';  // maria4 sur IN4
    private const DB_PORT = 3306;
    private const DB_USER = 'pra_geo_user';
    private const DB_PASS = 'd2jAAGGWi8fxFrWgXjOA';

    // Pour la base source (backup), on utilise pra_geo_user (avec SELECT sur geosector_*)
    // L'utilisateur root n'est pas accessible depuis pra-geo (13.23.33.22)
    private const DB_USER_ROOT = 'pra_geo_user';
    private const DB_PASS_ROOT = 'd2jAAGGWi8fxFrWgXjOA';
```

**NOUVEAU** :
```php
    private $sourceDbName;
    private $targetDbName;
    private $sourceDb;
    private $targetDb;
    private $mode;
    private $entityId;
    private $logFile;
    private $deleteBefore;
    private $env;

    // Configuration multi-environnement
    private const ENVIRONMENTS = [
        'rca' => [
            'host' => '13.23.33.3',       // maria3 sur IN3
            'port' => 3306,
            'user' => 'rca_geo_user',
            'pass' => 'UPf3C0cQ805LypyM71iW',
            'target_db' => 'rca_geo',
            'source_db' => 'geosector'    // Base synchronisée par PM7
        ],
        'pra' => [
            'host' => '13.23.33.4',       // maria4 sur IN4
            'port' => 3306,
            'user' => 'pra_geo_user',
            'pass' => 'd2jAAGGWi8fxFrWgXjOA',
            'target_db' => 'pra_geo',
            'source_db' => 'geosector'    // Base synchronisée par PM7
        ]
    ];
```

#### B. Modifier le constructeur (ligne 67)

**ANCIEN** :
```php
    public function __construct($sourceDbName, $targetDbName, $mode = 'global', $entityId = null, $logFile = null, $deleteBefore = false) {
        $this->sourceDbName = $sourceDbName;
        $this->targetDbName = $targetDbName;
        $this->mode = $mode;
        $this->entityId = $entityId;
        $this->logFile = $logFile ?? '/var/back/migration_' . date('Ymd_His') . '.log';
        $this->deleteBefore = $deleteBefore;

        $this->log("=== Migration depuis backup PM7 ===");
        $this->log("Source: {$sourceDbName}");
        $this->log("Cible: {$targetDbName}");
        $this->log("Mode: {$mode}");
```

**NOUVEAU** :
```php
    public function __construct($env, $mode = 'global', $entityId = null, $logFile = null, $deleteBefore = false) {
        // Validation de l'environnement
        if (!isset(self::ENVIRONMENTS[$env])) {
            throw new Exception("Invalid environment: $env. Use 'rca' or 'pra'");
        }

        $this->env = $env;
        $config = self::ENVIRONMENTS[$env];
        $this->sourceDbName = $config['source_db'];
        $this->targetDbName = $config['target_db'];
        $this->mode = $mode;
        $this->entityId = $entityId;
        $this->logFile = $logFile ?? '/var/back/migration_' . date('Ymd_His') . '.log';
        $this->deleteBefore = $deleteBefore;

        $this->log("=== Migration depuis backup PM7 ===");
        $this->log("Environment: {$env}");
        $this->log("Source: {$this->sourceDbName} → Cible: {$this->targetDbName}");
        $this->log("Mode: {$mode}");
```

#### C. Modifier connect() (lignes 90-112)

**Remplacer toutes les constantes** :
- `self::DB_HOST` → `self::ENVIRONMENTS[$this->env]['host']`
- `self::DB_PORT` → `self::ENVIRONMENTS[$this->env]['port']`
- `self::DB_USER_ROOT` → `self::ENVIRONMENTS[$this->env]['user']`
- `self::DB_PASS_ROOT` → `self::ENVIRONMENTS[$this->env]['pass']`
- `self::DB_USER` → `self::ENVIRONMENTS[$this->env]['user']`
- `self::DB_PASS` → `self::ENVIRONMENTS[$this->env]['pass']`

#### D. Modifier parseArguments() (vers la fin du fichier)

**ANCIEN** :
```php
    $args = [
        'source-db' => null,
        'target-db' => 'pra_geo',
        'mode' => 'global',
        'entity-id' => null,
        'log' => null,
        'delete-before' => true,
        'help' => false
    ];
```

**NOUVEAU** :
```php
    $args = [
        'env' => 'rca',  // Défaut: recette
        'mode' => 'global',
        'entity-id' => null,
        'log' => null,
        'delete-before' => true,
        'help' => false
    ];
```

#### E. Modifier showHelp()

**ANCIEN** :
```php
  --source-db=NAME      Nom de la base source (backup restauré, ex: geosector_20251007) [REQUIS]
  --target-db=NAME      Nom de la base cible (défaut: pra_geo)
```

**NOUVEAU** :
```php
  --env=ENV             Environment: 'rca' (recette) ou 'pra' (production) [défaut: rca]
```

**ANCIEN** (exemples) :
```php
  php migrate_from_backup.php --source-db=geosector_20251007 --target-db=pra_geo --mode=global
```

**NOUVEAU** :
```php
  php migrate_from_backup.php --env=pra --mode=global
  php migrate_from_backup.php --env=rca --mode=entity --entity-id=45
```

#### F. Modifier validation des arguments

**ANCIEN** :
```php
if (!$args['source-db']) {
    echo "Erreur: --source-db est requis\n\n";
    showHelp();
    exit(1);
}
```

**NOUVEAU** :
```php
if (!in_array($args['env'], ['rca', 'pra'])) {
    echo "Erreur: --env doit être 'rca' ou 'pra'\n\n";
    showHelp();
    exit(1);
}
```

#### G. Modifier instanciation BackupMigration

**ANCIEN** :
```php
$migration = new BackupMigration(
    $args['source-db'],
    $args['target-db'],
    $args['mode'],
    $args['entity-id'],
    $args['log'],
    (bool)$args['delete-before']
);
```

**NOUVEAU** :
```php
$migration = new BackupMigration(
    $args['env'],
    $args['mode'],
    $args['entity-id'],
    $args['log'],
    (bool)$args['delete-before']
);
```

---

### 2. migrate_batch.sh

#### A. Ajouter détection automatique de l'environnement (après ligne 22)

**AJOUTER** :
```bash
# Détection automatique de l'environnement
if [ -f "/etc/hostname" ]; then
    CONTAINER_NAME=$(cat /etc/hostname)
    case $CONTAINER_NAME in
        rca-geo)
            ENV="rca"
            ;;
        pra-geo)
            ENV="pra"
            ;;
        *)
            ENV="rca"  # Défaut
            ;;
    esac
else
    ENV="rca"  # Défaut
fi
```

#### B. Remplacer lignes 26-27

**ANCIEN** :
```bash
SOURCE_DB="geosector_20251013_13"
TARGET_DB="pra_geo"
```

**NOUVEAU** :
```bash
# SOURCE_DB et TARGET_DB ne sont plus utilisés
# Ils sont déduits de --env dans migrate_from_backup.php
```

#### C. Ajouter option --env dans le parsing (ligne 68)

**AJOUTER avant `--interactive|-i)` ** :
```bash
        --env)
            ENV="$2"
            shift 2
            ;;
```

#### D. Modifier les appels PHP - ligne 200-206

**ANCIEN** :
```bash
            php "$MIGRATION_SCRIPT" \
                --source-db="$SOURCE_DB" \
                --target-db="$TARGET_DB" \
                --mode=entity \
                --entity-id="$SPECIFIC_ENTITY_ID" \
                --log="$ENTITY_LOG" \
                $DELETE_FLAG
```

**NOUVEAU** :
```bash
            php "$MIGRATION_SCRIPT" \
                --env="$ENV" \
                --mode=entity \
                --entity-id="$SPECIFIC_ENTITY_ID" \
                --log="$ENTITY_LOG" \
                $DELETE_FLAG
```

#### E. Modifier les appels PHP - ligne 374-379

**ANCIEN** :
```bash
    php "$MIGRATION_SCRIPT" \
        --source-db="$SOURCE_DB" \
        --target-db="$TARGET_DB" \
        --mode=entity \
        --entity-id="$ENTITY_ID" \
        --log="$ENTITY_LOG" > /tmp/migration_output_$$.txt 2>&1
```

**NOUVEAU** :
```bash
    php "$MIGRATION_SCRIPT" \
        --env="$ENV" \
        --mode=entity \
        --entity-id="$ENTITY_ID" \
        --log="$ENTITY_LOG" > /tmp/migration_output_$$.txt 2>&1
```

#### F. Modifier les messages de log (lignes 289-291)

**ANCIEN** :
```bash
log "📅 Date: $(date '+%Y-%m-%d %H:%M:%S')"
log "📁 Source: $SOURCE_DB"
log "📁 Cible: $TARGET_DB"
```

**NOUVEAU** :
```bash
log "📅 Date: $(date '+%Y-%m-%d %H:%M:%S')"
log "🌍 Environment: $ENV"
log "📁 Source: geosector → Target: (déduit de \$ENV)"
```

---

## Nouveaux usages

### Sur rca-geo (IN3)
```bash
# Détection automatique
./migrate_batch.sh

# Ou explicite
./migrate_batch.sh --env=rca

# Migration PHP directe
php php/migrate_from_backup.php --env=rca --mode=entity --entity-id=45
```

### Sur pra-geo (IN4)
```bash
# Détection automatique
./migrate_batch.sh

# Ou explicite
./migrate_batch.sh --env=pra

# Migration PHP directe
php php/migrate_from_backup.php --env=pra --mode=entity --entity-id=45
```
