feat: Add migration script to apply or refresh database schema

This commit is contained in:
Hamza-Ayed
2026-08-29 00:28:46 +03:00
parent b3f70193a3
commit 294a60ecdb
+46
View File
@@ -0,0 +1,46 @@
<?php
require __DIR__ . '/../vendor/autoload.php';
$envFile = __DIR__ . '/../.env';
if (file_exists($envFile)) {
$lines = file($envFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach ($lines as $line) {
if (strpos(trim($line), '#') === 0) continue;
list($name, $value) = explode('=', $line, 2);
putenv(trim($name) . '=' . trim($value));
}
}
$host = getenv('DB_HOST');
$port = getenv('DB_PORT') ?: 3306;
$dbName = getenv('DB_DATABASE');
$user = getenv('DB_USERNAME');
$password = getenv('DB_PASSWORD');
try {
$pdo = new PDO("mysql:host=$host;port=$port", $user, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$isFresh = isset($argv[1]) && $argv[1] === 'fresh';
if ($isFresh) {
echo "Dropping database `$dbName`...\n";
$pdo->exec("DROP DATABASE IF EXISTS `$dbName`");
}
$pdo->exec("CREATE DATABASE IF NOT EXISTS `$dbName` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci");
$pdo->exec("USE `$dbName`");
$schema = file_get_contents(__DIR__ . '/../database_schema.sql');
if (!$schema) {
die("Could not read database_schema.sql\n");
}
echo "Executing database_schema.sql...\n";
$pdo->exec($schema);
echo "Database migration completed successfully!\n";
} catch (PDOException $e) {
die("Database Error: " . $e->getMessage() . "\n");
}