示例#1
0
 * @uses collect(function($value){return $value+1}, range(1,5));
 */
function collect($func, $array)
{
    $out = array();
    foreach ($array as $value) {
        array_push($out, $func($value));
    }
    return $out;
}
require_once dirname(__FILE__) . '/config/db_connect.php';
//load the test database
if (!defined('DATABASE_CREATED')) {
    $m = new Migration();
    $m->drop_database(MYSQL_DATABASE);
    $m->create_database(MYSQL_DATABASE);
    define('DATABASE_CREATED', true);
}
//load the table if it hasn't been loaded
if (!defined('TABLE_CREATED')) {
    $g = new TestMigration();
    $g->down();
    $g->up();
    define('TABLE_CREATED', true);
}
load_test_data();
foreach (array('Tim', 'Steve', 'Joe', 'Bob', 'John', 'Scott', 'Randy', 'Jessica', 'Julie') as $user) {
    $func_name = strtolower($user);
    $func = "function {$func_name}() {\n\t\t\treturn User::_find_by(array('conditions' => \"name = '" . $user . "'\"));\n\t\t};";
    eval($func);
}
     */
    public function up()
    {
        if (false === $this->hasTable('people')) {
            $this->table('people')->addColumn('person_id', 'integer', ['signed' => false])->addColumn('birth_date', 'datetime')->addPrimaryKey('person_id')->save();
        }
        if (false === $this->hasTable('users')) {
            $this->table('users')->addColumn('user_id', 'integer', ['signed' => false])->addColumn('person_id', 'integer', ['signed' => false])->addColumn('username', 'string')->addColumn('password', 'string')->addPrimaryKey('user_id')->addForeignKey(['person_id'], 'people', ['person_id'], ['delete' => 'cascade'])->addIndex('person_id')->save();
        }
    }
    /**
     * The down method is automatically run when you are migrating down and it detects the given
     * migration has been executed in the past.
     *
     * You should use the down method to reverse/undo the transformations described in the up method.
     *
     * @return void
     */
    public function down()
    {
    }
}
$adapter->beginTransaction();
$start = new \DateTime();
$migration = new TestMigration($adapter);
$migration->up();
$end = new \DateTime();
if ($adapter->hasTransactions()) {
    $adapter->migrated($migration->getMigration(), 'up', $start->format('Y-m-d H:i:s'), $end->format('Y-m-d H:i:s'));
}
$adapter->commitTransaction();
示例#3
0
function run_migration()
{
    $g = new TestMigration();
    $g->down();
    $g->up();
}