1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kaliop\eZMigrationBundle\Core\DefinitionParser; |
4
|
|
|
|
5
|
|
|
use Kaliop\eZMigrationBundle\API\DefinitionParserInterface; |
6
|
|
|
use Kaliop\eZMigrationBundle\API\Value\MigrationDefinition; |
7
|
|
|
use Kaliop\eZMigrationBundle\API\Value\MigrationStep; |
8
|
|
|
|
9
|
|
|
class SQLDefinitionParser implements DefinitionParserInterface |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Array of supported DB servers |
13
|
|
|
* @var array $supportedDatabases |
14
|
|
|
*/ |
15
|
|
|
private $supportedDatabases; |
16
|
|
|
|
17
|
76 |
|
public function __construct(array $supportedDatabases = array('mysql', 'postgres')) |
18
|
|
|
{ |
19
|
76 |
|
$this->supportedDatabases = $supportedDatabases; |
20
|
76 |
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Tells whether the given file can be handled by this handler, by checking e.g. the suffix |
24
|
|
|
* |
25
|
|
|
* @param string $migrationName typically a filename |
26
|
|
|
* @return bool |
27
|
|
|
*/ |
28
|
74 |
|
public function supports($migrationName) |
29
|
|
|
{ |
30
|
74 |
|
return pathinfo($migrationName, PATHINFO_EXTENSION) == 'sql'; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Parses a migration definition file, and returns the list of actions to take |
35
|
|
|
* |
36
|
|
|
* @param MigrationDefinition $definition |
37
|
|
|
* @return MigrationDefinition |
38
|
|
|
*/ |
39
|
5 |
|
public function parseMigrationDefinition(MigrationDefinition $definition) |
40
|
|
|
{ |
41
|
5 |
|
$dbType = $this->getDBFromFile($definition->name); |
42
|
|
|
|
43
|
5 |
|
if (!in_array($dbType, $this->supportedDatabases)) |
44
|
|
|
{ |
45
|
1 |
|
return new MigrationDefinition( |
46
|
1 |
|
$definition->name, |
47
|
1 |
|
$definition->path, |
48
|
1 |
|
$definition->rawDefinition, |
49
|
1 |
|
MigrationDefinition::STATUS_INVALID, |
50
|
1 |
|
array(), |
51
|
1 |
|
"Unsupported or missing database: '$dbType'. The database name is the part of the filename after the 1st underscore" |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
4 |
|
return new MigrationDefinition( |
56
|
4 |
|
$definition->name, |
57
|
4 |
|
$definition->path, |
58
|
4 |
|
$definition->rawDefinition, |
59
|
4 |
|
MigrationDefinition::STATUS_PARSED, |
60
|
|
|
array( |
61
|
4 |
|
new MigrationStep('sql', array($dbType => $definition->rawDefinition), array('path' => $definition->path)) |
62
|
|
|
) |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
// allow both aaaa_mysql_etc.sql and aaaa_mysql.sql |
67
|
5 |
|
protected function getDBFromFile($fileName) |
68
|
|
|
{ |
69
|
5 |
|
$parts = explode('_', preg_replace('/\.sql$/', '', $fileName)); |
70
|
5 |
|
return isset($parts[1]) ? $parts[1] : null; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|