-
Notifications
You must be signed in to change notification settings - Fork 412
/
Copy pathPropelMigrationTask.php
114 lines (104 loc) · 4.36 KB
/
PropelMigrationTask.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
<?php
/**
* This file is part of the Propel package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/
require_once dirname(__FILE__) . '/BasePropelMigrationTask.php';
require_once dirname(__FILE__) . '/../util/PropelMigrationManager.php';
/**
* This Task executes the next migrations up
*
* @author Francois Zaninotto
* @package propel.generator.task
*/
class PropelMigrationTask extends BasePropelMigrationTask
{
/**
* Main method builds all the targets for a typical propel project.
*/
public function main()
{
$manager = new PropelMigrationManager();
$manager->setConnections($this->getGeneratorConfig()->getBuildConnections());
$manager->setMigrationTable($this->getMigrationTable());
$manager->setMigrationDir($this->getOutputDirectory());
if (!$nextMigrationTimestamp = $manager->getFirstUpMigrationTimestamp()) {
$this->log('All migrations were already executed - nothing to migrate.');
return false;
}
$timestamps = $manager->getValidMigrationTimestamps();
if (count($timestamps) > 1) {
$this->log(sprintf('%d migrations to execute', count($timestamps)));
}
foreach ($timestamps as $timestamp) {
$this->log(sprintf(
'Executing migration %s up',
$manager->getMigrationClassName($timestamp)
));
$migration = $manager->getMigrationObject($timestamp);
if (false === $migration->preUp($manager)) {
$this->log(sprintf(
'[%s] preUp() returned false. Aborting migration.',
$manager->getMigrationClassName($timestamp)
));
return false;
}
foreach ($migration->getUpSQL() as $datasource => $sql) {
$connection = $manager->getConnection($datasource);
$this->log(sprintf(
'Connecting to database "%s" using DSN "%s"',
$datasource,
$connection['dsn']
), Project::MSG_VERBOSE);
$pdo = $manager->getPdoConnection($datasource);
$res = 0;
$statements = PropelSQLParser::parseString($sql);
foreach ($statements as $statement) {
try {
$this->log(sprintf('Executing statement "%s"', $statement), Project::MSG_VERBOSE);
$stmt = $pdo->prepare($statement);
$stmt->execute();
$res++;
} catch (PDOException $e) {
$this->log(sprintf(
'[%s] Failed to execute SQL "%s"',
$manager->getMigrationClassName($timestamp),
$statement
));
// continue
}
}
if (!$res) {
$this->log('No statement was executed. The version was not updated.');
$this->log(sprintf(
'Please review the code in "%s"',
$manager->getMigrationDir() . DIRECTORY_SEPARATOR . $manager->getMigrationClassName($timestamp)
));
$this->log('Migration aborted');
return false;
}
$this->log(sprintf(
'%d of %d SQL statements executed successfully on datasource "%s"',
$res,
count($statements),
$datasource
));
}
// migrations for datasources have passed - update the timestamp
// for all datasources
foreach ($manager->getConnections() as $datasource => $connection) {
$manager->updateLatestMigrationTimestamp($datasource, $timestamp);
$this->log(sprintf(
'Updated latest migration date to %d for datasource "%s"',
$timestamp,
$datasource
), Project::MSG_VERBOSE);
}
$migration->postUp($manager);
}
$this->log('Migration complete. No further migration to execute.');
}
}