30 lines
840 B
PHP
30 lines
840 B
PHP
<?php
|
|
// Migration runner executed automatically on deploy. Do NOT require from application code.
|
|
|
|
require_once __DIR__ . '/db.php';
|
|
|
|
// Create migrations tracking table
|
|
$pdo->exec("CREATE TABLE IF NOT EXISTS migrations (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
migration VARCHAR(255) NOT NULL UNIQUE,
|
|
ran_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
)");
|
|
|
|
// Get already-run migrations
|
|
$ran = $pdo->query("SELECT migration FROM migrations")->fetchAll(PDO::FETCH_COLUMN);
|
|
|
|
// Run pending migrations
|
|
$files = glob(__DIR__ . '/migrations/*.php');
|
|
sort($files);
|
|
|
|
foreach ($files as $file) {
|
|
$name = basename($file);
|
|
if (!in_array($name, $ran)) {
|
|
require $file;
|
|
$pdo->prepare("INSERT INTO migrations (migration) VALUES (?)")->execute([$name]);
|
|
echo "Migrated: {$name}\n";
|
|
}
|
|
}
|
|
|
|
echo "Migration complete.\n";
|