Skip to content

Commit

Permalink
- Added failure on column missing
Browse files Browse the repository at this point in the history
  • Loading branch information
dash8x committed May 29, 2024
1 parent 1d4b29f commit 7cd6c0c
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 27 deletions.
13 changes: 11 additions & 2 deletions src/SchemaServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Builder;
use Illuminate\Support\ServiceProvider;
use InvalidArgumentException;

class SchemaServiceProvider extends ServiceProvider
{
Expand All @@ -13,7 +14,7 @@ class SchemaServiceProvider extends ServiceProvider
*/
protected function registerBuilderMacros()
{
Builder::macro('getColumnComment', function (string $table, string $column) {
Builder::macro('getColumnComment', function (string $table, string $column, bool $fail_on_missing = false) {
/** @var $this Builder */
$column_info = $this->getColumns($table);

Expand All @@ -23,10 +24,14 @@ protected function registerBuilderMacros()
}
}

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) {
Builder::macro('getTableComment', function (string $table, bool $fail_on_missing = false) {
/** @var $this Builder */
$table_info = $this->getTables();

Expand All @@ -36,6 +41,10 @@ protected function registerBuilderMacros()
}
}

if ($fail_on_missing) {
throw new InvalidArgumentException(sprintf("No such table [%s] in the database [%s].", $table, $this->getConnection()->getDatabaseName()));
}

return null;
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/Testing/Constraints/HasDatabaseColumnComment.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function __construct(Connection $database, string $table, string $column)
*/
public function getColumnComment(): ?string
{
return $this->database->getSchemaBuilder()->getColumnComment($this->table, $this->column);
return $this->database->getSchemaBuilder()->getColumnComment($this->table, $this->column, true);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Testing/Constraints/HasDatabaseTableComment.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function __construct(Connection $database, string $table)
*/
public function getTableComment(): ?string
{
return $this->database->getSchemaBuilder()->getTableComment($this->table);
return $this->database->getSchemaBuilder()->getTableComment($this->table, true);
}

/**
Expand Down
3 changes: 3 additions & 0 deletions tests/Feature/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

namespace Javaabu\Schema\Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Javaabu\Schema\Tests\TestCase;
use Javaabu\Schema\Tests\TestSupport\Enums\CityStatus;

class SchemaTest extends TestCase
{
use RefreshDatabase;

/** @test */
public function it_can_create_an_enum_column(): void
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/SchemaTest.php → tests/Unit/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Javaabu\Schema\Tests\TestCase;
use Javaabu\Schema\Tests\TestSupport\Enums\CityStatus;

class SchemaTest extends TestCase
class BuilderTest extends TestCase
{
use RefreshDatabase;

Expand Down
22 changes: 0 additions & 22 deletions tests/Unit/HasDatabaseTableCommentTest.php

This file was deleted.

0 comments on commit 7cd6c0c

Please sign in to comment.