Skip to content

Commit

Permalink
Database: add new artisan hms:reseed command
Browse files Browse the repository at this point in the history
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`
  • Loading branch information
dpslwk committed May 21, 2019
1 parent 4b046a5 commit 3367500
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 13 deletions.
1 change: 1 addition & 0 deletions database/factories/UserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@
'rememberToken' => str_random(10),
'roles' => new ArrayCollection(),
'emailVerifiedAt' => Carbon::now(),
'google2faEnable' => false,
];
});
14 changes: 13 additions & 1 deletion database/seeds/DatabaseSeeder.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Illuminate\Database\Seeder;
use Doctrine\ORM\EntityManagerInterface;

class DatabaseSeeder extends Seeder
{
Expand All @@ -9,18 +10,29 @@ class DatabaseSeeder extends Seeder
*
* @return void
*/
public function run()
public function run(EntityManagerInterface $entityManager)
{
$this->call(UserTableSeeder::class);
$entityManager->clear();
$this->call(ProfileTableSeeder::class);
$entityManager->clear();
$this->call(AccountTableSeeder::class);
$entityManager->clear();
$this->call(PinTableSeeder::class);
$entityManager->clear();
$this->call(RfidTagTableSeeder::class);
$entityManager->clear();
$this->call(AccessLogTableSeeder::class);
$entityManager->clear();
$this->call(BankTransactionTableSeeder::class);
$entityManager->clear();
$this->call(RoleUpdateTableSeeder::class);
$entityManager->clear();
$this->call(ProductTableSeeder::class);
$entityManager->clear();
$this->call(TransactionTableSeeder::class);
$entityManager->clear();
$this->call(ToolTableSeeder::class);
$entityManager->clear();
}
}
21 changes: 17 additions & 4 deletions database/seeds/ProfileTableSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Illuminate\Database\Seeder;
use HMS\Repositories\RoleRepository;
use HMS\Repositories\UserRepository;
use LaravelDoctrine\ORM\Facades\EntityManager;
use Doctrine\ORM\EntityManagerInterface;

class ProfileTableSeeder extends Seeder
{
Expand All @@ -19,10 +19,23 @@ class ProfileTableSeeder extends Seeder
*/
protected $userRepository;

public function __construct(RoleRepository $roleRepository, UserRepository $userRepository)
/**
* @var EntityManagerInterface
*/
protected $entityManager;

/**
* Create a new TableSeeder instance.
*
* @param RoleRepository $roleRepository
* @param UserRepository $userRepository,
* @param EntityManagerInterface $entityManager
*/
public function __construct(RoleRepository $roleRepository, UserRepository $userRepository, EntityManagerInterface $entityManager)
{
$this->roleRepository = $roleRepository;
$this->userRepository = $userRepository;
$this->entityManager = $entityManager;
}

/**
Expand Down Expand Up @@ -52,9 +65,9 @@ public function run()
$p = entity(Profile::class)->make(['user' => $user]);
break;
}
EntityManager::persist($p);
$this->entityManager->persist($p);
}
}
EntityManager::flush();
$this->entityManager->flush();
}
}
19 changes: 13 additions & 6 deletions database/seeds/UserTableSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use HMS\Auth\PasswordStore;
use Illuminate\Database\Seeder;
use HMS\Repositories\RoleRepository;
use LaravelDoctrine\ORM\Facades\EntityManager;
use Doctrine\ORM\EntityManagerInterface;

class UserTableSeeder extends Seeder
{
Expand Down Expand Up @@ -35,16 +35,23 @@ class UserTableSeeder extends Seeder
*/
protected $passwordStore;

/**
* @var EntityManagerInterface
*/
protected $entityManager;

/**
* Create a new TableSeeder instance.
*
* @param RoleRepository $roleRepository
* @param PasswordStore $passwordStore
* @param EntityManagerInterface $entityManager
*/
public function __construct(RoleRepository $roleRepository, PasswordStore $passwordStore)
public function __construct(RoleRepository $roleRepository, PasswordStore $passwordStore, EntityManagerInterface $entityManager)
{
$this->roleRepository = $roleRepository;
$this->passwordStore = $passwordStore;
$this->entityManager = $entityManager;
}

/**
Expand Down Expand Up @@ -75,7 +82,7 @@ public function run()
->each(function ($u) {
$u->getRoles()->add($this->roleRepository->findOneByName(Role::MEMBER_CURRENT));
$this->passwordStore->add($u->getUsername(), 'password');
EntityManager::persist($u);
$this->entityManager->persist($u);
});

// create all the other types
Expand All @@ -85,7 +92,7 @@ public function run()
->each(function ($u) use ($role) {
$u->getRoles()->add($this->roleRepository->findOneByName($role));
$this->passwordStore->add($u->getUsername(), 'password');
EntityManager::persist($u);
$this->entityManager->persist($u);
});
}

Expand All @@ -95,9 +102,9 @@ public function run()
$admin->getRoles()->add($this->roleRepository->findOneByName(Role::SUPERUSER));
$admin->setEmailVerifiedAt(new Carbon);
$this->passwordStore->add($admin->getUsername(), 'admin');
EntityManager::persist($admin);
$this->entityManager->persist($admin);
}

EntityManager::flush();
$this->entityManager->flush();
}
}
4 changes: 2 additions & 2 deletions dev/reseed.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ mysql -uroot -proot mailserver -e "DELETE FROM alias"
mysql -uroot -proot mailserver -e "DELETE FROM mailbox"

php artisan migrate:reset
php artisan doctrine:migration:refresh
php artisan doctrine:migrations:refresh
php artisan migrate
php artisan hms:database:refresh-views
php artisan hms:database:refresh-procedures
php artisan permission:defaults
php artisan permissions:defaults
php artisan db:seed
php artisan passport:install
21 changes: 21 additions & 0 deletions routes/console.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,24 @@
Artisan::command('hms:auto-deploy', function () {
PostGitDeployedJob::dispatchNow();
})->describe('Run the GitDeployedJob');

Artisan::command('hms:reseed {--force}', function ($force) {
if (! App::environment('local') && ! $force) {
// The environment is not local
$this->error('Abort, not local environment (use --force to override)');

return;
}

exec('mysql -uroot -proot mailserver -e "DELETE FROM alias"');
exec('mysql -uroot -proot mailserver -e "DELETE FROM mailbox"');

$this->call('migrate:reset');
$this->call('doctrine:migrations:refresh');
$this->call('migrate');
$this->call('hms:database:refresh-views');
$this->call('hms:database:refresh-procedures');
$this->call('permissions:defaults');
$this->call('db:seed');
$this->call('passport:install');
})->describe('Reseed the database');

0 comments on commit 3367500

Please sign in to comment.