|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LaravelFreelancerNL\Aranguent\Migrations; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\ConnectionResolverInterface as IlluminateResolver; |
|
6
|
|
|
use Illuminate\Database\Migrations\DatabaseMigrationRepository as IlluminateDatabaseMigrationRepository; |
|
7
|
|
|
use LaravelFreelancerNL\Aranguent\Connection; |
|
8
|
|
|
use LaravelFreelancerNL\Aranguent\Query\Builder; |
|
9
|
|
|
use LaravelFreelancerNL\FluentAQL\QueryBuilder; |
|
10
|
|
|
|
|
11
|
|
|
class DatabaseMigrationRepository extends IlluminateDatabaseMigrationRepository |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* The name of the migration collection. |
|
15
|
|
|
* |
|
16
|
|
|
* @var string |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $table; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Create a new database migration repository instance. |
|
22
|
|
|
* |
|
23
|
|
|
* @param IlluminateResolver $resolver |
|
24
|
|
|
* @param string $table |
|
25
|
|
|
*/ |
|
26
|
207 |
|
public function __construct(IlluminateResolver $resolver, string $table) |
|
27
|
|
|
{ |
|
28
|
207 |
|
$this->table = $table; |
|
29
|
|
|
|
|
30
|
207 |
|
$this->resolver = $resolver; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Resolve the database connection instance. |
|
35
|
|
|
* |
|
36
|
|
|
* @return Connection |
|
37
|
|
|
*/ |
|
38
|
11 |
|
public function getConnection() |
|
39
|
|
|
{ |
|
40
|
11 |
|
return $this->resolver->connection($this->connection); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Get the completed migrations. |
|
45
|
|
|
* |
|
46
|
|
|
* @return array |
|
47
|
|
|
*/ |
|
48
|
4 |
|
public function getRan() |
|
49
|
|
|
{ |
|
50
|
4 |
|
$qb = (new QueryBuilder())->for('m', 'migrations') |
|
51
|
|
|
->sort('m.batch', 'm.migrations') |
|
52
|
|
|
->return('m.migration') |
|
53
|
|
|
->get(); |
|
54
|
|
|
|
|
55
|
4 |
|
return $this->getConnection()->select($qb->query); |
|
56
|
|
|
|
|
57
|
|
|
// return $this->table() |
|
58
|
|
|
// ->orderBy('batch', 'asc') |
|
59
|
|
|
// ->orderBy('migration', 'asc') |
|
60
|
|
|
// ->pluck('migration')->all(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Get list of migrations. |
|
65
|
|
|
* |
|
66
|
|
|
* @param int $steps |
|
67
|
|
|
* |
|
68
|
|
|
* @return array |
|
69
|
|
|
*/ |
|
70
|
1 |
|
public function getMigrations($steps) |
|
71
|
|
|
{ |
|
72
|
1 |
|
$qb = (new QueryBuilder())->for('m', 'migrations') |
|
73
|
|
|
->filter('m.batch', '>=', 1) |
|
74
|
|
|
->sort([['m.batch', 'DESC'], ['m.migration', 'DESC']]) |
|
75
|
|
|
->limit($steps) |
|
76
|
|
|
->return(['migration' => 'm.migration', 'batch' => 'm.batch']) |
|
77
|
|
|
->get(); |
|
78
|
|
|
|
|
79
|
1 |
|
return $this->getConnection()->select($qb->query); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
1 |
|
public function getLast() |
|
83
|
|
|
{ |
|
84
|
1 |
|
$batch = $this->getLastBatchNumber(); |
|
85
|
|
|
|
|
86
|
1 |
|
$qb = (new QueryBuilder())->for('m', 'migrations') |
|
87
|
|
|
->filter('m.batch', '==', $batch) |
|
88
|
|
|
->sort('m.migration', 'desc') |
|
89
|
|
|
->return('m') |
|
90
|
|
|
->get(); |
|
91
|
|
|
|
|
92
|
1 |
|
return $this->getConnection()->select($qb->query); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Get the completed migrations with their batch numbers. |
|
97
|
|
|
* |
|
98
|
|
|
* @return array |
|
99
|
|
|
*/ |
|
100
|
1 |
|
public function getMigrationBatches() |
|
101
|
|
|
{ |
|
102
|
1 |
|
$qb = (new QueryBuilder())->for('m', 'migrations') |
|
103
|
|
|
->sort([['m.batch'], ['m.migration']]) |
|
104
|
|
|
->return(['batch' => 'm.batch', 'migration' => 'm.migration']) |
|
105
|
|
|
->get(); |
|
106
|
|
|
|
|
107
|
1 |
|
return $this->getConnection()->select($qb->query); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Log that a migration was run. |
|
112
|
|
|
* |
|
113
|
|
|
* @param string $file |
|
114
|
|
|
* @param int $batch |
|
115
|
|
|
*/ |
|
116
|
10 |
|
public function log($file, $batch) |
|
117
|
|
|
{ |
|
118
|
10 |
|
$qb = (new QueryBuilder())->insert(['migration' => $file, 'batch' => $batch], 'migrations')->get(); |
|
119
|
|
|
|
|
120
|
10 |
|
$this->getConnection()->insert($qb->query, $qb->binds); |
|
121
|
|
|
|
|
122
|
|
|
// $record = ['migration' => $file, 'batch' => $batch]; |
|
123
|
|
|
// |
|
124
|
|
|
// $this->table()->insert($record); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Remove a migration from the log. |
|
129
|
|
|
* |
|
130
|
|
|
* @param object|string $migration |
|
131
|
|
|
* |
|
132
|
|
|
* @return void |
|
133
|
|
|
*/ |
|
134
|
7 |
|
public function delete($migration) |
|
135
|
|
|
{ |
|
136
|
7 |
|
if (is_object($migration)) { |
|
137
|
1 |
|
$migration = $migration->migration; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
7 |
|
$qb = (new QueryBuilder())->for('m', 'migrations') |
|
141
|
|
|
->filter('m.migration', '==', $migration) |
|
142
|
|
|
->remove('m', 'migrations') |
|
143
|
|
|
->get(); |
|
144
|
|
|
|
|
145
|
7 |
|
$this->getConnection()->delete($qb->query, $qb->binds); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Get the next migration batch number. |
|
150
|
|
|
* |
|
151
|
|
|
* @return int |
|
152
|
|
|
*/ |
|
153
|
3 |
|
public function getNextBatchNumber() |
|
154
|
|
|
{ |
|
155
|
3 |
|
return $this->getLastBatchNumber() + 1; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Get the last migration batch number. |
|
160
|
|
|
* |
|
161
|
|
|
* @return int |
|
162
|
|
|
*/ |
|
163
|
5 |
|
public function getLastBatchNumber() |
|
164
|
|
|
{ |
|
165
|
5 |
|
$qb = new QueryBuilder(); |
|
166
|
5 |
|
$qb = $qb->for('m', 'migrations') |
|
167
|
|
|
->collect() |
|
168
|
5 |
|
->aggregate('maxBatch', $qb->max('m.batch')) |
|
169
|
|
|
->return('maxBatch') |
|
170
|
|
|
->get(); |
|
171
|
|
|
|
|
172
|
5 |
|
$results = current($this->getConnection()->select($qb->query)); |
|
173
|
5 |
|
if ($results === null) { |
|
174
|
2 |
|
$results = 0; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
5 |
|
return $results; |
|
178
|
|
|
// return $this->table()->max('batch'); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Create the migration repository data store. |
|
183
|
|
|
* |
|
184
|
|
|
* @return void |
|
185
|
|
|
*/ |
|
186
|
3 |
|
public function createRepository() |
|
187
|
|
|
{ |
|
188
|
3 |
|
$schemaManager = $this->getConnection()->getArangoClient()->schema(); |
|
189
|
|
|
|
|
190
|
3 |
|
$schemaManager->createCollection($this->table); |
|
191
|
|
|
|
|
192
|
|
|
// $schema = $this->getConnection()->getSchemaBuilder(); |
|
193
|
|
|
// |
|
194
|
|
|
// $schema->create($this->table, function ($collection) { |
|
195
|
|
|
// // The migrations collection is responsible for keeping track of which of the |
|
196
|
|
|
// // migrations have actually run for the application. We'll create the |
|
197
|
|
|
// // collection to hold the migration file's path as well as the batch ID. |
|
198
|
|
|
// $collection->increments('id'); |
|
199
|
|
|
// $collection->string('migration'); |
|
200
|
|
|
// $collection->integer('batch'); |
|
201
|
|
|
// }); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Determine if the migration repository exists. |
|
206
|
|
|
* |
|
207
|
|
|
* @return bool |
|
208
|
|
|
*/ |
|
209
|
4 |
|
public function repositoryExists() |
|
210
|
|
|
{ |
|
211
|
4 |
|
$schemaManager = $this->getConnection()->getArangoClient()->schema(); |
|
212
|
|
|
|
|
213
|
4 |
|
return $schemaManager->hasCollection($this->table); |
|
214
|
|
|
|
|
215
|
|
|
// $schema = $this->getConnection()->getSchemaBuilder(); |
|
216
|
|
|
// |
|
217
|
|
|
// return $schema->hasCollection($this->table); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* Get a query builder for the migration collection. |
|
222
|
|
|
* |
|
223
|
|
|
* @return Builder |
|
224
|
|
|
*/ |
|
225
|
|
|
protected function collection() |
|
226
|
|
|
{ |
|
227
|
|
|
return $this->getConnection()->table($this->table); |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* {@inheritdoc} |
|
232
|
|
|
* |
|
233
|
|
|
* @return Builder |
|
234
|
|
|
*/ |
|
235
|
|
|
protected function table() |
|
236
|
|
|
{ |
|
237
|
|
|
return $this->collection(); |
|
238
|
|
|
} |
|
239
|
|
|
} |
|
240
|
|
|
|