Process.php 7.25 KB
Newer Older
Jonathan.Wage's avatar
Jonathan.Wage committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
<?php
/*
 *  $Id: Process.php 1080 2007-02-10 18:17:08Z jwage $
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the LGPL. For more information, see
19
 * <http://www.phpdoctrine.org>.
Jonathan.Wage's avatar
Jonathan.Wage committed
20
 */
21

Jonathan.Wage's avatar
Jonathan.Wage committed
22 23 24 25
/**
 * Doctrine_Migration_Process
 *
 * @package     Doctrine
26
 * @subpackage  Migration
Jonathan.Wage's avatar
Jonathan.Wage committed
27 28 29 30
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @link        www.phpdoctrine.com
 * @since       1.0
 * @version     $Revision: 1080 $
31
 * @author      Jonathan H. Wage <jwage@mac.com>
Jonathan.Wage's avatar
Jonathan.Wage committed
32 33 34
 */
class Doctrine_Migration_Process
{
35 36 37 38 39 40
    /**
     * getConnection
     *
     * @param string $tableName 
     * @return void
     */
Jonathan.Wage's avatar
Jonathan.Wage committed
41
    public function getConnection($tableName)
Jonathan.Wage's avatar
Jonathan.Wage committed
42
    {
Jonathan.Wage's avatar
Jonathan.Wage committed
43 44
        return Doctrine::getConnectionByTableName($tableName);
    }
45 46 47 48 49 50 51

    /**
     * processCreatedTables
     *
     * @param string $tables 
     * @return void
     */
Jonathan.Wage's avatar
Jonathan.Wage committed
52
    public function processCreatedTables($tables)
53
    {
Jonathan.Wage's avatar
Jonathan.Wage committed
54
        foreach ($tables as $table) {
Jonathan.Wage's avatar
Jonathan.Wage committed
55 56
            $conn = $this->getConnection($table['tableName']);
            
Jonathan.Wage's avatar
Jonathan.Wage committed
57 58 59
            $conn->export->createTable($table['tableName'], $table['fields'], $table['options']);
        }
    }
60 61 62 63 64 65 66

    /**
     * processDroppedTables
     *
     * @param string $tables 
     * @return void
     */
Jonathan.Wage's avatar
Jonathan.Wage committed
67 68 69
    public function processDroppedTables($tables)
    {
        foreach ($tables as $table) {
Jonathan.Wage's avatar
Jonathan.Wage committed
70 71
            $conn = $this->getConnection($table['tableName']);
            
Jonathan.Wage's avatar
Jonathan.Wage committed
72 73 74
            $conn->export->dropTable($table['tableName']);
        }
    }
75 76 77 78 79 80 81

    /**
     * processRenamedTables
     *
     * @param string $tables 
     * @return void
     */
Jonathan.Wage's avatar
Jonathan.Wage committed
82 83 84
    public function processRenamedTables($tables)
    {
        foreach ($tables as $table) {
Jonathan.Wage's avatar
Jonathan.Wage committed
85 86
            $conn = $this->getConnection($table['newTableName']);
            
87
            $conn->export->alterTable($table['oldTableName'], array('name' => $table['newTableName']));
Jonathan.Wage's avatar
Jonathan.Wage committed
88 89
        }
    }
90 91 92 93 94 95 96

    /**
     * processAddedColumns
     *
     * @param string $columns 
     * @return void
     */
Jonathan.Wage's avatar
Jonathan.Wage committed
97 98 99
    public function processAddedColumns($columns)
    {
        foreach ($columns as $column) {
Jonathan.Wage's avatar
Jonathan.Wage committed
100 101
            $conn = $this->getConnection($column['tableName']);
            
Jonathan.Wage's avatar
Jonathan.Wage committed
102 103 104 105
            $options = array();
            $options = $column['options'];
            $options['type'] = $column['type'];
            
106
            $conn->export->alterTable($column['tableName'], array('add' => array($column['columnName'] => $options)));
Jonathan.Wage's avatar
Jonathan.Wage committed
107 108
        }
    }
109 110 111 112 113 114 115

    /**
     * processRenamedColumns
     *
     * @param string $columns 
     * @return void
     */
Jonathan.Wage's avatar
Jonathan.Wage committed
116 117 118
    public function processRenamedColumns($columns)
    {
        foreach ($columns as $column) {
119 120 121 122 123 124 125
            $conn = $this->getConnection($column['tableName']);
            
            $columnList = $conn->import->listTableColumns($column['tableName']);
            if (isset($columnList[$column['oldColumnName']])) {
	            $conn->export->alterTable($column['tableName'], 
	                                      array('rename' => array($column['oldColumnName'] => array('name' => $column['newColumnName'],
	                                      																													'definition'=>$columnList[$column['oldColumnName']]))));
126
            }
Jonathan.Wage's avatar
Jonathan.Wage committed
127 128
        }
    }
129 130 131 132 133 134 135

    /**
     * processChangedColumns
     *
     * @param string $columns 
     * @return void
     */
Jonathan.Wage's avatar
Jonathan.Wage committed
136 137 138
    public function processChangedColumns($columns)
    {
        foreach ($columns as $column) {
Jonathan.Wage's avatar
Jonathan.Wage committed
139 140
            $conn = $this->getConnection($column['tableName']);
            
Jonathan.Wage's avatar
Jonathan.Wage committed
141 142 143 144
            $options = array();
            $options = $column['options'];
            $options['type'] = $column['type'];
            
145
            $conn->export->alterTable($column['tableName'], array('change' => array($column['columnName'] => array('definition' => $options))));
Jonathan.Wage's avatar
Jonathan.Wage committed
146 147
        }  
    }
148 149 150 151 152 153 154

    /**
     * processRemovedColumns
     *
     * @param string $columns 
     * @return void
     */
Jonathan.Wage's avatar
Jonathan.Wage committed
155 156 157
    public function processRemovedColumns($columns)
    {
        foreach ($columns as $column) {
Jonathan.Wage's avatar
Jonathan.Wage committed
158 159
            $conn = $this->getConnection($column['tableName']);
            
Jonathan.Wage's avatar
Jonathan.Wage committed
160 161 162
            $conn->export->alterTable($column['tableName'], array('remove' => array($column['columnName'] => array())));
        }
    }
163 164 165 166 167 168 169

    /**
     * processAddexIndexes
     *
     * @param string $indexes 
     * @return void
     */
Jonathan.Wage's avatar
Jonathan.Wage committed
170 171 172
    public function processAddedIndexes($indexes)
    {
        foreach ($indexes as $index) {
Jonathan.Wage's avatar
Jonathan.Wage committed
173 174
            $conn = $this->getConnection($index['tableName']);
            
Jonathan.Wage's avatar
Jonathan.Wage committed
175 176 177
            $conn->export->createIndex($index['tableName'], $index['indexName'], $index['definition']);
        }
    }
178 179 180 181 182 183 184

    /**
     * processRemovedIndexes
     *
     * @param string $indexes 
     * @return void
     */
Jonathan.Wage's avatar
Jonathan.Wage committed
185 186 187
    public function processRemovedIndexes($indexes)
    {
        foreach ($indexes as $index) {
Jonathan.Wage's avatar
Jonathan.Wage committed
188 189
            $conn = $this->getConnection($index['tableName']);
            
Jonathan.Wage's avatar
Jonathan.Wage committed
190 191 192
            $conn->export->dropIndex($index['tableName'], $index['indexName']);
        } 
    }
193 194 195 196 197 198 199

    /**
     * processCreatedConstraints
     *
     * @param string $constraints 
     * @return void
     */
200 201 202 203 204 205 206 207
    public function processCreatedConstraints($constraints)
    {
        foreach ($constraints as $constraint) {
            $conn = $this->getConnection($constraint['tableName']);
            $conn->export->createConstraint($constraint['tableName'], $constraint['constraintName'],
                    $constraint['definition']);
        }
    }
208 209 210 211 212 213 214

    /**
     * processDroppedConstraints
     *
     * @param string $constraints 
     * @return void
     */
215 216 217 218 219 220 221 222
    public function processDroppedConstraints($constraints)
    {
        foreach ($constraints as $constraint) {
            $conn = $this->getConnection($constraint['tableName']);
            $conn->export->dropConstraint($constraint['tableName'], $constraint['constraintName'],
                    $constraint['primary']);
        }
    }
223 224 225 226 227 228 229

    /**
     * processCreatedFks
     *
     * @param string $foreignKeys 
     * @return void
     */
230 231 232 233 234 235 236
    public function processCreatedFks($foreignKeys)
    {
        foreach ($foreignKeys as $fk) {
            $conn = $this->getConnection($fk['tableName']);
            $conn->export->createForeignKey($fk['tableName'], $fk['definition']);
        }
    }
237 238 239 240 241 242 243

    /**
     * processDroppedFks
     *
     * @param string $foreignKeys 
     * @return void
     */
244 245 246 247 248 249 250
    public function processDroppedFks($foreignKeys)
    {
        foreach ($foreignKeys as $fk) {
            $conn = $this->getConnection($fk['tableName']);
            $conn->export->dropForeignKey($fk['tableName'], $fk['fkName']);
        }
    }
Jonathan.Wage's avatar
Jonathan.Wage committed
251
}