Skip to content

Commit

Permalink
Merge pull request #259 from kitloong/feature/schema
Browse files Browse the repository at this point in the history
Get schema by schema name
  • Loading branch information
kitloong authored Feb 26, 2025
2 parents 542c416 + c212373 commit d04d666
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.3
php-version: 8.2
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, gd, redis, memcached
tools: composer:v2
coverage: none
Expand Down
3 changes: 2 additions & 1 deletion phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,11 @@
<rule ref="SlevomatCodingStandard.PHP.UselessParentheses"/>
<rule ref="SlevomatCodingStandard.PHP.UselessSemicolon"/>
<rule ref="SlevomatCodingStandard.Strings.DisallowVariableParsing"/>
<rule ref="SlevomatCodingStandard.TypeHints.ClassConstantTypeHint"/>
<!--<rule ref="SlevomatCodingStandard.TypeHints.DeclareStrictTypes"/>-->
<!--<rule ref="SlevomatCodingStandard.TypeHints.DisallowArrayTypeHintSyntax"/>-->
<!--<rule ref="SlevomatCodingStandard.TypeHints.DisallowMixedTypeHint"/>-->
<rule ref="SlevomatCodingStandard.TypeHints.DNFTypeHintFormat"/>
<rule ref="SlevomatCodingStandard.TypeHints.LongTypeHints"/>
<rule ref="SlevomatCodingStandard.TypeHints.NullTypeHintOnLastPosition"/>
<rule ref="SlevomatCodingStandard.TypeHints.NullableTypeForNullDefaultValue"/>
Expand All @@ -189,7 +191,6 @@
<rule ref="SlevomatCodingStandard.TypeHints.PropertyTypeHint"/>
<rule ref="SlevomatCodingStandard.TypeHints.ReturnTypeHint"/>
<rule ref="SlevomatCodingStandard.TypeHints.ReturnTypeHintSpacing"/>
<rule ref="SlevomatCodingStandard.TypeHints.UnionTypeHintFormat"/>
<rule ref="SlevomatCodingStandard.TypeHints.UselessConstantTypeHint"/>
<rule ref="SlevomatCodingStandard.Variables.DisallowVariableVariable"/>
<!--<rule ref="SlevomatCodingStandard.Variables.DisallowSuperGlobalVariable"/>-->
Expand Down
65 changes: 62 additions & 3 deletions src/Database/DatabaseSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Schema as SchemaFacade;
use KitLoong\MigrationsGenerator\Schema\Schema;
use KitLoong\MigrationsGenerator\Support\CheckLaravelVersion;
use KitLoong\MigrationsGenerator\Support\TableName;

/**
* @phpstan-type SchemaTable array{
* name: string,
* schema: ?string,
* schema_qualified_name: string,
* size: ?int,
* comment: ?string,
* collation: ?string,
Expand All @@ -20,6 +22,7 @@
* @phpstan-type SchemaView array{
* name: string,
* schema: ?string,
* schema_qualified_name: string,
* definition: string,
* }
*
Expand All @@ -32,12 +35,13 @@
* default: ?string,
* auto_increment: bool,
* comment: ?string,
* generation: ?array{type: 'stored'|'virtual', expression: string},
* }
*
* @phpstan-type SchemaIndex array{
* name: string,
* columns: string[],
* type: string,
* type: ?string,
* unique: bool,
* primary: bool,
* }
Expand All @@ -51,9 +55,20 @@
* on_update: string,
* on_delete: string,
* }
*
* @phpstan-type SchemaType array{
* name: string,
* schema: string,
* schema_qualified_name: string,
* implicit: bool,
* type: string,
* category: string,
* }
*/
abstract class DatabaseSchema implements Schema
{
use CheckLaravelVersion;

use TableName;

/**
Expand All @@ -66,7 +81,7 @@ abstract class DatabaseSchema implements Schema
*/
public function getTableNames(): Collection
{
return new Collection(array_column(SchemaFacade::getTables(), 'name'));
return new Collection(array_column($this->getSchemaTables(), 'name'));
}

/**
Expand All @@ -77,7 +92,7 @@ public function getTableNames(): Collection
protected function getSchemaTable(string $name): array
{
if ($this->tables === []) {
foreach (SchemaFacade::getTables() as $table) {
foreach ($this->getSchemaTables() as $table) {
/** @var SchemaTable $table */
$this->tables[$table['name']] = $table;
}
Expand All @@ -93,6 +108,10 @@ protected function getSchemaTable(string $name): array
*/
protected function getSchemaColumns(string $table): Collection
{
if ($this->atLeastLaravel12()) {
return new Collection(SchemaFacade::getColumns(SchemaFacade::getCurrentSchemaName() . '.' . $this->stripTablePrefix($table)));
}

return new Collection(SchemaFacade::getColumns($this->stripTablePrefix($table)));
}

Expand All @@ -103,6 +122,10 @@ protected function getSchemaColumns(string $table): Collection
*/
protected function getSchemaIndexes(string $table): Collection
{
if ($this->atLeastLaravel12()) {
return new Collection(SchemaFacade::getIndexes(SchemaFacade::getCurrentSchemaName() . '.' . $this->stripTablePrefix($table)));
}

return new Collection(SchemaFacade::getIndexes($this->stripTablePrefix($table)));
}

Expand All @@ -113,6 +136,10 @@ protected function getSchemaIndexes(string $table): Collection
*/
protected function getSchemaViews(): Collection
{
if ($this->atLeastLaravel12()) {
return new Collection(SchemaFacade::getViews(SchemaFacade::getCurrentSchemaName()));
}

return new Collection(SchemaFacade::getViews());
}

Expand All @@ -123,6 +150,38 @@ protected function getSchemaViews(): Collection
*/
protected function getSchemaForeignKeys(string $table): Collection
{
if ($this->atLeastLaravel12()) {
return new Collection(SchemaFacade::getForeignKeys(SchemaFacade::getCurrentSchemaName() . '.' . $this->stripTablePrefix($table)));
}

return new Collection(SchemaFacade::getForeignKeys($this->stripTablePrefix($table)));
}

/**
* Get all tables from the schema.
*
* @return SchemaTable[]
*/
protected function getSchemaTables(): array
{
if ($this->atLeastLaravel12()) {
return SchemaFacade::getTables(SchemaFacade::getCurrentSchemaName());
}

return SchemaFacade::getTables();
}

/**
* Get user defined types from the schema.
*
* @return SchemaType[]
*/
protected function getSchemaTypes(): array
{
if ($this->atLeastLaravel12()) {
return SchemaFacade::getTypes(SchemaFacade::getCurrentSchemaName());
}

return SchemaFacade::getTypes();
}
}
4 changes: 2 additions & 2 deletions src/Database/Models/DatabaseTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ public function __construct(array $table, Collection $columns, Collection $index
$this->comment = $table['comment'];
$this->collation = $table['collation'];

$this->columns = $columns->reduce(function ($columns, array $column) use ($userDefinedTypes) {
$this->columns = $columns->reduce(function (Collection $columns, array $column) use ($userDefinedTypes) {
if (!$userDefinedTypes->contains($column['type_name'])) {
$columns->push($this->makeColumn($this->name, $column));
}

return $columns;
}, new Collection())->values();

$this->udtColumns = $columns->reduce(function ($columns, array $column) use ($userDefinedTypes) {
$this->udtColumns = $columns->reduce(function (Collection $columns, array $column) use ($userDefinedTypes) {
if ($userDefinedTypes->contains($column['type_name'])) {
$columns->push($this->makeUDTColumn($this->name, $column));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Database/Models/SQLSrv/SQLSrvColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ private function getEnumPresetValues(): array
return $this->repository->getEnumPresetValues(
$this->tableName,
$this->name,
)->toArray();
)->all();
}

/**
Expand Down
8 changes: 5 additions & 3 deletions src/Database/PgSQLSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use KitLoong\MigrationsGenerator\Database\Models\PgSQL\PgSQLForeignKey;
use KitLoong\MigrationsGenerator\Database\Models\PgSQL\PgSQLProcedure;
use KitLoong\MigrationsGenerator\Database\Models\PgSQL\PgSQLTable;
Expand All @@ -13,9 +12,12 @@
use KitLoong\MigrationsGenerator\Repositories\PgSQLRepository;
use KitLoong\MigrationsGenerator\Schema\Models\Table;
use KitLoong\MigrationsGenerator\Schema\Models\View;
use KitLoong\MigrationsGenerator\Support\CheckLaravelVersion;

class PgSQLSchema extends DatabaseSchema
{
use CheckLaravelVersion;

/**
* @var \Illuminate\Support\Collection<int, string>
*/
Expand All @@ -33,7 +35,7 @@ public function __construct(private readonly PgSQLRepository $pgSQLRepository)
*/
public function getTableNames(): Collection
{
return (new Collection(Schema::getTables()))
return (new Collection($this->getSchemaTables()))
->filter(static function (array $table): bool {
if ($table['name'] === 'spatial_ref_sys') {
return false;
Expand Down Expand Up @@ -116,7 +118,7 @@ public function getForeignKeys(string $table): Collection
private function getUserDefinedTypes(): Collection
{
if (!$this->ranGetUserDefinedTypes) {
$this->userDefinedTypes = new Collection(array_column(Schema::getTypes(), 'name'));
$this->userDefinedTypes = new Collection(array_column($this->getSchemaTypes(), 'name'));
$this->ranGetUserDefinedTypes = true;
}

Expand Down
1 change: 1 addition & 0 deletions tests/Unit/Database/Models/PgSQL/PgSQLColumnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function testSpatialTypeName(string $type): void
'default' => null,
'auto_increment' => false,
'comment' => null,
'generation' => null,
]);

$this->assertSame(ColumnType::GEOGRAPHY, $column->getType());
Expand Down

0 comments on commit d04d666

Please sign in to comment.