Initial commit of the Asset Management System, including project structure, Docker configuration, database migrations, and core application files. Added user authentication, asset management features, and basic UI components.
This commit is contained in:
83
scripts/seed.php
Normal file
83
scripts/seed.php
Normal file
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
/**
|
||||
* Database Seeder Script
|
||||
*/
|
||||
|
||||
require_once __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
// Load environment variables
|
||||
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__ . '/..');
|
||||
$dotenv->load();
|
||||
|
||||
// Load configuration
|
||||
require_once __DIR__ . '/../config/config.php';
|
||||
|
||||
try {
|
||||
$pdo = new PDO(
|
||||
sprintf('mysql:host=%s;port=%d;dbname=%s;charset=utf8mb4', DB_HOST, DB_PORT, DB_NAME),
|
||||
DB_USER,
|
||||
DB_PASS,
|
||||
[
|
||||
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
||||
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
||||
]
|
||||
);
|
||||
|
||||
echo "Connected to database successfully.\n";
|
||||
|
||||
// Check if users table has data
|
||||
$stmt = $pdo->query("SELECT COUNT(*) as count FROM users");
|
||||
$userCount = $stmt->fetch()['count'];
|
||||
|
||||
if ($userCount > 0) {
|
||||
echo "Database already has data. Skipping seeders.\n";
|
||||
exit(0);
|
||||
}
|
||||
|
||||
echo "Database is empty. Running seeders...\n";
|
||||
|
||||
// Get all seeder files
|
||||
$seederFiles = glob(__DIR__ . '/../database/seeds/*.sql');
|
||||
sort($seederFiles);
|
||||
|
||||
foreach ($seederFiles as $file) {
|
||||
$seederName = basename($file);
|
||||
echo "Running seeder: {$seederName}\n";
|
||||
|
||||
// Read and execute seeder
|
||||
$sql = file_get_contents($file);
|
||||
|
||||
try {
|
||||
$pdo->exec($sql);
|
||||
echo "Seeder {$seederName} executed successfully.\n";
|
||||
|
||||
} catch (PDOException $e) {
|
||||
echo "Error executing seeder {$seederName}: " . $e->getMessage() . "\n";
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
// Create default admin user with proper password hash
|
||||
echo "Creating default admin user...\n";
|
||||
|
||||
$adminPassword = 'Admin!234';
|
||||
$adminHash = password_hash($adminPassword, PASSWORD_ARGON2ID);
|
||||
|
||||
$stmt = $pdo->prepare("
|
||||
UPDATE users
|
||||
SET passhash = ?
|
||||
WHERE email = 'admin@example.com'
|
||||
");
|
||||
$stmt->execute([$adminHash]);
|
||||
|
||||
echo "Default admin user created:\n";
|
||||
echo "Email: admin@example.com\n";
|
||||
echo "Password: {$adminPassword}\n";
|
||||
echo "Please change the password on first login!\n";
|
||||
|
||||
echo "Database seeding completed successfully.\n";
|
||||
|
||||
} catch (PDOException $e) {
|
||||
echo "Database connection failed: " . $e->getMessage() . "\n";
|
||||
exit(1);
|
||||
}
|
||||
Reference in New Issue
Block a user