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

Remove DBAL for Laravel 11 #668

Merged
merged 13 commits into from
Feb 17, 2024
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
45 changes: 13 additions & 32 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,27 @@ on:
schedule:
- cron: "0 0 * * *"

env:
COMPOSER_FLAGS: "--ansi --no-interaction --no-progress --prefer-dist -o -n"
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true

jobs:
tests:
name: PHP ${{ matrix.php-version }} - L${{ matrix.laravel-version }} - ${{ matrix.os }}
name: P${{ matrix.php-version }} - L${{ matrix.laravel-version }} - ${{ matrix.stability }} - ${{ matrix.os }}

strategy:
fail-fast: false
matrix:
php-version: ['8.1', '8.2', '8.3']
laravel-version: [10, 11]
os: [ubuntu-latest, windows-latest, macos-latest]
dependencies: [locked]
stability: [prefer-lowest, prefer-stable]
experimental: [false]
exclude:
- laravel-version: 11
php-version: 8.1
- laravel-version: 11
stability: prefer-lowest

runs-on: ${{ matrix.os }}
continue-on-error: ${{ matrix.experimental }}
Expand All @@ -42,8 +45,8 @@ jobs:
uses: actions/cache@v2
with:
path: ${{ steps.determine-composer-cache-directory.outputs.directory }}
key: dependencies-os-${{ matrix.os }}-php-${{ matrix.php-version }}-laravel-${{ matrix.laravel-version }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: dependencies-os-${{ matrix.os }}-php-${{ matrix.php-version }}-laravel-${{ matrix.laravel-version }}-composer-
key: dependencies-os-${{ matrix.os }}-php-${{ matrix.php-version }}-laravel-${{ matrix.laravel-version }}-${{ matrix.stability }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: dependencies-os-${{ matrix.os }}-php-${{ matrix.php-version }}-laravel-${{ matrix.laravel-version }}-${{ matrix.stability }}-composer-

- name: Setup PHP
uses: shivammathur/setup-php@v2
Expand All @@ -59,32 +62,10 @@ jobs:
- name: Setup problem matchers for PHPUnit
run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"

- name: Install highest dependencies from composer.json
if: matrix.dependencies == 'highest'
uses: nick-invision/retry@v1
with:
timeout_minutes: 5
max_attempts: 5
command: composer config platform --unset && composer update ${{ env.COMPOSER_FLAGS }}

- name: Install lowest dependencies from composer.json
if: matrix.dependencies == 'lowest'
uses: nick-invision/retry@v1
with:
timeout_minutes: 5
max_attempts: 5
command: composer install ${{ env.COMPOSER_FLAGS }} --prefer-lowest

- name: Install dependencies from composer.lock
if: matrix.dependencies == 'locked'
uses: nick-invision/retry@v1
with:
timeout_minutes: 5
max_attempts: 5
command: composer install ${{ env.COMPOSER_FLAGS }}

- name: Install Laravel
run: composer require laravel/framework:${{ matrix.laravel-version }}.* --no-update
- name: Install dependencies
run: |
composer require laravel/framework:${{ matrix.laravel-version }}.* --no-interaction --no-update
composer update --${{ matrix.stability }} --prefer-dist --no-interaction

- name: Run tests
run: vendor/bin/phpunit
4 changes: 2 additions & 2 deletions .github/workflows/demo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ jobs:
uses: actions/cache@v2
with:
path: ${{ steps.determine-composer-cache-directory.outputs.directory }}
key: dependencies-os-${{ matrix.os }}-php-${{ matrix.php-version }}-laravel-${{ matrix.laravel-version }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: dependencies-os-${{ matrix.os }}-php-${{ matrix.php-version }}-laravel-${{ matrix.laravel-version }}-composer-
key: dependencies-os-${{ matrix.os }}-php-${{ matrix.php-version }}-laravel-${{ matrix.laravel-version }}-prefer-stable-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: dependencies-os-${{ matrix.os }}-php-${{ matrix.php-version }}-laravel-${{ matrix.laravel-version }}-prefer-stable-composer-

- name: Setup PHP
uses: shivammathur/setup-php@v2
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
name: Lint
on:
- push
- pull_request
jobs:
lint:
name: Lint
Expand Down
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@
],
"license": "MIT",
"require": {
"doctrine/dbal": "^3.3",
"illuminate/console": "^10.0|^11.0",
"illuminate/filesystem": "^10.0|^11.0",
"illuminate/support": "^10.0|^11.0",
"illuminate/console": "^10.38|^11.0",
"illuminate/database": "^10.38|^11.0",
"illuminate/filesystem": "^10.38|^11.0",
"illuminate/support": "^10.38|^11.0",
"laravel-shift/faker-registry": "^0.3.0",
"symfony/yaml": ">=6.2"
},
"require-dev": {
"laravel/pint": "^1.2",
"mockery/mockery": "^1.4.4",
"orchestra/testbench": "^8.0",
"orchestra/testbench": "^8.0|^9.0",
"phpunit/phpunit": "^10.0"
},
"suggest": {
Expand Down
60 changes: 0 additions & 60 deletions src/EnumType.php

This file was deleted.

153 changes: 70 additions & 83 deletions src/Tracer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Blueprint;

use Doctrine\DBAL\Types\Type;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Str;

class Tracer
{
Expand Down Expand Up @@ -61,7 +61,7 @@ private function appClasses($paths): array

return array_filter(array_map(function (\SplFIleInfo $file) {
if ($file->getExtension() !== 'php') {
return;
return [];
}

$content = $this->filesystem->get($file->getPathName());
Expand Down Expand Up @@ -92,126 +92,113 @@ private function loadModel(string $class)

private function extractColumns(Model $model): array
{
$table = $model->getConnection()->getTablePrefix() . $model->getTable();
$schema = $model->getConnection()->getDoctrineSchemaManager();

if (!Type::hasType('enum')) {
Type::addType('enum', EnumType::class);
$databasePlatform = $schema->getDatabasePlatform();
$databasePlatform->registerDoctrineTypeMapping('enum', 'enum');
}

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

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

$uses_enums = collect($columns)->contains(fn ($column) => $column->getType() instanceof \Blueprint\EnumType);

if ($uses_enums) {
$definitions = $model->getConnection()->getDoctrineConnection()->fetchAllAssociative($schema->getDatabasePlatform()->getListTableColumnsSQL($table, $database));

collect($columns)->filter(fn ($column) => $column->getType() instanceof \Blueprint\EnumType)->each(function ($column, $key) use ($definitions) {
$definition = collect($definitions)->where('Field', $key)->first();

$column->options = \Blueprint\EnumType::extractOptions($definition['Type']);
});
}

return $columns;
return $model->getConnection()->getSchemaBuilder()->getColumns($model->getTable());
}

/**
* @param \Doctrine\DBAL\Schema\Column[] $columns
*/
private function mapColumns(array $columns): array
{
return collect($columns)
->map([self::class, 'columns'])
->keyBy('name')
->map([self::class, 'columnAttributes'])
->toArray();
}

public static function columns(\Doctrine\DBAL\Schema\Column $column, string $key): string
public static function columnAttributes(array $column): string
{
$attributes = [];

$type = self::translations($column->getType()->getName());
$type = self::translations($column);

if (in_array($type, ['decimal', 'float'])) {
if ($column->getPrecision()) {
$type .= ':' . $column->getPrecision();
if (in_array($type, ['decimal', 'float', 'time', 'timetz', 'datetime', 'datetimetz', 'timestamp', 'timestamptz', 'geography', 'geometry'])
&& str_contains($column['type'], '(')) {
$options = Str::between($column['type'], '(', ')');
if ($options) {
$type .= ':' . $options;
}
if ($column->getScale()) {
$type .= ',' . $column->getScale();
} elseif (in_array($type, ['string', 'char']) && str_contains($column['type'], '(')) {
$length = Str::between($column['type'], '(', ')');
if ($length != 255) {
$type .= ':' . $length;
}
} elseif ($type === 'string' && $column->getLength()) {
if ($column->getLength() !== 255) {
$type .= ':' . $column->getLength();
}
} elseif ($type === 'text') {
if ($column->getLength() > 65535) {
$type = 'longtext';
}
} elseif ($type === 'enum' && !empty($column->options)) {
$type .= ':' . implode(',', $column->options);
} elseif (in_array($type, ['enum', 'set'])) {
$options = Str::between($column['type'], '(', ')');
$type .= ':' . $options;
}

// TODO: guid/uuid

$attributes[] = $type;

if ($column->getUnsigned()) {
if (str_contains($column['type'], 'unsigned')) {
$attributes[] = 'unsigned';
}

if (!$column->getNotnull()) {
if ($column['nullable']) {
$attributes[] = 'nullable';
}

if ($column->getAutoincrement()) {
if ($column['auto_increment']) {
$attributes[] = 'autoincrement';
}

if (!is_null($column->getDefault())) {
$attributes[] = 'default:' . $column->getDefault();
if ($column['default']) {
$attributes[] = 'default:' . $column['default'];
}

return implode(' ', $attributes);
}

private static function translations(string $type): string
private static function translations(array $column): string
{
static $mappings = [
'array' => 'string',
'bigint' => 'biginteger',
'binary' => 'binary',
'blob' => 'binary',
'boolean' => 'boolean',
$type = match ($column['type']) {
'tinyint(1)', 'bit' => 'boolean',
'nvarchar(max)' => 'text',
default => null,
};

$type ??= match ($column['type_name']) {
'bigint', 'int8' => 'biginteger',
'binary', 'varbinary', 'bytea', 'image', 'blob', 'tinyblob', 'mediumblob', 'longblob' => 'binary',
// 'bit', 'varbit' => 'bit',
'boolean', 'bool' => 'boolean',
'char', 'bpchar', 'nchar' => 'char',
'date' => 'date',
'date_immutable' => 'date',
'dateinterval' => 'date',
'datetime' => 'datetime',
'datetime_immutable' => 'datetime',
'datetimetz' => 'datetimetz',
'datetimetz_immutable' => 'datetimetz',
'decimal' => 'decimal',
'datetime', 'datetime2' => 'datetime',
'datetimeoffset' => 'datetimetz',
'decimal', 'numeric' => 'decimal',
'double', 'float8' => 'double',
'enum' => 'enum',
'float' => 'float',
'guid' => 'string',
'integer' => 'integer',
'float', 'real', 'float4' => 'float',
'geography' => 'geography',
'geometry', 'geometrycollection', 'linestring', 'multilinestring', 'multipoint', 'multipolygon', 'point', 'polygon' => 'geometry',
// 'box', 'circle', 'line', 'lseg', 'path' => 'geometry',
'integer', 'int', 'int4' => 'integer',
'inet', 'cidr' => 'ipaddress',
// 'interval' => 'interval',
'json' => 'json',
'object' => 'string',
'simple_array' => 'string',
'smallint' => 'smallinteger',
'string' => 'string',
'text' => 'text',
'jsonb' => 'jsonb',
'longtext' => 'longtext',
'macaddr', 'macaddr8' => 'macadress',
'mediumint' => 'mediuminteger',
'mediumtext' => 'mediumtext',
// 'money', 'smallmoney' => 'money',
'set' => 'set',
'smallint', 'int2' => 'smallinteger',
'text', 'ntext' => 'text',
'time' => 'time',
'time_immutable' => 'time',
];

return $mappings[$type] ?? 'string';
'timestamp' => 'timestamp',
'timestamptz' => 'timestamptz',
'timetz' => 'timetz',
'tinyint' => 'tinyinteger',
'tinytext' => 'tinytext',
'uuid', 'uniqueidentifier' => 'uuid',
'varchar', 'nvarchar' => 'string',
// 'xml' => 'xml',
'year' => 'year',
default => null,
};

return $type ?? 'string';
}

private function translateColumns(array $columns): array
Expand Down
Loading
Loading