Skip to content

Commit

Permalink
Fix refactoring of setup command (NOJIRA)
Browse files Browse the repository at this point in the history
  • Loading branch information
Benn Oshrin committed Aug 12, 2024
1 parent 36d82a0 commit b24f06f
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 12 deletions.
2 changes: 1 addition & 1 deletion app/resources/locales/en_US/command.po
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ msgid "se.db.co"
msgstr "Creating COmanage CO"

msgid "se.db.co.done"
msgstr "COmanage CO created - CO Id: {0}"
msgstr "COmanage CO created and assigned ID {0}"

msgid "se.done"
msgstr "Done"
Expand Down
3 changes: 3 additions & 0 deletions app/src/Command/DatabaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ public function execute(Arguments $args, ConsoleIo $io) {
diffOnly: $args->getOption('not'));

// Next see which plugins are active and have database configurations
// Note running database -n on a brand new (empty) database will throw
// a stack trace here, since the plugins table doesn't exist. But we don't
// want to skip this with -n since most of the time the database won't be empty.
$Plugins = TableRegistry::getTableLocator()->get('Plugins');

// AR-Plugin-6 Only apply schemas from active plugins
Expand Down
24 changes: 21 additions & 3 deletions app/src/Command/SetupCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,27 @@ public function execute(Arguments $args, ConsoleIo $io)
$username = $io->ask(__d('command', 'opt.admin-username'));
}

// Setup the COmanage CO.

if(is_null($comanageCO)) {
// Setup the COmanage CO, but first check if it already exists.

$coTable = $this->getTableLocator()->get('Cos');

$coExists = $this->executeCommand(TestCommand::class, ['-t', 'setup']) === static::CODE_ERROR;
$co_id = null;

if($coExists) {
if(!$force) {
// TestCommand already echoed an error
$this->abort(static::CODE_ERROR);
}

// Pull the CO ID and keep going. We're basically reimplementing the same logic
// that was refactored into TestCommand because we need the CO ID.

$comanageCO = $coTable->find('COmanageCO')->first();
$co_id = $comanageCO->id;
}

if(is_null($co_id)) {
$io->out(__d('command', 'se.db.co'));
$co_id = $coTable->setupCOmanageCO();

Expand Down
10 changes: 5 additions & 5 deletions app/src/Command/TestCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,25 +144,25 @@ protected function testMail(int $recipient): int {
}

/**
* Test COmanage setup
* Test COmanage setup. Primarily intended for use by SetupCommand.
*
* @since COmanage Registry v5.0.0
* @return int Return Code (CODE_SUCCESS or CODE_ERROR)
* @return int CODE_SUCCESS if the CO does NOT exist or CODE_ERROR if it does
*/

protected function testSetup(): int {

// Check if the COmanage CO already exists, and if so abort.

$coTable = $this->getTableLocator()->get('Cos');
$query = $coTable->find();
$comanageCO = $coTable->findCOmanageCO($query)->first();
$comanageCO = $coTable->find('COmanageCO')->first();

if($comanageCO !== null) {
$this->io->out(__d('command', 'se.already'));
$this->abort(static::CODE_ERROR);
}

// Because this is primarily intended for use by SetupCommand, we want to return
// SUCCESS if there is no COmanage CO, ie it is OK to proceed.
return static::CODE_SUCCESS;
}
}
7 changes: 4 additions & 3 deletions app/src/Model/Table/CosTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,11 +281,12 @@ public function duplicate($id) {
* Find the COmanage CO.
*
* @since COmanage Registry v5.0.0
* @param \Cake\ORM\Query $query Query
* @return \Cake\ORM\Query Query
* @param \Cake\ORM\Query $query Query
* @param array $options Query options
* @return \Cake\ORM\Query Query
*/

public function findCOmanageCO(Query $query): Query {
public function findCOmanageCO(Query $query, array $options): Query {
return $query->where(['lower(name)' => 'comanage']);
}

Expand Down

0 comments on commit b24f06f

Please sign in to comment.