diff --git a/database/migrate.php b/database/migrate.php index 01199c8..8e803f1 100644 --- a/database/migrate.php +++ b/database/migrate.php @@ -2,59 +2,15 @@ declare(strict_types=1); -$root = dirname(__DIR__); +require dirname(__DIR__) . '/src/Database.php'; -$pdo = new PDO('sqlite:' . $root . '/database/api_tube.sqlite'); -$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); -$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); +$applied = Database::runMigrations(); -$pdo->exec( - "CREATE TABLE IF NOT EXISTS migrations ( - version TEXT PRIMARY KEY, - applied_at TEXT NOT NULL DEFAULT (datetime('now')) - )" -); - -$applied = array_column($pdo->query('SELECT version FROM migrations')->fetchAll(), 'version'); - -$files = glob($root . '/database/migrations/*.sql'); - -if ($files === false || $files === []) { - echo "Aucune migration trouvee.\n"; +if ($applied === []) { + echo "Nothing to migrate.\n"; exit(0); } -sort($files); - -$count = 0; - -foreach ($files as $file) { - $version = basename($file); - - if (in_array($version, $applied, true)) { - echo "Skipped {$version}\n"; - continue; - } - - $pdo->beginTransaction(); - - try { - $pdo->exec((string) file_get_contents($file)); - - $stmt = $pdo->prepare('INSERT INTO migrations (version) VALUES (:version)'); - $stmt->execute(['version' => $version]); - - $pdo->commit(); - } catch (Throwable $e) { - $pdo->rollBack(); - throw $e; - } - +foreach ($applied as $version) { echo "Applied {$version}\n"; - $applied[] = $version; - $count++; -} - -if ($count === 0) { - echo "Nothing to migrate.\n"; } \ No newline at end of file diff --git a/src/Database.php b/src/Database.php index 8ec9164..5d86104 100644 --- a/src/Database.php +++ b/src/Database.php @@ -8,21 +8,70 @@ final class Database public static function connect(): PDO { - if (self::$pdo !== null) { - return self::$pdo; + if (self::$pdo === null) { + self::init(); + self::runMigrations(); } - $root = dirname(__DIR__); - $dsn = 'sqlite:' . $root . '/database/api_tube.sqlite'; - - self::$pdo = new PDO($dsn); - self::$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); - self::$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); - self::$pdo->exec('PRAGMA foreign_keys = ON'); - - $schema = file_get_contents($root . '/database/schema.sql'); - self::$pdo->exec($schema); - return self::$pdo; } -} + + public static function runMigrations(): array + { + if (self::$pdo === null) { + self::init(); + } + + $root = dirname(__DIR__); + + self::$pdo->exec( + "CREATE TABLE IF NOT EXISTS migrations ( + version TEXT PRIMARY KEY, + applied_at TEXT NOT NULL DEFAULT (datetime('now')) + )" + ); + + $applied = array_column(self::$pdo->query('SELECT version FROM migrations')->fetchAll(), 'version'); + + $files = glob($root . '/database/migrations/*.sql') ?: []; + sort($files); + + $newlyApplied = []; + + foreach ($files as $file) { + $version = basename($file); + + if (in_array($version, $applied, true)) { + continue; + } + + self::$pdo->beginTransaction(); + + try { + self::$pdo->exec((string) file_get_contents($file)); + + $stmt = self::$pdo->prepare('INSERT INTO migrations (version) VALUES (:version)'); + $stmt->execute(['version' => $version]); + + self::$pdo->commit(); + } catch (Throwable $e) { + self::$pdo->rollBack(); + throw $e; + } + + $newlyApplied[] = $version; + } + + return $newlyApplied; + } + + private static function init(): void + { + $root = dirname(__DIR__); + + self::$pdo = new PDO('sqlite:' . $root . '/database/api_tube.sqlite'); + self::$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + self::$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); + self::$pdo->exec('PRAGMA foreign_keys = ON'); + } +} \ No newline at end of file