Skip to content

Commit

Permalink
Merge pull request #148: Fix setting precision and scale through …
Browse files Browse the repository at this point in the history
…attributes
  • Loading branch information
roxblnfk authored Dec 18, 2023
2 parents 0b4d063 + 905997d commit bbdb864
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/Driver/Postgres/Schema/PostgresColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,12 @@ class PostgresColumn extends AbstractColumn
#[ColumnAttribute(['interval'])]
protected ?string $intervalType = null;

#[ColumnAttribute(['numeric'])]
protected int $precision = 0;

#[ColumnAttribute(['numeric'])]
protected int $scale = 0;

/**
* Internal field to determine if the serial is PK.
*/
Expand Down
7 changes: 7 additions & 0 deletions src/Driver/SQLite/Schema/SQLiteColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Cycle\Database\Driver\DriverInterface;
use Cycle\Database\Schema\AbstractColumn;
use Cycle\Database\Schema\Attribute\ColumnAttribute;

class SQLiteColumn extends AbstractColumn
{
Expand Down Expand Up @@ -113,6 +114,12 @@ class SQLiteColumn extends AbstractColumn
*/
protected bool $primaryKey = false;

#[ColumnAttribute(['numeric'])]
protected int $precision = 0;

#[ColumnAttribute(['numeric'])]
protected int $scale = 0;

/**
* DBMS specific reverse mapping must map database specific type into limited set of abstract
* types.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Cycle\Database\Tests\Functional\Driver\Common\Schema;

use Cycle\Database\Tests\Functional\Driver\Common\BaseTest;

abstract class NumberColumnTest extends BaseTest
{
public function testSetPrecisionAndScaleViaAttribute(): void
{
$schema = $this->schema('table');

$column = $schema->column('column')->__call('decimal', ['precision' => 8, 'scale' => 2]);
$schema->save();
$this->assertSameAsInDB($schema);

$this->assertSame(8, $column->getPrecision());
$this->assertSame(2, $column->getScale());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

// phpcs:ignore
use Cycle\Database\Driver\Handler;
use Cycle\Database\Tests\Functional\Driver\Common\BaseTest;
use Cycle\Database\Tests\Functional\Driver\Common\Schema\NumberColumnTest as BaseTest;

/**
* @group driver
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Cycle\Database\Tests\Functional\Driver\Postgres\Schema;

// phpcs:ignore
use Cycle\Database\Tests\Functional\Driver\Common\Schema\NumberColumnTest as CommonClass;

/**
* @group driver
* @group driver-postgres
*/
final class NumberColumnTest extends CommonClass
{
public const DRIVER = 'postgres';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Cycle\Database\Tests\Functional\Driver\SQLServer\Schema;

// phpcs:ignore
use Cycle\Database\Tests\Functional\Driver\Common\Schema\NumberColumnTest as CommonClass;

/**
* @group driver
* @group driver-sqlserver
*/
final class NumberColumnTest extends CommonClass
{
public const DRIVER = 'sqlserver';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Cycle\Database\Tests\Functional\Driver\SQLite\Schema;

// phpcs:ignore
use Cycle\Database\Tests\Functional\Driver\Common\Schema\NumberColumnTest as CommonClass;

/**
* @group driver
* @group driver-sqlite
*/
final class NumberColumnTest extends CommonClass
{
public const DRIVER = 'sqlite';
}

0 comments on commit bbdb864

Please sign in to comment.