Skip to content

Commit

Permalink
Merge branch '2.0' into 2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent4vx committed Jun 26, 2023
2 parents ca4008d + 76eda48 commit 375bf15
Show file tree
Hide file tree
Showing 19 changed files with 69 additions and 34 deletions.
2 changes: 1 addition & 1 deletion src/Entity/EntityGenerator.php
Expand Up @@ -196,8 +196,8 @@ public function generate(Mapper $mapper, ?string $file = null)
// If entity doesn't exist or we're re-generating the entities entirely
if ($this->isNew || !$file) {
return $this->generateEntityClass($mapper);
// If entity exists and we're allowed to update the entity class
} elseif ($this->updateEntityIfExists) {
// If entity exists and we're allowed to update the entity class
return $this->generateUpdatedEntityClass($mapper, $file);
}

Expand Down
2 changes: 2 additions & 0 deletions src/Migration/Console/CheckCommand.php
Expand Up @@ -3,6 +3,7 @@
namespace Bdf\Prime\Migration\Console;

use Bdf\Util\Console\BdfStyle;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Helper\TableSeparator;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand All @@ -11,6 +12,7 @@
/**
* CheckCommand
*/
#[AsCommand('prime:migration:check', 'Check all migrations have been run, exit with non-zero if not')]
class CheckCommand extends AbstractCommand
{
protected static $defaultName = 'prime:migration:check';
Expand Down
2 changes: 2 additions & 0 deletions src/Migration/Console/DownCommand.php
Expand Up @@ -3,13 +3,15 @@
namespace Bdf\Prime\Migration\Console;

use Bdf\Util\Console\BdfStyle;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Class DownCommand
*/
#[AsCommand('prime:migration:down', 'Revert a specific migration')]
class DownCommand extends AbstractCommand
{
protected static $defaultName = 'prime:migration:down';
Expand Down
2 changes: 2 additions & 0 deletions src/Migration/Console/GenerateCommand.php
Expand Up @@ -3,6 +3,7 @@
namespace Bdf\Prime\Migration\Console;

use Bdf\Prime\Migration\MigrationInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand All @@ -11,6 +12,7 @@
/**
* Class GenerateCommand
*/
#[AsCommand('prime:migration:generate', 'Generate a new migration')]
class GenerateCommand extends AbstractCommand
{
protected static $defaultName = 'prime:migration:generate';
Expand Down
2 changes: 2 additions & 0 deletions src/Migration/Console/InitCommand.php
Expand Up @@ -2,12 +2,14 @@

namespace Bdf\Prime\Migration\Console;

use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Class InitCommand
*/
#[AsCommand('prime:migration:init', 'Initialise this directory for use with migration')]
class InitCommand extends AbstractCommand
{
protected static $defaultName = 'prime:migration:init';
Expand Down
2 changes: 2 additions & 0 deletions src/Migration/Console/MigrateCommand.php
Expand Up @@ -4,6 +4,7 @@

use Bdf\Prime\Migration\MigrationInterface;
use Bdf\Util\Console\BdfStyle;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand All @@ -12,6 +13,7 @@
/**
* Class MigrateCommand
*/
#[AsCommand('prime:migration:migrate', 'Run all migrations')]
class MigrateCommand extends AbstractCommand
{
protected static $defaultName = 'prime:migration:migrate';
Expand Down
2 changes: 2 additions & 0 deletions src/Migration/Console/RedoCommand.php
Expand Up @@ -3,13 +3,15 @@
namespace Bdf\Prime\Migration\Console;

use Bdf\Util\Console\BdfStyle;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Class RedoCommand
*/
#[AsCommand('prime:migration:redo', 'Redo a specific migration')]
class RedoCommand extends AbstractCommand
{
protected static $defaultName = 'prime:migration:redo';
Expand Down
2 changes: 2 additions & 0 deletions src/Migration/Console/RollbackCommand.php
Expand Up @@ -4,6 +4,7 @@

use Bdf\Prime\Migration\MigrationInterface;
use Bdf\Util\Console\BdfStyle;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand All @@ -12,6 +13,7 @@
/**
* Class RollbackCommand
*/
#[AsCommand('prime:migration:rollback', 'Rollback last, or to a specific migration')]
class RollbackCommand extends AbstractCommand
{
protected static $defaultName = 'prime:migration:rollback';
Expand Down
2 changes: 2 additions & 0 deletions src/Migration/Console/StatusCommand.php
Expand Up @@ -3,6 +3,7 @@
namespace Bdf\Prime\Migration\Console;

use Bdf\Util\Console\BdfStyle;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Helper\TableSeparator;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand All @@ -11,6 +12,7 @@
/**
* Class StatusCommand
*/
#[AsCommand('prime:migration:status', 'Show the up/down status of all migrations')]
class StatusCommand extends AbstractCommand
{
protected static $defaultName = 'prime:migration:status';
Expand Down
2 changes: 2 additions & 0 deletions src/Migration/Console/UpCommand.php
Expand Up @@ -3,13 +3,15 @@
namespace Bdf\Prime\Migration\Console;

use Bdf\Util\Console\BdfStyle;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Class UpCommand
*/
#[AsCommand('prime:migration:up', 'Run a specific migration')]
class UpCommand extends AbstractCommand
{
protected static $defaultName = 'prime:migration:up';
Expand Down
6 changes: 1 addition & 5 deletions src/Query/Expression/Field.php
Expand Up @@ -3,7 +3,6 @@
namespace Bdf\Prime\Query\Expression;

use Bdf\Prime\Query\CompilableClause;
use Bdf\Prime\Query\Compiler\CompilerInterface;
use Bdf\Prime\Query\Compiler\QuoteCompilerInterface;

/**
Expand Down Expand Up @@ -48,10 +47,7 @@ public function __construct($search, array $values)
*/
public function build(CompilableClause $query, object $compiler)
{
// if ($compiler->platform()->name() === 'mysql') {
// @todo only mysql ?
return 'FIELD('.$compiler->quoteIdentifier($query, $query->preprocessor()->field($this->search)).','.implode(',', $this->values).')';
// }
//
// return '';
}
}
5 changes: 0 additions & 5 deletions src/Relations/BelongsToMany.php
Expand Up @@ -164,11 +164,6 @@ protected function extractConstraints($constraints)
*/
public function join(EntityJoinable $query, string $alias): void
{
// @fixme ??
// if ($alias === null) {
// $alias = $this->attributeAim;
// }

// TODO rechercher l'alias de through dans les tables alias du query builder

$query->joinEntity($this->through->entityName(), $this->throughLocal, $this->getLocalAlias($query).$this->localKey, $this->attributeAim.'Through');
Expand Down
5 changes: 0 additions & 5 deletions src/Relations/OneOrMany.php
Expand Up @@ -63,11 +63,6 @@ protected function applyConstraints(ReadCommandInterface $query, $constraints =
*/
public function join(EntityJoinable $query, string $alias): void
{
// @fixme ?
// if ($alias === null) {
// $alias = $this->attributeAim;
// }

$query->joinEntity($this->distant->entityName(), $this->distantKey, $this->getLocalAlias($query).$this->localKey, $alias);

// apply relation constraints
Expand Down
2 changes: 1 addition & 1 deletion src/Relations/RelationInterface.php
Expand Up @@ -25,7 +25,7 @@ interface RelationInterface
public const BY_INHERITANCE = 'byInheritance';
public const MORPH_TO = 'morphTo';
public const CUSTOM = 'custom';
// const MORPH_TO_MANY = 'morphToMany';
//const MORPH_TO_MANY = 'morphToMany';

/**
* Get the relation repository.
Expand Down
5 changes: 0 additions & 5 deletions src/Relations/Util/SimpleTableJoinRelation.php
Expand Up @@ -39,11 +39,6 @@ public function link($owner): ReadCommandInterface
*/
public function join(EntityJoinable $query, string $alias): void
{
// @fixme ?
// if ($alias === null) {
// $alias = $this->attributeAim;
// }

$query->joinEntity(
$this->distant->entityName(),
function (JoinClause $clause) use ($alias, $query) {
Expand Down
12 changes: 6 additions & 6 deletions src/Schema/Builder/TypesHelperTableBuilder.php
Expand Up @@ -352,12 +352,12 @@ public function boolean(string $name, ?bool $default = null)
*
* @return $this
*/
// public function enum($name, array $allowed)
// {
// $this->addTypeAsString($name, 'enum', ['allowed' => $allowed]);
//
// return $this;
// }
//public function enum($name, array $allowed)
//{
// $this->addTypeAsString($name, 'enum', ['allowed' => $allowed]);
//
// return $this;
//}

/**
* {@inheritdoc}
Expand Down
10 changes: 5 additions & 5 deletions src/Schema/Visitor/Graphviz.php
Expand Up @@ -44,11 +44,11 @@ public function acceptSchema(Schema $schema)
$this->output .= 'graph [fontname="helvetica", fontsize=12];' . "\n";
$this->output .= 'node [fontname="helvetica", fontsize=12];' . "\n";
$this->output .= 'edge [fontname="helvetica", fontsize=12];' . "\n";
// $this->output .= 'splines = true;' . "\n";
// $this->output .= 'overlap = false;' . "\n";
// $this->output .= 'outputorder=edgesfirst;'."\n";
// $this->output .= 'mindist = 0.6;' . "\n";
// $this->output .= 'sep = .2;' . "\n";
//$this->output .= 'splines = true;' . "\n";
//$this->output .= 'overlap = false;' . "\n";
//$this->output .= 'outputorder=edgesfirst;'."\n";
//$this->output .= 'mindist = 0.6;' . "\n";
//$this->output .= 'sep = .2;' . "\n";
}

/**
Expand Down
9 changes: 8 additions & 1 deletion src/Sharding/ShardingQuery.php
Expand Up @@ -54,9 +54,16 @@ protected function executeUpdate(string $type): int
try {
$distributionKey = $this->connection->getDistributionKey();

if (!$this->shardId && isset($this->statements['values']['data'][$distributionKey])) {
if ($this->shardId) {
$this->connection->useShard($this->shardId);
} elseif (isset($this->statements['values']['data'][$distributionKey])) {
$this->connection->pickShard($this->statements['values']['data'][$distributionKey]);
} elseif ($this->statements['where']) {
$this->explodeQueryClauses($this->statements['where'], $distributionKey);
} else {
// To keep the old behavior : use $this->shardId instead of null constant
// because falsy comparison is used instead of strict null comparison, so $this->shardId can be 0 or "0"
// which has a different meaning than null
$this->connection->useShard($this->shardId);
}

Expand Down
29 changes: 29 additions & 0 deletions tests/Sharding/ShardingQueryTest.php
Expand Up @@ -5,6 +5,7 @@
use Bdf\Prime\Connection\SimpleConnection;
use Bdf\Prime\PrimeTestCase;
use Bdf\Prime\Schema\Builder\TypesHelperTableBuilder;
use Doctrine\DBAL\Logging\DebugStack;
use PHPUnit\Framework\TestCase;

/**
Expand Down Expand Up @@ -149,6 +150,34 @@ public function test_pick_shard()
$this->assertSame(0, $this->connection->getShardConnection('shard2')->from('test')->count());
}

public function test_update_should_use_where_to_pick_shard()
{
$this->connection->getConfiguration()->setSQLLogger($logger = new DebugStack());

$this->query()->insert(['id' => 1, 'name' => 'John']);
$this->query()->insert(['id' => 2, 'name' => 'Mike']);

$this->assertCount(2, $logger->queries);

$this->query()->where('id', 1)->update(['name' => 'Jean']);
$this->assertCount(3, $logger->queries);

$this->assertSame('Jean', $this->query()->pickShard(1)->first()['name']);
}

public function test_update_should_priorize_data_to_where()
{
$this->query()->insert(['id' => 1, 'name' => 'John']);
$this->query()->insert(['id' => 2, 'name' => 'Mike']);

$this->query()->where('id', 1)->update(['id' => 4]);

$this->assertEquals([
['id' => 2, 'name' => 'Mike'],
['id' => 1, 'name' => 'John'],
], $this->query()->all());
}

/**
* @return ShardingQuery
*/
Expand Down

0 comments on commit 375bf15

Please sign in to comment.