Advertisement
Guest User

Josh Ribakoff

a guest
Feb 3rd, 2010
481
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.01 KB | None | 0 0
  1. <?php
  2. /**
  3. * This source file is subject to the new BSD license that is bundled
  4. * with this package in the file LICENSE.txt.
  5. *
  6. * @copyright Copyright (c) 2008-2009 Ne8, LLC <josh.ribakoff@gmail.com>
  7. * @license New BSD License
  8. */
  9. abstract class Shuffler_DB_Refactor
  10. {
  11.     /**
  12.     * @var Shuffler_Db_Factory
  13.     */
  14.     protected $factory;
  15.    
  16.     public function __construct( Shuffler_Db_Factory $factory )
  17.     {
  18.         $this->factory = $factory;
  19.     }
  20.    
  21.     /**
  22.     * @return integer
  23.     */
  24.     abstract protected function getCurrentVersion();
  25.    
  26.     /**
  27.     * @return array
  28.     */
  29.     abstract protected function getTables();
  30.    
  31.     abstract protected function createDatabases();
  32.    
  33.     abstract protected function dropDatabases();
  34.     abstract protected function createVersionTable();
  35.     abstract protected function getSchemaNameForVersionTable();
  36.     abstract protected function getMysqlCommand();
  37.    
  38.     public function execute( $pathToScripts, $reset = false, $toVersion = null )
  39.     {
  40.         try
  41.         {
  42.             $currentVersion = $this->getCurrentVersion();
  43.         } catch ( Exception $e )
  44.         {
  45.             $this->createDatabases();
  46.             $currentVersion = 0;
  47.         }
  48.  
  49.         if( $reset || $currentVersion == 0 )
  50.         {
  51.             $this->dropDatabases();
  52.             $this->createDatabases();
  53.             $this->createVersionTable();
  54.             $currentVersion = 0;          
  55.         }                                                                  
  56.          
  57.         self::migrate( $pathToScripts, $currentVersion, $toVersion );
  58.     }
  59.    
  60.     /**
  61.     * Bring the database to a requested version #
  62.     */
  63.     function migrate( $pathToScripts, $fromVersion = 0, $toVersion = NULL )
  64.     {
  65.         // get all the database refactoring scripts
  66.         $files = glob( $pathToScripts . '/*' );
  67.         foreach( $files as $key => $val )
  68.         {
  69.             $files[ $key ] = basename( $val );
  70.         }
  71.         // sort the files in version order
  72.         asort( $files, SORT_NUMERIC );
  73.         foreach( $files as $file )
  74.         {
  75.             // get the version # from the filename
  76.             preg_match( '#^([0-9]+)_#', basename( $file ), $matches );
  77.             if( !isset( $matches[1] ) )
  78.             {
  79.                 continue;
  80.             }
  81.             $version = $matches[1];
  82.             if( $version <= $fromVersion )
  83.             {
  84.                 // already loaded this version, skip it
  85.                 continue;
  86.             }
  87.             if( !is_null( $toVersion ) )
  88.             {
  89.                 if( $version > $toVersion )
  90.                 {
  91.                     echo 'stopping short at version ' . $toVersion;
  92.                     break;
  93.                 }
  94.             }
  95.             self::runScript( $version, $pathToScripts . '/' . $file );
  96.         }
  97.     }
  98.    
  99.     /**
  100.     * run the version + script as php or SQL
  101.     * updates the version table
  102.     */
  103.     function runScript( $version, $file )
  104.     {
  105.         ob_implicit_flush();
  106.         echo basename($file) .  "\n";
  107.        
  108.         $schema = '';
  109.         if( $version != 1 )
  110.         {
  111.             $schema = $this->getSchemaNameForVersionTable();
  112.         }
  113.        
  114.         if( substr( $file, -3 ) == 'php' )
  115.         {
  116.             if( $version != 1 )
  117.             {
  118.                 mysql_select_db( $schema );
  119.             }
  120.             require( $file );
  121.         }
  122.         else // or as sql
  123.         {
  124.            
  125.             $cmd = $this->getMysqlCommand() . ' ' . $schema . ' < "' . $file . '"';
  126.             //echo( $cmd );
  127.             passthru( $cmd );
  128.         }
  129.         mysql_query( 'UPDATE `version` SET `version` = ' . (int)$version );
  130.     }
  131.  
  132.     /**
  133.     * Drop requested tables
  134.     *
  135.     * @param Shuffler_Factory
  136.     * @param array of tables
  137.     */
  138.     function dropTables( $factory, $tables )
  139.     {    
  140.         foreach( $tables as $table )
  141.         {
  142.             mysql_query( 'DROP TABLE `' . $table . '`' );
  143.         }                    
  144.     }
  145. }
  146.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement