Skip to content

Commit 51effbf

Browse files
Remove DBAL (#668)
1 parent e52f517 commit 51effbf

File tree

8 files changed

+91
-213
lines changed

8 files changed

+91
-213
lines changed

.github/workflows/build.yml

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,27 @@ on:
88
schedule:
99
- cron: "0 0 * * *"
1010

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

1415
jobs:
1516
tests:
16-
name: PHP ${{ matrix.php-version }} - L${{ matrix.laravel-version }} - ${{ matrix.os }}
17+
name: P${{ matrix.php-version }} - L${{ matrix.laravel-version }} - ${{ matrix.stability }} - ${{ matrix.os }}
1718

1819
strategy:
1920
fail-fast: false
2021
matrix:
2122
php-version: ['8.1', '8.2', '8.3']
2223
laravel-version: [10, 11]
2324
os: [ubuntu-latest, windows-latest, macos-latest]
24-
dependencies: [locked]
25+
stability: [prefer-lowest, prefer-stable]
2526
experimental: [false]
2627
exclude:
2728
- laravel-version: 11
2829
php-version: 8.1
30+
- laravel-version: 11
31+
stability: prefer-lowest
2932

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

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

62-
- name: Install highest dependencies from composer.json
63-
if: matrix.dependencies == 'highest'
64-
uses: nick-invision/retry@v1
65-
with:
66-
timeout_minutes: 5
67-
max_attempts: 5
68-
command: composer config platform --unset && composer update ${{ env.COMPOSER_FLAGS }}
69-
70-
- name: Install lowest dependencies from composer.json
71-
if: matrix.dependencies == 'lowest'
72-
uses: nick-invision/retry@v1
73-
with:
74-
timeout_minutes: 5
75-
max_attempts: 5
76-
command: composer install ${{ env.COMPOSER_FLAGS }} --prefer-lowest
77-
78-
- name: Install dependencies from composer.lock
79-
if: matrix.dependencies == 'locked'
80-
uses: nick-invision/retry@v1
81-
with:
82-
timeout_minutes: 5
83-
max_attempts: 5
84-
command: composer install ${{ env.COMPOSER_FLAGS }}
85-
86-
- name: Install Laravel
87-
run: composer require laravel/framework:${{ matrix.laravel-version }}.* --no-update
65+
- name: Install dependencies
66+
run: |
67+
composer require laravel/framework:${{ matrix.laravel-version }}.* --no-interaction --no-update
68+
composer update --${{ matrix.stability }} --prefer-dist --no-interaction
8869
8970
- name: Run tests
9071
run: vendor/bin/phpunit

.github/workflows/demo.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ jobs:
4949
uses: actions/cache@v2
5050
with:
5151
path: ${{ steps.determine-composer-cache-directory.outputs.directory }}
52-
key: dependencies-os-${{ matrix.os }}-php-${{ matrix.php-version }}-laravel-${{ matrix.laravel-version }}-composer-${{ hashFiles('**/composer.lock') }}
53-
restore-keys: dependencies-os-${{ matrix.os }}-php-${{ matrix.php-version }}-laravel-${{ matrix.laravel-version }}-composer-
52+
key: dependencies-os-${{ matrix.os }}-php-${{ matrix.php-version }}-laravel-${{ matrix.laravel-version }}-prefer-stable-composer-${{ hashFiles('**/composer.lock') }}
53+
restore-keys: dependencies-os-${{ matrix.os }}-php-${{ matrix.php-version }}-laravel-${{ matrix.laravel-version }}-prefer-stable-composer-
5454

5555
- name: Setup PHP
5656
uses: shivammathur/setup-php@v2

.github/workflows/lint.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
name: Lint
22
on:
33
- push
4-
- pull_request
54
jobs:
65
lint:
76
name: Lint

composer.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@
99
],
1010
"license": "MIT",
1111
"require": {
12-
"doctrine/dbal": "^3.3",
13-
"illuminate/console": "^10.0|^11.0",
14-
"illuminate/filesystem": "^10.0|^11.0",
15-
"illuminate/support": "^10.0|^11.0",
12+
"illuminate/console": "^10.38|^11.0",
13+
"illuminate/database": "^10.38|^11.0",
14+
"illuminate/filesystem": "^10.38|^11.0",
15+
"illuminate/support": "^10.38|^11.0",
1616
"laravel-shift/faker-registry": "^0.3.0",
1717
"symfony/yaml": ">=6.2"
1818
},
1919
"require-dev": {
2020
"laravel/pint": "^1.2",
2121
"mockery/mockery": "^1.4.4",
22-
"orchestra/testbench": "^8.0",
22+
"orchestra/testbench": "^8.0|^9.0",
2323
"phpunit/phpunit": "^10.0"
2424
},
2525
"suggest": {

src/EnumType.php

Lines changed: 0 additions & 60 deletions
This file was deleted.

src/Tracer.php

Lines changed: 70 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace Blueprint;
44

5-
use Doctrine\DBAL\Types\Type;
65
use Illuminate\Database\Eloquent\Model;
76
use Illuminate\Filesystem\Filesystem;
7+
use Illuminate\Support\Str;
88

99
class Tracer
1010
{
@@ -61,7 +61,7 @@ private function appClasses($paths): array
6161

6262
return array_filter(array_map(function (\SplFIleInfo $file) {
6363
if ($file->getExtension() !== 'php') {
64-
return;
64+
return [];
6565
}
6666

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

9393
private function extractColumns(Model $model): array
9494
{
95-
$table = $model->getConnection()->getTablePrefix() . $model->getTable();
96-
$schema = $model->getConnection()->getDoctrineSchemaManager();
97-
98-
if (!Type::hasType('enum')) {
99-
Type::addType('enum', EnumType::class);
100-
$databasePlatform = $schema->getDatabasePlatform();
101-
$databasePlatform->registerDoctrineTypeMapping('enum', 'enum');
102-
}
103-
104-
$database = null;
105-
if (strpos($table, '.')) {
106-
[$database, $table] = explode('.', $table);
107-
}
108-
109-
$columns = $schema->listTableColumns($table, $database);
110-
111-
$uses_enums = collect($columns)->contains(fn ($column) => $column->getType() instanceof \Blueprint\EnumType);
112-
113-
if ($uses_enums) {
114-
$definitions = $model->getConnection()->getDoctrineConnection()->fetchAllAssociative($schema->getDatabasePlatform()->getListTableColumnsSQL($table, $database));
115-
116-
collect($columns)->filter(fn ($column) => $column->getType() instanceof \Blueprint\EnumType)->each(function ($column, $key) use ($definitions) {
117-
$definition = collect($definitions)->where('Field', $key)->first();
118-
119-
$column->options = \Blueprint\EnumType::extractOptions($definition['Type']);
120-
});
121-
}
122-
123-
return $columns;
95+
return $model->getConnection()->getSchemaBuilder()->getColumns($model->getTable());
12496
}
12597

126-
/**
127-
* @param \Doctrine\DBAL\Schema\Column[] $columns
128-
*/
12998
private function mapColumns(array $columns): array
13099
{
131100
return collect($columns)
132-
->map([self::class, 'columns'])
101+
->keyBy('name')
102+
->map([self::class, 'columnAttributes'])
133103
->toArray();
134104
}
135105

136-
public static function columns(\Doctrine\DBAL\Schema\Column $column, string $key): string
106+
public static function columnAttributes(array $column): string
137107
{
138108
$attributes = [];
139109

140-
$type = self::translations($column->getType()->getName());
110+
$type = self::translations($column);
141111

142-
if (in_array($type, ['decimal', 'float'])) {
143-
if ($column->getPrecision()) {
144-
$type .= ':' . $column->getPrecision();
112+
if (in_array($type, ['decimal', 'float', 'time', 'timetz', 'datetime', 'datetimetz', 'timestamp', 'timestamptz', 'geography', 'geometry'])
113+
&& str_contains($column['type'], '(')) {
114+
$options = Str::between($column['type'], '(', ')');
115+
if ($options) {
116+
$type .= ':' . $options;
145117
}
146-
if ($column->getScale()) {
147-
$type .= ',' . $column->getScale();
118+
} elseif (in_array($type, ['string', 'char']) && str_contains($column['type'], '(')) {
119+
$length = Str::between($column['type'], '(', ')');
120+
if ($length != 255) {
121+
$type .= ':' . $length;
148122
}
149-
} elseif ($type === 'string' && $column->getLength()) {
150-
if ($column->getLength() !== 255) {
151-
$type .= ':' . $column->getLength();
152-
}
153-
} elseif ($type === 'text') {
154-
if ($column->getLength() > 65535) {
155-
$type = 'longtext';
156-
}
157-
} elseif ($type === 'enum' && !empty($column->options)) {
158-
$type .= ':' . implode(',', $column->options);
123+
} elseif (in_array($type, ['enum', 'set'])) {
124+
$options = Str::between($column['type'], '(', ')');
125+
$type .= ':' . $options;
159126
}
160127

161128
// TODO: guid/uuid
162129

163130
$attributes[] = $type;
164131

165-
if ($column->getUnsigned()) {
132+
if (str_contains($column['type'], 'unsigned')) {
166133
$attributes[] = 'unsigned';
167134
}
168135

169-
if (!$column->getNotnull()) {
136+
if ($column['nullable']) {
170137
$attributes[] = 'nullable';
171138
}
172139

173-
if ($column->getAutoincrement()) {
140+
if ($column['auto_increment']) {
174141
$attributes[] = 'autoincrement';
175142
}
176143

177-
if (!is_null($column->getDefault())) {
178-
$attributes[] = 'default:' . $column->getDefault();
144+
if ($column['default']) {
145+
$attributes[] = 'default:' . $column['default'];
179146
}
180147

181148
return implode(' ', $attributes);
182149
}
183150

184-
private static function translations(string $type): string
151+
private static function translations(array $column): string
185152
{
186-
static $mappings = [
187-
'array' => 'string',
188-
'bigint' => 'biginteger',
189-
'binary' => 'binary',
190-
'blob' => 'binary',
191-
'boolean' => 'boolean',
153+
$type = match ($column['type']) {
154+
'tinyint(1)', 'bit' => 'boolean',
155+
'nvarchar(max)' => 'text',
156+
default => null,
157+
};
158+
159+
$type ??= match ($column['type_name']) {
160+
'bigint', 'int8' => 'biginteger',
161+
'binary', 'varbinary', 'bytea', 'image', 'blob', 'tinyblob', 'mediumblob', 'longblob' => 'binary',
162+
// 'bit', 'varbit' => 'bit',
163+
'boolean', 'bool' => 'boolean',
164+
'char', 'bpchar', 'nchar' => 'char',
192165
'date' => 'date',
193-
'date_immutable' => 'date',
194-
'dateinterval' => 'date',
195-
'datetime' => 'datetime',
196-
'datetime_immutable' => 'datetime',
197-
'datetimetz' => 'datetimetz',
198-
'datetimetz_immutable' => 'datetimetz',
199-
'decimal' => 'decimal',
166+
'datetime', 'datetime2' => 'datetime',
167+
'datetimeoffset' => 'datetimetz',
168+
'decimal', 'numeric' => 'decimal',
169+
'double', 'float8' => 'double',
200170
'enum' => 'enum',
201-
'float' => 'float',
202-
'guid' => 'string',
203-
'integer' => 'integer',
171+
'float', 'real', 'float4' => 'float',
172+
'geography' => 'geography',
173+
'geometry', 'geometrycollection', 'linestring', 'multilinestring', 'multipoint', 'multipolygon', 'point', 'polygon' => 'geometry',
174+
// 'box', 'circle', 'line', 'lseg', 'path' => 'geometry',
175+
'integer', 'int', 'int4' => 'integer',
176+
'inet', 'cidr' => 'ipaddress',
177+
// 'interval' => 'interval',
204178
'json' => 'json',
205-
'object' => 'string',
206-
'simple_array' => 'string',
207-
'smallint' => 'smallinteger',
208-
'string' => 'string',
209-
'text' => 'text',
179+
'jsonb' => 'jsonb',
180+
'longtext' => 'longtext',
181+
'macaddr', 'macaddr8' => 'macadress',
182+
'mediumint' => 'mediuminteger',
183+
'mediumtext' => 'mediumtext',
184+
// 'money', 'smallmoney' => 'money',
185+
'set' => 'set',
186+
'smallint', 'int2' => 'smallinteger',
187+
'text', 'ntext' => 'text',
210188
'time' => 'time',
211-
'time_immutable' => 'time',
212-
];
213-
214-
return $mappings[$type] ?? 'string';
189+
'timestamp' => 'timestamp',
190+
'timestamptz' => 'timestamptz',
191+
'timetz' => 'timetz',
192+
'tinyint' => 'tinyinteger',
193+
'tinytext' => 'tinytext',
194+
'uuid', 'uniqueidentifier' => 'uuid',
195+
'varchar', 'nvarchar' => 'string',
196+
// 'xml' => 'xml',
197+
'year' => 'year',
198+
default => null,
199+
};
200+
201+
return $type ?? 'string';
215202
}
216203

217204
private function translateColumns(array $columns): array

0 commit comments

Comments
 (0)