Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/*
3
 *  $Id: Migration.php 1080 2007-02-10 18:17:08Z jwage $
4
 *
5
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
6
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
9
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
10
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
11
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
12
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
13
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
14
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
15
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16
 *
17
 * This software consists of voluntary contributions made by many individuals
18
 * and is licensed under the LGPL. For more information, see
19
 * <http://www.doctrine-project.org>.
20
 */
21
 
22
/**
23
 * Base migration class. All migration classes must extend from this base class
24
 *
25
 * @package     Doctrine
26
 * @subpackage  Migration
27
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
28
 * @link        www.doctrine-project.org
29
 * @since       1.1
30
 * @version     $Revision: 1080 $
31
 * @author      Jonathan H. Wage <jonwage@gmail.com>
32
 */
33
abstract class Doctrine_Migration_Base
34
{
35
    /**
36
     * The default options for tables created using Doctrine_Migration_Base::createTable()
37
     *
38
     * @var array
39
     */
40
    private static $defaultTableOptions = array();
41
 
42
    protected $_changes = array();
43
 
44
    protected static $_opposites = array('created_table'       => 'dropped_table',
45
                                         'dropped_table'       => 'created_table',
46
                                         'created_constraint'  => 'dropped_constraint',
47
                                         'dropped_constraint'  => 'created_constraint',
48
                                         'created_foreign_key' => 'dropped_foreign_key',
49
                                         'dropped_foreign_key' => 'created_foreign_key',
50
                                         'created_column'      => 'dropped_column',
51
                                         'dropped_column'      => 'created_column',
52
                                         'created_index'       => 'dropped_index',
53
                                         'dropped_index'       => 'created_index',
54
                                         );
55
 
56
    /**
57
     * Get the changes that have been added on this migration class instance
58
     *
59
     * @return array $changes
60
     */
61
    public function getChanges()
62
    {
63
        return $this->_changes;
64
    }
65
 
66
    public function getNumChanges()
67
    {
68
        return count($this->_changes);
69
    }
70
 
71
    /**
72
     * Add a change to the stack of changes to execute
73
     *
74
     * @param string $type    The type of change
75
     * @param array  $change   The array of information for the change
76
     * @return void
77
     */
78
    protected function _addChange($type, array $change = array())
79
    {
80
        if (isset($change['upDown']) && $change['upDown'] !== null && isset(self::$_opposites[$type])) {
81
            $upDown = $change['upDown'];
82
            unset($change['upDown']);
83
            if ($upDown == 'down') {
84
                $opposite = self::$_opposites[$type];
85
                return $this->_changes[] = array($opposite, $change);
86
            }
87
        }
88
        return $this->_changes[] = array($type, $change);
89
    }
90
 
91
    /**
92
     * Sets the default options for tables created using Doctrine_Migration_Base::createTable()
93
     *
94
     * @param array $options
95
     */
96
    public static function setDefaultTableOptions(array $options)
97
    {
98
        self::$defaultTableOptions = $options;
99
    }
100
 
101
    /**
102
     * Returns the default options for tables created using Doctrine_Migration_Base::createTable()
103
     *
104
     * @return array
105
     */
106
    public static function getDefaultTableOptions()
107
    {
108
        return self::$defaultTableOptions;
109
    }
110
 
111
    /**
112
     * Add a create or drop table change.
113
     *
114
     * @param string $upDown     Whether to add the up(create) or down(drop) table change.
115
     * @param string $tableName  Name of the table
116
     * @param array  $fields     Array of fields for table
117
     * @param array  $options    Array of options for the table
118
     * @return void
119
     */
120
    public function table($upDown, $tableName, array $fields = array(), array $options = array())
121
    {
122
        $options = get_defined_vars();
123
 
124
        $this->_addChange('created_table', $options);
125
    }
126
 
127
    /**
128
     * Add a create table change.
129
     *
130
     * @param string $tableName  Name of the table
131
     * @param array  $fields     Array of fields for table
132
     * @param array  $options    Array of options for the table
133
     * @return void
134
     */
135
    public function createTable($tableName, array $fields = array(), array $options = array())
136
    {
137
        $this->table('up', $tableName, $fields, array_merge(self::getDefaultTableOptions(), $options));
138
    }
139
 
140
    /**
141
     * Add a drop table change.
142
     *
143
     * @param string $tableName  Name of the table
144
     * @return void
145
     */
146
    public function dropTable($tableName)
147
    {
148
        $this->table('down', $tableName);
149
    }
150
 
151
    /**
152
     * Add a rename table change
153
     *
154
     * @param string $oldTableName      Name of the table to change
155
     * @param string $newTableName      Name to change the table to
156
     * @return void
157
     */
158
    public function renameTable($oldTableName, $newTableName)
159
    {
160
        $options = get_defined_vars();
161
 
162
        $this->_addChange('renamed_table', $options);
163
    }
164
 
165
    /**
166
     * Add a create or drop constraint change.
167
     *
168
     * @param string $upDown            Whether to add the up(create) or down(drop) create change.
169
     * @param string $tableName         Name of the table.
170
     * @param string $constraintName    Name of the constraint.
171
     * @param array  $definition        Array for the constraint definition.
172
     * @return void
173
     */
174
    public function constraint($upDown, $tableName, $constraintName, array $definition)
175
    {
176
        $options = get_defined_vars();
177
 
178
        $this->_addChange('created_constraint', $options);
179
    }
180
 
181
    /**
182
     * Add a create constraint change.
183
     *
184
     * @param string $tableName         Name of the table.
185
     * @param string $constraintname    Name of the constraint.
186
     * @param array  $definition        Array for the constraint definition.
187
     * @return void
188
     */
189
    public function createConstraint($tableName, $constraintName, array $definition)
190
    {
191
        $this->constraint('up', $tableName, $constraintName, $definition);
192
    }
193
 
194
    /**
195
     * Add a drop constraint change.
196
     *
197
     * @param string $tableName         Name of the table.
198
     * @param string $constraintname    Name of the constraint.
199
     * @return void
200
     */
201
    public function dropConstraint($tableName, $constraintName, $primary = false)
202
    {
203
        $this->constraint('down', $tableName, $constraintName, array('primary' => $primary));
204
    }
205
 
206
    /**
207
     * Convenience method for creating or dropping primary keys.
208
     *
209
     * @param string $direction
210
     * @param string $tableName     Name of the table
211
     * @param string $columnNames   Array of column names and column definitions
212
     * @return void
213
     */
214
    public function primaryKey($direction, $tableName, $columnNames)
215
    {
216
        if ($direction == 'up') {
217
            $this->createPrimaryKey($tableName, $columnNames);
218
        } else {
219
            $this->dropPrimaryKey($tableName, $columnNames);
220
        }
221
    }
222
 
223
    /**
224
     * Convenience method for creating primary keys
225
     *
226
     *     [php]
227
     *     $columns = array(
228
     *         'id' => array(
229
     *             'type' => 'integer
230
     *             'autoincrement' => true
231
     *          )
232
     *     );
233
     *     $this->createPrimaryKey('my_table', $columns);
234
     *
235
     * Equivalent to doing:
236
     *
237
     *  * Add new columns (addColumn())
238
     *  * Create primary constraint on columns (createConstraint())
239
     *  * Change autoincrement = true field to be autoincrement
240
     *
241
     * @param string $tableName     Name of the table
242
     * @param string $columnNames   Array of column names and column definitions
243
     * @return void
244
     */
245
    public function createPrimaryKey($tableName, $columnNames)
246
    {
247
        $autoincrement = false;
248
        $fields = array();
249
 
250
        // Add the columns
251
        foreach ($columnNames as $columnName => $def) {
252
            $type = $def['type'];
253
            $length = isset($def['length']) ? $def['length'] : null;
254
            $options = isset($def['options']) ? $def['options'] : array();
255
 
256
            $this->addColumn($tableName, $columnName, $type, $length, $options);
257
 
258
            $fields[$columnName] = array();
259
 
260
            if (isset($def['autoincrement'])) {
261
                $autoincrement = true;
262
                $autoincrementColumn = $columnName;
263
                $autoincrementType = $type;
264
                $autoincrementLength = $length;
265
                $autoincrementOptions = $options;
266
                $autoincrementOptions['autoincrement'] = true;
267
            }
268
        }
269
 
270
        // Create the primary constraint for the columns
271
        $this->createConstraint($tableName, null, array(
272
            'primary' => true,
273
            'fields' => $fields
274
        ));
275
 
276
        // If auto increment change the column to be so
277
        if ($autoincrement) {
278
            $this->changeColumn($tableName, $autoincrementColumn, $autoincrementType, $autoincrementLength, $autoincrementOptions);
279
        }
280
    }
281
 
282
    /**
283
     * Convenience method for dropping primary keys.
284
     *
285
     *     [php]
286
     *     $columns = array(
287
     *         'id' => array(
288
     *             'type' => 'integer
289
     *             'autoincrement' => true
290
     *          )
291
     *     );
292
     *     $this->dropPrimaryKey('my_table', $columns);
293
     *
294
     * Equivalent to doing:
295
     *
296
     *  * Change autoincrement column so it's not (changeColumn())
297
     *  * Remove primary constraint (dropConstraint())
298
     *  * Removing columns (removeColumn())
299
     *
300
     * @param string $tableName     Name of the table
301
     * @param string $columnNames   Array of column names and column definitions
302
     * @return void
303
     */
304
    public function dropPrimaryKey($tableName, $columnNames)
305
    {
306
        // un-autoincrement
307
        foreach ((array) $columnNames as $columnName => $def) {
308
            if (isset($def['autoincrement'])) {
309
                $changeDef = $def;
310
                unset($changeDef['autoincrement']);
311
                $this->changeColumn($tableName, $columnName, $changeDef['type'], $changeDef['length'], $changeDef);
312
            }
313
        }
314
 
315
        // Remove primary constraint
316
        $this->dropConstraint($tableName, null, true);
317
 
318
        // Remove columns
319
        foreach (array_keys((array) $columnNames) as $columnName) {
320
            $this->removeColumn($tableName, $columnName);
321
        }
322
    }
323
 
324
    /**
325
     * Add a create or drop foreign key change.
326
     *
327
     * @param string $upDown        Whether to add the up(create) or down(drop) foreign key change.
328
     * @param string $tableName     Name of the table.
329
     * @param string $name          Name of the foreign key.
330
     * @param array  $definition    Array for the foreign key definition
331
     * @return void
332
     */
333
    public function foreignKey($upDown, $tableName, $name, array $definition = array())
334
    {
335
        $definition['name'] = $name;
336
        $options = get_defined_vars();
337
 
338
        $this->_addChange('created_foreign_key', $options);
339
    }
340
 
341
    /**
342
     * Add a create foreign key change.
343
     *
344
     * @param string $tableName     Name of the table.
345
     * @param string $name          Name of the foreign key.
346
     * @param array  $definition    Array for the foreign key definition
347
     * @return void
348
     */
349
    public function createForeignKey($tableName, $name, array $definition)
350
    {
351
        $this->foreignKey('up', $tableName, $name, $definition);
352
    }
353
 
354
    /**
355
     * Add a drop foreign key change.
356
     *
357
     * @param string $tableName     Name of the table.
358
     * @param string $name          Name of the foreign key.
359
     * @return void
360
     */
361
    public function dropForeignKey($tableName, $name)
362
    {
363
        $this->foreignKey('down', $tableName, $name);
364
    }
365
 
366
    /**
367
     * Add a add or remove column change.
368
     *
369
     * @param string $upDown        Whether to add the up(add) or down(remove) column change.
370
     * @param string $tableName     Name of the table
371
     * @param string $columnName    Name of the column
372
     * @param string $type          Type of the column
373
     * @param string $length        Length of the column
374
     * @param array  $options       Array of options for the column
375
     * @return void
376
     */
377
    public function column($upDown, $tableName, $columnName, $type = null, $length = null, array $options = array())
378
    {
379
        $options = get_defined_vars();
380
        if ( ! isset($options['options']['length'])) {
381
            $options['options']['length'] = $length;
382
        }
383
        $options = array_merge($options, $options['options']);
384
        unset($options['options']);
385
 
386
        $this->_addChange('created_column', $options);
387
    }
388
 
389
    /**
390
     * Add a add column change.
391
     *
392
     * @param string $tableName     Name of the table
393
     * @param string $columnName    Name of the column
394
     * @param string $type          Type of the column
395
     * @param string $length        Length of the column
396
     * @param array  $options       Array of options for the column
397
     * @return void
398
     */
399
    public function addColumn($tableName, $columnName, $type, $length = null, array $options = array())
400
    {
401
        $this->column('up', $tableName, $columnName, $type, $length, $options);
402
    }
403
 
404
    /**
405
     * Add a remove column change.
406
     *
407
     * @param string $tableName     Name of the table
408
     * @param string $columnName    Name of the column
409
     * @return void
410
     */
411
    public function removeColumn($tableName, $columnName)
412
    {
413
        $this->column('down', $tableName, $columnName);
414
    }
415
 
416
    /**
417
     * Add a rename column change
418
     *
419
     * @param string $tableName         Name of the table to rename the column on
420
     * @param string $oldColumnName     The old column name
421
     * @param string $newColumnName     The new column name
422
     * @return void
423
     */
424
    public function renameColumn($tableName, $oldColumnName, $newColumnName)
425
    {
426
        $options = get_defined_vars();
427
 
428
        $this->_addChange('renamed_column', $options);
429
    }
430
 
431
    /**
432
     * Add a change column change
433
     *
434
     * @param string $tableName     Name of the table to change the column on
435
     * @param string $columnName    Name of the column to change
436
     * @param string $type          New type of column
437
     * @param string $length        The length of the column
438
     * @param array  $options       New options for the column
439
     * @return void
440
     */
441
    public function changeColumn($tableName, $columnName, $type = null, $length = null, array $options = array())
442
    {
443
        $options = get_defined_vars();
444
        $options['options']['length'] = $length;
445
 
446
        $this->_addChange('changed_column', $options);
447
    }
448
 
449
    /**
450
     * Add a add or remove index change.
451
     *
452
     * @param string $upDown       Whether to add the up(add) or down(remove) index change.
453
     * @param string $tableName    Name of the table
454
     * @param string $indexName    Name of the index
455
     * @param array  $definition   Array for the index definition
456
     * @return void
457
     */
458
    public function index($upDown, $tableName, $indexName, array $definition = array())
459
    {
460
        $options = get_defined_vars();
461
 
462
        $this->_addChange('created_index', $options);
463
    }
464
 
465
    /**
466
     * Add a add index change.
467
     *
468
     * @param string $tableName    Name of the table
469
     * @param string $indexName    Name of the index
470
     * @param array  $definition   Array for the index definition
471
     * @return void
472
     */
473
    public function addIndex($tableName, $indexName, array $definition)
474
    {
475
        $this->index('up', $tableName, $indexName, $definition);
476
    }
477
 
478
    /**
479
     * Add a remove index change.
480
     *
481
     * @param string $tableName    Name of the table
482
     * @param string $indexName    Name of the index
483
     * @return void
484
     */
485
    public function removeIndex($tableName, $indexName)
486
    {
487
        $this->index('down', $tableName, $indexName);
488
    }
489
 
490
    public function preUp()
491
    {
492
    }
493
 
494
    public function postUp()
495
    {
496
    }
497
 
498
    public function preDown()
499
    {
500
    }
501
 
502
    public function postDown()
503
    {
504
    }
505
}