ARUNOX\Migrations\Entity
Diese Dokumentation beschreibt die Klasse Migrations\Entity, die für die Verwaltung und Nachverfolgung von Datenbankmigrationen zuständig ist.
Einleitung
Die Klasse Migrations\Entity bietet Methoden zum Verwalten der migrations-Tabelle. Dazu gehören die Initialisierung der Tabelle, das Abrufen bereits ausgeführter Migrationen sowie das Speichern neuer Migrationen.
Strukturübersicht
Die Klasse Migrations\Entity befindet sich im folgenden Verzeichnis:
library/ARUNOX/Migrations/Entity.php
Voraussetzungen
Folgende Voraussetzungen müssen erfüllt sein, um die Klasse erfolgreich nutzen zu können:
- Die Datenbank muss korrekt konfiguriert sein.
- Die Klasse
ARUNOX\Core\Databasemuss verfügbar sein.
Anwendung
So verwenden Sie die Klasse Migrations\Entity:
Initialisierung der migrations-Tabelle
$entity = new \ARUNOX\Migrations\Entity();
$entity->initTable();
Ausgeführte Migrationen abrufen
$appliedMigrations = $entity->getApplied();
Neue Migration speichern
$entity->record('MigrationClassName');
Sourcecode
Pfad: library/ARUNOX/Migrations/Entity.php
<?php
/**
* ARUNOX\Migrations\Entity
*
* Manages database interactions for the migrations table.
* Ensures proper tracking of executed migrations.
*
* @author Sparky
* @copyright 2025 ARUNOX
* @version 1.0.3
*/
namespace ARUNOX\Migrations;
use ARUNOX\Core\Database;
class Entity
{
private string $table = 'migrations';
/**
* Ensures the migrations table exists.
*/
public function initTable(): void
{
Database::query(
"CREATE TABLE IF NOT EXISTS {$this->table} (
id INT AUTO_INCREMENT PRIMARY KEY,
migration VARCHAR(255) NOT NULL UNIQUE,
executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)")->execute();
}
/**
* Retrieves the list of applied migrations.
*
* @return array
*/
public function getApplied(): array
{
return Database::query("SELECT migration FROM {$this->table}")
->fetchAll(\PDO::FETCH_COLUMN);
}
/**
* Records a migration as executed.
*
* @param string $migration
*/
public function record(string $migration): void
{
Database::query("INSERT INTO {$this->table} (migration) VALUES (:migration)")
->bind(['migration' => $migration])
->execute();
}
}