cycle / database

Database Abstraction Layer, Schema Introspection, Schema Generation, Query Builders
MIT License
53 stars 22 forks source link

Add the ability to set JSON default value as an array #138

Closed msmakouz closed 8 months ago

msmakouz commented 9 months ago

Added the ability to use an array as a default value in JSON columns.

AbstractColumn

$column = $schema->json('settings')->defaultValue(['theme' => 'dark']);

Migrations (with cycle/migrations package)

public function up(): void
{
    $this->table('users')
        ->addColumn('settings', 'json', ['nullable' => false, 'default' => ['theme' => 'dark']])
        ->create();
}

Attributes (with cycle/orm and cycle/annotated packages)

use Cycle\Annotated\Annotation\Column;
use Cycle\Annotated\Annotation\Entity;

#[Entity]
class User
{
    // ...

    #[Column(type: 'json', default: ['theme' => 'dark'])]
    private array $settings;

    // ...
}

Closes: #54

codecov[bot] commented 9 months ago

Codecov Report

Merging #138 (b044e69) into 2.x (73d5f47) will increase coverage by 0.01%. The diff coverage is 100.00%.

@@             Coverage Diff              @@
##                2.x     #138      +/-   ##
============================================
+ Coverage     94.71%   94.72%   +0.01%     
- Complexity     1682     1689       +7     
============================================
  Files            96       96              
  Lines          4522     4535      +13     
============================================
+ Hits           4283     4296      +13     
  Misses          239      239              
Files Coverage Δ
src/Driver/Postgres/Schema/PostgresColumn.php 93.00% <100.00%> (+0.07%) :arrow_up:
src/Driver/SQLServer/Schema/SQLServerColumn.php 95.42% <100.00%> (+0.06%) :arrow_up:
src/Driver/SQLite/Schema/SQLiteColumn.php 96.55% <100.00%> (+0.12%) :arrow_up:
src/Schema/AbstractColumn.php 98.64% <100.00%> (+0.04%) :arrow_up: