/usr/share/php/Horde/Db/Migration/Migrator.php is in php-horde-db 2.3.4-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 | <?php
/**
* Copyright 2007 Maintainable Software, LLC
* Copyright 2006-2016 Horde LLC (http://www.horde.org/)
*
* @author Mike Naberezny <mike@maintainable.com>
* @author Derek DeVries <derek@maintainable.com>
* @author Chuck Hagenbuch <chuck@horde.org>
* @license http://www.horde.org/licenses/bsd
* @category Horde
* @package Db
* @subpackage Migration
*/
/**
* @author Mike Naberezny <mike@maintainable.com>
* @author Derek DeVries <derek@maintainable.com>
* @author Chuck Hagenbuch <chuck@horde.org>
* @license http://www.horde.org/licenses/bsd
* @category Horde
* @package Db
* @subpackage Migration
*/
class Horde_Db_Migration_Migrator
{
/**
* @var string
*/
protected $_direction = null;
/**
* @var string
*/
protected $_migrationsPath = null;
/**
* @var integer
*/
protected $_targetVersion = null;
/**
* @var string
*/
protected $_schemaTableName = 'schema_info';
/**
* Constructor.
*
* @param Horde_Db_Adapter $connection A DB connection object.
* @param Horde_Log_Logger $logger A logger object.
* @param array $options Additional options for the migrator:
* - migrationsPath: directory with the
* migration files.
* - schemaTableName: table for storing
* the schema version.
*
* @throws Horde_Db_Migration_Exception
*/
public function __construct(Horde_Db_Adapter $connection,
Horde_Log_Logger $logger = null,
array $options = array())
{
if (!$connection->supportsMigrations()) {
throw new Horde_Db_Migration_Exception('This database does not yet support migrations');
}
$this->_connection = $connection;
$this->_logger = $logger ? $logger : new Horde_Support_Stub();
$this->_inflector = new Horde_Support_Inflector();
if (isset($options['migrationsPath'])) {
$this->_migrationsPath = $options['migrationsPath'];
}
if (isset($options['schemaTableName'])) {
$this->_schemaTableName = $options['schemaTableName'];
}
$this->_initializeSchemaInformation();
}
/**
* @param string $targetVersion
*/
public function migrate($targetVersion = null)
{
$currentVersion = $this->getCurrentVersion();
if ($targetVersion == null || $currentVersion < $targetVersion) {
$this->up($targetVersion);
} elseif ($currentVersion > $targetVersion) {
// migrate down
$this->down($targetVersion);
}
}
/**
* @param string $targetVersion
*/
public function up($targetVersion = null)
{
$this->_targetVersion = $targetVersion;
$this->_direction = 'up';
$this->_doMigrate();
}
/**
* @param string $targetVersion
*/
public function down($targetVersion = null)
{
$this->_targetVersion = $targetVersion;
$this->_direction = 'down';
$this->_doMigrate();
}
/**
* @return integer
*/
public function getCurrentVersion()
{
return in_array($this->_schemaTableName, $this->_connection->tables())
? $this->_connection->selectValue('SELECT version FROM ' . $this->_schemaTableName)
: 0;
}
/**
* @return integer
*/
public function getTargetVersion()
{
$migrations = array();
foreach ($this->_getMigrationFiles() as $migrationFile) {
list($version, $name) = $this->_getMigrationVersionAndName($migrationFile);
$this->_assertUniqueMigrationVersion($migrations, $version);
$migrations[$version] = $name;
}
// Sort by version.
uksort($migrations, 'strnatcmp');
return key(array_reverse($migrations, true));
}
/**
* @param string $migrationsPath Path to migration files.
*/
public function setMigrationsPath($migrationsPath)
{
$this->_migrationsPath = $migrationsPath;
}
/**
* @param Horde_Log_Logger $logger
*/
public function setLogger(Horde_Log_Logger $logger)
{
$this->_logger = $logger;
}
/**
* @param Horde_Support_Inflector $inflector
*/
public function setInflector(Horde_Support_Inflector $inflector)
{
$this->_inflector = $inflector;
}
/**
* Performs the migration.
*/
protected function _doMigrate()
{
foreach ($this->_getMigrationClasses() as $migration) {
if ($this->_hasReachedTargetVersion($migration->version)) {
$this->_logger->info('Reached target version: ' . $this->_targetVersion);
return;
}
if ($this->_isIrrelevantMigration($migration->version)) {
continue;
}
$this->_logger->info('Migrating ' . ($this->_direction == 'up' ? 'to ' : 'from ') . get_class($migration) . ' (' . $migration->version . ')');
$migration->migrate($this->_direction);
$this->_setSchemaVersion($migration->version);
}
}
/**
* @return array
*/
protected function _getMigrationClasses()
{
$migrations = array();
foreach ($this->_getMigrationFiles() as $migrationFile) {
require_once $migrationFile;
list($version, $name) = $this->_getMigrationVersionAndName($migrationFile);
$this->_assertUniqueMigrationVersion($migrations, $version);
$migrations[$version] = $this->_getMigrationClass($name, $version);
}
// Sort by version.
uksort($migrations, 'strnatcmp');
$sorted = array_values($migrations);
return $this->_isDown() ? array_reverse($sorted) : $sorted;
}
/**
* @param array $migrations
* @param integer $version
*
* @throws Horde_Db_Migration_Exception
*/
protected function _assertUniqueMigrationVersion($migrations, $version)
{
if (isset($migrations[$version])) {
throw new Horde_Db_Migration_Exception('Multiple migrations have the version number ' . $version);
}
}
/**
* Returns the list of migration files.
*
* @return array
*/
protected function _getMigrationFiles()
{
return array_keys(
iterator_to_array(
new RegexIterator(
new RecursiveIteratorIterator(
new RecursiveDirectoryIterator(
$this->_migrationsPath
)
),
'/' . preg_quote(DIRECTORY_SEPARATOR, '/') . '\d+_.*\.php$/',
RecursiveRegexIterator::MATCH,
RegexIterator::USE_KEY
)
)
);
}
/**
* Actually returns object, and not class.
*
* @param string $migrationName
* @param integer $version
*
* @return Horde_Db_Migration_Base
*/
protected function _getMigrationClass($migrationName, $version)
{
$className = $this->_inflector->camelize($migrationName);
$class = new $className($this->_connection, $version);
$class->setLogger($this->_logger);
return $class;
}
/**
* @param string $migrationFile
*
* @return array ($version, $name)
*/
protected function _getMigrationVersionAndName($migrationFile)
{
preg_match_all('/([0-9]+)_([_a-z0-9]*).php/', $migrationFile, $matches);
return array($matches[1][0], $matches[2][0]);
}
/**
* @TODO
*/
protected function _initializeSchemaInformation()
{
if (in_array($this->_schemaTableName, $this->_connection->tables())) {
return;
}
$schemaTable = $this->_connection->createTable($this->_schemaTableName, array('autoincrementKey' => false));
$schemaTable->column('version', 'integer');
$schemaTable->end();
$this->_connection->insert('INSERT INTO ' . $this->_schemaTableName . ' (version) VALUES (0)', null, null, null, 1);
}
/**
* @param integer $version
*/
protected function _setSchemaVersion($version)
{
$version = $this->_isDown() ? $version - 1 : $version;
if ($version) {
$sql = 'UPDATE ' . $this->_schemaTableName . ' SET version = ' . (int)$version;
$this->_connection->update($sql);
} else {
$this->_connection->dropTable($this->_schemaTableName);
}
}
/**
* @return boolean
*/
protected function _isUp()
{
return $this->_direction == 'up';
}
/**
* @return boolean
*/
protected function _isDown()
{
return $this->_direction == 'down';
}
/**
* @return boolean
*/
protected function _hasReachedTargetVersion($version)
{
if ($this->_targetVersion === null) {
return false;
}
return ($this->_isUp() && $version - 1 >= $this->_targetVersion) ||
($this->_isDown() && $version <= $this->_targetVersion);
}
/**
* @param integer $version
*
* @return boolean
*/
protected function _isIrrelevantMigration($version)
{
return ($this->_isUp() && $version <= self::getCurrentVersion()) ||
($this->_isDown() && $version > self::getCurrentVersion());
}
}
|