Skip to content

Commit

Permalink
Merge 67a5bc0 into 4298834
Browse files Browse the repository at this point in the history
  • Loading branch information
pvsaintpe committed Jul 20, 2021
2 parents 4298834 + 67a5bc0 commit 98d17ef
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 0 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ composer require umbrellio/laravel-pg-extensions
- [Working with CHECK constraints](#check-constraints-creation)
- [Working with partitions](#partitions)
- [Check existing index before manipulation](#check-existing-index)
- [Getting foreign keys for table](#get-foreign-keys)

### Extended table creation

Expand Down Expand Up @@ -74,6 +75,21 @@ Schema::create('users', function (Blueprint $table) {
});
```

### Get foreign keys

Example:
```php
// Facade methods:
/** @var ForeignKeyDefinition[] $fks */
$fks = Schema::getForeignKeys('some_table');

foreach ($fks as $fk) {
// $fk->source_column_name
// $fk->target_table_name
// $fk->target_column_name
}
```

### Extended unique indexes creation

Example:
Expand Down
11 changes: 11 additions & 0 deletions src/.meta.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
<?php

namespace Illuminate\Support\Facades {
use Umbrellio\Postgres\Schema\Definitions\ForeignKeyDefinition;

/**
* @method ForeignKeyDefinition[] getForeignKeys(string $tableName)
*/
class Schema {

}
}

namespace Illuminate\Database\Schema {

use Closure;
Expand Down
7 changes: 7 additions & 0 deletions src/Schema/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ public function hasView(string $view): bool
])) > 0;
}

public function getForeignKeys(string $tableName): array
{
return $this->connection->selectFromWriteConnection(
$this->grammar->compileForeignKeysListing($tableName)
);
}

public function getViewDefinition($view): string
{
$results = $this->connection->selectFromWriteConnection($this->grammar->compileViewDefinition(), [
Expand Down
13 changes: 13 additions & 0 deletions src/Schema/Definitions/ForeignKeyDefinition.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php declare(strict_types=1);


namespace Umbrellio\Postgres\Schema\Definitions;

/**
* @property string $source_column_name
* @property string $target_table_name
* @property string $target_column_name
*/
class ForeignKeyDefinition
{
}
19 changes: 19 additions & 0 deletions src/Schema/Grammars/PostgresGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,25 @@ public function compileViewExists(): string
return 'select * from information_schema.views where table_schema = ? and table_name = ?';
}

public function compileForeignKeysListing(string $tableName): string
{
return sprintf("
SELECT
kcu.column_name as source_column_name,
ccu.table_name AS target_table_name,
ccu.column_name AS target_column_name
FROM
information_schema.table_constraints AS tc
JOIN information_schema.key_column_usage AS kcu
ON tc.constraint_name = kcu.constraint_name
AND tc.table_schema = kcu.table_schema
JOIN information_schema.constraint_column_usage AS ccu
ON ccu.constraint_name = tc.constraint_name
AND ccu.table_schema = tc.table_schema
WHERE tc.constraint_type = 'FOREIGN KEY' AND tc.table_name='%s';
", $tableName);
}

public function compileViewDefinition(): string
{
return 'select view_definition from information_schema.views where table_schema = ? and table_name = ?';
Expand Down
75 changes: 75 additions & 0 deletions tests/Functional/Schema/GetForeignKeysTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

declare(strict_types=1);

namespace Umbrellio\Postgres\Tests\Functional;

use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\Schema;
use Umbrellio\Postgres\Helpers\ViewAssertions;
use Umbrellio\Postgres\Schema\Blueprint;
use Umbrellio\Postgres\Tests\FunctionalTestCase;

class GetForeignKeysTest extends FunctionalTestCase
{
use DatabaseTransactions;

protected function setUp(): void
{
parent::setUp();

Schema::create('test_foreign_table1', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
});

Schema::create('test_foreign_table2', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
});

Schema::create('test_table', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->bigInteger('test1_id');
$table->bigInteger('test2_id');
$table->foreign(['test1_id'])->references('id')->on('test_foreign_table1');
$table->foreign(['test2_id'])->references('id')->on('test_foreign_table2');
});
}

protected function tearDown(): void
{
Schema::dropIfExists('test_table');
Schema::dropIfExists('test_foreign_table1');
Schema::dropIfExists('test_foreign_table2');

parent::tearDown();
}

/**
* @test
*/
public function getForeignKeys(): void
{
$foreignKeys = Schema::getForeignKeys('test_table');

$this->assertSame(
(array) $foreignKeys[0],
[
'source_column_name' => 'test1_id',
'target_table_name' => 'test_foreign_table1',
'target_column_name' => 'id',
]
);

$this->assertSame(
(array) $foreignKeys[1],
[
'source_column_name' => 'test2_id',
'target_table_name' => 'test_foreign_table2',
'target_column_name' => 'id',
]
);
}
}

0 comments on commit 98d17ef

Please sign in to comment.