Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

root/tags/rel_2-0-2/activerecord/lib/active_record/schema.rb

Revision 8089, 1.8 kB (checked in by marcel, 1 year ago)

Update Schema documentation to use updated sexy migration notation. Closes #10086 [sjgman9]

Line 
1 module ActiveRecord
2   # Allows programmers to programmatically define a schema in a portable
3   # DSL. This means you can define tables, indexes, etc. without using SQL
4   # directly, so your applications can more easily support multiple
5   # databases.
6   #
7   # Usage:
8   #
9   #   ActiveRecord::Schema.define do
10   #     create_table :authors do |t|
11   #       t.string :name, :null => false
12   #     end
13   #
14   #     add_index :authors, :name, :unique
15   #
16   #     create_table :posts do |t|
17   #       t.integer :author_id, :null => false
18   #       t.string :subject
19   #       t.text :body
20   #       t.boolean :private, :default => false
21   #     end
22   #
23   #     add_index :posts, :author_id
24   #   end
25   #
26   # ActiveRecord::Schema is only supported by database adapters that also
27   # support migrations, the two features being very similar.
28   class Schema < Migration
29     private_class_method :new
30
31     # Eval the given block. All methods available to the current connection
32     # adapter are available within the block, so you can easily use the
33     # database definition DSL to build up your schema (#create_table,
34     # #add_index, etc.).
35     #
36     # The +info+ hash is optional, and if given is used to define metadata
37     # about the current schema (like the schema's version):
38     #
39     #   ActiveRecord::Schema.define(:version => 15) do
40     #     ...
41     #   end
42     def self.define(info={}, &block)
43       instance_eval(&block)
44
45       unless info.empty?
46         initialize_schema_information
47         cols = columns('schema_info')
48
49         info = info.map do |k,v|
50           v = Base.connection.quote(v, cols.detect { |c| c.name == k.to_s })
51           "#{k} = #{v}"
52         end
53
54         Base.connection.update "UPDATE #{Migrator.schema_info_table_name} SET #{info.join(", ")}"
55       end
56     end
57   end
58 end
Note: See TracBrowser for help on using the browser.