Passed
Push — 127-support-multidimensional-i... ( fe24b7 )
by Bas
10:53 queued 06:57
created

Builder::rename()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LaravelFreelancerNL\Aranguent\Schema;
6
7
use ArangoClient\Exceptions\ArangoException;
8
use ArangoClient\Schema\SchemaManager;
9
use Closure;
10
use Illuminate\Support\Facades\Log;
11
use Illuminate\Support\Fluent;
12
use LaravelFreelancerNL\Aranguent\Connection;
13
use LaravelFreelancerNL\Aranguent\Exceptions\QueryException;
14
use LaravelFreelancerNL\Aranguent\Schema\Concerns\HandlesAnalyzers;
15
use LaravelFreelancerNL\Aranguent\Schema\Concerns\HandlesIndexNaming;
16
use LaravelFreelancerNL\Aranguent\Schema\Concerns\HandlesViews;
17
use LaravelFreelancerNL\Aranguent\Schema\Concerns\UsesBlueprints;
18
19
class Builder extends \Illuminate\Database\Schema\Builder
20
{
21
    use HandlesAnalyzers;
22
    use HandlesIndexNaming;
0 ignored issues
show
Bug introduced by
The trait LaravelFreelancerNL\Aran...erns\HandlesIndexNaming requires the property $table which is not provided by LaravelFreelancerNL\Aranguent\Schema\Builder.
Loading history...
23
    use HandlesViews;
0 ignored issues
show
Bug introduced by
The trait LaravelFreelancerNL\Aran...a\Concerns\HandlesViews requires the property $name which is not provided by LaravelFreelancerNL\Aranguent\Schema\Builder.
Loading history...
24
    use UsesBlueprints;
25
26
    /**
27
     * The database connection instance.
28
     *
29
     * @var Connection
30
     */
31
    protected $connection;
32
33
    public SchemaManager $schemaManager;
34
35
    /**
36
     * The schema grammar instance.
37
     *
38
     * @var Grammar
39
     */
40
    public $grammar;
41
42
43
    /**
44
     * index prefixes?
45
     */
46
    public ?bool $prefixIndexes;
47
48
    /**
49
     * The table prefix.
50
     */
51
    public string $prefix;
52
53
    /**
54
     * Create a new database Schema manager.
55
     *
56
     * Builder constructor.
57
     */
58 94
    public function __construct(Connection $connection)
59
    {
60 94
        $this->connection = $connection;
61
62 94
        $this->grammar = $connection->getSchemaGrammar();
63
64 94
        $this->schemaManager = $connection->getArangoClient()->schema();
65
66 94
        $this->prefixIndexes = $this->connection->getConfig('prefix_indexes');
67
68 94
        $this->prefix = $this->prefixIndexes
69
            ? $this->connection->getConfig('prefix')
70 94
            : '';
71
72
    }
73
74
    /**
75
     * Determine if the given table exists.
76
     *
77
     * @param  string  $table
78
     * @return bool
79
     */
80 53
    public function hasTable($table)
81
    {
82 53
        return $this->handleExceptionsAsQueryExceptions(function () use ($table) {
83 53
            return $this->schemaManager->hasCollection($table);
84 53
        });
85
    }
86
87
    /**
88
     * @throws ArangoException
89
     */
90 14
    public function dropIfExists($table): void
91
    {
92 14
        $tableExists = $this->hasTable($table);
93 14
        if ($tableExists) {
94 14
            $this->drop($table);
95
        }
96
    }
97
98
    /**
99
     * Get all the tables for the database; excluding ArangoDB system collections
100
     *
101
     * @return array<mixed>
102
     *
103
     * @throws ArangoException
104
     */
105 33
    public function getAllTables(): array
106
    {
107 33
        return $this->schemaManager->getCollections(true);
108
    }
109
110
    /**
111
     * Get the tables that belong to the database.
112
     *
113
     * @return array
114
     * @throws ArangoException
115
     */
116 1
    public function getTables()
117
    {
118 1
        return $this->schemaManager->getCollections(true);
119
    }
120
121
    /**
122
     * Rename a table (collection).
123
     *
124
     * @param  string  $from
125
     * @param  string  $to
126
     *
127
     * @throws ArangoException
128
     */
129 2
    public function rename($from, $to): bool
130
    {
131 2
        return (bool) $this->schemaManager->renameCollection($from, $to);
132
    }
133
134
    /**
135
     * Drop a table (collection) from the schema.
136
     *
137
     * @throws ArangoException
138
     */
139 19
    public function drop($table)
140
    {
141 19
        $this->schemaManager->deleteCollection($table);
142
    }
143
144
    /**
145
     * Drop all tables (collections) from the schema.
146
     *
147
     * @throws ArangoException
148
     */
149 33
    public function dropAllTables(): void
150
    {
151 33
        $collections = $this->getAllTables();
152
153 33
        foreach ($collections as $name) {
154 33
            $this->schemaManager->deleteCollection($name->name);
155
        }
156
    }
157
158
    /**
159
     * Determine if the given table has a given column.
160
     *
161
     * @param  string  $table
162
     * @param string|string[] $column
163
     * @return bool
164
     */
165 7
    public function hasColumn($table, $column)
166
    {
167 7
        return $this->hasColumns($table, $column);
168
    }
169
170
    /**
171
     * Determine if the given table has given columns.
172
     *
173
     * @param string $table
174
     * @param string|string[] $columns
175
     * @return bool
176
     */
177 7
    public function hasColumns($table, $columns)
178
    {
179 7
        if (is_string($columns)) {
180 5
            $columns = [$columns];
181
        }
182
183 7
        $parameters = [];
184 7
        $parameters['name'] = 'hasColumn';
185 7
        $parameters['handler'] = 'aql';
186 7
        $parameters['columns'] = $columns;
187
188 7
        $command = new Fluent($parameters);
189
190 7
        $compilation = $this->grammar->compileHasColumn($table, $command);
191 7
        return $this->connection->select($compilation['aqb'])[0];
192
    }
193
194
    /**
195
     * Determine if the given table has a given index.
196
     *
197
     * @param  string  $table
198
     * @param  string|array<string>  $index
199
     * @param  string|null  $type
200
     * @return bool
201
     */
202 2
    public function hasIndex($table, $index, $type = null, array $options = [])
203
    {
204 2
        $name = $index;
205
206 2
        if ($type === null) {
207 2
            $type = 'persistent';
208
        }
209
210 2
        if (is_array($index)) {
211 1
            $name = $this->createIndexName($type, $index, $options, $table);
212
        }
213
214 2
        return !!$this->schemaManager->getIndexByName($table, $name);
0 ignored issues
show
Bug introduced by
It seems like $name can also be of type string[]; however, parameter $name of ArangoClient\Schema\Sche...nager::getIndexByName() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

214
        return !!$this->schemaManager->getIndexByName($table, /** @scrutinizer ignore-type */ $name);
Loading history...
215
    }
216
217
    /**
218
     * Create a database in the schema.
219
     *
220
     * @param  string  $name
221
     * @return bool
222
     *
223
     * @throws ArangoException
224
     */
225 4
    public function createDatabase($name)
226
    {
227 4
        return $this->schemaManager->createDatabase($name);
228
    }
229
230
    /**
231
     * Drop a database from the schema if the database exists.
232
     *
233
     * @param  string  $name
234
     * @return bool
235
     *
236
     * @throws ArangoException
237
     */
238 4
    public function dropDatabaseIfExists($name)
239
    {
240 4
        if ($this->schemaManager->hasDatabase($name)) {
241 2
            return $this->schemaManager->deleteDatabase($name);
242
        }
243
244 2
        return true;
245
    }
246
247
    /**
248
     * Get the database connection instance.
249
     *
250
     * @return Connection
251
     */
252 1
    public function getConnection()
253
    {
254 1
        return $this->connection;
255
    }
256
257
    /**
258
     * @throws QueryException
259
     */
260 55
    protected function handleExceptionsAsQueryExceptions(Closure $callback): mixed
261
    {
262
        try {
263 55
            return $callback();
264 1
        } catch (\Exception $e) {
265 1
            throw new QueryException($this->connection->getName(), $e->getMessage(), [], $e);
266
        }
267
    }
268
269
    /**
270
     * Disable foreign key constraints during the execution of a callback.
271
     *
272
     * ArangoDB doesn't have foreign keys so this is just a dummy to keep things working
273
     * for functionality that expect this method.
274
     *
275
     * @param  \Closure  $callback
276
     * @return mixed
277
     */
278 1
    public function withoutForeignKeyConstraints(Closure $callback)
279
    {
280 1
        return $callback();
281
    }
282
283
    /**
284
     * Silently catch the use of unsupported builder methods.
285
     */
286 2
    public function __call($method, $parameters)
287
    {
288 2
        Log::warning("The ArangoDB driver's schema builder doesn't support method '$method'\n");
289
    }
290
}
291