diff --git a/app/resources/locales/en_US/command.po b/app/resources/locales/en_US/command.po index 02c58a081..332654692 100644 --- a/app/resources/locales/en_US/command.po +++ b/app/resources/locales/en_US/command.po @@ -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" diff --git a/app/src/Command/DatabaseCommand.php b/app/src/Command/DatabaseCommand.php index 8f8578f17..54362b010 100644 --- a/app/src/Command/DatabaseCommand.php +++ b/app/src/Command/DatabaseCommand.php @@ -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 diff --git a/app/src/Command/SetupCommand.php b/app/src/Command/SetupCommand.php index d3e3a8deb..e8218d55c 100644 --- a/app/src/Command/SetupCommand.php +++ b/app/src/Command/SetupCommand.php @@ -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(); diff --git a/app/src/Command/TestCommand.php b/app/src/Command/TestCommand.php index 85bf32b20..6040f1001 100644 --- a/app/src/Command/TestCommand.php +++ b/app/src/Command/TestCommand.php @@ -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; } } \ No newline at end of file diff --git a/app/src/Model/Table/CosTable.php b/app/src/Model/Table/CosTable.php index c126c03b2..061e964b8 100644 --- a/app/src/Model/Table/CosTable.php +++ b/app/src/Model/Table/CosTable.php @@ -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']); }