1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace LaravelFreelancerNL\Aranguent\Migrations; |
6
|
|
|
|
7
|
|
|
use ArangoClient\Exceptions\ArangoException; |
8
|
|
|
use ArangoClient\Schema\SchemaManager; |
9
|
|
|
use Illuminate\Database\ConnectionResolverInterface as IlluminateResolver; |
10
|
|
|
use Illuminate\Database\Migrations\DatabaseMigrationRepository as IlluminateDatabaseMigrationRepository; |
11
|
|
|
use Illuminate\Database\Query\Builder as IlluminateQueryBuilder; |
12
|
|
|
use LaravelFreelancerNL\Aranguent\Connection; |
13
|
|
|
use LaravelFreelancerNL\Aranguent\Console\Concerns\ArangoCommands; |
14
|
|
|
use LaravelFreelancerNL\Aranguent\Exceptions\AranguentException; |
15
|
|
|
use LaravelFreelancerNL\Aranguent\Query\Builder; |
16
|
|
|
|
17
|
|
|
class DatabaseMigrationRepository extends IlluminateDatabaseMigrationRepository |
18
|
|
|
{ |
19
|
|
|
use ArangoCommands; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The name of the migration collection. |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $table; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Create a new database migration repository instance. |
30
|
|
|
*/ |
31
|
11 |
|
public function __construct(IlluminateResolver $resolver, string $table) |
32
|
|
|
{ |
33
|
11 |
|
parent::__construct($resolver, $table); |
34
|
|
|
} |
35
|
|
|
|
36
|
3 |
|
protected function getSchemaManager(): SchemaManager |
37
|
|
|
{ |
38
|
3 |
|
$connection = $this->getConnection(); |
39
|
|
|
assert($connection instanceof Connection); |
40
|
|
|
|
41
|
3 |
|
$arangoClient = $connection->getArangoClient(); |
42
|
|
|
|
43
|
3 |
|
if ($arangoClient === null) { |
44
|
|
|
throw new AranguentException('No arangodb client set.'); |
45
|
|
|
} |
46
|
|
|
|
47
|
3 |
|
return $arangoClient->schema(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Create the migration repository data store. |
52
|
|
|
* |
53
|
|
|
* @return void |
54
|
|
|
*/ |
55
|
2 |
|
public function createRepository() |
56
|
|
|
{ |
57
|
2 |
|
if ($this->useFallback()) { |
58
|
|
|
parent::createRepository(); |
59
|
|
|
return; |
60
|
|
|
} |
61
|
|
|
|
62
|
2 |
|
$schemaManager = $this->getSchemaManager(); |
63
|
|
|
|
64
|
2 |
|
$schemaManager->createCollection($this->table); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get the list of migrations. |
69
|
|
|
* |
70
|
|
|
* @param int $steps |
71
|
|
|
* @return array<mixed> |
72
|
|
|
*/ |
73
|
11 |
|
public function getMigrations($steps) |
74
|
|
|
{ |
75
|
11 |
|
if ($this->useFallback()) { |
76
|
|
|
return parent::getMigrations($steps); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
//TODO: the only difference with the parent function is that type of the batch value: |
80
|
|
|
// 1 instead of '1'. This should probably be changed in the Laravel framework as it |
81
|
|
|
// seems unnecessary to use a numeric string here. |
82
|
|
|
|
83
|
11 |
|
$query = $this->table() |
84
|
11 |
|
->where('batch', '>=', 1) |
85
|
11 |
|
->orderBy('batch', 'desc') |
|
|
|
|
86
|
11 |
|
->orderBy('migration', 'desc') |
87
|
11 |
|
->take($steps); |
88
|
|
|
|
89
|
11 |
|
return $query->get()->all(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Determine if the migration repository exists. |
95
|
|
|
* |
96
|
|
|
* @return bool |
97
|
|
|
* @throws ArangoException |
98
|
|
|
* @throws AranguentException |
99
|
|
|
*/ |
100
|
3 |
|
public function repositoryExists() |
101
|
|
|
{ |
102
|
3 |
|
if ($this->useFallback()) { |
103
|
|
|
return parent::repositoryExists(); |
104
|
|
|
} |
105
|
|
|
|
106
|
3 |
|
$schemaManager = $this->getSchemaManager(); |
107
|
|
|
|
108
|
3 |
|
return $schemaManager->hasCollection($this->table); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Get a query builder for the migration collection. |
113
|
|
|
* |
114
|
|
|
* @return IlluminateQueryBuilder |
115
|
|
|
*/ |
116
|
|
|
protected function collection() |
117
|
|
|
{ |
118
|
|
|
return $this->table(); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|