Skip to content

Commit

Permalink
Merge 76e8279 into 9eb4b12
Browse files Browse the repository at this point in the history
  • Loading branch information
Zlob committed Jul 31, 2019
2 parents 9eb4b12 + 76e8279 commit f8eab09
Show file tree
Hide file tree
Showing 5 changed files with 56 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 Fluent numeric(string $column, ?int $precision = null, ?int $scale = null)
*/
class Blueprint
{
Expand Down
12 changes: 12 additions & 0 deletions src/Schema/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,18 @@ 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)
* @param string $column
* @param int|null $precision
* @param int|null $scale
* @return \Illuminate\Database\Schema\ColumnDefinition
*/
public function numeric(string $column, ?int $precision = null, ?int $scale = null)
{
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 f8eab09

Please sign in to comment.