-
Notifications
You must be signed in to change notification settings - Fork 122
Expand file tree
/
Copy pathMigrations.php
More file actions
175 lines (158 loc) · 5.02 KB
/
Migrations.php
File metadata and controls
175 lines (158 loc) · 5.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
declare(strict_types=1);
/**
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @license https://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Migrations;
use Migrations\Migration\BackendInterface;
use Migrations\Migration\BuiltinBackend;
/**
* The Migrations class is responsible for handling migrations command
* within a non-shell application.
*/
class Migrations
{
/**
* Default options to use
*
* @var array<string, mixed>
*/
protected array $default = [];
/**
* Current command being run.
* Useful if some logic needs to be applied in the ConfigurationTrait depending
* on the command
*
* @var string
*/
protected string $command = '';
/**
* Constructor
*
* @param array<string, mixed> $default Default option to be used when calling a method.
* Available options are :
* - `connection` The datasource connection to use
* - `source` The folder where migrations are in
* - `plugin` The plugin containing the migrations
*/
public function __construct(array $default = [])
{
if ($default) {
$this->default = $default;
}
}
/**
* Gets the command
*
* @return string Command name
*/
public function getCommand(): string
{
return $this->command;
}
/**
* Get the Migrations interface backend.
*
* @return \Migrations\Migration\BackendInterface
*/
protected function getBackend(): BackendInterface
{
return new BuiltinBackend($this->default);
}
/**
* Returns the status of each migrations based on the options passed
*
* @param array<string, mixed> $options Options to pass to the command
* Available options are :
*
* - `format` Format to output the response. Can be 'json'
* - `connection` The datasource connection to use
* - `source` The folder where migrations are in
* - `plugin` The plugin containing the migrations
* @return array The migrations list and their statuses
*/
public function status(array $options = []): array
{
$backend = $this->getBackend();
return $backend->status($options);
}
/**
* Migrates available migrations
*
* @param array<string, mixed> $options Options to pass to the command
* Available options are :
*
* - `target` The version number to migrate to. If not provided, will migrate
* everything it can
* - `connection` The datasource connection to use
* - `source` The folder where migrations are in
* - `plugin` The plugin containing the migrations
* - `date` The date to migrate to
* @return bool Success
*/
public function migrate(array $options = []): bool
{
$backend = $this->getBackend();
return $backend->migrate($options);
}
/**
* Rollbacks migrations
*
* @param array<string, mixed> $options Options to pass to the command
* Available options are :
*
* - `target` The version number to migrate to. If not provided, will only migrate
* the last migrations registered in the phinx log
* - `connection` The datasource connection to use
* - `source` The folder where migrations are in
* - `plugin` The plugin containing the migrations
* - `date` The date to rollback to
* @return bool Success
*/
public function rollback(array $options = []): bool
{
$backend = $this->getBackend();
return $backend->rollback($options);
}
/**
* Marks a migration as migrated
*
* @param int|string|null $version The version number of the migration to mark as migrated
* @param array<string, mixed> $options Options to pass to the command
* Available options are :
*
* - `connection` The datasource connection to use
* - `source` The folder where migrations are in
* - `plugin` The plugin containing the migrations
* @return bool Success
*/
public function markMigrated(int|string|null $version = null, array $options = []): bool
{
$backend = $this->getBackend();
return $backend->markMigrated($version, $options);
}
/**
* Seed the database using a seed file
*
* @param array<string, mixed> $options Options to pass to the command
* Available options are :
*
* - `connection` The datasource connection to use
* - `source` The folder where migrations are in
* - `plugin` The plugin containing the migrations
* - `seed` The seed file to use
* @return bool Success
*/
public function seed(array $options = []): bool
{
$backend = $this->getBackend();
return $backend->seed($options);
}
}