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

RonasIT / laravel-helpers / 15582941545

11 Jun 2025 10:53AM UTC coverage: 77.985% (+0.3%) from 77.654%
15582941545

Pull #206

github

web-flow
Merge faf7b01db into c09b8a113
Pull Request #206: 199 implement ability to test different version

5 of 5 new or added lines in 1 file covered. (100.0%)

27 existing lines in 1 file now uncovered.

1130 of 1449 relevant lines covered (77.98%)

12.61 hits per line

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

39.06
/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($fromEntity, $toEntity, $needAddField = false, $onDelete = 'cascade')
89
    {
UNCOV
90
        Schema::table(
×
91
            $this->getTableName($fromEntity),
×
UNCOV
92
            function (Blueprint $table) use ($toEntity, $needAddField, $onDelete) {
×
UNCOV
93
                $fieldName = Str::snake($toEntity) . '_id';
×
94

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

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

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

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

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

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

128
        Schema::create($bridgeTableName, function (Blueprint $table) {
×
UNCOV
129
            $table->increments('id');
×
UNCOV
130
        });
×
131

UNCOV
132
        $this->addForeignKey($bridgeTableName, $fromEntity, true);
×
133
        $this->addForeignKey($bridgeTableName, $toEntity, true);
×
134
    }
135

136
    public function dropBridgeTable($fromEntity, $toEntity)
137
    {
UNCOV
138
        $bridgeTableName = $this->getBridgeTable($fromEntity, $toEntity);
×
139

UNCOV
140
        $this->dropForeignKey($bridgeTableName, $fromEntity, true);
×
UNCOV
141
        $this->dropForeignKey($bridgeTableName, $toEntity, true);
×
142

UNCOV
143
        Schema::drop($bridgeTableName);
×
144
    }
145

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

UNCOV
151
        return implode('_', $entities);
×
152
    }
153

154
    protected function getTableName($entityName)
155
    {
UNCOV
156
        if (Schema::hasTable($entityName)) {
×
UNCOV
157
            return $entityName;
×
158
        }
159

UNCOV
160
        $entityName = Str::snake($entityName);
×
161

UNCOV
162
        return Str::plural($entityName);
×
163
    }
164
}
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