Skip to content

Commit 3d39092

Browse files
committed
Database: add new artisan hms:reseed command
has to fix up UserFactory to give google2faEnable a default and fix a bunch of other bits this replaces `dev/reeseed.sh` with `php artisan hms:reseed`
1 parent 4b046a5 commit 3d39092

File tree

6 files changed

+66
-13
lines changed

6 files changed

+66
-13
lines changed

database/factories/UserFactory.php

+1
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,6 @@
2323
'rememberToken' => str_random(10),
2424
'roles' => new ArrayCollection(),
2525
'emailVerifiedAt' => Carbon::now(),
26+
'google2faEnable' => false,
2627
];
2728
});

database/seeds/DatabaseSeeder.php

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use Illuminate\Database\Seeder;
4+
use Doctrine\ORM\EntityManagerInterface;
45

56
class DatabaseSeeder extends Seeder
67
{
@@ -9,18 +10,29 @@ class DatabaseSeeder extends Seeder
910
*
1011
* @return void
1112
*/
12-
public function run()
13+
public function run(EntityManagerInterface $entityManager)
1314
{
1415
$this->call(UserTableSeeder::class);
16+
$entityManager->clear();
1517
$this->call(ProfileTableSeeder::class);
18+
$entityManager->clear();
1619
$this->call(AccountTableSeeder::class);
20+
$entityManager->clear();
1721
$this->call(PinTableSeeder::class);
22+
$entityManager->clear();
1823
$this->call(RfidTagTableSeeder::class);
24+
$entityManager->clear();
1925
$this->call(AccessLogTableSeeder::class);
26+
$entityManager->clear();
2027
$this->call(BankTransactionTableSeeder::class);
28+
$entityManager->clear();
2129
$this->call(RoleUpdateTableSeeder::class);
30+
$entityManager->clear();
2231
$this->call(ProductTableSeeder::class);
32+
$entityManager->clear();
2333
$this->call(TransactionTableSeeder::class);
34+
$entityManager->clear();
2435
$this->call(ToolTableSeeder::class);
36+
$entityManager->clear();
2537
}
2638
}

database/seeds/ProfileTableSeeder.php

+17-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Illuminate\Database\Seeder;
66
use HMS\Repositories\RoleRepository;
77
use HMS\Repositories\UserRepository;
8-
use LaravelDoctrine\ORM\Facades\EntityManager;
8+
use Doctrine\ORM\EntityManagerInterface;
99

1010
class ProfileTableSeeder extends Seeder
1111
{
@@ -19,10 +19,23 @@ class ProfileTableSeeder extends Seeder
1919
*/
2020
protected $userRepository;
2121

22-
public function __construct(RoleRepository $roleRepository, UserRepository $userRepository)
22+
/**
23+
* @var EntityManagerInterface
24+
*/
25+
protected $entityManager;
26+
27+
/**
28+
* Create a new TableSeeder instance.
29+
*
30+
* @param RoleRepository $roleRepository
31+
* @param UserRepository $userRepository,
32+
* @param EntityManagerInterface $entityManager
33+
*/
34+
public function __construct(RoleRepository $roleRepository, UserRepository $userRepository, EntityManagerInterface $entityManager)
2335
{
2436
$this->roleRepository = $roleRepository;
2537
$this->userRepository = $userRepository;
38+
$this->entityManager = $entityManager;
2639
}
2740

2841
/**
@@ -52,9 +65,9 @@ public function run()
5265
$p = entity(Profile::class)->make(['user' => $user]);
5366
break;
5467
}
55-
EntityManager::persist($p);
68+
$this->entityManager->persist($p);
5669
}
5770
}
58-
EntityManager::flush();
71+
$this->entityManager->flush();
5972
}
6073
}

database/seeds/UserTableSeeder.php

+13-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use HMS\Auth\PasswordStore;
77
use Illuminate\Database\Seeder;
88
use HMS\Repositories\RoleRepository;
9-
use LaravelDoctrine\ORM\Facades\EntityManager;
9+
use Doctrine\ORM\EntityManagerInterface;
1010

1111
class UserTableSeeder extends Seeder
1212
{
@@ -35,16 +35,23 @@ class UserTableSeeder extends Seeder
3535
*/
3636
protected $passwordStore;
3737

38+
/**
39+
* @var EntityManagerInterface
40+
*/
41+
protected $entityManager;
42+
3843
/**
3944
* Create a new TableSeeder instance.
4045
*
4146
* @param RoleRepository $roleRepository
4247
* @param PasswordStore $passwordStore
48+
* @param EntityManagerInterface $entityManager
4349
*/
44-
public function __construct(RoleRepository $roleRepository, PasswordStore $passwordStore)
50+
public function __construct(RoleRepository $roleRepository, PasswordStore $passwordStore, EntityManagerInterface $entityManager)
4551
{
4652
$this->roleRepository = $roleRepository;
4753
$this->passwordStore = $passwordStore;
54+
$this->entityManager = $entityManager;
4855
}
4956

5057
/**
@@ -75,7 +82,7 @@ public function run()
7582
->each(function ($u) {
7683
$u->getRoles()->add($this->roleRepository->findOneByName(Role::MEMBER_CURRENT));
7784
$this->passwordStore->add($u->getUsername(), 'password');
78-
EntityManager::persist($u);
85+
$this->entityManager->persist($u);
7986
});
8087

8188
// create all the other types
@@ -85,7 +92,7 @@ public function run()
8592
->each(function ($u) use ($role) {
8693
$u->getRoles()->add($this->roleRepository->findOneByName($role));
8794
$this->passwordStore->add($u->getUsername(), 'password');
88-
EntityManager::persist($u);
95+
$this->entityManager->persist($u);
8996
});
9097
}
9198

@@ -95,9 +102,9 @@ public function run()
95102
$admin->getRoles()->add($this->roleRepository->findOneByName(Role::SUPERUSER));
96103
$admin->setEmailVerifiedAt(new Carbon);
97104
$this->passwordStore->add($admin->getUsername(), 'admin');
98-
EntityManager::persist($admin);
105+
$this->entityManager->persist($admin);
99106
}
100107

101-
EntityManager::flush();
108+
$this->entityManager->flush();
102109
}
103110
}

dev/reseed.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ mysql -uroot -proot mailserver -e "DELETE FROM alias"
66
mysql -uroot -proot mailserver -e "DELETE FROM mailbox"
77

88
php artisan migrate:reset
9-
php artisan doctrine:migration:refresh
9+
php artisan doctrine:migrations:refresh
1010
php artisan migrate
1111
php artisan hms:database:refresh-views
1212
php artisan hms:database:refresh-procedures
13-
php artisan permission:defaults
13+
php artisan permissions:defaults
1414
php artisan db:seed
1515
php artisan passport:install

routes/console.php

+20
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,23 @@
2121
Artisan::command('hms:auto-deploy', function () {
2222
PostGitDeployedJob::dispatchNow();
2323
})->describe('Run the GitDeployedJob');
24+
25+
Artisan::command('hms:reseed {--force}', function ($force) {
26+
if (! App::environment('local') && ! $force) {
27+
// The environment is not local
28+
$this->error('Abort, not local environment (use --force to override)');
29+
return;
30+
}
31+
32+
exec('mysql -uroot -proot mailserver -e "DELETE FROM alias"');
33+
exec('mysql -uroot -proot mailserver -e "DELETE FROM mailbox"');
34+
35+
$this->call('migrate:reset');
36+
$this->call('doctrine:migrations:refresh');
37+
$this->call('migrate');
38+
$this->call('hms:database:refresh-views');
39+
$this->call('hms:database:refresh-procedures');
40+
$this->call('permissions:defaults');
41+
$this->call('db:seed');
42+
$this->call('passport:install');
43+
})->describe('Reseed the database');

0 commit comments

Comments
 (0)