選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

108 行
4.7KB

  1. <?php
  2. declare(strict_types=1);
  3. require_once __DIR__ . '/../vendor/autoload.php';
  4. function seed_employees(int $targetTotal = 1000, bool $resetExisting = false): void
  5. {
  6. $targetTotal = max(1, $targetTotal);
  7. $migrationManager = migration_manager();
  8. $migrationManager->runPending();
  9. $database = database();
  10. if ($resetExisting) {
  11. $database->execute('DELETE FROM employees');
  12. }
  13. $currentTotal = (int) (database()->first('SELECT COUNT(*) AS total FROM employees')['total'] ?? 0);
  14. if ($currentTotal >= $targetTotal) {
  15. echo "Employee table already has {$currentTotal} records." . PHP_EOL;
  16. return;
  17. }
  18. $firstNames = [
  19. 'Ava', 'Liam', 'Noah', 'Emma', 'Olivia', 'Mason', 'Sophia', 'Ethan', 'Isabella', 'Lucas',
  20. 'Mia', 'Amelia', 'James', 'Harper', 'Benjamin', 'Ella', 'Henry', 'Evelyn', 'Jack', 'Abigail',
  21. 'Alexander', 'Emily', 'Michael', 'Charlotte', 'Daniel', 'Grace', 'Elijah', 'Scarlett', 'William', 'Chloe',
  22. 'Matthew', 'Victoria', 'Samuel', 'Lily', 'David', 'Aria', 'Joseph', 'Zoey', 'Carter', 'Hannah',
  23. 'Owen', 'Addison', 'Wyatt', 'Natalie', 'John', 'Aubrey', 'Luke', 'Brooklyn', 'Gabriel', 'Layla',
  24. 'Anthony', 'Zoe', 'Isaac', 'Penelope', 'Dylan', 'Riley', 'Grayson', 'Nora', 'Levi', 'Lillian',
  25. 'Julian', 'Eleanor', 'Christopher', 'Stella', 'Joshua', 'Savannah', 'Andrew', 'Audrey', 'Nathan', 'Claire',
  26. 'Thomas', 'Skylar', 'Caleb', 'Lucy', 'Ryan', 'Paisley', 'Christian', 'Everly', 'Hunter', 'Anna',
  27. 'Jonathan', 'Caroline', 'Aaron', 'Nova', 'Charles', 'Genesis', 'Connor', 'Kennedy', 'Eli', 'Samantha',
  28. 'Landon', 'Maya', 'Adrian', 'Willow', 'Nicholas', 'Kinsley', 'Jeremiah', 'Naomi', 'Easton', 'Ariana',
  29. ];
  30. $lastNames = [
  31. 'Carter', 'Brooks', 'Hayes', 'Parker', 'Turner', 'Sullivan', 'Reed', 'Ward', 'Price', 'Foster',
  32. 'Powell', 'Bennett', 'Coleman', 'Russell', 'Long', 'Perry', 'Morgan', 'Peterson', 'Cooper', 'Bailey',
  33. 'Smith', 'Johnson', 'Williams', 'Brown', 'Jones', 'Garcia', 'Miller', 'Davis', 'Rodriguez', 'Martinez',
  34. 'Hernandez', 'Lopez', 'Gonzalez', 'Wilson', 'Anderson', 'Thomas', 'Taylor', 'Moore', 'Jackson', 'Martin',
  35. 'Lee', 'Perez', 'Thompson', 'White', 'Harris', 'Sanchez', 'Clark', 'Ramirez', 'Lewis', 'Robinson',
  36. 'Walker', 'Young', 'Allen', 'King', 'Wright', 'Scott', 'Torres', 'Nguyen', 'Hill', 'Flores',
  37. 'Green', 'Adams', 'Nelson', 'Baker', 'Hall', 'Rivera', 'Campbell', 'Mitchell', 'Roberts', 'Gomez',
  38. 'Phillips', 'Evans', 'Edwards', 'Collins', 'Stewart', 'Morris', 'Rogers', 'Murphy', 'Cook', 'Ramos',
  39. 'Richardson', 'Cox', 'Howard', 'Bell', 'Ortiz', 'Gutierrez', 'Chavez', 'Wood', 'James', 'Bennett',
  40. 'Gray', 'Mendoza', 'Ruiz', 'Hughes', 'Grant', 'Stone', 'Spencer', 'Warren', 'Porter', 'Bryant',
  41. ];
  42. $departments = [
  43. 'Engineering', 'Finance', 'Operations', 'Sales', 'Marketing', 'People', 'Support', 'Legal',
  44. ];
  45. $jobTitles = [
  46. 'Coordinator', 'Analyst', 'Manager', 'Specialist', 'Administrator', 'Engineer', 'Consultant', 'Lead',
  47. ];
  48. $statement = $database->pdo()->prepare(
  49. 'INSERT INTO employees (first_name, last_name, email, department, job_title, start_date)
  50. VALUES (:first_name, :last_name, :email, :department, :job_title, :start_date)'
  51. );
  52. $database->pdo()->beginTransaction();
  53. try {
  54. for ($i = $currentTotal + 1; $i <= $targetTotal; $i++) {
  55. $firstName = $firstNames[$i % count($firstNames)];
  56. $lastName = $lastNames[$i % count($lastNames)];
  57. $department = $departments[$i % count($departments)];
  58. $jobTitle = $jobTitles[$i % count($jobTitles)];
  59. $email = sprintf(
  60. '%s.%s.%04d@example.test',
  61. strtolower($firstName),
  62. strtolower($lastName),
  63. $i
  64. );
  65. $month = (($i - 1) % 12) + 1;
  66. $day = (($i - 1) % 28) + 1;
  67. $year = 2019 + (($i - 1) % 8);
  68. $startDate = sprintf('%04d-%02d-%02d', $year, $month, $day);
  69. $statement->execute([
  70. 'first_name' => $firstName,
  71. 'last_name' => $lastName,
  72. 'email' => $email,
  73. 'department' => $department,
  74. 'job_title' => $jobTitle,
  75. 'start_date' => $startDate,
  76. ]);
  77. }
  78. $database->pdo()->commit();
  79. } catch (Throwable $exception) {
  80. $database->pdo()->rollBack();
  81. throw $exception;
  82. }
  83. $inserted = $targetTotal - $currentTotal;
  84. echo "Inserted {$inserted} sample employees. Total is now {$targetTotal}." . PHP_EOL;
  85. }
  86. if (PHP_SAPI === 'cli' && realpath($_SERVER['SCRIPT_FILENAME'] ?? '') === __FILE__) {
  87. $targetTotal = isset($argv[1]) ? max(1, (int) $argv[1]) : 1000;
  88. seed_employees($targetTotal);
  89. }

Powered by TurnKey Linux.