43 lines
1.2 KiB
PHP
43 lines
1.2 KiB
PHP
<?php
|
|
require_once __DIR__ . '/config.php';
|
|
|
|
function run_migrations() {
|
|
$pdo = db();
|
|
$migrations_dir = __DIR__ . '/migrations';
|
|
$files = glob($migrations_dir . '/*.sql');
|
|
sort($files);
|
|
|
|
foreach ($files as $file) {
|
|
$sql = file_get_contents($file);
|
|
try {
|
|
$pdo->exec($sql);
|
|
echo 'Migrated: ' . basename($file) . PHP_EOL;
|
|
} catch (PDOException $e) {
|
|
die('Migration failed: ' . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
// Check if default user exists
|
|
$stmt = $pdo->prepare('SELECT id FROM users WHERE email = ?');
|
|
$stmt->execute(['admin@example.com']);
|
|
if ($stmt->fetch()) {
|
|
echo 'Default admin user already exists.' . PHP_EOL;
|
|
return;
|
|
}
|
|
|
|
// Insert a default admin user
|
|
$name = 'Super Admin';
|
|
$email = 'admin@example.com';
|
|
$password = password_hash('password', PASSWORD_DEFAULT);
|
|
|
|
try {
|
|
$stmt = $pdo->prepare('INSERT INTO users (name, email, password) VALUES (?, ?, ?)');
|
|
$stmt->execute([$name, $email, $password]);
|
|
echo 'Default admin user created (admin@example.com / password).' . PHP_EOL;
|
|
} catch (PDOException $e) {
|
|
die('Failed to create default user: ' . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
run_migrations();
|