forked from zarplata/mongodb-migrations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAbstractMigration.php
160 lines (136 loc) · 3.85 KB
/
AbstractMigration.php
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
<?php
/*
* This file is part of the AntiMattr MongoDB Migrations Library, a library by Matthew Fitzgerald.
*
* (c) 2014 Matthew Fitzgerald
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace AntiMattr\MongoDB\Migrations;
use AntiMattr\MongoDB\Migrations\Exception\AbortException;
use AntiMattr\MongoDB\Migrations\Exception\IrreversibleException;
use AntiMattr\MongoDB\Migrations\Exception\SkipException;
use MongoDB\Collection;
use MongoDB\Database;
/**
* @author Matthew Fitzgerald <matthewfitz@gmail.com>
*/
abstract class AbstractMigration
{
/**
* @var \AntiMattr\MongoDB\Migrations\Configuration\Configuration
*/
private $configuration;
/**
* @var \AntiMattr\MongoDB\Migrations\OutputWriter
*/
private $outputWriter;
/**
* @var \AntiMattr\MongoDB\Migrations\Version
*/
protected $version;
public function __construct(Version $version)
{
$this->configuration = $version->getConfiguration();
$this->outputWriter = $this->configuration->getOutputWriter();
$this->version = $version;
}
/**
* Get custom migration description.
*
* @return string
*/
abstract public function getDescription();
abstract public function up(Database $db);
abstract public function down(Database $db);
/**
* @param \MongoDB\Collection
*/
protected function analyze(Collection $collection)
{
$this->version->analyze($collection);
}
/**
* @param \MongoDB\Database
* @param string $filename
*/
protected function executeScript(Database $db, $filename)
{
return $this->version->executeScript($db, $filename);
}
/**
* @param string
*/
protected function write($message)
{
$this->outputWriter->write($message);
}
/**
* @param string $message
*
* @throws AntiMattr\MongoDB\Migrations\Exception\IrreversibleException
*/
protected function throwIrreversibleMigrationException($message = null)
{
if (null === $message) {
$message = 'This migration is irreversible and cannot be reverted.';
}
throw new IrreversibleException($message);
}
/**
* Print a warning message if the condition evalutes to TRUE.
*
* @param bool $condition
* @param string $message
*/
public function warnIf($condition, $message = '')
{
$message = (strlen($message)) ? $message : 'Unknown Reason';
if (true === $condition) {
$this->outputWriter->write(' <warning>Warning during ' . $this->version->getExecutionState() . ': ' . $message . '</warning>');
}
}
/**
* Abort the migration if the condition evalutes to TRUE.
*
* @param bool $condition
* @param string $message
*
* @throws AntiMattr\MongoDB\Migrations\Exception\AbortException
*/
public function abortIf($condition, $message = '')
{
$message = (strlen($message)) ? $message : 'Unknown Reason';
if (true === $condition) {
throw new AbortException($message);
}
}
/**
* Skip this migration (but not the next ones) if condition evalutes to TRUE.
*
* @param bool $condition
* @param string $message
*
* @throws AntiMattr\MongoDB\Migrations\Exception\SkipException
*/
public function skipIf($condition, $message = '')
{
$message = (strlen($message)) ? $message : 'Unknown Reason';
if (true === $condition) {
throw new SkipException($message);
}
}
public function preUp(Database $db)
{
}
public function postUp(Database $db)
{
}
public function preDown(Database $db)
{
}
public function postDown(Database $db)
{
}
}