Skip to content

Commit

Permalink
Remove doctrine (#1512)
Browse files Browse the repository at this point in the history
* Remove doctrine

* Fix tests

* normalize composer.json

* Add back platform

* Bump integration versions

* composer fix-style

* Fix stability

* Bump minimum to 10.36

* Bump minimum to 10.38

* More types

* Fast fail off

* Bump docblock

* composer fix-style

* add bpchar

* Use match for type detection

Co-authored-by: Hafez Divandari <hafezdivandari@gmail.com>

* Use driverName

Co-authored-by: Hafez Divandari <hafezdivandari@gmail.com>

* fix match

* TWeak types

* Update snapshots

* Remove testbench 7

* composer fix-style

* Update src/Console/ModelsCommand.php

Co-authored-by: Hafez Divandari <hafezdivandari@gmail.com>

* Update src/Console/ModelsCommand.php

Co-authored-by: Hafez Divandari <hafezdivandari@gmail.com>

* Tweak bool

* Update src/Console/ModelsCommand.php

Co-authored-by: Hafez Divandari <hafezdivandari@gmail.com>

* composer fix-style

* Remove custom types, default to string

---------

Co-authored-by: barryvdh <barryvdh@users.noreply.github.com>
Co-authored-by: laravel-ide-helper <laravel-ide-helper@users.noreply.github.com>
Co-authored-by: Hafez Divandari <hafezdivandari@gmail.com>
  • Loading branch information
4 people committed Feb 17, 2024
1 parent ed39977 commit 6579c03
Show file tree
Hide file tree
Showing 22 changed files with 74 additions and 187 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/fix-code-style.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.0
php-version: 8.3
coverage: none

- name: Install dependencies
Expand Down
10 changes: 3 additions & 7 deletions .github/workflows/run-integration-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,9 @@ jobs:
strategy:
fail-fast: false
matrix:
php: [8.3, 8.2, 8.1, 8.0]
laravel: [10.*, 9.*]
exclude:
- php: 8.0
laravel: 10.*
- php: 8.3
laravel: 9.*
php: [8.3, 8.2, 8.1]
laravel: [10.*]

name: P${{ matrix.php }} - Laravel${{ matrix.laravel }}
steps:
- name: Checkout code
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/run-static-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ jobs:

steps:
- uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.1
php-version: 8.3
coverage: none
extensions: pdo_sqlite

Expand Down
15 changes: 6 additions & 9 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,13 @@ jobs:
COMPOSER_NO_INTERACTION: 1

strategy:
fail-fast: false
matrix:
os: [ubuntu-20.04, windows-2019]
php: [8.3, 8.2, 8.1, 8.0]
laravel: [9.*, 10.*]
dependency-version: [prefer-lowest, prefer-stable]
exclude:
- php: 8.0
laravel: 10.*
- php: 8.0
dependency-version: prefer-lowest
php: [8.3, 8.2, 8.1]
laravel: [10.*]
stability: [prefer-lowest, prefer-stable]

steps:
- name: Set git to use LF
if: ${{ matrix.os == 'windows-2019' }}
Expand All @@ -50,7 +47,7 @@ jobs:
composer remove vimeo/psalm --no-update --dev
composer remove friendsofphp/php-cs-fixer --no-update --dev
composer require "laravel/framework:${{ matrix.laravel }}" --no-update --no-progress
composer update --prefer-dist --no-progress
composer update --prefer-dist --no-progress --${{ matrix.stability }}
- name: Execute Unit Tests
run: composer test-ci
20 changes: 0 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,26 +271,6 @@ add support for creating a new dedicated class instead of using local scopes in

If for some reason it's undesired to have them generated (one for each column), you can disable this via config `write_model_external_builder_methods` and setting it to `false`.

#### Unsupported or custom database types

Common column types (e.g. varchar, integer) are correctly mapped to PHP types (`string`, `int`).

But sometimes you may want to use custom column types in your database like `geography`, `jsonb`, `citext`, `bit`, etc. which may throw an "Unknown database type"-Exception.

For those special cases, you can map them via the config `custom_db_types`. Example:
```php
'custom_db_types' => [
'mysql' => [
'geography' => 'array',
'point' => 'array',
],
'postgresql' => [
'jsonb' => 'string',
'_int4' => 'array',
],
],
```

#### Custom Relationship Types

If you are using relationships not built into Laravel you will need to specify the name and returning class in the config to get proper generation.
Expand Down
12 changes: 6 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
"require": {
"php": "^8.0",
"ext-json": "*",
"barryvdh/reflection-docblock": "^2.0.6",
"barryvdh/reflection-docblock": "^2.1.1",
"composer/class-map-generator": "^1.0",
"doctrine/dbal": "^2.6 || ^3.1.4",
"illuminate/console": "^9 || ^10",
"illuminate/filesystem": "^9 || ^10",
"illuminate/support": "^9 || ^10",
"illuminate/console": "^10",
"illuminate/database": "^10.38",
"illuminate/filesystem": "^10",
"illuminate/support": "^10",
"nikic/php-parser": "^4.18 || ^5",
"phpdocumentor/type-resolver": "^1.1.0"
},
Expand All @@ -37,7 +37,7 @@
"illuminate/config": "^9 || ^10",
"illuminate/view": "^9 || ^10",
"mockery/mockery": "^1.4",
"orchestra/testbench": "^7 || ^8",
"orchestra/testbench": "^8",
"phpunit/phpunit": "^9",
"spatie/phpunit-snapshot-assertions": "^4",
"vimeo/psalm": "^5.4"
Expand Down
30 changes: 0 additions & 30 deletions config/ide-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,36 +200,6 @@

],

/*
|--------------------------------------------------------------------------
| Support for custom DB types
|--------------------------------------------------------------------------
|
| This setting allow you to map any custom database type (that you may have
| created using CREATE TYPE statement or imported using database plugin
| / extension to a Doctrine type.
|
| Each key in this array is a name of the Doctrine2 DBAL Platform. Currently valid names are:
| 'postgresql', 'db2', 'drizzle', 'mysql', 'oracle', 'sqlanywhere', 'sqlite', 'mssql'
|
| This name is returned by getName() method of the specific Doctrine/DBAL/Platforms/AbstractPlatform descendant
|
| The value of the array is an array of type mappings. Key is the name of the custom type,
| (for example, "jsonb" from Postgres 9.4) and the value is the name of the corresponding Doctrine2 type (in
| our case it is 'json_array'. Doctrine types are listed here:
| https://www.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/types.html#types
|
| So to support jsonb in your models when working with Postgres, just add the following entry to the array below:
|
| "postgresql" => array(
| "jsonb" => "json_array",
| ),
|
*/
'custom_db_types' => [

],

/*
|--------------------------------------------------------------------------
| Support for camel cased models
Expand Down
112 changes: 28 additions & 84 deletions src/Console/ModelsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
use Barryvdh\Reflection\DocBlock\Serializer as DocBlockSerializer;
use Barryvdh\Reflection\DocBlock\Tag;
use Composer\ClassMapGenerator\ClassMapGenerator;
use Doctrine\DBAL\Exception as DBALException;
use Doctrine\DBAL\Types\Type;
use Illuminate\Console\Command;
use Illuminate\Contracts\Database\Eloquent\Castable;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
Expand All @@ -39,6 +37,7 @@
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Database\Schema\Builder;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
Expand Down Expand Up @@ -244,8 +243,6 @@ protected function generateDocs($loadModels, $ignore = '')
*/
\n\n";

$hasDoctrine = interface_exists('Doctrine\DBAL\Driver');

if (empty($loadModels)) {
$models = $this->loadModels();
} else {
Expand Down Expand Up @@ -288,9 +285,7 @@ protected function generateDocs($loadModels, $ignore = '')

$model = $this->laravel->make($name);

if ($hasDoctrine) {
$this->getPropertiesFromTable($model);
}
$this->getPropertiesFromTable($model);

if (method_exists($model, 'getCasts')) {
$this->castPropertiesType($model);
Expand All @@ -314,13 +309,6 @@ protected function generateDocs($loadModels, $ignore = '')
}
}

if (!$hasDoctrine) {
$this->error(
'Warning: `"doctrine/dbal": "~2.3"` is required to load database information. ' .
'Please require that in your composer.json and run `composer update`.'
);
}

return $output;
}

Expand Down Expand Up @@ -508,95 +496,52 @@ protected function getTypeOverride($type)
*
* @param Model $model
*
* @throws DBALException If custom field failed to register
*/
public function getPropertiesFromTable($model)
{
$database = $model->getConnection()->getDatabaseName();
$table = $model->getConnection()->getTablePrefix() . $model->getTable();
$schema = $model->getConnection()->getDoctrineSchemaManager();
$databasePlatform = $schema->getDatabasePlatform();
$databasePlatform->registerDoctrineTypeMapping('enum', 'string');

if (strpos($table, '.')) {
[$database, $table] = explode('.', $table);
}

$platformName = $databasePlatform->getName();
$customTypes = $this->laravel['config']->get("ide-helper.custom_db_types.{$platformName}", []);
foreach ($customTypes as $yourTypeName => $doctrineTypeName) {
try {
if (!Type::hasType($yourTypeName)) {
Type::addType($yourTypeName, get_class(Type::getType($doctrineTypeName)));
}
} catch (DBALException $exception) {
$this->error("Failed registering custom db type \"$yourTypeName\" as \"$doctrineTypeName\"");
throw $exception;
}
$databasePlatform->registerDoctrineTypeMapping($yourTypeName, $doctrineTypeName);
}
$table = $model->getTable();
$schema = $model->getConnection()->getSchemaBuilder();
$columns = $schema->getColumns($table);
$driverName = $model->getConnection()->getDriverName();

$columns = $schema->listTableColumns($table, $database);

if (!$columns) {
return;
}

$this->setForeignKeys($schema, $table);
foreach ($columns as $column) {
$name = $column->getName();
$name = $column['name'];
if (in_array($name, $model->getDates())) {
$type = $this->dateClass;
} else {
$type = $column->getType()->getName();
switch ($type) {
case 'string':
case 'text':
case 'date':
case 'time':
case 'guid':
case 'datetimetz':
case 'datetime':
case 'decimal':
case 'binary':
$type = 'string';
break;
case 'integer':
case 'bigint':
case 'smallint':
$type = 'integer';
break;
case 'boolean':
switch ($platformName) {
case 'sqlite':
case 'mysql':
$type = 'integer';
break;
default:
$type = 'boolean';
break;
}
break;
case 'float':
$type = 'float';
break;
default:
$type = 'mixed';
break;
}
// Match types to php equivalent
$type = match ($column['type_name']) {
'tinyint', 'bit',
'integer', 'int', 'int4',
'smallint', 'int2',
'mediumint',
'bigint', 'int8' => 'integer',

'boolean', 'bool' => 'boolean',

'float', 'real', 'float4',
'double', 'float8' => 'float',

default => 'string',
};
}

$comment = $column->getComment();
if (!$column->getNotnull()) {
if ($column['nullable']) {
$this->nullableColumns[$name] = true;
}
$this->setProperty(
$name,
$this->getTypeInModel($model, $type),
true,
true,
$comment,
!$column->getNotnull()
$column['comment'],
$column['nullable']
);
if ($this->write_model_magic_where) {
$builderClass = $this->write_model_external_builder_methods
Expand Down Expand Up @@ -1681,14 +1626,13 @@ protected function runModelHooks($model): void
}

/**
* @param \Doctrine\DBAL\Schema\AbstractSchemaManager $schema
* @param Builder $schema
* @param string $table
* @throws DBALException
*/
protected function setForeignKeys($schema, $table)
{
foreach ($schema->listTableForeignKeys($table) as $foreignKeyConstraint) {
foreach ($foreignKeyConstraint->getLocalColumns() as $columnName) {
foreach ($schema->getForeignKeys($table) as $foreignKeyConstraint) {
foreach ($foreignKeyConstraint['columns'] as $columnName) {
$this->foreignKeyConstraintsColumns[] = $columnName;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@
* @property string $timestamptz_not_nullable
* @property integer|null $year_nullable
* @property integer $year_not_nullable
* @property mixed|null $binary_nullable
* @property mixed $binary_not_nullable
* @property string|null $binary_nullable
* @property string $binary_not_nullable
* @property string|null $uuid_nullable
* @property string $uuid_not_nullable
* @property string|null $ipaddress_nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@
* @property string $timestamptz_not_nullable
* @property integer|null $year_nullable
* @property integer $year_not_nullable
* @property mixed|null $binary_nullable
* @property mixed $binary_not_nullable
* @property string|null $binary_nullable
* @property string $binary_not_nullable
* @property string|null $uuid_nullable
* @property string $uuid_not_nullable
* @property string|null $ipaddress_nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@
* @property string $timestamptz_not_nullable
* @property integer|null $year_nullable
* @property integer $year_not_nullable
* @property mixed|null $binary_nullable
* @property mixed $binary_not_nullable
* @property string|null $binary_nullable
* @property string $binary_not_nullable
* @property string|null $uuid_nullable
* @property string $uuid_not_nullable
* @property string|null $ipaddress_nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@
* @property string $timestamptz_not_nullable
* @property integer|null $year_nullable
* @property integer $year_not_nullable
* @property mixed|null $binary_nullable
* @property mixed $binary_not_nullable
* @property string|null $binary_nullable
* @property string $binary_not_nullable
* @property string|null $uuid_nullable
* @property string $uuid_not_nullable
* @property string|null $ipaddress_nullable
Expand Down

0 comments on commit 6579c03

Please sign in to comment.