Skip to content

Commit

Permalink
Define generated fields in ORM Schema (#12)
Browse files Browse the repository at this point in the history
Fix CI
  • Loading branch information
msmakouz committed Feb 8, 2024
1 parent 3772826 commit 04b27fc
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 65 deletions.
3 changes: 3 additions & 0 deletions .github/FUNDING.yml
@@ -0,0 +1,3 @@
# These are supported funding model platforms

github: cycle
13 changes: 8 additions & 5 deletions .github/workflows/ci-mssql.yml
Expand Up @@ -24,10 +24,13 @@ jobs:
extensions: pdo, pdo_sqlsrv
mssql: 'server:2019-latest'
- php: '8.1'
extensions: pdo, pdo_sqlsrv-5.11.0
extensions: pdo, pdo_sqlsrv
mssql: 'server:2019-latest'
- php: '8.2'
extensions: pdo, pdo_sqlsrv-5.11.0
extensions: pdo, pdo_sqlsrv
mssql: 'server:2019-latest'
- php: '8.3'
extensions: pdo, pdo_sqlsrv
mssql: 'server:2019-latest'

services:
Expand Down Expand Up @@ -68,11 +71,11 @@ jobs:
run: composer self-update

- name: Install dependencies with composer
if: matrix.php != '8.2'
if: matrix.php != '8.4'
run: composer update --prefer-dist --no-interaction --no-progress --optimize-autoloader --ansi

- name: Install dependencies with composer php 8.2
if: matrix.php == '8.2'
- name: Install dependencies with composer php 8.4
if: matrix.php == '8.4'
run: composer update --ignore-platform-reqs --prefer-dist --no-interaction --no-progress --optimize-autoloader --ansi

- name: Run tests with phpunit without coverage
Expand Down
7 changes: 4 additions & 3 deletions .github/workflows/ci-mysql.yml
Expand Up @@ -23,6 +23,7 @@ jobs:
- "8.0"
- "8.1"
- "8.2"
- "8.3"

mysql-version:
- "5.7"
Expand Down Expand Up @@ -79,11 +80,11 @@ jobs:
php${{ matrix.php-version }}-composer-${{ matrix.dependencies }}-
- name: Install dependencies with composer
if: matrix.php-version != '8.2'
if: matrix.php-version != '8.4'
run: composer update --prefer-dist --no-interaction --no-progress --optimize-autoloader --ansi

- name: Install dependencies with composer php 8.2
if: matrix.php-version == '8.2'
- name: Install dependencies with composer php 8.4
if: matrix.php-version == '8.4'
run: composer update --ignore-platform-reqs --prefer-dist --no-interaction --no-progress --optimize-autoloader --ansi

- name: Run mysql tests with phpunit
Expand Down
7 changes: 4 additions & 3 deletions .github/workflows/ci-pgsql.yml
Expand Up @@ -22,6 +22,7 @@ jobs:
- "8.0"
- "8.1"
- "8.2"
- "8.3"

pgsql-version:
- "10"
Expand Down Expand Up @@ -80,11 +81,11 @@ jobs:
php${{ matrix.php-version }}-composer-${{ matrix.dependencies }}-
- name: Install dependencies with composer
if: matrix.php-version != '8.2'
if: matrix.php-version != '8.4'
run: composer update --prefer-dist --no-interaction --no-progress --optimize-autoloader --ansi

- name: Install dependencies with composer php 8.2
if: matrix.php-version == '8.2'
- name: Install dependencies with composer php 8.4
if: matrix.php-version == '8.4'
run: composer update --ignore-platform-reqs --prefer-dist --no-interaction --no-progress --optimize-autoloader --ansi

- name: Run pgsql tests with phpunit
Expand Down
15 changes: 9 additions & 6 deletions .github/workflows/main.yml
Expand Up @@ -17,6 +17,7 @@ jobs:
- "8.0"
- "8.1"
- "8.2"
- "8.3"
steps:
- name: Checkout
uses: actions/checkout@v2
Expand All @@ -42,10 +43,10 @@ jobs:
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: ${{ runner.os }}-composer-
- name: Install dependencies with composer
if: matrix.php-version != '8.2'
if: matrix.php-version != '8.4'
run: composer update --prefer-dist --no-interaction --no-progress --optimize-autoloader --ansi
- name: Install dependencies with composer php 8.2
if: matrix.php-version == '8.2'
- name: Install dependencies with composer php 8.4
if: matrix.php-version == '8.4'
run: composer update --ignore-platform-reqs --prefer-dist --no-interaction --no-progress --optimize-autoloader --ansi
- name: Execute Tests
run: |
Expand Down Expand Up @@ -74,6 +75,8 @@ jobs:
php-version:
- "8.0"
- "8.1"
- "8.2"
- "8.3"
steps:
- name: Checkout
uses: actions/checkout@v2
Expand All @@ -95,11 +98,11 @@ jobs:
restore-keys: ${{ runner.os }}-composer-

- name: Install dependencies with composer
if: matrix.php-version != '8.2'
if: matrix.php-version != '8.4'
run: composer update --prefer-dist --no-interaction --no-progress --optimize-autoloader --ansi

- name: Install dependencies with composer php 8.2
if: matrix.php-version == '8.2'
- name: Install dependencies with composer php 8.4
if: matrix.php-version == '8.4'
run: composer update --ignore-platform-reqs --prefer-dist --no-interaction --no-progress --optimize-autoloader --ansi

- name: Execute Tests
Expand Down
55 changes: 11 additions & 44 deletions .github/workflows/static.yml
@@ -1,47 +1,14 @@
name: static analysis
on:
pull_request: null
push:
branches:
- '*.*'

on: [push, pull_request]
name: static analysis

jobs:
mutation:
name: PHP ${{ matrix.php }}-${{ matrix.os }}

runs-on: ${{ matrix.os }}

strategy:
matrix:
os:
- ubuntu-latest

php:
- "8.2"

steps:
- name: Checkout
uses: actions/checkout@v2.3.4

- name: Install PHP
uses: shivammathur/setup-php@v2
with:
php-version: "${{ matrix.php }}"
tools: composer:v2, cs2pr
coverage: none

- name: Determine composer cache directory
run: echo "COMPOSER_CACHE_DIR=$(composer config cache-dir)" >> $GITHUB_ENV

- name: Cache dependencies installed with composer
uses: actions/cache@v2
with:
path: ${{ env.COMPOSER_CACHE_DIR }}
key: php${{ matrix.php }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: |
php${{ matrix.php }}-composer-
- name: Update composer
run: composer self-update

- name: Install dependencies with composer
run: composer update --prefer-dist --no-interaction --no-progress --optimize-autoloader --ansi

- name: Static analysis
run: vendor/bin/psalm --shepherd --stats --output-format=checkstyle
psalm:
uses: spiral/gh-actions/.github/workflows/psalm.yml@master
with:
os: >-
['ubuntu-latest']
36 changes: 34 additions & 2 deletions composer.json
@@ -1,9 +1,42 @@
{
"name": "cycle/entity-behavior-uuid",
"type": "library",
"license": "MIT",
"description": "Provides an ability to use ramsey/uuid as a Cycle ORM entity column type",
"homepage": "https://cycle-orm.dev",
"support": {
"issues": "https://github.com/cycle/entity-behavior-uuid/issues",
"source": "https://github.com/cycle/entity-behavior-uuid",
"docs": "https://cycle-orm.dev/docs",
"chat": "https://discord.gg/spiralphp"
},
"authors": [
{
"name": "Anton Titov (wolfy-j)",
"email": "wolfy-j@spiralscout.com"
},
{
"name": "Aleksei Gagarin (roxblnfk)",
"email": "alexey.gagarin@spiralscout.com"
},
{
"name": "Pavel Butchnev (butschster)",
"email": "pavel.buchnev@spiralscout.com"
},
{
"name": "Maksim Smakouz (msmakouz)",
"email": "maksim.smakouz@spiralscout.com"
}
],
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/cycle"
}
],
"require": {
"php": ">=8.0",
"cycle/entity-behavior": "^1.0",
"cycle/entity-behavior": "^1.3",
"ramsey/uuid": "^4.5"
},
"require-dev": {
Expand All @@ -12,7 +45,6 @@
"spiral/tokenizer": "^2.8",
"vimeo/psalm": "^5.11"
},
"license": "MIT",
"autoload": {
"psr-4": {
"Cycle\\ORM\\Entity\\Behavior\\Uuid\\": "src/"
Expand Down
13 changes: 11 additions & 2 deletions src/Uuid.php
Expand Up @@ -6,6 +6,7 @@

use Cycle\ORM\Entity\Behavior\Schema\BaseModifier;
use Cycle\ORM\Entity\Behavior\Schema\RegistryModifier;
use Cycle\ORM\Schema\GeneratedField;
use Cycle\Schema\Registry;
use Ramsey\Uuid\Uuid as RamseyUuid;

Expand All @@ -20,7 +21,11 @@ public function compute(Registry $registry): void
$modifier = new RegistryModifier($registry, $this->role);
$this->column = $modifier->findColumnName($this->field, $this->column);
if ($this->column !== null) {
$modifier->addUuidColumn($this->column, $this->field)->nullable($this->nullable);
$modifier->addUuidColumn(
$this->column,
$this->field,
$this->nullable ? null : GeneratedField::BEFORE_INSERT
)->nullable($this->nullable);
$modifier->setTypecast(
$registry->getEntity($this->role)->getFields()->get($this->field),
[RamseyUuid::class, 'fromString']
Expand All @@ -33,7 +38,11 @@ public function render(Registry $registry): void
$modifier = new RegistryModifier($registry, $this->role);
$this->column = $modifier->findColumnName($this->field, $this->column) ?? $this->field;

$modifier->addUuidColumn($this->column, $this->field)->nullable($this->nullable);
$modifier->addUuidColumn(
$this->column,
$this->field,
$this->nullable ? null : GeneratedField::BEFORE_INSERT
)->nullable($this->nullable);
$modifier->setTypecast(
$registry->getEntity($this->role)->getFields()->get($this->field),
[RamseyUuid::class, 'fromString']
Expand Down
8 changes: 8 additions & 0 deletions tests/Uuid/Functional/Driver/Common/Uuid/UuidTest.php
Expand Up @@ -9,6 +9,7 @@
use Cycle\ORM\Entity\Behavior\Uuid\Tests\Fixtures\Uuid\Post;
use Cycle\ORM\Entity\Behavior\Uuid\Tests\Fixtures\Uuid\User;
use Cycle\ORM\Entity\Behavior\Uuid\Tests\Functional\Driver\Common\BaseTest;
use Cycle\ORM\Schema\GeneratedField;
use Cycle\Schema\Registry;
use Ramsey\Uuid\Uuid;
use Spiral\Attributes\ReaderInterface;
Expand Down Expand Up @@ -44,6 +45,7 @@ public function testColumnExist(ReaderInterface $reader): void
$this->assertTrue($fields->hasColumn('uuid'));
$this->assertSame('uuid', $fields->get('uuid')->getType());
$this->assertSame([Uuid::class, 'fromString'], $fields->get('uuid')->getTypecast());
$this->assertSame(GeneratedField::BEFORE_INSERT, $fields->get('uuid')->getGenerated());
$this->assertSame(1, $fields->count());
}

Expand All @@ -60,6 +62,7 @@ public function testAddColumn(ReaderInterface $reader): void
$this->assertTrue($fields->hasColumn('custom_uuid'));
$this->assertSame('uuid', $fields->get('customUuid')->getType());
$this->assertSame([Uuid::class, 'fromString'], $fields->get('customUuid')->getTypecast());
$this->assertSame(GeneratedField::BEFORE_INSERT, $fields->get('customUuid')->getGenerated());
}

/**
Expand All @@ -75,21 +78,25 @@ public function testMultipleUuid(ReaderInterface $reader): void
$this->assertTrue($fields->hasColumn('uuid'));
$this->assertSame('uuid', $fields->get('uuid')->getType());
$this->assertSame([Uuid::class, 'fromString'], $fields->get('uuid')->getTypecast());
$this->assertSame(GeneratedField::BEFORE_INSERT, $fields->get('uuid')->getGenerated());

$this->assertTrue($fields->has('otherUuid'));
$this->assertTrue($fields->hasColumn('other_uuid'));
$this->assertSame('uuid', $fields->get('otherUuid')->getType());
$this->assertSame([Uuid::class, 'fromString'], $fields->get('otherUuid')->getTypecast());
$this->assertSame(GeneratedField::BEFORE_INSERT, $fields->get('otherUuid')->getGenerated());

$this->assertTrue($fields->has('uuid7'));
$this->assertTrue($fields->hasColumn('uuid7'));
$this->assertSame('uuid', $fields->get('uuid7')->getType());
$this->assertSame([Uuid::class, 'fromString'], $fields->get('uuid7')->getTypecast());
$this->assertSame(GeneratedField::BEFORE_INSERT, $fields->get('uuid7')->getGenerated());

$this->assertTrue($fields->has('otherUuid7'));
$this->assertTrue($fields->hasColumn('other_uuid7'));
$this->assertSame('uuid', $fields->get('otherUuid7')->getType());
$this->assertSame([Uuid::class, 'fromString'], $fields->get('otherUuid7')->getTypecast());
$this->assertSame(GeneratedField::BEFORE_INSERT, $fields->get('otherUuid7')->getGenerated());
}

/**
Expand All @@ -111,5 +118,6 @@ public function testAddNullableColumn(ReaderInterface $reader): void
->column('not_defined_uuid')
->isNullable()
);
$this->assertNull($fields->get('notDefinedUuid')->getGenerated());
}
}

0 comments on commit 04b27fc

Please sign in to comment.