Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removing the logic for sorting tables from the Atomizer #50

Merged
merged 4 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci-mssql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
matrix:
include:
- php: '8.1'
extensions: pdo, pdo_sqlsrv-5.10.0beta2
extensions: pdo, pdo_sqlsrv
mssql: 'server:2019-latest'

services:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
php-version: ${{ matrix.php-versions }}
coverage: pcov
tools: pecl
extensions: mbstring, pdo, pdo_sqlite, pdo_pgsql, pdo_sqlsrv-5.10.0beta2, pdo_mysql
extensions: mbstring, pdo, pdo_sqlite, pdo_pgsql, pdo_sqlsrv, pdo_mysql
- name: Get Composer Cache Directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
Expand Down
38 changes: 14 additions & 24 deletions src/Atomizer/Atomizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Cycle\Migrations\Atomizer;

use Cycle\Database\Schema\AbstractTable;
use Cycle\Database\Schema\Reflector;
use Spiral\Reactor\Partial\Method;

/**
Expand All @@ -18,7 +17,8 @@ final class Atomizer
protected array $tables = [];

public function __construct(
private readonly RendererInterface $renderer
private readonly RendererInterface $renderer,
private readonly TableSorter $tableSorter = new TableSorter()
) {
}

Expand All @@ -32,6 +32,16 @@ public function addTable(AbstractTable $table): self
return $this;
}

/**
* @param AbstractTable[] $tables
*/
public function setTables(array $tables): self
{
$this->tables = $tables;

return $this;
}

/**
* Get all atomizer tables.
*
Expand All @@ -47,7 +57,7 @@ public function getTables(): array
*/
public function declareChanges(Method $method): void
{
foreach ($this->sortedTables() as $table) {
foreach ($this->tableSorter->sort($this->tables) as $table) {
if (!$table->getComparator()->hasChanges()) {
continue;
}
Expand All @@ -65,7 +75,7 @@ public function declareChanges(Method $method): void
*/
public function revertChanges(Method $method): void
{
foreach ($this->sortedTables(true) as $table) {
foreach ($this->tableSorter->sort($this->tables, true) as $table) {
if (!$table->getComparator()->hasChanges()) {
continue;
}
Expand All @@ -77,24 +87,4 @@ public function revertChanges(Method $method): void
}
}
}

/**
* Tables sorted in order of their dependencies.
*
* @return AbstractTable[]
*/
protected function sortedTables($reverse = false): array
{
$reflector = new Reflector();
foreach ($this->tables as $table) {
$reflector->addTable($table);
}

$sorted = $reflector->sortedTables();
if ($reverse) {
return \array_reverse($sorted);
}

return $sorted;
}
}
33 changes: 33 additions & 0 deletions src/Atomizer/TableSorter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Cycle\Migrations\Atomizer;

use Cycle\Database\Schema\AbstractTable;
use Cycle\Database\Schema\Reflector;

final class TableSorter
{
/**
* Tables sorted in order of their dependencies.
*
* @param AbstractTable[] $tables
*
* @return AbstractTable[]
*/
public function sort(array $tables, $reverse = false): array
{
$reflector = new Reflector();
foreach ($tables as $table) {
$reflector->addTable($table);
}

$sorted = $reflector->sortedTables();
if ($reverse) {
return \array_reverse($sorted);
}

return $sorted;
}
}
18 changes: 18 additions & 0 deletions tests/Migrations/AtomizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
namespace Cycle\Migrations\Tests;

use Cycle\Database\Injection\Fragment;
use Cycle\Database\Schema\AbstractTable;
use Cycle\Migrations\Atomizer\Atomizer;
use Cycle\Migrations\Atomizer\Renderer;
use Cycle\Migrations\Migration;
use Cycle\Migrations\State;

Expand Down Expand Up @@ -428,4 +431,19 @@ public function testChangeColumnScale(): void
$this->migrator->rollback();
$this->assertFalse($this->db->hasTable('sample'));
}

public function testSetTables(): void
{
$atomizer = new Atomizer(new Renderer());

$table1 = $this->createMock(AbstractTable::class);
$table2 = $this->createMock(AbstractTable::class);
$table3 = $this->createMock(AbstractTable::class);

$atomizer->addTable($table1);
$this->assertSame([$table1], $atomizer->getTables());

$atomizer->setTables([$table2, $table3]);
$this->assertSame([$table2, $table3], $atomizer->getTables());
}
}
10 changes: 10 additions & 0 deletions tests/Migrations/MySQL/TableSorterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Cycle\Migrations\Tests\MySQL;

final class TableSorterTest extends \Cycle\Migrations\Tests\TableSorterTest
{
public const DRIVER = 'mysql';
}
10 changes: 10 additions & 0 deletions tests/Migrations/Postgres/TableSorterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Cycle\Migrations\Tests\Postgres;

final class TableSorterTest extends \Cycle\Migrations\Tests\TableSorterTest
{
public const DRIVER = 'postgres';
}
10 changes: 10 additions & 0 deletions tests/Migrations/SQLServer/TableSorterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Cycle\Migrations\Tests\SQLServer;

final class TableSorterTest extends \Cycle\Migrations\Tests\TableSorterTest
{
public const DRIVER = 'sqlserver';
}
10 changes: 10 additions & 0 deletions tests/Migrations/SQLite/TableSorterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Cycle\Migrations\Tests\SQLite;

final class TableSorterTest extends \Cycle\Migrations\Tests\TableSorterTest
{
public const DRIVER = 'sqlite';
}
54 changes: 54 additions & 0 deletions tests/Migrations/TableSorterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

namespace Cycle\Migrations\Tests;

use Cycle\Migrations\Atomizer\TableSorter;

abstract class TableSorterTest extends BaseTest
{
public function testSortWithoutDeps(): void
{
$sorter = new TableSorter();
$table1 = $this->schema('table1');
$table1->primary('id');
$table1->integer('value');

$table2 = $this->schema('table2');
$table2->primary('id');
$table1->integer('value');

$this->assertSame([$table1, $table2], $sorter->sort([$table1, $table2]));
}

public function testSortWithCorrectOrder(): void
{
$sorter = new TableSorter();
$table1 = $this->schema('table1');
$table1->primary('id');
$table1->integer('value');

$table2 = $this->schema('table2');
$table2->primary('id');
$table2->integer('sample_id');
$table2->foreignKey(['sample_id'])->references('table1', ['id']);

$this->assertSame([$table1, $table2], $sorter->sort([$table1, $table2]));
}

public function testSortWithIncorrectOrder(): void
{
$sorter = new TableSorter();
$table1 = $this->schema('table1');
$table1->primary('id');
$table1->integer('sample_id');
$table1->foreignKey(['sample_id'])->references('table2', ['id']);

$table2 = $this->schema('table2');
$table2->primary('id');
$table2->integer('value');

$this->assertSame([$table2, $table1], $sorter->sort([$table1, $table2]));
}
}
Loading