Skip to content

Commit

Permalink
Add numeric column type (#21)
Browse files Browse the repository at this point in the history
This PR adds numeric column type to schema builder. that. unlike 'decimal' type can be with variable precision
  • Loading branch information
Zlob authored and lazeevv committed Jul 31, 2019
1 parent 9eb4b12 commit 14d26b4
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 0 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@ Schema::table('some_table', function (Blueprint $table) {
});
```

### Numeric column type
Unlike standard laravel `decimal` type, this type can be with [variable precision](https://www.postgresql.org/docs/current/datatype-numeric.html)
```php
Schema::table('some_table', function (Blueprint $table) {
$table->numeric('column_with_variable_precision');
$table->numeric('column_with_defined_precision', 8);
$table->numeric('column_with_defined_precision_and_scale', 8, 2);
});
```

## Custom Extensions

1). Create a repository for your extension.
Expand Down
1 change: 1 addition & 0 deletions src/.meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* @method UniqueDefinition uniquePartial($columns, ?string $index = null, ?string $algorithm = null)
* @method Fluent gin($columns, ?string $name = null)
* @method Fluent gist($columns, ?string $name = null)
* @method ColumnDefinition numeric(string $column, ?int $precision = null, ?int $scale = null):
*/
class Blueprint
{
Expand Down
9 changes: 9 additions & 0 deletions src/Schema/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Umbrellio\Postgres\Schema;

use Illuminate\Database\Schema\Blueprint as BaseBlueprint;
use Illuminate\Database\Schema\ColumnDefinition;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Fluent;
use Umbrellio\Postgres\Schema\Builders\UniquePartialBuilder;
Expand Down Expand Up @@ -86,6 +87,14 @@ public function hasIndex($index, bool $unique = false): bool
return array_key_exists($index, $this->getSchemaManager()->listTableIndexes($this->getTable()));
}

/**
* Almost like 'decimal' type, but can be with variable precision (by default)
*/
public function numeric(string $column, ?int $precision = null, ?int $scale = null): ColumnDefinition
{
return $this->addColumn('numeric', $column, compact('precision', 'scale'));
}

protected function addFluentIndexes(): void
{
foreach ($this->columns as $column) {
Expand Down
12 changes: 12 additions & 0 deletions src/Schema/Grammars/PostgresGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,16 @@ public function compileGist(Blueprint $blueprint, Fluent $command): string
$this->columnize($command->columns)
);
}

protected function typeNumeric(Fluent $column): string
{
$type = 'numeric';
if ($column->precision && $column->scale) {
return "${type}({$column->precision}, {$column->scale})";
}
if ($column->precision) {
return "${type}({$column->precision})";
}
return $type;
}
}
21 changes: 21 additions & 0 deletions tests/Unit/Schema/BlueprintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,27 @@ public function attachPartitionRangeDates(): void
. "for values from ('{$today->toDateTimeString()}') to ('{$tomorrow->toDateTimeString()}')");
}

/** @test */
public function addingNumericColumnWithVariablePrecicion()
{
$this->blueprint->numeric('foo');
$this->assertSameSql('alter table "test_table" add column "foo" numeric not null');
}

/** @test */
public function addingNumericColumnWithDefinedPrecicion()
{
$this->blueprint->numeric('foo', 8);
$this->assertSameSql('alter table "test_table" add column "foo" numeric(8) not null');
}

/** @test */
public function addingNumericColumnWithDefinedPrecicionAndScope()
{
$this->blueprint->numeric('foo', 8, 2);
$this->assertSameSql('alter table "test_table" add column "foo" numeric(8, 2) not null');
}

private function assertSameSql(string $sql): void
{
$this->assertSame([$sql], $this->runToSql());
Expand Down

0 comments on commit 14d26b4

Please sign in to comment.