migrations/Version20251002095927.php line 1

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace DoctrineMigrations;
  4. use Doctrine\DBAL\Schema\Schema;
  5. use Doctrine\Migrations\AbstractMigration;
  6. /**
  7.  * Migration para Sistema de Rate Limiting por IP
  8.  *
  9.  * Crea las tablas:
  10.  * - tipo_seccion: Catálogo de endpoints protegidos
  11.  * - rate_limit: Configuración de límites por endpoint
  12.  * - blacklist: Registro de IPs, intentos y bloqueos
  13.  */
  14. final class Version20251002095927 extends AbstractMigration
  15. {
  16.     public function getDescription(): string
  17.     {
  18.         return 'Crea tablas para sistema de Rate Limiting por IP: tipo_seccion, rate_limit y blacklist';
  19.     }
  20.     public function up(Schema $schema): void
  21.     {
  22.         // Tabla tipo_seccion
  23.         $this->addSql('CREATE TABLE tipo_seccion (
  24.             id INT AUTO_INCREMENT NOT NULL,
  25.             endpoint VARCHAR(255) NOT NULL,
  26.             nombre VARCHAR(100) NOT NULL,
  27.             created_at DATETIME NOT NULL,
  28.             updated_at DATETIME NOT NULL,
  29.             deleted_at DATETIME DEFAULT NULL,
  30.             UNIQUE INDEX UNIQ_TIPO_SECCION_ENDPOINT (endpoint),
  31.             INDEX IDX_ENDPOINT (endpoint),
  32.             PRIMARY KEY(id)
  33.         ) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
  34.         // Tabla rate_limit
  35.         $this->addSql('CREATE TABLE rate_limit (
  36.             id INT AUTO_INCREMENT NOT NULL,
  37.             tipo_seccion_id INT NOT NULL,
  38.             intentos INT NOT NULL,
  39.             ventana INT NOT NULL,
  40.             bloqueos INT NOT NULL,
  41.             tiempo_bloqueo INT NOT NULL,
  42.             created_at DATETIME NOT NULL,
  43.             updated_at DATETIME NOT NULL,
  44.             UNIQUE INDEX UNIQ_RATE_LIMIT_TIPO_SECCION (tipo_seccion_id),
  45.             PRIMARY KEY(id)
  46.         ) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
  47.         // Tabla blacklist
  48.         $this->addSql('CREATE TABLE blacklist (
  49.             id INT AUTO_INCREMENT NOT NULL,
  50.             rate_limit_id INT NOT NULL,
  51.             ip VARCHAR(45) NOT NULL,
  52.             fecha_primer_intento DATETIME NOT NULL,
  53.             fecha_ultimo_intento DATETIME NOT NULL,
  54.             nro_intento INT NOT NULL DEFAULT 0,
  55.             contador_bloqueos INT NOT NULL DEFAULT 0,
  56.             estado VARCHAR(20) DEFAULT NULL,
  57.             fecha_bloqueo DATETIME DEFAULT NULL,
  58.             fecha_desbloqueo DATETIME DEFAULT NULL,
  59.             created_at DATETIME NOT NULL,
  60.             updated_at DATETIME NOT NULL,
  61.             INDEX IDX_IP_ESTADO (ip, estado),
  62.             INDEX IDX_FECHA_PRIMER_INTENTO (fecha_primer_intento),
  63.             INDEX IDX_RATE_LIMIT (rate_limit_id),
  64.             UNIQUE INDEX UNIQUE_IP_RATE_LIMIT (ip, rate_limit_id),
  65.             PRIMARY KEY(id)
  66.         ) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
  67.         // Foreign Keys
  68.         $this->addSql('ALTER TABLE rate_limit
  69.             ADD CONSTRAINT FK_RATE_LIMIT_TIPO_SECCION
  70.             FOREIGN KEY (tipo_seccion_id)
  71.             REFERENCES tipo_seccion (id)
  72.             ON DELETE CASCADE');
  73.         $this->addSql('ALTER TABLE blacklist
  74.             ADD CONSTRAINT FK_BLACKLIST_RATE_LIMIT
  75.             FOREIGN KEY (rate_limit_id)
  76.             REFERENCES rate_limit (id)
  77.             ON DELETE CASCADE');
  78.     }
  79.     public function down(Schema $schema): void
  80.     {
  81.         // Eliminar tablas en orden inverso (primero las dependientes)
  82.         $this->addSql('ALTER TABLE blacklist DROP FOREIGN KEY FK_BLACKLIST_RATE_LIMIT');
  83.         $this->addSql('ALTER TABLE rate_limit DROP FOREIGN KEY FK_RATE_LIMIT_TIPO_SECCION');
  84.         $this->addSql('DROP TABLE blacklist');
  85.         $this->addSql('DROP TABLE rate_limit');
  86.         $this->addSql('DROP TABLE tipo_seccion');
  87.     }
  88. }