• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

RonasIT / laravel-helpers / 17398353014

02 Sep 2025 08:51AM UTC coverage: 78.127% (-0.05%) from 78.181%
17398353014

push

github

web-flow
Merge pull request #129 from RonasIT/fix-codestyle-and-legacy-code

Fix codestyle and legacy code

33 of 40 new or added lines in 8 files covered. (82.5%)

2 existing lines in 2 files now uncovered.

1118 of 1431 relevant lines covered (78.13%)

13.04 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

40.32
/src/Traits/MigrationTrait.php
1
<?php
2

3
namespace RonasIT\Support\Traits;
4

5
use Doctrine\DBAL\Types\Type;
6
use Doctrine\DBAL\Types\StringType;
7
use Illuminate\Support\Arr;
8
use Illuminate\Support\Facades\DB;
9
use Illuminate\Support\Str;
10
use Illuminate\Support\Facades\Schema;
11
use Illuminate\Database\Schema\Blueprint;
12
use Exception;
13

14
trait MigrationTrait
15
{
16
    public function __construct()
17
    {
18
        $this->registerEnumType();
5✔
19
    }
20

21
    public function changeEnum(string $table, string $field, array $values, array $valuesToRename = []): void
22
    {
23
        $databaseDriver = config('database.default');
5✔
24

25
        match ($databaseDriver) {
5✔
26
            'pgsql' => $this->changePostgresEnum($table, $field, $values, $valuesToRename),
2✔
27
            'mysql' => $this->changeMySqlEnum($table, $field, $values, $valuesToRename),
2✔
28
            default => throw new Exception("Database driver \"{$databaseDriver}\" not available"),
1✔
29
        };
5✔
30
    }
31

32
    private function changeMySqlEnum(string $table, string $field, array $values, array $valuesToRename = []): void
33
    {
34
        if (!empty($valuesToRename)) {
2✔
35
            $withRenamedValues = array_merge($values, array_keys($valuesToRename));
1✔
36

37
            $this->setMySqlEnum($table, $field, $withRenamedValues);
1✔
38

39
            $this->updateRenamedValues($table, $field, $valuesToRename);
1✔
40
        }
41

42
        $this->setMySqlEnum($table, $field, $values);
2✔
43
    }
44

45
    private function changePostgresEnum(string $table, string $field, array $values, array $valuesToRename = []): void
46
    {
47
        $check = "{$table}_{$field}_check";
2✔
48

49
        DB::statement("ALTER TABLE {$table} DROP CONSTRAINT {$check}");
2✔
50

51
        $this->updateRenamedValues($table, $field, $valuesToRename);
2✔
52

53
        $values = $this->preparePostgresValues($values);
2✔
54

55
        DB::statement("ALTER TABLE {$table} ADD CONSTRAINT {$check} CHECK ({$field}::text = ANY (ARRAY[{$values}]::text[]))");
2✔
56
    }
57

58
    private function updateRenamedValues(string $table, string $field, array $valuesToRename): void
59
    {
60
        foreach ($valuesToRename as $key => $value) {
3✔
61
            DB::table($table)->where([$field => $key])->update([$field => $value]);
2✔
62
        }
63
    }
64

65
    private function setMySqlEnum(string $table, string $field, array $values): void
66
    {
67
        $values = Arr::map($values, fn ($value) => "'{$value}'");
2✔
68

69
        $enum = implode( ', ', $values);
2✔
70

71
        DB::statement("ALTER TABLE {$table} MODIFY COLUMN {$field} ENUM({$enum})");
2✔
72
    }
73

74
    private function preparePostgresValues(array $values): string
75
    {
76
        $values = array_map(fn ($value) => "'{$value}'::character varying", $values);
2✔
77

78
        return join(', ', $values);
2✔
79
    }
80

81
    private function registerEnumType(): void
82
    {
83
        if (!Type::hasType('enum')) {
5✔
84
            Type::addType('enum', StringType::class);
×
85
        }
86
    }
87

88
    public function addForeignKey(string $fromEntity, string $toEntity, bool $needAddField = false, $onDelete = 'cascade'): void
89
    {
90
        Schema::table(
×
NEW
91
            table: $this->getTableName($fromEntity),
×
NEW
92
            callback: function (Blueprint $table) use ($toEntity, $needAddField, $onDelete) {
×
UNCOV
93
                $fieldName = Str::snake($toEntity) . '_id';
×
94

95
                if ($needAddField) {
×
96
                    $table->unsignedInteger($fieldName);
×
97
                }
98

99
                $table
×
100
                    ->foreign($fieldName)
×
101
                    ->references('id')
×
102
                    ->on($this->getTableName($toEntity))
×
103
                    ->onDelete($onDelete);
×
104
            }
×
105
        );
×
106
    }
107

108
    public function dropForeignKey(string $fromEntity, string $toEntity, bool $needDropField = false): void
109
    {
110
        $field = Str::snake($toEntity) . '_id';
×
111
        $table = $this->getTableName($fromEntity);
×
112

113
        if (Schema::hasColumn($table, $field)) {
×
114
            Schema::table($table, function (Blueprint $table) use ($field, $needDropField) {
×
115
                $table->dropForeign([$field]);
×
116

117
                if ($needDropField) {
×
118
                    $table->dropColumn([$field]);
×
119
                }
120
            });
×
121
        }
122
    }
123

124
    public function createBridgeTable(string $fromEntity, string $toEntity): void
125
    {
126
        $bridgeTableName = $this->getBridgeTable($fromEntity, $toEntity);
×
127

NEW
128
        Schema::create($bridgeTableName, fn (Blueprint $table) => $table->increments('id'));
×
129

130
        $this->addForeignKey($bridgeTableName, $fromEntity, true);
×
131
        $this->addForeignKey($bridgeTableName, $toEntity, true);
×
132
    }
133

134
    public function dropBridgeTable(string $fromEntity, string $toEntity): void
135
    {
136
        $bridgeTableName = $this->getBridgeTable($fromEntity, $toEntity);
×
137

138
        $this->dropForeignKey($bridgeTableName, $fromEntity, true);
×
139
        $this->dropForeignKey($bridgeTableName, $toEntity, true);
×
140

141
        Schema::drop($bridgeTableName);
×
142
    }
143

144
    protected function getBridgeTable(string $fromEntity, string $toEntity): string
145
    {
146
        $entities = [Str::snake($fromEntity), Str::snake($toEntity)];
×
147
        sort($entities, SORT_STRING);
×
148

149
        return implode('_', $entities);
×
150
    }
151

152
    protected function getTableName(string $entityName): string
153
    {
154
        if (Schema::hasTable($entityName)) {
×
155
            return $entityName;
×
156
        }
157

158
        $entityName = Str::snake($entityName);
×
159

160
        return Str::plural($entityName);
×
161
    }
162
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc