Skip to content

Commit

Permalink
Add ability to avoid Index creation when ForeignKey is created (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
msmakouz authored May 16, 2023
1 parent 351e0be commit 177b191
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 2 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"license": "MIT",
"require": {
"php": ">=8.1",
"cycle/database": "^2.3",
"cycle/database": "^2.5",
"spiral/core": "^3.0",
"spiral/files": "^3.0",
"spiral/tokenizer": "^3.0",
Expand Down
1 change: 1 addition & 0 deletions src/Atomizer/Renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ private function foreignKeyOptions(AbstractForeignKey $reference): array
'name' => $reference->getName(),
'delete' => $reference->getDeleteRule(),
'update' => $reference->getUpdateRule(),
'indexCreate' => $reference->hasIndex(),
];
}

Expand Down
2 changes: 1 addition & 1 deletion src/Operation/ForeignKey/Add.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function execute(CapsuleInterface $capsule): void
}
}

$foreignKey = $schema->foreignKey($this->columns)->references(
$foreignKey = $schema->foreignKey($this->columns, $this->getOption('indexCreate', true))->references(
$this->foreignTable,
$this->foreignKeys
);
Expand Down
15 changes: 15 additions & 0 deletions tests/Migrations/BlueprintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,21 @@ public function testCreateWithForeignAliased(): void
$this->assertTrue($blueprint->getSchema()->exists());
}

public function testCreateForeignWithIndex(): void
{
$blueprint = new TableBlueprint(new Capsule($this->db), 'sample1');

$blueprint->addColumn('id', 'primary')->create();
$blueprint = new TableBlueprint(new Capsule($this->db), 'sample');

$blueprint->addColumn('id', 'primary')
->addColumn('sample_id', 'int')
->addForeignKey(['sample_id'], 'sample1', ['id'])
->create();

$this->assertTrue($blueprint->getSchema()->hasIndex(['sample_id']));
}

public function testUpdateTableError(): void
{
$this->expectException(\Cycle\Migrations\Exception\Operation\TableException::class);
Expand Down
18 changes: 18 additions & 0 deletions tests/Migrations/Postgres/BlueprintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,25 @@

namespace Cycle\Migrations\Tests\Postgres;

use Cycle\Migrations\Capsule;
use Cycle\Migrations\TableBlueprint;

class BlueprintTest extends \Cycle\Migrations\Tests\BlueprintTest
{
public const DRIVER = 'postgres';

public function testCreateForeignWithoutIndex(): void
{
$blueprint = new TableBlueprint(new Capsule($this->db), 'sample1');

$blueprint->addColumn('id', 'primary')->create();
$blueprint = new TableBlueprint(new Capsule($this->db), 'sample');

$blueprint->addColumn('id', 'primary')
->addColumn('sample_id', 'int')
->addForeignKey(['sample_id'], 'sample1', ['id'], ['indexCreate' => false])
->create();

$this->assertFalse($blueprint->getSchema()->hasIndex(['sample_id']));
}
}
18 changes: 18 additions & 0 deletions tests/Migrations/SQLServer/BlueprintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,25 @@

namespace Cycle\Migrations\Tests\SQLServer;

use Cycle\Migrations\Capsule;
use Cycle\Migrations\TableBlueprint;

class BlueprintTest extends \Cycle\Migrations\Tests\BlueprintTest
{
public const DRIVER = 'sqlserver';

public function testCreateForeignWithoutIndex(): void
{
$blueprint = new TableBlueprint(new Capsule($this->db), 'sample1');

$blueprint->addColumn('id', 'primary')->create();
$blueprint = new TableBlueprint(new Capsule($this->db), 'sample');

$blueprint->addColumn('id', 'primary')
->addColumn('sample_id', 'int')
->addForeignKey(['sample_id'], 'sample1', ['id'], ['indexCreate' => false])
->create();

$this->assertFalse($blueprint->getSchema()->hasIndex(['sample_id']));
}
}
18 changes: 18 additions & 0 deletions tests/Migrations/SQLite/BlueprintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,25 @@

namespace Cycle\Migrations\Tests\SQLite;

use Cycle\Migrations\Capsule;
use Cycle\Migrations\TableBlueprint;

class BlueprintTest extends \Cycle\Migrations\Tests\BlueprintTest
{
public const DRIVER = 'sqlite';

public function testCreateForeignWithoutIndex(): void
{
$blueprint = new TableBlueprint(new Capsule($this->db), 'sample1');

$blueprint->addColumn('id', 'primary')->create();
$blueprint = new TableBlueprint(new Capsule($this->db), 'sample');

$blueprint->addColumn('id', 'primary')
->addColumn('sample_id', 'int')
->addForeignKey(['sample_id'], 'sample1', ['id'], ['indexCreate' => false])
->create();

$this->assertFalse($blueprint->getSchema()->hasIndex(['sample_id']));
}
}

0 comments on commit 177b191

Please sign in to comment.