From 242278f42448051ade46d392e08b667a03efefd7 Mon Sep 17 00:00:00 2001 From: Arushad Ahmed Date: Thu, 30 May 2024 01:19:07 +0500 Subject: [PATCH] - Adding Laravel 9 support --- src/BuilderMacros.php | 43 -------------------------------- src/SchemaServiceProvider.php | 29 +++++++++++++++++++-- tests/Feature/BlueprintTest.php | 8 +++++- tests/Unit/BuilderMacrosTest.php | 26 ------------------- 4 files changed, 34 insertions(+), 72 deletions(-) delete mode 100644 src/BuilderMacros.php delete mode 100644 tests/Unit/BuilderMacrosTest.php diff --git a/src/BuilderMacros.php b/src/BuilderMacros.php deleted file mode 100644 index c49cbef..0000000 --- a/src/BuilderMacros.php +++ /dev/null @@ -1,43 +0,0 @@ -getColumns($table); - - foreach ($column_info as $col) { - if ($col['name'] == $column) { - return $col['comment']; - } - } - - if ($fail_on_missing) { - throw new InvalidArgumentException(sprintf("No such column [%s] in the table [%s].", $column, $table)); - } - - return null; - } - - public static function getTableComment(Builder $builder, string $table, bool $fail_on_missing = false): ?string - { - $table_info = $builder->getTables(); - - foreach ($table_info as $info) { - if ($info['name'] == $table) { - return $info['comment']; - } - } - - if ($fail_on_missing) { - throw new InvalidArgumentException(sprintf("No such table [%s] in the database [%s].", $table, $builder->getConnection()->getDatabaseName())); - } - - return null; - } -} diff --git a/src/SchemaServiceProvider.php b/src/SchemaServiceProvider.php index ccac9ea..84e3a61 100644 --- a/src/SchemaServiceProvider.php +++ b/src/SchemaServiceProvider.php @@ -5,6 +5,7 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Builder; use Illuminate\Support\ServiceProvider; +use InvalidArgumentException; class SchemaServiceProvider extends ServiceProvider { @@ -19,12 +20,36 @@ protected function registerBuilderMacros() Builder::macro('getColumnComment', function (string $table, string $column, bool $fail_on_missing = false) { /** @var $this Builder */ - return BuilderMacros::getColumnComment($this, $table, $column, $fail_on_missing); + $column_info = $this->getColumns($table); + + foreach ($column_info as $col) { + if ($col['name'] == $column) { + return $col['comment']; + } + } + + if ($fail_on_missing) { + throw new InvalidArgumentException(sprintf("No such column [%s] in the table [%s].", $column, $table)); + } + + return null; }); Builder::macro('getTableComment', function (string $table, bool $fail_on_missing = false) { /** @var $this Builder */ - return BuilderMacros::getTableComment($this, $table, $fail_on_missing); + $table_info = $this->getTables(); + + foreach ($table_info as $info) { + if ($info['name'] == $table) { + return $info['comment']; + } + } + + if ($fail_on_missing) { + throw new InvalidArgumentException(sprintf("No such table [%s] in the database [%s].", $table, $builder->getConnection()->getDatabaseName())); + } + + return null; }); } diff --git a/tests/Feature/BlueprintTest.php b/tests/Feature/BlueprintTest.php index 0c8292d..433a7ed 100644 --- a/tests/Feature/BlueprintTest.php +++ b/tests/Feature/BlueprintTest.php @@ -3,6 +3,7 @@ namespace Javaabu\Schema\Tests\Feature; use Illuminate\Foundation\Testing\RefreshDatabase; +use Illuminate\Support\Facades\Schema; use Javaabu\Schema\Tests\TestCase; use Javaabu\Schema\Tests\TestSupport\Enums\CityStatus; @@ -13,6 +14,11 @@ class BlueprintTest extends TestCase /** @test */ public function it_can_create_an_enum_column(): void { - $this->assertDatabaseColumnHasComment('cities', 'status', 'enum:' . CityStatus::class); + $this->assertTrue(Schema::hasColumn('cities', 'status')); + $this->assertEquals('varchar', Schema::getColumnType('cities', 'status')); + + if (! static::isLaravel9()) { + $this->assertDatabaseColumnHasComment('cities', 'status', 'enum:' . CityStatus::class); + } } } diff --git a/tests/Unit/BuilderMacrosTest.php b/tests/Unit/BuilderMacrosTest.php deleted file mode 100644 index 5b47d6b..0000000 --- a/tests/Unit/BuilderMacrosTest.php +++ /dev/null @@ -1,26 +0,0 @@ -assertEquals('enum:' . CityStatus::class, BuilderMacros::getColumnComment(app()->make(Builder::class), 'cities', 'status')); - } - - /** @test */ - public function it_can_get_table_comments(): void - { - $this->assertEquals('files', BuilderMacros::getTableComment(app()->make(Builder::class), 'cities')); - } -}