|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace AbterPhp\Framework\Databases\Migrations; |
|
6
|
|
|
|
|
7
|
|
|
use AbterPhp\Framework\Filesystem\FileFinder; |
|
8
|
|
|
use DateTime; |
|
9
|
|
|
use Opulence\Databases\IConnection; |
|
10
|
|
|
use Opulence\Databases\Migrations\Migration; |
|
11
|
|
|
|
|
12
|
|
|
class BaseMigration extends Migration |
|
13
|
|
|
{ |
|
14
|
|
|
const FILENAME = 'foo-bar'; |
|
15
|
|
|
|
|
16
|
|
|
const UP = 'up'; |
|
17
|
|
|
const DOWN = 'down'; |
|
18
|
|
|
|
|
19
|
|
|
/** @var FileFinder */ |
|
20
|
|
|
protected $fileFinder; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Init constructor. |
|
24
|
|
|
* |
|
25
|
|
|
* @param IConnection $connection |
|
26
|
|
|
* @param string $migrationsPath |
|
27
|
|
|
* @param string $driver |
|
28
|
|
|
*/ |
|
29
|
|
|
public function __construct(IConnection $connection, FileFinder $fileFinder) |
|
30
|
|
|
{ |
|
31
|
|
|
parent::__construct($connection); |
|
32
|
|
|
|
|
33
|
|
|
$this->fileFinder = $fileFinder; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Gets the creation date, which is used for ordering |
|
38
|
|
|
* |
|
39
|
|
|
* @return DateTime The date this migration was created |
|
40
|
|
|
*/ |
|
41
|
|
|
public static function getCreationDate(): DateTime |
|
42
|
|
|
{ |
|
43
|
|
|
return new DateTime(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Executes the query that rolls back the migration |
|
48
|
|
|
*/ |
|
49
|
|
|
public function down(): void |
|
50
|
|
|
{ |
|
51
|
|
|
$sql = $this->load(static::FILENAME, static::DOWN); |
|
52
|
|
|
$statement = $this->connection->prepare($sql); |
|
53
|
|
|
$statement->execute(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Executes the query that commits the migration |
|
58
|
|
|
*/ |
|
59
|
|
|
public function up(): void |
|
60
|
|
|
{ |
|
61
|
|
|
$sql = $this->load(static::FILENAME, static::UP); |
|
62
|
|
|
$statement = $this->connection->prepare($sql); |
|
63
|
|
|
$statement->execute(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param string $filename |
|
68
|
|
|
* @param string $direction |
|
69
|
|
|
* |
|
70
|
|
|
* @return string |
|
71
|
|
|
*/ |
|
72
|
|
|
protected function load(string $filename, string $direction): string |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->fileFinder->read(sprintf('%s/%s', $direction, $filename)); |
|
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|