Skip to content

Commit

Permalink
Merge pull request #58 from leepeuker/improve-cli-database-management
Browse files Browse the repository at this point in the history
Improve cli database management
  • Loading branch information
leepeuker authored Jul 15, 2022
2 parents aa22482 + 1566456 commit a977f74
Show file tree
Hide file tree
Showing 10 changed files with 153 additions and 73 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ volumes:
## Important: First steps
- Run database migrations: `docker exec movary php bin/console.php database:migration --migrate`
- Run database migrations: `docker exec movary php bin/console.php database:migration:migrate`
- Create a user: `docker exec movary php bin/console.php user:create [email protected] your-password`

List all available cli commands: `docker exec movary php bin/console.php`
Expand Down
4 changes: 3 additions & 1 deletion bin/console.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
$application->add($container->get(Movary\Command\SyncTrakt::class));
$application->add($container->get(Movary\Command\SyncTmdb::class));
// $application->add($container->get(Movary\Command\SyncLetterboxd::class));
$application->add($container->get(Movary\Command\DatabaseMigration::class));
$application->add($container->get(Movary\Command\DatabaseMigrationStatus::class));
$application->add($container->get(Movary\Command\DatabaseMigrationMigrate::class));
$application->add($container->get(Movary\Command\DatabaseMigrationRollback::class));
$application->add($container->get(Movary\Command\UserCreate::class));
$application->add($container->get(Movary\Command\UserDelete::class));
$application->add($container->get(Movary\Command\UserUpdate::class));
Expand Down
4 changes: 3 additions & 1 deletion bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
\Movary\Api\Tmdb\Client::class => DI\factory([Factory::class, 'createTmdbApiClient']),
\Movary\HttpController\SettingsController::class => DI\factory([Factory::class, 'createSettingsController']),
\Movary\ValueObject\Http\Request::class => DI\factory([Factory::class, 'createCurrentHttpRequest']),
\Movary\Command\DatabaseMigration::class => DI\factory([Factory::class, 'createDatabaseMigrationCommand']),
\Movary\Command\DatabaseMigrationStatus::class => DI\factory([Factory::class, 'createDatabaseMigrationStatusCommand']),
\Movary\Command\DatabaseMigrationMigrate::class => DI\factory([Factory::class, 'createDatabaseMigrationMigrateCommand']),
\Movary\Command\DatabaseMigrationRollback::class => DI\factory([Factory::class, 'createDatabaseMigrationRollbackCommand']),
\Psr\Http\Client\ClientInterface::class => DI\factory([Factory::class, 'createHttpClient']),
\Psr\Log\LoggerInterface::class => DI\factory([Factory::class, 'createFileLogger']),
\Doctrine\DBAL\Connection::class => DI\factory([Factory::class, 'createDbConnection']),
Expand Down
24 changes: 13 additions & 11 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 0 additions & 56 deletions src/Command/DatabaseMigration.php

This file was deleted.

38 changes: 38 additions & 0 deletions src/Command/DatabaseMigrationMigrate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php declare(strict_types=1);

namespace Movary\Command;

use Phinx\Console\PhinxApplication;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class DatabaseMigrationMigrate extends Command
{
protected static $defaultName = 'database:migration:migrate';

public function __construct(
private readonly PhinxApplication $phinxApplication,
private readonly string $phinxConfigurationFile
) {
parent::__construct();
}

protected function configure() : void
{
$this->setDescription('Execute missing database migrations.');
}

// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter
protected function execute(InputInterface $input, OutputInterface $output) : int
{
$command = $this->phinxApplication->find('migrate');

$arguments = [
'command' => $command,
'--configuration' => $this->phinxConfigurationFile,
];

return $command->run(new ArrayInput($arguments), $output);
}
}
38 changes: 38 additions & 0 deletions src/Command/DatabaseMigrationRollback.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php declare(strict_types=1);

namespace Movary\Command;

use Phinx\Console\PhinxApplication;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class DatabaseMigrationRollback extends Command
{
protected static $defaultName = 'database:migration:rollback';

public function __construct(
private readonly PhinxApplication $phinxApplication,
private readonly string $phinxConfigurationFile
) {
parent::__construct();
}

protected function configure() : void
{
$this->setDescription('Rollback last database migration.');
}

// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter
protected function execute(InputInterface $input, OutputInterface $output) : int
{
$command = $this->phinxApplication->find('rollback');

$arguments = [
'command' => $command,
'--configuration' => $this->phinxConfigurationFile,
];

return $command->run(new ArrayInput($arguments), $output);
}
}
38 changes: 38 additions & 0 deletions src/Command/DatabaseMigrationStatus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php declare(strict_types=1);

namespace Movary\Command;

use Phinx\Console\PhinxApplication;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class DatabaseMigrationStatus extends Command
{
protected static $defaultName = 'database:migration:status';

public function __construct(
private readonly PhinxApplication $phinxApplication,
private readonly string $phinxConfigurationFile
) {
parent::__construct();
}

protected function configure() : void
{
$this->setDescription('Status of database migrations.');
}

// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter
protected function execute(InputInterface $input, OutputInterface $output) : int
{
$command = $this->phinxApplication->find('status');

$arguments = [
'command' => $command,
'--configuration' => $this->phinxConfigurationFile,
];

return $command->run(new ArrayInput($arguments), $output);
}
}
2 changes: 1 addition & 1 deletion src/Command/SyncTrakt.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function __construct(

protected function configure() : void
{
$this->setDescription('Sync trakt.tv movie history and rating with local database')
$this->setDescription('Sync trakt.tv movie history and rating with local database.')
->addOption(self::OPTION_NAME_USER_ID, [], InputOption::VALUE_REQUIRED, 'Id of user to sync to.')
->addOption(self::OPTION_NAME_HISTORY, [], InputOption::VALUE_NONE, 'Sync movie history.')
->addOption(self::OPTION_NAME_RATINGS, [], InputOption::VALUE_NONE, 'Sync movie ratings.')
Expand Down
20 changes: 18 additions & 2 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,25 @@ public static function createCurrentHttpRequest() : Request
return Request::createFromGlobals();
}

public static function createDatabaseMigrationCommand(ContainerInterface $container) : Command\DatabaseMigration
public static function createDatabaseMigrationMigrateCommand(ContainerInterface $container) : Command\DatabaseMigrationMigrate
{
return new Command\DatabaseMigration(
return new Command\DatabaseMigrationMigrate(
$container->get(PhinxApplication::class),
__DIR__ . '/../settings/phinx.php'
);
}

public static function createDatabaseMigrationRollbackCommand(ContainerInterface $container) : Command\DatabaseMigrationRollback
{
return new Command\DatabaseMigrationRollback(
$container->get(PhinxApplication::class),
__DIR__ . '/../settings/phinx.php'
);
}

public static function createDatabaseMigrationStatusCommand(ContainerInterface $container) : Command\DatabaseMigrationStatus
{
return new Command\DatabaseMigrationStatus(
$container->get(PhinxApplication::class),
__DIR__ . '/../settings/phinx.php'
);
Expand Down

0 comments on commit a977f74

Please sign in to comment.