Migrations

创建和运行Migrations

创建一个Migration

一个migration使用一个installer文件。存放于app/installers/refmt_installer.php。他被用来说明ActiveRecord在数据库表中维护多种语言的能力。

<?php
class RefmtInstaller extends AkInstaller
{
    function up_1()
    {
        $this->createTable('mono_langs', "
            id,
            lang,
            name
        ");
 
        $this->createTable('multi_langs', "
            id,
            en_name,
            fi_name,
            sv_name
        ");
    }
 
    function down_1()
    {
        $this->dropTable('mono_langs','multi_langs');
    }
}
?>

运行migration

在终端,转到你项目的根目录。然后输入

./script/migrate Refmt install

“Refmt” 在”Installer之前”是类名的一部分”Installer”. 如果表不存在,他们将被创建;如果表存在,表不会被覆盖。

这里重要的事情是使用migration文件,来使你能附加的up和down的功能来添加表,索引,列等等等。down的功能应该始终和up功能所做的。 ActiveRecord保存了你在migration的轨迹。你第一次执行install时, up_1()将会被执行。如果你执行了uninstall, down_1()会被执行。每个一成功的install将会引发up_x()的执行(x是前一个up_x()加1)。

让我们看一下,如果你有up_1(), down_1(), up_2(), and down_2()这几个功能。你第一次执行时

./script/migrate Refmt install

up_1()被执行。第二次执行时,执行up_2()。如果你执行uninstall

./script/migrate Refmt uninstall

在这一点,执行down_2()。你的表的条件应该在up_1()中被定义。你可以根据你的需要添加任意多的up_x()和down_x(),每一次给x加1。

 
creating-and-running-migrations_cn.txt · Last modified: 2008/07/15 10:50 by liyh
 

The Akelos Framework was created by Bermi Ferrer and other contributors.
Potions of the code and documentation have been ported from Ruby on Rails.

The Akelos Framework is released under the LGPL license.

"Akelos", "Akelos Framework", and the Akelos logo are trademarks of Bermi Labs All rights reserved.

Wiki driven by DokuWiki