diff --git a/README.md b/README.md index 0712d0f..9b7d8ae 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/.meta.php b/src/.meta.php index 1fdb06a..c0a170a 100644 --- a/src/.meta.php +++ b/src/.meta.php @@ -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 { diff --git a/src/Schema/Blueprint.php b/src/Schema/Blueprint.php index f2d37ed..2cb1ea7 100644 --- a/src/Schema/Blueprint.php +++ b/src/Schema/Blueprint.php @@ -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) { diff --git a/src/Schema/Grammars/PostgresGrammar.php b/src/Schema/Grammars/PostgresGrammar.php index 088a14e..a56b0b2 100644 --- a/src/Schema/Grammars/PostgresGrammar.php +++ b/src/Schema/Grammars/PostgresGrammar.php @@ -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; + } } diff --git a/tests/Unit/Schema/BlueprintTest.php b/tests/Unit/Schema/BlueprintTest.php index e70633f..8c76c0f 100644 --- a/tests/Unit/Schema/BlueprintTest.php +++ b/tests/Unit/Schema/BlueprintTest.php @@ -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());