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