Changeset 21782
- Timestamp:
- 09/08/09 14:49:32 (4 years ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
plugins/sfPropelMigrationsLightPlugin/trunk/lib/sfMigrator.class.php
r10225 r21782 4 4 * This file is part of the sfPropelMigrationsLightPlugin package. 5 5 * (c) 2006-2008 Martin Kreidenweis <sf@kreidenweis.com> 6 * 6 * 7 7 * For the full copyright and license information, please view the LICENSE 8 8 * file that was distributed with this source code. … … 11 11 /** 12 12 * Manage all calls to the sfMigration class instances. 13 * 13 * 14 14 * @package symfony 15 15 * @subpackage plugin … … 21 21 /** 22 22 * Migration filenames. 23 * 23 * 24 24 * @var array $migrations 25 25 */ … … 28 28 /** 29 29 * Perform an update on the database. 30 * 30 * 31 31 * @param string $sql 32 * 32 * 33 33 * @return integer 34 34 */ … … 42 42 /** 43 43 * Perform a query on the database. 44 * 44 * 45 45 * @param string $sql 46 46 * @param string $fetchmode 47 * 47 * 48 48 * @return mixed 49 49 */ … … 51 51 { 52 52 $con = Propel::getConnection(); 53 53 54 54 if ($con instanceof PropelPDO) 55 55 { … … 75 75 /** 76 76 * Execute migrations. 77 * 78 * @param integer $destVersion Version number to migrate to, defaults to 77 * 78 * @param integer $destVersion Version number to migrate to, defaults to 79 79 * the max existing 80 * 80 * 81 81 * @return integer Number of executed migrations 82 82 */ … … 119 119 /** 120 120 * Generate a new migration stub 121 * 121 * 122 122 * @param string $name Name of the new migration 123 * 123 * 124 124 * @return string Filename of the new migration file 125 125 */ 126 public function generateMigration($name) 126 public function generateMigration($name) 127 127 { 128 128 // calculate version number for new migration … … 177 177 /** 178 178 * Get the list of migration filenames. 179 * 179 * 180 180 * @return array 181 181 */ … … 188 188 * @return integer The lowest migration that exists 189 189 */ 190 public function getMinVersion() 190 public function getMinVersion() 191 191 { 192 192 return $this->migrations ? $this->getMigrationNumberFromFile($this->migrations[0]) : 0; … … 205 205 /** 206 206 * Get the current schema version from the database. 207 * 208 * If no schema version is currently stored in the database, one is created 207 * 208 * If no schema version is currently stored in the database, one is created 209 209 * and initialized with 0. 210 210 * 211 211 * @return integer 212 212 */ 213 public function getCurrentVersion() 214 { 215 // check if schema_info table exists 216 $result = $this->executeQuery('SHOW TABLES LIKE "schema_info"'); 217 218 if ($result instanceof PDOStatement ? $result->rowCount() : $result->getRecordCount()) 213 public function getCurrentVersion() 214 { 215 try 219 216 { 220 217 $result = $this->executeQuery('SELECT version FROM schema_info'); 221 222 218 if ($result instanceof PDOStatement) 223 219 { … … 228 224 if ($result->next()) 229 225 { 230 $currentVersion = $result->getInt( "version");226 $currentVersion = $result->getInt('version'); 231 227 } 232 228 else … … 236 232 } 237 233 } 238 else239 { 240 // no schema_info table exists yet so we create it241 $this->executeUpdate('CREATE TABLE schema_info (version INTEGER UNSIGNED)');234 catch (Exception $e) 235 { 236 // assume no schema_info table exists yet so we create it 237 $this->executeUpdate('CREATE TABLE schema_info (version INTEGER)'); 242 238 243 239 // and insert the version record as 0 244 $this->executeUpdate('INSERT INTO schema_info SET version = 0');240 $this->executeUpdate('INSERT INTO schema_info (version) VALUES (0)'); 245 241 $currentVersion = 0; 246 242 } … … 248 244 return $currentVersion; 249 245 } 250 246 251 247 /** 252 248 * Get the number encoded in the given migration file name. 253 * 249 * 254 250 * @param string $file The filename to look at 255 * 251 * 256 252 * @return integer 257 253 */ 258 public function getMigrationNumberFromFile($file) 254 public function getMigrationNumberFromFile($file) 259 255 { 260 256 $number = substr(basename($file), 0, 3); … … 270 266 /** 271 267 * Get the directory where migration classes are saved. 272 * 268 * 273 269 * @return string 274 270 */ 275 public function getMigrationsDir() 271 public function getMigrationsDir() 276 272 { 277 273 return sfConfig::get('sf_data_dir').DIRECTORY_SEPARATOR.'migrations'; … … 280 276 /** 281 277 * Get the directory where migration fixtures are saved. 282 * 278 * 283 279 * @return string 284 280 */ … … 290 286 /** 291 287 * Write the given version as current version to the database. 292 * 288 * 293 289 * @param integer $version New current version 294 290 */ … … 302 298 /** 303 299 * Migrate down, from version $from to version $to. 304 * 300 * 305 301 * @param integer $from 306 302 * @param integer $to 307 * 303 * 308 304 * @return integer Number of executed migrations 309 305 */ … … 341 337 /** 342 338 * Migrate up, from version $from to version $to. 343 * 339 * 344 340 * @param integer $from 345 341 * @param integer $to … … 379 375 /** 380 376 * Get the migration object for the given version. 381 * 377 * 382 378 * @param integer $version 383 * 379 * 384 380 * @return sfMigration 385 381 */ … … 399 395 * 400 396 * @param integer $version 401 * 397 * 402 398 * @return string Filename 403 399 */ … … 421 417 $maxVersion = $this->getMaxVersion(); 422 418 423 if (1 != $minVersion) 419 if (1 != $minVersion) 424 420 { 425 421 throw new sfInitializationException('First migration is not migration 1. Some migration files may be missing.'); … … 432 428 } 433 429 } 434 430 435 431 /** 436 432 * Auto generate logic for the first migration. 437 * 433 * 438 434 * @param string $name 439 435 * @param string $newVersion