0
@@ -815,7 +815,7 @@ It doesn't delete columns, and it can't detect renaming them.
0
カラムは削除しませんし、カラム名の変更も検出できません。
0
+#### Migration Files
(マイグレーションファイル)0
Whilst the preceding commands and tasks can keep the database schema in
0
perfect sync with the models, they can also wipe out any data you might have in
0
@@ -823,6 +823,10 @@ the database, or fail to remove columns which are no longer needed. To avoid
0
this, AR style migrations are also supported. These are stored in `schema/migrations`
0
+先ほど紹介したコマンドとタスクは、データベーススキーマとモデルとを完全に同期させるものでしたが、データベース中のデータをすべて消し去ってしまったり、もう必要のなくなったカラムが削除されないという問題がありました。
0
+これを避けるために、AR スタイルのマイグレーションもサポートされています。
0
+これらは Ruby ファイルであり、`schema/migrations` に格納されます。
0
migration(1, :add_homepage_to_comments ) do
0
modify_table :comments do
0
@@ -845,12 +849,21 @@ applied. This number doesn't have to be unique, although a migration mustn't
0
have a higher number than a migration it depends on. You shouldn't define
0
modifications to a table to happen before that table is made, for example.
0
+ファイルの最初の行は、マイグレーションを一意に特定するためのものであり、2 つのコンポーネントから構成されています。
0
+〔訳注: 2 つのうち〕より重要なのは `:add_homepage_to_comments` という名前のほうであり、これはデータベースに対して適用されたすべてのマイグレーションにおいて一意である必要があります。
0
+数字のほうは一意である必要はありませんが、そのマイグレーションが依存する別のマイグレーションより高い〔訳注: 小さい〕値であってはいけません。
0
+たとえば、あるテーブルに対する修正を、そのテーブルが作成されるより前に定義することはできません。
0
The `up` and `down` blocks describe the actual behaviour of the migration. `up`
0
is what happens when the migration is applied, and `down` happens when the
0
migration is 'undone'. This might not mean undone in the literal sense - if you
0
migrate to remove a column, and add it back in the `down` migration, while the
0
column will be there, all the data will be lost.
0
+`up` と `down` のブロックには、マイグレーションの実際の動作を記述します。
0
+`up` のほうはマイグレーションが適用されるときに実行され、`down` のほうはマイグレーションを 「元に戻す」ときに実行されます。
0
+これは、文字通りの意味で元に戻されるわけではありません - もしカラムを削除するようなマイグレーションを作成し、かつそれを追加し直すような `down` マイグレーションを作成した場合、〔訳注: `down` マイグレーションで元に戻そうとしても〕そのカラムのデータは失われてしまうでしょう。
0
In this case, as the name suggested, we add a homepage column to comments.
0
It's specified much like a property (and should match up with the relevant
0
property in the model.rb file). It also takes many of the same options -
0
@@ -858,9 +871,16 @@ essentially all those which are just database features: `:length`, `:nullable`
0
are valid, but `:private`, which is a pure ruby option is not allowed. The
0
`down` migration is the opposite - it removes the column.
0
+今回の場合、名前を見ればわかりますが、comments テーブルに homepage カラムを追加しています。
0
+これはプロパティによく似た感じで指定されます (また model.rb ファイルの中で関係のあるプロパティと一致しなければいけません)。
0
+カラムの追加にはたくさんの〔訳注: プロパティと〕同じオプションを指定可能です - 本質的にはデータベースの機能であるものすべてが使用可能です: たとえば `:length` と `:nullable` が使用可能ですが、純粋に Ruby のオプションである `:private` は使用できません。
0
+`down` マイグレーションは逆であり、カラムを削除します。
0
To apply the migrations, there are a couple of rake tasks available through
0
+マイグレーションを適用するために、merb_datamapper によって 2 つの Rake タスクが用意されてます。
0
rake dm:db:migrate:up # migrates the database up
0
rake dm:db:migrate:down # migrates the database down
0
@@ -871,8 +891,16 @@ and down migrations, the version determines the highest order that will be
0
reflected in the table, either by applying `up` migrations until the level is
0
complete or applying all the `down` migrations greater than the given level.
0
+これらはすべてのマイグレーションを順に適用するか、削除します。
0
+すべての up マイグレーション (または down マイグレーション) を適用したくはない場合もあるでしょう。
0
+その場合、〔訳注: Rake コマンドに〕`VERSION=2` を追加するか、`rake dm:db:migrate:up[2]` のようにしてタスクを起動するかしてください。
0
+up と down のマイグレーションの両方において、テーブルに対して反映されるいちばん高いレベル番号が、バージョンによって決定されます〔訳注: ???〕。
0
+`up` マイグレーションの場合はそのレベルに達するまで、また `down` マイグレーションの場合は指定されたレベルより大きいマイグレーションが、すべて適用されます。
0
There are a couple of generators to make migrations
0
+マイグレーションを作成するためのジェネレータが 2 つ用意されています。
0
merb-gen migration name_of_migration # an empty migration
0
merb-gen resource_migration Post # a migration for the post class
0
@@ -881,14 +909,22 @@ The first creates an empty migration stub with the name defined and an `up` and
0
does it's best to construct the appropriate migration from the properties of the
0
model. It currently doesn't generate anything to do with relationships, however.
0
+最初のほうは、マイグレーションのスタブを `up` と `down` のブロックが空の状態で作成し、それに指定された名前をつけます。
0
+2 番目のほうは、該当するクラスを app/models から読み込み、そのモデルのプロパティから適切なマイグレーションを構築するよう最大限努力します。
0
+ただし現在のところ、関連づけについては何も生成してくれません。
0
+### Other Misc Things (その他の雑多なこと)
0
+#### Callbacks
(コールバック)0
Callbacks in DataMapper > 0.9 are very powerful. In any DataMapper::Resource
0
you can set before and after callbacks on any instance/class method. There are
0
a couple of different ways to define callbacks:
0
+DataMapper 0.9 以降では、コールバックが非常に強力です。
0
+どの DataMapper::Resource のどのインスタンスメソッド/クラスメソッドに対しても、呼び出し前と後のコールバックを設定することが可能です。
0
+コールバックを設定する方法は 2 つあります:
0
include DataMapper::Resource
0
@@ -896,6 +932,7 @@ a couple of different ways to define callbacks:
0
property :title, String, :length => 200
0
# before save call the instance method make_permalink
0
+ # save メソッドを呼び出す前に、インスタンスメソッド make_permalink を呼び出す
0
before :save, :make_permalink
0
@@ -903,6 +940,7 @@ a couple of different ways to define callbacks:
0
#callbacks can be defined for any method
0
+ # コールバックはどのメソッドに対しても定義できる
0
after :publish, :send_message
0
@@ -914,13 +952,14 @@ a couple of different ways to define callbacks:
0
# defining a callback on a class method, passing in a block to run before its created.
0
+ # 作成前に実行されるブロックを渡すことで、コールバックをクラスメソッドに定義する。
0
before_class_method :create do
0
# do something before a record is created
0
+#### Bulk Operations
(大量のデータ操作)0
Sometimes, you have to operate on a large number of records at once, to do
0
exactly the same thing to each of them. The example earlier for deleting old
0
@@ -930,20 +969,38 @@ could just go `Comments.all(:date.lt => Date.today - 20).destroy!` and it would
0
produce an appropriate query to do it in one operation and without loading all
0
those posts which are about to be deleted?
0
+ときどき、一度に大量のレコードに対して、まったく同じ操作をしなければならない場合もあります。
0
+先の例では、古い投稿を each で消していました。
0
+この場合、`SELECT` 文を何回かと、大量の `DELETE` 文が実行されました。
0
+この `DELETE` 文は、データベースの大きさによっては数百回実行される可能性が潜在的にあります。
0
+もし `Comments.all(:date.lt => Date.today - 20).destroy!` みたいなことができて、それが適切なクエリーが発行されることによって 1 回の操作で済み、かつ削除すべきデータをすべて読み込むようなことを避けるようになってくれてたらいいと思いませんか?
0
Well, that's what happens. `Collection`s are 'lazily evaluated', which is to
0
say, they don't do anything until they've been 'kicked'. `.each`, mentioned
0
earlier, is a kicker method. It issues a `SELECT` appropriate to the conditions.
0
`.destroy!` is another one, except it issues a `DELETE`. The other bulk method is
0
`update!`, which looks like (example taken from the DataMapper source)
0
+`Collection`s は「遅延評価」されます。
0
+これはいうなれば、「開始する (kick)」までは〔訳注: データベースに対して〕何もしないということです。
0
+前に出てきた `.each` は「開始する」メソッドであり、条件に対して適切な `SELECT` 文を発行します。
0
+`.destroy!` は別の「開始する」メソッドであり、〔訳注: `SELECT` 文のかわりに〕`DELETE` 文を発行すること以外は同じです。
0
+〔訳注: こういったメソッドは bulk メソッドといい、そして〕他の bulk メソッドとしては `update!` があり、これは次のように使います (DataMapper のソースから持ってきた例です):
0
Person.all(:age.gte => 21).update!(:allow_beer => true)
0
This command would update the `allow_beer` attribute of all people aged 21 or
0
older in the database, all in one `UPDATE` statement.
0
+このコマンドは、データベースにある 21 歳以上の人たち全員の `allow_beer` 属性を、1 回の `UPDATE` 分で更新します。
0
Note: ActiveRecord has a well known `Model.delete_all` class method to erase all table entries. In DataMapper to delete all instances of an Object in the database, you would do `Model.all.destroy!`
0
+注意: ActiveRecord は、よく知られているように `Model.delete_all` クラスメソッドがあり、これでテーブルのエントリをすべて消去することができます。
0
+DataMapper では、データベース中のオブジェクトのインスタンスをすべて削除するには `Model.all.destroy!` を使います。
0
DataMapper by default does not provide aggregator methods, but dm-aggregates
0
in dm-more does. After adding `dependency "dm-aggregates"` to your merb `init.rb`
0
@@ -951,6 +1008,11 @@ file, your resource model will have aggregator methods including `count`, `min`,
0
`max`, `avg`, and `sum`. You can pass conditions to any of these aggregator
0
methods the same as Resource.first or Resource.all
0
+DataMapper は、デフォルトでは集計用メソッドを提供していません。
0
+しかし dm-more に含まれる dm-aggregates がそれらを提供してくれます。
0
+Merb の設定ファイル `init.rb` に `dependency "dm-aggregates"` を追加すると、モデルクラスで `count`, `min`, `max`, `avg`, `sum` という集計用メソッドが使えるようになります。
0
+これらの集計用メソッドには、Resource.first や Resource.all と同じように、条件を渡すことができます。
0
Post.count :title.like => "%hello world%"
0
# you can also do a count on an association:
0
@@ -961,7 +1023,7 @@ methods the same as Resource.first or Resource.all
0
Post.sum(:comments_count)
0
Each works like like expected iterating over a number of rows and you can pass
0
a block to it. The difference between `Comments.all.each` and `Comments.each`
0
@@ -969,6 +1031,11 @@ is that instead of retrieving all the rows at once, each works in batches
0
instantiating a few objects at a time and executing the block on them (so is less
0
resource intensive). Each is similar to a finder as it can also take options:
0
+Each は、たくさんの行に対して繰り返しを行ないます〔訳注: "like like" ってなんじゃ???〕。
0
+また Each にブロックを渡すことができます。
0
+`Comments.all.each` と `Comments.each` の違いは、前者が一度にすべての行を取り出すのに対し、後者はオブジェクトを少しずつインスタンス化してブロックを実行するという点です (そのため後者のほうがリソースの消費が劇的に少なくて済みます)。
0
+Each はオプションを取ることができるので、finder に似ています:
0
Comments.all.each(:date.lt => Date.today - 20).each do |c|
0
@@ -976,11 +1043,18 @@ resource intensive). Each is similar to a finder as it can also take options:
0
NB: This isn't currently working in DataMapper. It instead fetches all the
0
records. However, it will be reimplemented soon.
0
-#### Changing the Table Name
0
+NB: これは、まだ DataMapper では動作しません。
0
+#### Changing the Table Name (テーブル名の変更)
0
You can set the name of the database table in your model if it is called
0
something different by overriding a method in the class:
0
+モデルの中で、データベーステーブル名を設定することができます。
0
+そのためには、クラスの中でメソッドをオーバーライドします:
0
def default_storage_name
0
@@ -989,4 +1063,10 @@ This is only necessary if you are using an already existing database. If you
0
have a lot of tables to rename, consider instead a `NamingConvention`, detailed
0
+これは、既存のデータベースを使う場合にのみ必要です。
0
+もし名前を変更しなければいけないテーブルがたくさんある場合は、次に説明する `NamingConvention` を使うことを検討してください。
0
TODO: Write NamingConventions section.
0
+TODO: NamingConventions の章を書く。
Comments
No one has commented yet.