47 lines
1.4 KiB
PHP
47 lines
1.4 KiB
PHP
<?php
|
|
// Generated by setup_mariadb_project.sh — edit as needed.
|
|
define('DB_HOST', '127.0.0.1');
|
|
define('DB_NAME', 'app_35995');
|
|
define('DB_USER', 'app_35995');
|
|
define('DB_PASS', 'b8ef9913-5f22-4623-8ebd-b995c4416af0');
|
|
|
|
function db() {
|
|
static $pdo;
|
|
if (!$pdo) {
|
|
$pdo = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME.';charset=utf8mb4', DB_USER, DB_PASS, [
|
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
|
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
|
]);
|
|
}
|
|
return $pdo;
|
|
}
|
|
|
|
function run_migrations() {
|
|
$pdo = db();
|
|
$migrations_dir = __DIR__ . '/migrations';
|
|
if (!is_dir($migrations_dir)) {
|
|
return;
|
|
}
|
|
$migration_files = glob($migrations_dir . '/*.sql');
|
|
foreach ($migration_files as $file) {
|
|
$p = pathinfo($file);
|
|
$migration_name = $p['basename'];
|
|
try {
|
|
$stmt = $pdo->query("SELECT 1 FROM migrations WHERE name = '$migration_name'");
|
|
if ($stmt->fetchColumn()) {
|
|
continue;
|
|
}
|
|
} catch (PDOException $e) {
|
|
// migrations table doesn't exist, create it
|
|
$pdo->exec("CREATE TABLE IF NOT EXISTS migrations (name VARCHAR(255) PRIMARY KEY, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP)");
|
|
}
|
|
|
|
$sql = file_get_contents($file);
|
|
$pdo->exec($sql);
|
|
$stmt = $pdo->prepare("INSERT INTO migrations (name) VALUES (?)");
|
|
$stmt->execute([$migration_name]);
|
|
}
|
|
}
|
|
|
|
run_migrations();
|