Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix setting precision and scale through attributes #148

Merged
merged 2 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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';
}
Loading