[ Index ]
 

Code source de eZ Publish 3.9.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/bin/php/ -> ezsqldiff.php (source)

   1  #!/usr/bin/env php
   2  <?php
   3  //
   4  // Created on: <19-Mar-2004 09:51:56 amos>
   5  //
   6  // SOFTWARE NAME: eZ publish
   7  // SOFTWARE RELEASE: 3.9.0
   8  // BUILD VERSION: 17785
   9  // COPYRIGHT NOTICE: Copyright (C) 1999-2006 eZ systems AS
  10  // SOFTWARE LICENSE: GNU General Public License v2.0
  11  // NOTICE: >
  12  //   This program is free software; you can redistribute it and/or
  13  //   modify it under the terms of version 2.0  of the GNU General
  14  //   Public License as published by the Free Software Foundation.
  15  //
  16  //   This program is distributed in the hope that it will be useful,
  17  //   but WITHOUT ANY WARRANTY; without even the implied warranty of
  18  //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19  //   GNU General Public License for more details.
  20  //
  21  //   You should have received a copy of version 2.0 of the GNU General
  22  //   Public License along with this program; if not, write to the Free
  23  //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  24  //   MA 02110-1301, USA.
  25  //
  26  //
  27  
  28  include_once ( 'lib/ezutils/classes/ezcli.php' );
  29  include_once ( 'kernel/classes/ezscript.php' );
  30  
  31  $cli =& eZCLI::instance();
  32  $script =& eZScript::instance( array( 'description' => ( "eZ publish SQL diff\n\n" .
  33                                                           "Displays differences between two database schemas,\n" .
  34                                                           "and sets exit code based whether there is a difference or not\n" .
  35                                                           "\n" .
  36                                                           "ezsqldiff.php --type mysql --user=root stable32 stable33" ),
  37                                        'use-session' => false,
  38                                        'use-modules' => true,
  39                                        'use-extensions' => true ) );
  40  
  41  $script->startup();
  42  
  43  $options = $script->getOptions( "[source-type:][source-host:][source-user:][source-password;][source-socket:]" .
  44                                  "[match-type:][match-host:][match-user:][match-password;][match-socket:]" .
  45                                  "[t:|type:][host:][u:|user:][p:|password;][socket:]" .
  46                                  "[lint-check]" .
  47                                  "[reverse][check-only]",
  48                                  "[source][match]",
  49                                  array( 'source-type' => ( "Which database type to use for source, can be one of:\n" .
  50                                                            "mysql, postgresql" ),
  51                                         'source-host' => "Connect to host source database",
  52                                         'source-user' => "User for login to source database",
  53                                         'source-password' => "Password to use when connecting to source database",
  54                                         'source-socket' => 'Socket to connect to source database (only for MySQL)',
  55                                         'match-type' => ( "Which database type to use for match, can be one of:\n" .
  56                                                           "mysql, postgresql" ),
  57                                         'match-host' => "Connect to host match database",
  58                                         'match-user' => "User for login to match database",
  59                                         'match-password' => "Password to use when connecting to match database",
  60                                         'match-socket' => 'Socket to connect to match database (only for MySQL)',
  61                                         'type' => ( "Which database type to use for match and source, can be one of:\n" .
  62                                                     "mysql, postgresql" ),
  63                                         'host' => "Connect to host match and source database",
  64                                         'user' => "User for login to match and source database",
  65                                         'password' => "Password to use when connecting to match and source database",
  66                                         'socket' => 'Socket to connect to match and source database (only for MySQL)',
  67                                         'reverse' => "Reverse the differences",
  68                                         'check-only' => "Don't show SQLs for the differences, just set exit code and return"
  69                                         ) );
  70  $script->initialize();
  71  
  72  if ( count( $options['arguments'] ) < 1 )
  73  {
  74      $cli->error( "Missing source database" );
  75      $script->shutdown( 1 );
  76  }
  77  
  78  if ( count( $options['arguments'] ) < 2 and
  79       !$options['lint-check'] )
  80  {
  81      $cli->error( "Missing match database" );
  82      $script->shutdown( 1 );
  83  }
  84  
  85  $sourceType = $options['source-type'] ? $options['source-type'] : $options['type'];
  86  $sourceDBHost = $options['source-host'] ? $options['source-host'] : $options['host'];
  87  $sourceDBUser = $options['source-user'] ? $options['source-user'] : $options['user'];
  88  $sourceDBPassword = $options['source-password'] ? $options['source-password'] : $options['password'];
  89  $sourceDBSocket = $options['source-socket'] ? $options['source-socket'] : $options['socket'];
  90  $sourceDB = $options['arguments'][0];
  91  
  92  if( !is_string( $sourceDBPassword ) )
  93      $sourceDBPassword = '';
  94  
  95  $matchType = $options['match-type'] ? $options['match-type'] : $options['type'];
  96  $matchDBHost = $options['match-host'] ? $options['match-host'] : $options['host'];
  97  $matchDBUser = $options['match-user'] ? $options['match-user'] : $options['user'];
  98  $matchDBPassword = $options['match-password'] ? $options['match-password'] : $options['password'];
  99  $matchDBSocket = $options['match-socket'] ? $options['match-socket'] : $options['socket'];
 100  $matchDB = count( $options['arguments'] ) >= 2 ? $options['arguments'][1] : '';
 101  
 102  if ( !is_string( $matchDBPassword ) )
 103      $matchDBPassword = '';
 104  
 105  if ( strlen( trim( $sourceType ) ) == 0 )
 106  {
 107      $cli->error( "No source type chosen" );
 108      $script->shutdown( 1 );
 109  }
 110  if ( strlen( trim( $matchType ) ) == 0 )
 111  {
 112      if ( !$options['lint-check'] )
 113      {
 114          $cli->error( "No match type chosen" );
 115          $script->shutdown( 1 );
 116      }
 117  }
 118  
 119  $ini =& eZINI::instance();
 120  
 121  function &loadDatabaseSchema( $type, $host, $user, $password, $socket, $db, &$cli )
 122  {
 123      $dbSchema = false;
 124      if ( file_exists( $db ) and is_file( $db ) )
 125      {
 126          include_once ( 'lib/ezdbschema/classes/ezdbschema.php' );
 127          $dbSchema = eZDBSchema::instance( array( 'type' => $type,
 128                                                   'schema' => eZDBSchema::read( $db ) ) );
 129          return $dbSchema;
 130      }
 131      else
 132      {
 133          include_once ( 'lib/ezdbschema/classes/ezdbschema.php' );
 134          include_once ( 'lib/ezdb/classes/ezdb.php' );
 135          $parameters = array( 'use_defaults' => false,
 136                               'server' => $host,
 137                               'user' => $user,
 138                               'password' => $password,
 139                               'database' => $db );
 140          if ( $socket )
 141              $parameters['socket'] = $socket;
 142          $dbInstance =& eZDB::instance( 'ez' . $type,
 143                                         $parameters,
 144                                         true );
 145  
 146          if ( !is_object( $dbInstance ) )
 147          {
 148              $cli->error( 'Could not initialize database:' );
 149              $cli->error( '* No database handler was found for $type' );
 150              return $dbSchema;
 151          }
 152          if ( !$dbInstance->isConnected() )
 153          {
 154              $cli->error( "Could not initialize database:" );
 155              $msg = "* Tried database '$db'";
 156              if ( strlen( $host ) > 0 )
 157              {
 158                  $msg .= " at host '$host'";
 159              }
 160              else
 161              {
 162                  $msg .= " locally";
 163              }
 164              if ( strlen( $user ) > 0 )
 165              {
 166                  $msg .= " with user '$user'";
 167              }
 168              if ( strlen( $password ) > 0 )
 169                  $msg .= " and with a password";
 170              $cli->error( $msg );
 171  
 172              // Fetch the database error message if there is one
 173              // It will give more feedback to the user what is wrong
 174              $msg = $dbInstance->errorMessage();
 175              if ( $msg )
 176              {
 177                  $number = $dbInstance->errorNumber();
 178                  if ( $number > 0 )
 179                      $msg .= '(' . $number . ')';
 180                  $cli->error( '* ' . $msg );
 181              }
 182              return $dbSchema;
 183          }
 184  
 185          $dbSchema = eZDBSchema::instance( $dbInstance );
 186          return $dbSchema;
 187      }
 188  }
 189  
 190  function &loadLintSchema( &$dbSchema, &$cli )
 191  {
 192      include_once ( 'lib/ezdbschema/classes/ezlintschema.php' );
 193      $lintSchema = new eZLintSchema( false, $dbSchema );
 194      return $lintSchema;
 195  }
 196  
 197  $sourceSchema = loadDatabaseSchema( $sourceType, $sourceDBHost, $sourceDBUser, $sourceDBPassword, $sourceDBSocket, $sourceDB, $cli );
 198  if ( !$sourceSchema )
 199  {
 200      $cli->error( "Failed to load schema from source database" );
 201      $script->shutdown( 1 );
 202  }
 203  
 204  if ( $options['lint-check'] )
 205  {
 206      $matchType = $sourceType;
 207      $matchSchema = $sourceSchema;
 208      unset( $sourceSchema );
 209      $sourceSchema = loadLintSchema( $matchSchema, $cli );
 210  }
 211  else
 212  {
 213      $matchSchema = loadDatabaseSchema( $matchType, $matchDBHost, $matchDBUser, $matchDBPassword, $matchDBSocket, $matchDB, $cli );
 214      if ( !$matchSchema )
 215      {
 216          $cli->error( "Failed to load schema from match database" );
 217          $script->shutdown( 1 );
 218      }
 219  }
 220  
 221  include_once ( 'lib/ezdbschema/classes/ezdbschemachecker.php' );
 222  
 223  if ( $options['reverse'] )
 224  {
 225      $differences = eZDbSchemaChecker::diff( $sourceSchema->schema(), $matchSchema->schema(), $sourceType, $matchType );
 226      if ( !$options['check-only'] )
 227      {
 228          $cli->output( "-- Difference in SQL commands for " . $sourceSchema->schemaName() );
 229          $sql = $sourceSchema->generateUpgradeFile( $differences );
 230          $cli->output( $sql );
 231      }
 232  }
 233  else
 234  {
 235      $differences = eZDbSchemaChecker::diff( $matchSchema->schema(), $sourceSchema->schema(), $matchType, $sourceType );
 236      if ( !$options['check-only'] )
 237      {
 238          $cli->output( "-- Difference in SQL commands from " . $sourceSchema->schemaName() . " to " . $matchSchema->schemaName() );
 239          $sql = $matchSchema->generateUpgradeFile( $differences );
 240          $cli->output( $sql );
 241      }
 242  }
 243  
 244  if ( count( $differences ) > 0 )
 245      $script->setExitCode( 1 );
 246  
 247  $script->shutdown();
 248  
 249  ?>


Généré le : Sat Feb 24 10:30:04 2007 par Balluche grâce à PHPXref 0.7