Skip to content
This repository
Newer
Older
100644 550 lines (496 sloc) 20.0 kb
42775686 » jeremy
2006-06-25 PostgreSQL: support microsecond time resolution. Closes #5492.
1 require 'date'
cdf0f1aa » jeremy
2008-07-12 Faster and clearer value_to_boolean
2 require 'set'
2a12b568 » jeremy
2006-07-08 r4704@asus: jeremy | 2006-06-27 12:00:19 -0700
3 require 'bigdecimal'
4 require 'bigdecimal/util'
ee5fe97a » jeremy
2005-09-30 Ticket #2282 - fix parsing of timestamps with time zone
5
b3df9598 » dhh
2005-09-25 Refactored the AbstractAdapter to be a lot less scary. Cleaned up the…
6 module ActiveRecord
7 module ConnectionAdapters #:nodoc:
92887ca3 » waseem
2012-03-02 Add documentation for IndexDefinition.
8 # Abstract representation of an index definition on a table. Instances of
9 # this type are typically created and returned by methods in database
10 # adapters. e.g. ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter#indexes
9600e0b0 » kenmazaika
2013-03-27 Add support for FULLTEXT and SPATIAL indexes using the :type flag for…
11 class IndexDefinition < Struct.new(:table, :name, :unique, :columns, :lengths, :orders, :where, :type, :using) #:nodoc:
b3df9598 » dhh
2005-09-25 Refactored the AbstractAdapter to be a lot less scary. Cleaned up the…
12 end
13
a17027d1 » lifo
2008-09-13 Merge docrails
14 # Abstract representation of a column definition. Instances of this type
15 # are typically created by methods in TableDefinition, and added to the
16 # +columns+ attribute of said TableDefinition object, in order to be used
17 # for generating a number of table creation or table changing SQL statements.
b404613c » sgrif
2014-06-28 Always pass a column with a type object to quote
18 class ColumnDefinition < Struct.new(:name, :type, :limit, :precision, :scale, :default, :null, :first, :after, :primary_key, :sql_type, :cast_type) #:nodoc:
4b2e16ed » tenderlove
2010-10-12 all columns respond to string_to_binary, so no need to check respond_to?
19
c1003d99 » tenderlove
2013-03-14 ask column if it is a pk
20 def primary_key?
d25e4076 » tenderlove
2013-03-22 separate primary key from column type
21 primary_key || type.to_sym == :primary_key
c1003d99 » tenderlove
2013-03-14 ask column if it is a pk
22 end
b3df9598 » dhh
2005-09-25 Refactored the AbstractAdapter to be a lot less scary. Cleaned up the…
23 end
24
ea72430b » jeran
2013-04-04 Moving add_column_options! up to SchemaCreation
25 class ChangeColumnDefinition < Struct.new(:column, :type, :options) #:nodoc:
26 end
27
c153503a » rafaelfranca
2014-06-26 Add nodoc to internal class [ci skip]
28 class ForeignKeyDefinition < Struct.new(:from_table, :to_table, :options) #:nodoc:
74b2fe4c » senny
2014-06-10 fk: `foreign_keys`, `add_foreign_key` and `remove_foreign_key` for MySQL
29 def name
30 options[:name]
31 end
32
33 def column
34 options[:column]
35 end
36
37 def primary_key
8768305f » senny
2014-06-20 fk: use random digest names
38 options[:primary_key] || default_primary_key
74b2fe4c » senny
2014-06-10 fk: `foreign_keys`, `add_foreign_key` and `remove_foreign_key` for MySQL
39 end
402f303f » senny
2014-06-10 fk: support dependent option (:delete, :nullify and :restrict).
40
6955d864 » senny
2014-06-11 fk: rename `dependent` to `on_delete`
41 def on_delete
42 options[:on_delete]
402f303f » senny
2014-06-10 fk: support dependent option (:delete, :nullify and :restrict).
43 end
acd0287d » senny
2014-06-11 fk: support for on_update
44
45 def on_update
46 options[:on_update]
47 end
8768305f » senny
2014-06-20 fk: use random digest names
48
49 def custom_primary_key?
50 options[:primary_key] != default_primary_key
51 end
52
53 private
54 def default_primary_key
55 "id"
56 end
74b2fe4c » senny
2014-06-10 fk: `foreign_keys`, `add_foreign_key` and `remove_foreign_key` for MySQL
57 end
58
a17027d1 » lifo
2008-09-13 Merge docrails
59 # Represents the schema of an SQL table in an abstract way. This class
60 # provides methods for manipulating the schema representation.
61 #
36abb550 » senny
2013-02-28 update docs, change_table does not use TableDefinition.
62 # Inside migration files, the +t+ object in +create_table+
63 # is actually of this type:
a17027d1 » lifo
2008-09-13 Merge docrails
64 #
65 # class SomeMigration < ActiveRecord::Migration
55b203da » amatsuda
2011-11-07 self.up, self.down => up, down
66 # def up
a17027d1 » lifo
2008-09-13 Merge docrails
67 # create_table :foo do |t|
68 # puts t.class # => "ActiveRecord::ConnectionAdapters::TableDefinition"
69 # end
70 # end
c3f53f41 » lifo
2008-12-19 Merge docrails
71 #
55b203da » amatsuda
2011-11-07 self.up, self.down => up, down
72 # def down
a17027d1 » lifo
2008-09-13 Merge docrails
73 # ...
74 # end
75 # end
76 #
77 # The table definitions
78 # The Columns are stored as a ColumnDefinition in the +columns+ attribute.
b9fa3541 » Marcel Molina
2005-09-27 Add extensive documentation to the ActiveRecord::AbstractAdapter. #2250
79 class TableDefinition
a17027d1 » lifo
2008-09-13 Merge docrails
80 # An array of ColumnDefinition objects, representing the column changes
81 # that have been defined.
b8a533d5 » tenderlove
2013-03-16 @columns list is no longer necessary
82 attr_accessor :indexes
75a2e4a8 » ccutrer
2013-12-13 support creating temporary tables from queries
83 attr_reader :name, :temporary, :options, :as
b3df9598 » dhh
2005-09-25 Refactored the AbstractAdapter to be a lot less scary. Cleaned up the…
84
75a2e4a8 » ccutrer
2013-12-13 support creating temporary tables from queries
85 def initialize(types, name, temporary, options, as = nil)
76c29a64 » tenderlove
2011-12-07 Use a hash to look up column definitions
86 @columns_hash = {}
ca0af822 » joshuap
2012-03-02 Automatically create indexes for references/belongs_to statements in …
87 @indexes = {}
a80bcc39 » tenderlove
2013-03-15 mostly decouple TableDefinition from the database connection
88 @native = types
14d7dc08 » tenderlove
2013-03-15 push SQL generation inside the schema creation object
89 @temporary = temporary
90 @options = options
75a2e4a8 » ccutrer
2013-12-13 support creating temporary tables from queries
91 @as = as
14d7dc08 » tenderlove
2013-03-15 push SQL generation inside the schema creation object
92 @name = name
b3df9598 » dhh
2005-09-25 Refactored the AbstractAdapter to be a lot less scary. Cleaned up the…
93 end
94
b8a533d5 » tenderlove
2013-03-16 @columns list is no longer necessary
95 def columns; @columns_hash.values; end
96
b9fa3541 » Marcel Molina
2005-09-27 Add extensive documentation to the ActiveRecord::AbstractAdapter. #2250
97 # Appends a primary key definition to the table definition.
98 # Can be called multiple times, but this is probably not a good idea.
bc8ebefe » tenderlove
2013-03-22 add uuid primary key support
99 def primary_key(name, type = :primary_key, options = {})
0215faf4 » rafaelfranca
2013-05-11 Merge pull request #10572 from nertzy/dont-modify-options-hash-in-pri…
100 column(name, type, options.merge(:primary_key => true))
b3df9598 » dhh
2005-09-25 Refactored the AbstractAdapter to be a lot less scary. Cleaned up the…
101 end
b9fa3541 » Marcel Molina
2005-09-27 Add extensive documentation to the ActiveRecord::AbstractAdapter. #2250
102
103 # Returns a ColumnDefinition for the column with name +name+.
b3df9598 » dhh
2005-09-25 Refactored the AbstractAdapter to be a lot less scary. Cleaned up the…
104 def [](name)
76c29a64 » tenderlove
2011-12-07 Use a hash to look up column definitions
105 @columns_hash[name.to_s]
b3df9598 » dhh
2005-09-25 Refactored the AbstractAdapter to be a lot less scary. Cleaned up the…
106 end
107
b9fa3541 » Marcel Molina
2005-09-27 Add extensive documentation to the ActiveRecord::AbstractAdapter. #2250
108 # Instantiates a new column for the table.
632ab246 » Marcel Molina
2007-12-05 Reveal that the type option in migrations can be any supported column…
109 # The +type+ parameter is normally one of the migrations native types,
110 # which is one of the following:
b9fa3541 » Marcel Molina
2005-09-27 Add extensive documentation to the ActiveRecord::AbstractAdapter. #2250
111 # <tt>:primary_key</tt>, <tt>:string</tt>, <tt>:text</tt>,
2a12b568 » jeremy
2006-07-08 r4704@asus: jeremy | 2006-06-27 12:00:19 -0700
112 # <tt>:integer</tt>, <tt>:float</tt>, <tt>:decimal</tt>,
113 # <tt>:datetime</tt>, <tt>:timestamp</tt>, <tt>:time</tt>,
114 # <tt>:date</tt>, <tt>:binary</tt>, <tt>:boolean</tt>.
b9fa3541 » Marcel Molina
2005-09-27 Add extensive documentation to the ActiveRecord::AbstractAdapter. #2250
115 #
632ab246 » Marcel Molina
2007-12-05 Reveal that the type option in migrations can be any supported column…
116 # You may use a type not in this list as long as it is supported by your
117 # database (for example, "polygon" in MySQL), but this will not be database
118 # agnostic and should usually be avoided.
119 #
b9fa3541 » Marcel Molina
2005-09-27 Add extensive documentation to the ActiveRecord::AbstractAdapter. #2250
120 # Available options are (none of these exists by default):
94502623 » Marcel Molina
2007-11-06 Standardize on using hyphens rather than colons to separate option na…
121 # * <tt>:limit</tt> -
b451de0d » spastorino
2010-08-14 Deletes trailing whitespaces (over text files only find * -type f -ex…
122 # Requests a maximum column length. This is number of characters for <tt>:string</tt> and
f0490624 » smartinez87
2011-05-03 Fixes on schema_definitions docs
123 # <tt>:text</tt> columns and number of bytes for <tt>:binary</tt> and <tt>:integer</tt> columns.
94502623 » Marcel Molina
2007-11-06 Standardize on using hyphens rather than colons to separate option na…
124 # * <tt>:default</tt> -
6019c268 » jeremy
2007-01-15 change_column accepts :default => nil. Closes #6956.
125 # The column's default value. Use nil for NULL.
94502623 » Marcel Molina
2007-11-06 Standardize on using hyphens rather than colons to separate option na…
126 # * <tt>:null</tt> -
46f30f90 » lifo
2008-05-16 Merge documentation changes from docrails.
127 # Allows or disallows +NULL+ values in the column. This option could
b9fa3541 » Marcel Molina
2005-09-27 Add extensive documentation to the ActiveRecord::AbstractAdapter. #2250
128 # have been named <tt>:null_allowed</tt>.
94502623 » Marcel Molina
2007-11-06 Standardize on using hyphens rather than colons to separate option na…
129 # * <tt>:precision</tt> -
46f30f90 » lifo
2008-05-16 Merge documentation changes from docrails.
130 # Specifies the precision for a <tt>:decimal</tt> column.
94502623 » Marcel Molina
2007-11-06 Standardize on using hyphens rather than colons to separate option na…
131 # * <tt>:scale</tt> -
46f30f90 » lifo
2008-05-16 Merge documentation changes from docrails.
132 # Specifies the scale for a <tt>:decimal</tt> column.
9a0d35e8 » schuetzm
2014-02-20 Make `:index` in migrations work with all column types
133 # * <tt>:index</tt> -
134 # Create an index for the column. Can be either <tt>true</tt> or an options hash.
2a12b568 » jeremy
2006-07-08 r4704@asus: jeremy | 2006-06-27 12:00:19 -0700
135 #
4513f354 » prathamesh-sonpatki
2014-06-04 Change wording of explanation about precision & scale of decimal numb…
136 # Note: The precision is the total number of significant digits
137 # and the scale is the number of digits that can be stored following
a17027d1 » lifo
2008-09-13 Merge docrails
138 # the decimal point. For example, the number 123.45 has a precision of 5
139 # and a scale of 2. A decimal with a precision of 5 and a scale of 2 can
140 # range from -999.99 to 999.99.
141 #
2a12b568 » jeremy
2006-07-08 r4704@asus: jeremy | 2006-06-27 12:00:19 -0700
142 # Please be aware of different RDBMS implementations behavior with
143 # <tt>:decimal</tt> columns:
144 # * The SQL standard says the default scale should be 0, <tt>:scale</tt> <=
145 # <tt>:precision</tt>, and makes no comments about the requirements of
146 # <tt>:precision</tt>.
46f30f90 » lifo
2008-05-16 Merge documentation changes from docrails.
147 # * MySQL: <tt>:precision</tt> [1..63], <tt>:scale</tt> [0..30].
2a12b568 » jeremy
2006-07-08 r4704@asus: jeremy | 2006-06-27 12:00:19 -0700
148 # Default is (10,0).
46f30f90 » lifo
2008-05-16 Merge documentation changes from docrails.
149 # * PostgreSQL: <tt>:precision</tt> [1..infinity],
2a12b568 » jeremy
2006-07-08 r4704@asus: jeremy | 2006-06-27 12:00:19 -0700
150 # <tt>:scale</tt> [0..infinity]. No default.
46f30f90 » lifo
2008-05-16 Merge documentation changes from docrails.
151 # * SQLite2: Any <tt>:precision</tt> and <tt>:scale</tt> may be used.
2a12b568 » jeremy
2006-07-08 r4704@asus: jeremy | 2006-06-27 12:00:19 -0700
152 # Internal storage as strings. No default.
6019c268 » jeremy
2007-01-15 change_column accepts :default => nil. Closes #6956.
153 # * SQLite3: No restrictions on <tt>:precision</tt> and <tt>:scale</tt>,
2a12b568 » jeremy
2006-07-08 r4704@asus: jeremy | 2006-06-27 12:00:19 -0700
154 # but the maximum supported <tt>:precision</tt> is 16. No default.
46f30f90 » lifo
2008-05-16 Merge documentation changes from docrails.
155 # * Oracle: <tt>:precision</tt> [1..38], <tt>:scale</tt> [-84..127].
2a12b568 » jeremy
2006-07-08 r4704@asus: jeremy | 2006-06-27 12:00:19 -0700
156 # Default is (38,0).
46f30f90 » lifo
2008-05-16 Merge documentation changes from docrails.
157 # * DB2: <tt>:precision</tt> [1..63], <tt>:scale</tt> [0..62].
2a12b568 » jeremy
2006-07-08 r4704@asus: jeremy | 2006-06-27 12:00:19 -0700
158 # Default unknown.
46f30f90 » lifo
2008-05-16 Merge documentation changes from docrails.
159 # * SqlServer?: <tt>:precision</tt> [1..38], <tt>:scale</tt> [0..38].
2a12b568 » jeremy
2006-07-08 r4704@asus: jeremy | 2006-06-27 12:00:19 -0700
160 # Default (38,0).
b9fa3541 » Marcel Molina
2005-09-27 Add extensive documentation to the ActiveRecord::AbstractAdapter. #2250
161 #
162 # This method returns <tt>self</tt>.
163 #
4cbbebb1 » dhh
2007-05-06 Added short-hand declaration style to migrations (inspiration from Se…
164 # == Examples
f0490624 » smartinez87
2011-05-03 Fixes on schema_definitions docs
165 # # Assuming +td+ is an instance of TableDefinition
53924078 » technoweenie
2006-06-21 Don't use keywords as local vars in documentation. Closes #5291. [jer…
166 # td.column(:granted, :boolean)
98dc5827 » lifo
2008-05-25 Merge docrails.
167 # # granted BOOLEAN
b9fa3541 » Marcel Molina
2005-09-27 Add extensive documentation to the ActiveRecord::AbstractAdapter. #2250
168 #
9edd4e18 » AvnerCohen
2012-10-23 Migration of docs to 1.9 hash syntax
169 # td.column(:picture, :binary, limit: 2.megabytes)
98dc5827 » lifo
2008-05-25 Merge docrails.
170 # # => picture BLOB(2097152)
b9fa3541 » Marcel Molina
2005-09-27 Add extensive documentation to the ActiveRecord::AbstractAdapter. #2250
171 #
9edd4e18 » AvnerCohen
2012-10-23 Migration of docs to 1.9 hash syntax
172 # td.column(:sales_stage, :string, limit: 20, default: 'new', null: false)
98dc5827 » lifo
2008-05-25 Merge docrails.
173 # # => sales_stage VARCHAR(20) DEFAULT 'new' NOT NULL
2a12b568 » jeremy
2006-07-08 r4704@asus: jeremy | 2006-06-27 12:00:19 -0700
174 #
9edd4e18 » AvnerCohen
2012-10-23 Migration of docs to 1.9 hash syntax
175 # td.column(:bill_gates_money, :decimal, precision: 15, scale: 2)
98dc5827 » lifo
2008-05-25 Merge docrails.
176 # # => bill_gates_money DECIMAL(15,2)
2a12b568 » jeremy
2006-07-08 r4704@asus: jeremy | 2006-06-27 12:00:19 -0700
177 #
9edd4e18 » AvnerCohen
2012-10-23 Migration of docs to 1.9 hash syntax
178 # td.column(:sensor_reading, :decimal, precision: 30, scale: 20)
98dc5827 » lifo
2008-05-25 Merge docrails.
179 # # => sensor_reading DECIMAL(30,20)
2a12b568 » jeremy
2006-07-08 r4704@asus: jeremy | 2006-06-27 12:00:19 -0700
180 #
181 # # While <tt>:scale</tt> defaults to zero on most databases, it
182 # # probably wouldn't hurt to include it.
9edd4e18 » AvnerCohen
2012-10-23 Migration of docs to 1.9 hash syntax
183 # td.column(:huge_integer, :decimal, precision: 30)
98dc5827 » lifo
2008-05-25 Merge docrails.
184 # # => huge_integer DECIMAL(30)
4cbbebb1 » dhh
2007-05-06 Added short-hand declaration style to migrations (inspiration from Se…
185 #
a17027d1 » lifo
2008-09-13 Merge docrails
186 # # Defines a column with a database-specific type.
187 # td.column(:foo, 'polygon')
188 # # => foo polygon
189 #
4cbbebb1 » dhh
2007-05-06 Added short-hand declaration style to migrations (inspiration from Se…
190 # == Short-hand examples
191 #
98dc5827 » lifo
2008-05-25 Merge docrails.
192 # Instead of calling +column+ directly, you can also work with the short-hand definitions for the default types.
4cbbebb1 » dhh
2007-05-06 Added short-hand declaration style to migrations (inspiration from Se…
193 # They use the type as the method name instead of as a parameter and allow for multiple columns to be defined
194 # in a single statement.
195 #
196 # What can be written like this with the regular calls to column:
197 #
27ca9151 » fxn
2012-12-07 revises a RDoc example to make it idiomatic
198 # create_table :products do |t|
9a0d35e8 » schuetzm
2014-02-20 Make `:index` in migrations work with all column types
199 # t.column :shop_id, :integer
200 # t.column :creator_id, :integer
201 # t.column :item_number, :string
202 # t.column :name, :string, default: "Untitled"
203 # t.column :value, :string, default: "Untitled"
204 # t.column :created_at, :datetime
205 # t.column :updated_at, :datetime
4cbbebb1 » dhh
2007-05-06 Added short-hand declaration style to migrations (inspiration from Se…
206 # end
9a0d35e8 » schuetzm
2014-02-20 Make `:index` in migrations work with all column types
207 # add_index :products, :item_number
4cbbebb1 » dhh
2007-05-06 Added short-hand declaration style to migrations (inspiration from Se…
208 #
27ca9151 » fxn
2012-12-07 revises a RDoc example to make it idiomatic
209 # can also be written as follows using the short-hand:
4cbbebb1 » dhh
2007-05-06 Added short-hand declaration style to migrations (inspiration from Se…
210 #
211 # create_table :products do |t|
212 # t.integer :shop_id, :creator_id
9a0d35e8 » schuetzm
2014-02-20 Make `:index` in migrations work with all column types
213 # t.string :item_number, index: true
9edd4e18 » AvnerCohen
2012-10-23 Migration of docs to 1.9 hash syntax
214 # t.string :name, :value, default: "Untitled"
4cbbebb1 » dhh
2007-05-06 Added short-hand declaration style to migrations (inspiration from Se…
215 # t.timestamps
216 # end
217 #
46f30f90 » lifo
2008-05-16 Merge documentation changes from docrails.
218 # There's a short-hand method for each of the type values declared at the top. And then there's
d53c2e49 » smartinez87
2011-05-02 fixed-fonts on SchemaDefinitions
219 # TableDefinition#timestamps that'll add +created_at+ and +updated_at+ as datetimes.
6ddde027 » NZKoz
2007-10-19 Add t.belongs_to and t.references to sexy migrations [arthurgeek]
220 #
221 # TableDefinition#references will add an appropriately-named _id column, plus a corresponding _type
b451de0d » spastorino
2010-08-14 Deletes trailing whitespaces (over text files only find * -type f -ex…
222 # column if the <tt>:polymorphic</tt> option is supplied. If <tt>:polymorphic</tt> is a hash of
ca0af822 » joshuap
2012-03-02 Automatically create indexes for references/belongs_to statements in …
223 # options, these will be used when creating the <tt>_type</tt> column. The <tt>:index</tt> option
224 # will also create an index, similar to calling <tt>add_index</tt>. So what can be written like this:
6ddde027 » NZKoz
2007-10-19 Add t.belongs_to and t.references to sexy migrations [arthurgeek]
225 #
226 # create_table :taggings do |t|
227 # t.integer :tag_id, :tagger_id, :taggable_id
228 # t.string :tagger_type
9edd4e18 » AvnerCohen
2012-10-23 Migration of docs to 1.9 hash syntax
229 # t.string :taggable_type, default: 'Photo'
6ddde027 » NZKoz
2007-10-19 Add t.belongs_to and t.references to sexy migrations [arthurgeek]
230 # end
9edd4e18 » AvnerCohen
2012-10-23 Migration of docs to 1.9 hash syntax
231 # add_index :taggings, :tag_id, name: 'index_taggings_on_tag_id'
ca0af822 » joshuap
2012-03-02 Automatically create indexes for references/belongs_to statements in …
232 # add_index :taggings, [:tagger_id, :tagger_type]
6ddde027 » NZKoz
2007-10-19 Add t.belongs_to and t.references to sexy migrations [arthurgeek]
233 #
234 # Can also be written as follows using references:
235 #
236 # create_table :taggings do |t|
9edd4e18 » AvnerCohen
2012-10-23 Migration of docs to 1.9 hash syntax
237 # t.references :tag, index: { name: 'index_taggings_on_tag_id' }
238 # t.references :tagger, polymorphic: true, index: true
239 # t.references :taggable, polymorphic: { default: 'Photo' }
6ddde027 » NZKoz
2007-10-19 Add t.belongs_to and t.references to sexy migrations [arthurgeek]
240 # end
b3df9598 » dhh
2005-09-25 Refactored the AbstractAdapter to be a lot less scary. Cleaned up the…
241 def column(name, type, options = {})
483a3cf2 » tenderlove
2011-12-07 automatically add the column definition to the columns list if creati…
242 name = name.to_s
243 type = type.to_sym
244
e4790a2c » senny
2012-10-28 raise `ArgumentError` when redefining the primary key column. Closes #…
245 if primary_key_column_name == name
246 raise ArgumentError, "you can't redefine the primary key column '#{name}'. To define a custom primary key, pass { id: false } to create_table."
247 end
248
9a0d35e8 » schuetzm
2014-02-20 Make `:index` in migrations work with all column types
249 index_options = options.delete(:index)
250 index(name, index_options.is_a?(Hash) ? index_options : {}) if index_options
f84cf418 » tenderlove
2013-03-16 there is no reason to check for an already defined column
251 @columns_hash[name] = new_column_definition(name, type, options)
b3df9598 » dhh
2005-09-25 Refactored the AbstractAdapter to be a lot less scary. Cleaned up the…
252 self
253 end
68160b34 » dhh
2005-10-29 Added migration support for Oracle (closes #2647) [Michael Schoen]
254
c5e03e87 » tenderlove
2013-03-16 keep ivars private, do not manipulate them outside their owner object
255 def remove_column(name)
256 @columns_hash.delete name.to_s
257 end
258
2f997797 » jonleighton
2013-02-15 Revert "Revert "Merge pull request #9206 from ranjaykrishna/use_defin…
259 [:string, :text, :integer, :float, :decimal, :datetime, :timestamp, :time, :date, :binary, :boolean].each do |column_type|
260 define_method column_type do |*args|
261 options = args.extract_options!
262 column_names = args
263 column_names.each { |name| column(name, column_type, options) }
264 end
4cbbebb1 » dhh
2007-05-06 Added short-hand declaration style to migrations (inspiration from Se…
265 end
719ea779 » rafaelfranca
2012-07-03 Don't need to use delete in the options hash
266
ca0af822 » joshuap
2012-03-02 Automatically create indexes for references/belongs_to statements in …
267 # Adds index options to the indexes hash, keyed by column name
268 # This is primarily used to track indexes that need to be created after the table
5277d550 » vijaydev
2012-05-10 remove unnecessary 'examples' noise [ci skip]
269 #
9edd4e18 » AvnerCohen
2012-10-23 Migration of docs to 1.9 hash syntax
270 # index(:account_id, name: 'index_projects_on_account_id')
ca0af822 » joshuap
2012-03-02 Automatically create indexes for references/belongs_to statements in …
271 def index(column_name, options = {})
272 indexes[column_name] = options
273 end
46f30f90 » lifo
2008-05-16 Merge documentation changes from docrails.
274
275 # Appends <tt>:datetime</tt> columns <tt>:created_at</tt> and
4570cf66 » Marcel Molina
2007-12-05 Document the timestamps schema definition method. Closes #9000 [mikong]
276 # <tt>:updated_at</tt> to the table.
683ff235 » reagent
2008-08-22 Ensure t.timestamps respects options. [#828 state:resolved]
277 def timestamps(*args)
ccf7cd05 » Dave Kroondyk
2012-07-18 revert Default timestamps to non-null
278 options = args.extract_options!
683ff235 » reagent
2008-08-22 Ensure t.timestamps respects options. [#828 state:resolved]
279 column(:created_at, :datetime, options)
280 column(:updated_at, :datetime, options)
4cbbebb1 » dhh
2007-05-06 Added short-hand declaration style to migrations (inspiration from Se…
281 end
282
6ddde027 » NZKoz
2007-10-19 Add t.belongs_to and t.references to sexy migrations [arthurgeek]
283 def references(*args)
284 options = args.extract_options!
285 polymorphic = options.delete(:polymorphic)
ca0af822 » joshuap
2012-03-02 Automatically create indexes for references/belongs_to statements in …
286 index_options = options.delete(:index)
6ddde027 » NZKoz
2007-10-19 Add t.belongs_to and t.references to sexy migrations [arthurgeek]
287 args.each do |col|
288 column("#{col}_id", :integer, options)
68f0ef90 » lexmag
2012-07-02 Refactor references schema definitions
289 column("#{col}_type", :string, polymorphic.is_a?(Hash) ? polymorphic : options) if polymorphic
ad906200 » pwnall
2013-04-01 Make references with index:true pass Hash options to add_index.
290 index(polymorphic ? %w(id type).map { |t| "#{col}_#{t}" } : "#{col}_id", index_options.is_a?(Hash) ? index_options : {}) if index_options
6ddde027 » NZKoz
2007-10-19 Add t.belongs_to and t.references to sexy migrations [arthurgeek]
291 end
292 end
293 alias :belongs_to :references
294
f20b2f4e » tenderlove
2013-03-16 push alter table add column sql in to the schema modification visitor
295 def new_column_definition(name, type, options) # :nodoc:
d0f8c46e » sgrif
2014-05-19 Remove :timestamp column type
296 type = aliased_types[type] || type
d43edf67 » tenderlove
2013-03-16 push column initialization down to the factory method
297 column = create_column_definition name, type
298 limit = options.fetch(:limit) do
299 native[type][:limit] if native[type].is_a?(Hash)
300 end
301
d25e4076 » tenderlove
2013-03-22 separate primary key from column type
302 column.limit = limit
e4fe4973 » scudco
2013-05-05 Fixes #10432 add_column not creating array columns in PostgreSQL
303 column.array = options[:array] if column.respond_to?(:array)
d25e4076 » tenderlove
2013-03-22 separate primary key from column type
304 column.precision = options[:precision]
305 column.scale = options[:scale]
306 column.default = options[:default]
307 column.null = options[:null]
308 column.first = options[:first]
309 column.after = options[:after]
310 column.primary_key = type == :primary_key || options[:primary_key]
d43edf67 » tenderlove
2013-03-16 push column initialization down to the factory method
311 column
483a3cf2 » tenderlove
2011-12-07 automatically add the column definition to the columns list if creati…
312 end
313
f20b2f4e » tenderlove
2013-03-16 push alter table add column sql in to the schema modification visitor
314 private
315 def create_column_definition(name, type)
316 ColumnDefinition.new name, type
317 end
318
e4790a2c » senny
2012-10-28 raise `ArgumentError` when redefining the primary key column. Closes #…
319 def primary_key_column_name
c1003d99 » tenderlove
2013-03-14 ask column if it is a pk
320 primary_key_column = columns.detect { |c| c.primary_key? }
e4790a2c » senny
2012-10-28 raise `ArgumentError` when redefining the primary key column. Closes #…
321 primary_key_column && primary_key_column.name
322 end
323
483a3cf2 » tenderlove
2011-12-07 automatically add the column definition to the columns list if creati…
324 def native
a80bcc39 » tenderlove
2013-03-15 mostly decouple TableDefinition from the database connection
325 @native
483a3cf2 » tenderlove
2011-12-07 automatically add the column definition to the columns list if creati…
326 end
d0f8c46e » sgrif
2014-05-19 Remove :timestamp column type
327
328 def aliased_types
329 HashWithIndifferentAccess.new(
330 timestamp: :datetime,
331 )
332 end
b3df9598 » dhh
2005-09-25 Refactored the AbstractAdapter to be a lot less scary. Cleaned up the…
333 end
96980bd5 » dhh
2008-05-03 Added change_table for migrations (Jeff Dean) [#71 state:resolved]
334
f20b2f4e » tenderlove
2013-03-16 push alter table add column sql in to the schema modification visitor
335 class AlterTable # :nodoc:
739a7203 » tenderlove
2013-03-16 allow multiple add columns
336 attr_reader :adds
1c170fde » senny
2014-06-10 fk: generalize using `AlterTable` and `SchemaCreation`.
337 attr_reader :foreign_key_adds
338 attr_reader :foreign_key_drops
f20b2f4e » tenderlove
2013-03-16 push alter table add column sql in to the schema modification visitor
339
340 def initialize(td)
739a7203 » tenderlove
2013-03-16 allow multiple add columns
341 @td = td
342 @adds = []
1c170fde » senny
2014-06-10 fk: generalize using `AlterTable` and `SchemaCreation`.
343 @foreign_key_adds = []
344 @foreign_key_drops = []
f20b2f4e » tenderlove
2013-03-16 push alter table add column sql in to the schema modification visitor
345 end
346
347 def name; @td.name; end
348
1c170fde » senny
2014-06-10 fk: generalize using `AlterTable` and `SchemaCreation`.
349 def add_foreign_key(to_table, options)
350 @foreign_key_adds << ForeignKeyDefinition.new(name, to_table, options)
351 end
352
353 def drop_foreign_key(name)
354 @foreign_key_drops << name
355 end
356
f20b2f4e » tenderlove
2013-03-16 push alter table add column sql in to the schema modification visitor
357 def add_column(name, type, options)
358 name = name.to_s
359 type = type.to_sym
739a7203 » tenderlove
2013-03-16 allow multiple add columns
360 @adds << @td.new_column_definition(name, type, options)
f20b2f4e » tenderlove
2013-03-16 push alter table add column sql in to the schema modification visitor
361 end
362 end
363
d191db76 » fxn
2010-08-05 standarizes the use of the article "an" for "SQL" and "SQLite"
364 # Represents an SQL table in an abstract way for updating a table.
96980bd5 » dhh
2008-05-03 Added change_table for migrations (Jeff Dean) [#71 state:resolved]
365 # Also see TableDefinition and SchemaStatements#create_table
366 #
367 # Available transformations are:
368 #
369 # change_table :table do |t|
370 # t.column
371 # t.index
1a782b2b » CvX
2012-11-19 Add rename_index to change_table.
372 # t.rename_index
96980bd5 » dhh
2008-05-03 Added change_table for migrations (Jeff Dean) [#71 state:resolved]
373 # t.timestamps
374 # t.change
375 # t.change_default
376 # t.rename
377 # t.references
378 # t.belongs_to
379 # t.string
380 # t.text
381 # t.integer
382 # t.float
383 # t.decimal
384 # t.datetime
385 # t.timestamp
386 # t.time
387 # t.date
388 # t.binary
389 # t.boolean
390 # t.remove
391 # t.remove_references
392 # t.remove_belongs_to
393 # t.remove_index
394 # t.remove_timestamps
395 # end
396 #
397 class Table
398 def initialize(table_name, base)
399 @table_name = table_name
400 @base = base
401 end
402
403 # Adds a new column to the named table.
404 # See TableDefinition#column for details of the options you can use.
5277d550 » vijaydev
2012-05-10 remove unnecessary 'examples' noise [ci skip]
405 #
46f30f90 » lifo
2008-05-16 Merge documentation changes from docrails.
406 # ====== Creating a simple column
96980bd5 » dhh
2008-05-03 Added change_table for migrations (Jeff Dean) [#71 state:resolved]
407 # t.column(:name, :string)
408 def column(column_name, type, options = {})
409 @base.add_column(@table_name, column_name, type, options)
410 end
411
11ff3da5 » pixeltrix
2010-03-18 Add column and index query methods to ActiveRecord::Schema
412 # Checks to see if a column exists. See SchemaStatements#column_exists?
3cc83a8f » grantneufeld
2011-06-18 Changed the default value for the options argument on ActiveRecord::C…
413 def column_exists?(column_name, type = nil, options = {})
11ff3da5 » pixeltrix
2010-03-18 Add column and index query methods to ActiveRecord::Schema
414 @base.column_exists?(@table_name, column_name, type, options)
415 end
416
46f30f90 » lifo
2008-05-16 Merge documentation changes from docrails.
417 # Adds a new index to the table. +column_name+ can be a single Symbol, or
418 # an Array of Symbols. See SchemaStatements#add_index
96980bd5 » dhh
2008-05-03 Added change_table for migrations (Jeff Dean) [#71 state:resolved]
419 #
420 # ====== Creating a simple index
421 # t.index(:name)
422 # ====== Creating a unique index
9edd4e18 » AvnerCohen
2012-10-23 Migration of docs to 1.9 hash syntax
423 # t.index([:branch_id, :party_id], unique: true)
96980bd5 » dhh
2008-05-03 Added change_table for migrations (Jeff Dean) [#71 state:resolved]
424 # ====== Creating a named index
9edd4e18 » AvnerCohen
2012-10-23 Migration of docs to 1.9 hash syntax
425 # t.index([:branch_id, :party_id], unique: true, name: 'by_branch_party')
96980bd5 » dhh
2008-05-03 Added change_table for migrations (Jeff Dean) [#71 state:resolved]
426 def index(column_name, options = {})
427 @base.add_index(@table_name, column_name, options)
428 end
429
11ff3da5 » pixeltrix
2010-03-18 Add column and index query methods to ActiveRecord::Schema
430 # Checks to see if an index exists. See SchemaStatements#index_exists?
431 def index_exists?(column_name, options = {})
432 @base.index_exists?(@table_name, column_name, options)
433 end
434
1a782b2b » CvX
2012-11-19 Add rename_index to change_table.
435 # Renames the given index on the table.
436 #
437 # t.rename_index(:user_id, :account_id)
438 def rename_index(index_name, new_index_name)
439 @base.rename_index(@table_name, index_name, new_index_name)
440 end
441
d53c2e49 » smartinez87
2011-05-02 fixed-fonts on SchemaDefinitions
442 # Adds timestamps (+created_at+ and +updated_at+) columns to the table. See SchemaStatements#add_timestamps
5277d550 » vijaydev
2012-05-10 remove unnecessary 'examples' noise [ci skip]
443 #
96980bd5 » dhh
2008-05-03 Added change_table for migrations (Jeff Dean) [#71 state:resolved]
444 # t.timestamps
445 def timestamps
446 @base.add_timestamps(@table_name)
447 end
448
449 # Changes the column's definition according to the new options.
450 # See TableDefinition#column for details of the options you can use.
5277d550 » vijaydev
2012-05-10 remove unnecessary 'examples' noise [ci skip]
451 #
9edd4e18 » AvnerCohen
2012-10-23 Migration of docs to 1.9 hash syntax
452 # t.change(:name, :string, limit: 80)
96980bd5 » dhh
2008-05-03 Added change_table for migrations (Jeff Dean) [#71 state:resolved]
453 # t.change(:description, :text)
454 def change(column_name, type, options = {})
455 @base.change_column(@table_name, column_name, type, options)
456 end
457
46f30f90 » lifo
2008-05-16 Merge documentation changes from docrails.
458 # Sets a new default value for a column. See SchemaStatements#change_column_default
5277d550 » vijaydev
2012-05-10 remove unnecessary 'examples' noise [ci skip]
459 #
96980bd5 » dhh
2008-05-03 Added change_table for migrations (Jeff Dean) [#71 state:resolved]
460 # t.change_default(:qualification, 'new')
461 # t.change_default(:authorized, 1)
462 def change_default(column_name, default)
463 @base.change_column_default(@table_name, column_name, default)
464 end
465
466 # Removes the column(s) from the table definition.
5277d550 » vijaydev
2012-05-10 remove unnecessary 'examples' noise [ci skip]
467 #
96980bd5 » dhh
2008-05-03 Added change_table for migrations (Jeff Dean) [#71 state:resolved]
468 # t.remove(:qualification)
469 # t.remove(:qualification, :experience)
470 def remove(*column_names)
e28ddea0 » marcandre
2012-11-19 Differentiate between remove_column and remove_columns. Make remove_c…
471 @base.remove_columns(@table_name, *column_names)
96980bd5 » dhh
2008-05-03 Added change_table for migrations (Jeff Dean) [#71 state:resolved]
472 end
473
46f30f90 » lifo
2008-05-16 Merge documentation changes from docrails.
474 # Removes the given index from the table.
96980bd5 » dhh
2008-05-03 Added change_table for migrations (Jeff Dean) [#71 state:resolved]
475 #
144a388d » raviolicode
2011-06-17 Update remove_index documentation
476 # ====== Remove the index_table_name_on_column in the table_name table
477 # t.remove_index :column
478 # ====== Remove the index named index_table_name_on_branch_id in the table_name table
9edd4e18 » AvnerCohen
2012-10-23 Migration of docs to 1.9 hash syntax
479 # t.remove_index column: :branch_id
144a388d » raviolicode
2011-06-17 Update remove_index documentation
480 # ====== Remove the index named index_table_name_on_branch_id_and_party_id in the table_name table
9edd4e18 » AvnerCohen
2012-10-23 Migration of docs to 1.9 hash syntax
481 # t.remove_index column: [:branch_id, :party_id]
144a388d » raviolicode
2011-06-17 Update remove_index documentation
482 # ====== Remove the index named by_branch_party in the table_name table
9edd4e18 » AvnerCohen
2012-10-23 Migration of docs to 1.9 hash syntax
483 # t.remove_index name: :by_branch_party
96980bd5 » dhh
2008-05-03 Added change_table for migrations (Jeff Dean) [#71 state:resolved]
484 def remove_index(options = {})
485 @base.remove_index(@table_name, options)
486 end
487
d53c2e49 » smartinez87
2011-05-02 fixed-fonts on SchemaDefinitions
488 # Removes the timestamp columns (+created_at+ and +updated_at+) from the table.
5277d550 » vijaydev
2012-05-10 remove unnecessary 'examples' noise [ci skip]
489 #
96980bd5 » dhh
2008-05-03 Added change_table for migrations (Jeff Dean) [#71 state:resolved]
490 # t.remove_timestamps
491 def remove_timestamps
492 @base.remove_timestamps(@table_name)
493 end
494
495 # Renames a column.
5277d550 » vijaydev
2012-05-10 remove unnecessary 'examples' noise [ci skip]
496 #
96980bd5 » dhh
2008-05-03 Added change_table for migrations (Jeff Dean) [#71 state:resolved]
497 # t.rename(:description, :name)
498 def rename(column_name, new_column_name)
499 @base.rename_column(@table_name, column_name, new_column_name)
500 end
501
f0490624 » smartinez87
2011-05-03 Fixes on schema_definitions docs
502 # Adds a reference. Optionally adds a +type+ column, if <tt>:polymorphic</tt> option is provided.
46f30f90 » lifo
2008-05-16 Merge documentation changes from docrails.
503 # <tt>references</tt> and <tt>belongs_to</tt> are acceptable.
5277d550 » vijaydev
2012-05-10 remove unnecessary 'examples' noise [ci skip]
504 #
68f0ef90 » lexmag
2012-07-02 Refactor references schema definitions
505 # t.references(:user)
506 # t.belongs_to(:supplier, polymorphic: true)
507 #
96980bd5 » dhh
2008-05-03 Added change_table for migrations (Jeff Dean) [#71 state:resolved]
508 def references(*args)
509 options = args.extract_options!
68f0ef90 » lexmag
2012-07-02 Refactor references schema definitions
510 args.each do |ref_name|
511 @base.add_reference(@table_name, ref_name, options)
96980bd5 » dhh
2008-05-03 Added change_table for migrations (Jeff Dean) [#71 state:resolved]
512 end
513 end
514 alias :belongs_to :references
515
46f30f90 » lifo
2008-05-16 Merge documentation changes from docrails.
516 # Removes a reference. Optionally removes a +type+ column.
517 # <tt>remove_references</tt> and <tt>remove_belongs_to</tt> are acceptable.
5277d550 » vijaydev
2012-05-10 remove unnecessary 'examples' noise [ci skip]
518 #
68f0ef90 » lexmag
2012-07-02 Refactor references schema definitions
519 # t.remove_references(:user)
520 # t.remove_belongs_to(:supplier, polymorphic: true)
521 #
96980bd5 » dhh
2008-05-03 Added change_table for migrations (Jeff Dean) [#71 state:resolved]
522 def remove_references(*args)
523 options = args.extract_options!
68f0ef90 » lexmag
2012-07-02 Refactor references schema definitions
524 args.each do |ref_name|
525 @base.remove_reference(@table_name, ref_name, options)
96980bd5 » dhh
2008-05-03 Added change_table for migrations (Jeff Dean) [#71 state:resolved]
526 end
527 end
68f0ef90 » lexmag
2012-07-02 Refactor references schema definitions
528 alias :remove_belongs_to :remove_references
96980bd5 » dhh
2008-05-03 Added change_table for migrations (Jeff Dean) [#71 state:resolved]
529
530 # Adds a column or columns of a specified type
5277d550 » vijaydev
2012-05-10 remove unnecessary 'examples' noise [ci skip]
531 #
96980bd5 » dhh
2008-05-03 Added change_table for migrations (Jeff Dean) [#71 state:resolved]
532 # t.string(:goat)
533 # t.string(:goat, :sheep)
2f997797 » jonleighton
2013-02-15 Revert "Revert "Merge pull request #9206 from ranjaykrishna/use_defin…
534 [:string, :text, :integer, :float, :decimal, :datetime, :timestamp, :time, :date, :binary, :boolean].each do |column_type|
535 define_method column_type do |*args|
536 options = args.extract_options!
537 args.each do |name|
538 @base.add_column(@table_name, name, column_type, options)
539 end
36abb550 » senny
2013-02-28 update docs, change_table does not use TableDefinition.
540 end
96980bd5 » dhh
2008-05-03 Added change_table for migrations (Jeff Dean) [#71 state:resolved]
541 end
542
543 private
544 def native
545 @base.native_database_types
546 end
547 end
548
b3df9598 » dhh
2005-09-25 Refactored the AbstractAdapter to be a lot less scary. Cleaned up the…
549 end
0d07152a » Marcel Molina
2005-09-28 Make schema definition column lookups agnostic w/r/t/ whether a key o…
550 end
Something went wrong with that request. Please try again.