マイグレーション

□未翻訳

□翻訳中

■翻訳完了(細田謙二)

■レビュー(Omi Chiba)

マイグレーション

define_tableは、対応するテーブルが存在するかどうかをチェックします。存在しない場合は、それを作成するSQLを生成し、そのSQLを実行します。テーブルが存在してもここで定義されているものと違うものであれば、そのテーブルを変更するSQLを生成し、実行します。フィー ルドの型は変更したが名前は変更してない場合、データを変更しようと試みます(そうしたくない場合は、テーブルを二度、定義し直す必要があります。一度目 はフィールドを除くことによって、そのフィールドを削除するようにweb2pyに指示します。二度目は、新規に定義したフィールドを加えて、web2py にそれを作らせます)。テーブルが存在して、現在の定義と一致する場合は、そのままになります。すべての場合において、そのテーブルを表現するdb.personオブジェクトが作られます。

define_table checks whether or not the corresponding table exists. If it does not, it generates the SQL to create it and executes the SQL. If the table does exist but differs from the one being defined, it generates the SQL to alter the table and executes it. If a field has changed type but not name, it will try to convert the data(If you do not want this, you need to redefine the table twice, the first time, letting web2py drop the field by removing it, and the second time adding the newly defined field so that web2py can create it.). If the table exists and matches the current definition, it will leave it alone. In all cases it will create the db.person object that represents the table.

このような挙動を、ここでは"マイグレーション"として参照します。web2pyはすべてのマイグレーションとマイグレーションの試みを"databases/sql.log"ファイルにログとして記録します。

We refer to this behavior as a "migration". web2py logs all migrations and migration attempts in the file "databases/sql.log".

define_tableの最初の引数は常にテーブルの名前です。他の無名引数はフィールド(Field)です。この関数はまた、"migrate"という省略可能な最後の引数をとることができます。これは、次のように名前によって明示的に参照されなければなりません。

The first argument of define_table is always the table name. The other unnamed arguments are the fields (Field). The function also takes an optional last argument called "migrate" which must be referred to explicitly by name as in:

1.

>>> db.define_table('person', Field('name'), migrate='person.table')

migrateの値は、(アプリケーションの"database"フォルダ内の)ファイル名です。このファイルには、このテーブルの内部的なマイグレーション情報がweb2pyによって保存されています。これらのファイルはとても重要で、データベース全体を削除するとき以外には、削除すべきではありません。この場合は、".table"ファイルは手動で削除する必要があります。デフォルトでは、migrateはTrueに設定されています。こうすると、web2pyは接続文字列のハッシュからファイル名を生成します。migrateがFalseに設定されていると、マイグレーションは実行されません。web2pyは、データベースにテーブルが存在し、define_tableに列挙されたフィールドを含んでいると想定します。ベストプラクティスは、明示的な名前をこのmigrateテーブルに与えることです。

The value of migrate is the filename (in the "databases" folder for the application) where web2py stores internal migration information for this table. These files are very important and should never be removed except when the entire database is dropped. In this case, the ".table" files have to be removed manually. By default, migrate is set to True. This causes web2py to generate the filename from a hash of the connection string. If migrate is set to False, the migration is not performed, and web2py assumes that the table exists in the datastore and it contains (at least) the fields listed in define_table. The best practice is to give an explicit name to the migrate table.

同じアプリケーションに、同じmigrateファイルを持つ2つのテーブルが存在することはありません。

There may not be two tables in the same application with the same migrate filename.

DALクラスはまた、"migrate"引数をとります。これは、define_tableが呼び出されたときのmigrateのデフォルト値を設定します。例 :

The DAL class also takes a "migrate" argument, which determines the default value of migrate for calls to define_table. For example,

1.

>>> db = DAL('sqlite://storage.db', migrate=False)

このようにすると、db.define_tableがmigrate引数なしに呼び出されたときは常に、migrateのデフォルト値がFalseに設定されます。

will set the default value of migrate to False whenever db.define_table is called without a migrate argument.