vendor/doctrine-migrations/lib/Doctrine/DBAL/Migrations/AbstractMigration.php
changeset 39 03b14b0fe101
equal deleted inserted replaced
38:bbdc7f9aa25e 39:03b14b0fe101
       
     1 <?php
       
     2 /*
       
     3  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
       
     4  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
       
     5  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
       
     6  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
       
     7  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
       
     8  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
       
     9  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
       
    10  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
       
    11  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
       
    12  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
       
    13  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
       
    14  *
       
    15  * This software consists of voluntary contributions made by many individuals
       
    16  * and is licensed under the LGPL. For more information, see
       
    17  * <http://www.doctrine-project.org>.
       
    18 */
       
    19 
       
    20 namespace Doctrine\DBAL\Migrations;
       
    21 
       
    22 use Doctrine\DBAL\Schema\Schema,
       
    23     Doctrine\DBAL\Migrations\Configuration\Configuration,
       
    24     Doctrine\DBAL\Migrations\Version;
       
    25 
       
    26 /**
       
    27  * Abstract class for individual migrations to extend from.
       
    28  *
       
    29  * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
       
    30  * @link        www.doctrine-project.org
       
    31  * @since       2.0
       
    32  * @author      Jonathan H. Wage <jonwage@gmail.com>
       
    33  */
       
    34 abstract class AbstractMigration
       
    35 {
       
    36     /**
       
    37      * The Migrations Configuration instance for this migration
       
    38      *
       
    39      * @var Configuration
       
    40      */
       
    41     private $configuration;
       
    42 
       
    43     /**
       
    44      * The OutputWriter object instance used for outputting information
       
    45      *
       
    46      * @var OutputWriter
       
    47      */
       
    48     private $outputWriter;
       
    49 
       
    50     /**
       
    51      * The Doctrine\DBAL\Connection instance we are migrating
       
    52      *
       
    53      * @var Connection
       
    54      */
       
    55     protected $connection;
       
    56 
       
    57     /**
       
    58      * Reference to the SchemaManager instance referened by $_connection
       
    59      *
       
    60      * @var \Doctrine\DBAL\Schema\AbstractSchemaManager
       
    61      */
       
    62     protected $sm;
       
    63 
       
    64     /**
       
    65      * Reference to the DatabasePlatform instance referenced by $_conection
       
    66      *
       
    67      * @var \Doctrine\DBAL\Platforms\AbstractPlatform
       
    68      */
       
    69     protected $platform;
       
    70 
       
    71     /**
       
    72      * Reference to the Version instance representing this migration
       
    73      *
       
    74      * @var Version
       
    75      */
       
    76     protected $version;
       
    77 
       
    78     public function __construct(Version $version)
       
    79     {
       
    80         $this->configuration = $version->getConfiguration();
       
    81         $this->outputWriter = $this->configuration->getOutputWriter();
       
    82         $this->connection = $this->configuration->getConnection();
       
    83         $this->sm = $this->connection->getSchemaManager();
       
    84         $this->platform = $this->connection->getDatabasePlatform();
       
    85         $this->version = $version;
       
    86     }
       
    87 
       
    88     /**
       
    89      * Get custom migration name
       
    90      *
       
    91      * @return string
       
    92      */
       
    93     public function getName()
       
    94     {
       
    95     }
       
    96 
       
    97     abstract public function up(Schema $schema);
       
    98     abstract public function down(Schema $schema);
       
    99 
       
   100     protected function addSql($sql, array $params = array())
       
   101     {
       
   102         return $this->version->addSql($sql, $params);
       
   103     }
       
   104 
       
   105     protected function write($message)
       
   106     {
       
   107         $this->outputWriter->write($message);
       
   108     }
       
   109 
       
   110     protected function throwIrreversibleMigrationException($message = null)
       
   111     {
       
   112         if ($message === null) {
       
   113             $message = 'This migration is irreversible and cannot be reverted.';
       
   114         }
       
   115         throw new IrreversibleMigrationException($message);
       
   116     }
       
   117 
       
   118     /**
       
   119      * Print a warning message if the condition evalutes to TRUE.
       
   120      *
       
   121      * @param bool $condition
       
   122      * @param string $message
       
   123      */
       
   124     public function warnIf($condition, $message = '')
       
   125     {
       
   126         $message = (strlen($message)) ? $message : 'Unknown Reason';
       
   127 
       
   128         if ($condition === true) {
       
   129             $this->outputWriter->write('    <warning>Warning during ' . $this->version->getExecutionState() . ': ' . $message . '</warning>');
       
   130         }
       
   131     }
       
   132 
       
   133     /**
       
   134      * Abort the migration if the condition evalutes to TRUE.
       
   135      *
       
   136      * @param bool $condition
       
   137      * @param string $message
       
   138      */
       
   139     public function abortIf($condition, $message = '')
       
   140     {
       
   141         $message = (strlen($message)) ? $message : 'Unknown Reason';
       
   142 
       
   143         if ($condition === true) {
       
   144             throw new AbortMigrationException($message);
       
   145         }
       
   146     }
       
   147 
       
   148     /**
       
   149      * Skip this migration (but not the next ones) if condition evalutes to TRUE.
       
   150      *
       
   151      * @param bool $condition
       
   152      * @param string $message
       
   153      */
       
   154     public function skipIf($condition, $message = '')
       
   155     {
       
   156         $message = (strlen($message)) ? $message : 'Unknown Reason';
       
   157 
       
   158         if ($condition === true) {
       
   159             throw new SkipMigrationException($message);
       
   160         }
       
   161     }
       
   162 
       
   163     public function preUp(Schema $schema)
       
   164     {
       
   165     }
       
   166 
       
   167     public function postUp(Schema $schema)
       
   168     {
       
   169     }
       
   170 
       
   171     public function preDown(Schema $schema)
       
   172     {
       
   173     }
       
   174 
       
   175     public function postDown(Schema $schema)
       
   176     {
       
   177     }
       
   178 }