|
- <?php
-
- declare(strict_types=1);
-
- require_once __DIR__ . '/../vendor/autoload.php';
-
- function seed_employees(int $targetTotal = 1000, bool $resetExisting = false): void
- {
- $targetTotal = max(1, $targetTotal);
- $migrationManager = migration_manager();
- $migrationManager->runPending();
- $database = database();
-
- if ($resetExisting) {
- $database->execute('DELETE FROM employees');
- }
-
- $currentTotal = (int) (database()->first('SELECT COUNT(*) AS total FROM employees')['total'] ?? 0);
-
- if ($currentTotal >= $targetTotal) {
- echo "Employee table already has {$currentTotal} records." . PHP_EOL;
- return;
- }
-
- $firstNames = [
- 'Ava', 'Liam', 'Noah', 'Emma', 'Olivia', 'Mason', 'Sophia', 'Ethan', 'Isabella', 'Lucas',
- 'Mia', 'Amelia', 'James', 'Harper', 'Benjamin', 'Ella', 'Henry', 'Evelyn', 'Jack', 'Abigail',
- 'Alexander', 'Emily', 'Michael', 'Charlotte', 'Daniel', 'Grace', 'Elijah', 'Scarlett', 'William', 'Chloe',
- 'Matthew', 'Victoria', 'Samuel', 'Lily', 'David', 'Aria', 'Joseph', 'Zoey', 'Carter', 'Hannah',
- 'Owen', 'Addison', 'Wyatt', 'Natalie', 'John', 'Aubrey', 'Luke', 'Brooklyn', 'Gabriel', 'Layla',
- 'Anthony', 'Zoe', 'Isaac', 'Penelope', 'Dylan', 'Riley', 'Grayson', 'Nora', 'Levi', 'Lillian',
- 'Julian', 'Eleanor', 'Christopher', 'Stella', 'Joshua', 'Savannah', 'Andrew', 'Audrey', 'Nathan', 'Claire',
- 'Thomas', 'Skylar', 'Caleb', 'Lucy', 'Ryan', 'Paisley', 'Christian', 'Everly', 'Hunter', 'Anna',
- 'Jonathan', 'Caroline', 'Aaron', 'Nova', 'Charles', 'Genesis', 'Connor', 'Kennedy', 'Eli', 'Samantha',
- 'Landon', 'Maya', 'Adrian', 'Willow', 'Nicholas', 'Kinsley', 'Jeremiah', 'Naomi', 'Easton', 'Ariana',
- ];
-
- $lastNames = [
- 'Carter', 'Brooks', 'Hayes', 'Parker', 'Turner', 'Sullivan', 'Reed', 'Ward', 'Price', 'Foster',
- 'Powell', 'Bennett', 'Coleman', 'Russell', 'Long', 'Perry', 'Morgan', 'Peterson', 'Cooper', 'Bailey',
- 'Smith', 'Johnson', 'Williams', 'Brown', 'Jones', 'Garcia', 'Miller', 'Davis', 'Rodriguez', 'Martinez',
- 'Hernandez', 'Lopez', 'Gonzalez', 'Wilson', 'Anderson', 'Thomas', 'Taylor', 'Moore', 'Jackson', 'Martin',
- 'Lee', 'Perez', 'Thompson', 'White', 'Harris', 'Sanchez', 'Clark', 'Ramirez', 'Lewis', 'Robinson',
- 'Walker', 'Young', 'Allen', 'King', 'Wright', 'Scott', 'Torres', 'Nguyen', 'Hill', 'Flores',
- 'Green', 'Adams', 'Nelson', 'Baker', 'Hall', 'Rivera', 'Campbell', 'Mitchell', 'Roberts', 'Gomez',
- 'Phillips', 'Evans', 'Edwards', 'Collins', 'Stewart', 'Morris', 'Rogers', 'Murphy', 'Cook', 'Ramos',
- 'Richardson', 'Cox', 'Howard', 'Bell', 'Ortiz', 'Gutierrez', 'Chavez', 'Wood', 'James', 'Bennett',
- 'Gray', 'Mendoza', 'Ruiz', 'Hughes', 'Grant', 'Stone', 'Spencer', 'Warren', 'Porter', 'Bryant',
- ];
-
- $departments = [
- 'Engineering', 'Finance', 'Operations', 'Sales', 'Marketing', 'People', 'Support', 'Legal',
- ];
-
- $jobTitles = [
- 'Coordinator', 'Analyst', 'Manager', 'Specialist', 'Administrator', 'Engineer', 'Consultant', 'Lead',
- ];
-
- $statement = $database->pdo()->prepare(
- 'INSERT INTO employees (first_name, last_name, email, department, job_title, start_date)
- VALUES (:first_name, :last_name, :email, :department, :job_title, :start_date)'
- );
-
- $database->pdo()->beginTransaction();
-
- try {
- for ($i = $currentTotal + 1; $i <= $targetTotal; $i++) {
- $firstName = $firstNames[$i % count($firstNames)];
- $lastName = $lastNames[$i % count($lastNames)];
- $department = $departments[$i % count($departments)];
- $jobTitle = $jobTitles[$i % count($jobTitles)];
- $email = sprintf(
- '%s.%s.%04d@example.test',
- strtolower($firstName),
- strtolower($lastName),
- $i
- );
-
- $month = (($i - 1) % 12) + 1;
- $day = (($i - 1) % 28) + 1;
- $year = 2019 + (($i - 1) % 8);
- $startDate = sprintf('%04d-%02d-%02d', $year, $month, $day);
-
- $statement->execute([
- 'first_name' => $firstName,
- 'last_name' => $lastName,
- 'email' => $email,
- 'department' => $department,
- 'job_title' => $jobTitle,
- 'start_date' => $startDate,
- ]);
- }
-
- $database->pdo()->commit();
- } catch (Throwable $exception) {
- $database->pdo()->rollBack();
- throw $exception;
- }
-
- $inserted = $targetTotal - $currentTotal;
- echo "Inserted {$inserted} sample employees. Total is now {$targetTotal}." . PHP_EOL;
- }
-
- if (PHP_SAPI === 'cli' && realpath($_SERVER['SCRIPT_FILENAME'] ?? '') === __FILE__) {
- $targetTotal = isset($argv[1]) ? max(1, (int) $argv[1]) : 1000;
- seed_employees($targetTotal);
- }
|