Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 697 lines (621 sloc) 22.575 kB
b3df959 @dhh Refactored the AbstractAdapter to be a lot less scary. Cleaned up the…
dhh authored
1 module ActiveRecord
2 module ConnectionAdapters #:nodoc:
92887ca @waseem Add documentation for IndexDefinition.
waseem authored
3 # Abstract representation of an index definition on a table. Instances of
4 # this type are typically created and returned by methods in database
5 # adapters. e.g. ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter#indexes
9600e0b @kenmazaika Add support for FULLTEXT and SPATIAL indexes using the :type flag for…
kenmazaika authored
6 class IndexDefinition < Struct.new(:table, :name, :unique, :columns, :lengths, :orders, :where, :type, :using) #:nodoc:
b3df959 @dhh Refactored the AbstractAdapter to be a lot less scary. Cleaned up the…
dhh authored
7 end
8
a17027d @lifo Merge docrails
lifo authored
9 # Abstract representation of a column definition. Instances of this type
10 # are typically created by methods in TableDefinition, and added to the
11 # +columns+ attribute of said TableDefinition object, in order to be used
12 # for generating a number of table creation or table changing SQL statements.
446f252 @kamipo Move the collation handling code from the MySQL adapter to common cla…
kamipo authored
13 class ColumnDefinition < Struct.new(:name, :type, :limit, :precision, :scale, :default, :null, :first, :after, :auto_increment, :primary_key, :collation, :sql_type) #:nodoc:
4b2e16e @tenderlove all columns respond to string_to_binary, so no need to check respond_to?
tenderlove authored
14
c1003d9 @tenderlove ask column if it is a pk
tenderlove authored
15 def primary_key?
d25e407 @tenderlove separate primary key from column type
tenderlove authored
16 primary_key || type.to_sym == :primary_key
c1003d9 @tenderlove ask column if it is a pk
tenderlove authored
17 end
b3df959 @dhh Refactored the AbstractAdapter to be a lot less scary. Cleaned up the…
dhh authored
18 end
19
cd00526 @kamipo Change the `visit_AddColumn` visiblity for the internal API
kamipo authored
20 class AddColumnDefinition < Struct.new(:column) # :nodoc:
21 end
22
3c10043 @kamipo Refactor `visit_ChangeColumnDefinition`
kamipo authored
23 class ChangeColumnDefinition < Struct.new(:column, :name) #:nodoc:
ea72430 @jeran Moving add_column_options! up to SchemaCreation
jeran authored
24 end
25
c153503 @rafaelfranca Add nodoc to internal class [ci skip]
rafaelfranca authored
26 class ForeignKeyDefinition < Struct.new(:from_table, :to_table, :options) #:nodoc:
74b2fe4 @senny fk: `foreign_keys`, `add_foreign_key` and `remove_foreign_key` for MySQL
senny authored
27 def name
28 options[:name]
29 end
30
31 def column
32 options[:column]
33 end
34
35 def primary_key
8768305 @senny fk: use random digest names
senny authored
36 options[:primary_key] || default_primary_key
74b2fe4 @senny fk: `foreign_keys`, `add_foreign_key` and `remove_foreign_key` for MySQL
senny authored
37 end
402f303 @senny fk: support dependent option (:delete, :nullify and :restrict).
senny authored
38
6955d86 @senny fk: rename `dependent` to `on_delete`
senny authored
39 def on_delete
40 options[:on_delete]
402f303 @senny fk: support dependent option (:delete, :nullify and :restrict).
senny authored
41 end
acd0287 @senny fk: support for on_update
senny authored
42
43 def on_update
44 options[:on_update]
45 end
8768305 @senny fk: use random digest names
senny authored
46
47 def custom_primary_key?
48 options[:primary_key] != default_primary_key
49 end
50
f0ae503 @estum Add methods to get foreign key matching arguments
estum authored
51 def defined_for?(options_or_to_table = {})
52 if options_or_to_table.is_a?(Hash)
6bd777c @senny Merge pull request #18662 from estum/foreign-key-exists
senny authored
53 options_or_to_table.all? {|key, value| options[key].to_s == value.to_s }
f0ae503 @estum Add methods to get foreign key matching arguments
estum authored
54 else
55 to_table == options_or_to_table.to_s
56 end
57 end
58
8768305 @senny fk: use random digest names
senny authored
59 private
60 def default_primary_key
61 "id"
62 end
74b2fe4 @senny fk: `foreign_keys`, `add_foreign_key` and `remove_foreign_key` for MySQL
senny authored
63 end
64
d26704a @sgrif Refactor a common class to reduce the duplication for `references`
sgrif authored
65 class ReferenceDefinition # :nodoc:
66 def initialize(
67 name,
68 polymorphic: false,
69 index: false,
70 foreign_key: false,
71 type: :integer,
72 **options
73 )
74 @name = name
75 @polymorphic = polymorphic
76 @index = index
77 @foreign_key = foreign_key
78 @type = type
79 @options = options
80
81 if polymorphic && foreign_key
82 raise ArgumentError, "Cannot add a foreign key to a polymorphic relation"
83 end
84 end
85
86 def add_to(table)
87 columns.each do |column_options|
88 table.column(*column_options)
89 end
90
91 if index
92 table.index(column_names, index_options)
93 end
94
95 if foreign_key
96 table.foreign_key(foreign_table_name, foreign_key_options)
97 end
98 end
99
100 protected
101
102 attr_reader :name, :polymorphic, :index, :foreign_key, :type, :options
103
104 private
105
106 def as_options(value, default = {})
107 if value.is_a?(Hash)
108 value
109 else
110 default
111 end
112 end
113
114 def polymorphic_options
115 as_options(polymorphic, options)
116 end
117
118 def index_options
119 as_options(index)
120 end
121
122 def foreign_key_options
123 as_options(foreign_key)
124 end
125
126 def columns
127 result = [["#{name}_id", type, options]]
128 if polymorphic
129 result.unshift(["#{name}_type", :string, polymorphic_options])
130 end
131 result
132 end
133
134 def column_names
135 columns.map(&:first)
136 end
137
138 def foreign_table_name
8c11807 @meinac use singular table name if pluralize_table_names is setted as false w…
meinac authored
139 Base.pluralize_table_names ? name.to_s.pluralize : name
d26704a @sgrif Refactor a common class to reduce the duplication for `references`
sgrif authored
140 end
141 end
142
d47357e @kamipo Extract `primary_key` method into `ColumnMethods`
kamipo authored
143 module ColumnMethods
144 # Appends a primary key definition to the table definition.
145 # Can be called multiple times, but this is probably not a good idea.
146 def primary_key(name, type = :primary_key, **options)
147 column(name, type, options.merge(primary_key: true))
148 end
8fade12 @kamipo Extract the short-hand methods into `ColumnMethods`
kamipo authored
149
150 # Appends a column or columns of a specified type.
151 #
152 # t.string(:goat)
153 # t.string(:goat, :sheep)
154 #
155 # See TableDefinition#column
156 [
157 :bigint,
158 :binary,
159 :boolean,
160 :date,
161 :datetime,
162 :decimal,
163 :float,
164 :integer,
165 :string,
166 :text,
167 :time,
168 :timestamp,
169 ].each do |column_type|
170 module_eval <<-CODE, __FILE__, __LINE__ + 1
171 def #{column_type}(*args, **options)
172 args.each { |name| column(name, :#{column_type}, options) }
173 end
174 CODE
175 end
d47357e @kamipo Extract `primary_key` method into `ColumnMethods`
kamipo authored
176 end
177
a17027d @lifo Merge docrails
lifo authored
178 # Represents the schema of an SQL table in an abstract way. This class
179 # provides methods for manipulating the schema representation.
180 #
36abb55 @senny update docs, change_table does not use TableDefinition.
senny authored
181 # Inside migration files, the +t+ object in +create_table+
182 # is actually of this type:
a17027d @lifo Merge docrails
lifo authored
183 #
184 # class SomeMigration < ActiveRecord::Migration
55b203d @amatsuda self.up, self.down => up, down
amatsuda authored
185 # def up
a17027d @lifo Merge docrails
lifo authored
186 # create_table :foo do |t|
187 # puts t.class # => "ActiveRecord::ConnectionAdapters::TableDefinition"
188 # end
189 # end
c3f53f4 @lifo Merge docrails
lifo authored
190 #
55b203d @amatsuda self.up, self.down => up, down
amatsuda authored
191 # def down
a17027d @lifo Merge docrails
lifo authored
192 # ...
193 # end
194 # end
195 #
196 # The table definitions
197 # The Columns are stored as a ColumnDefinition in the +columns+ attribute.
b9fa354 Add extensive documentation to the ActiveRecord::AbstractAdapter. #2250
Marcel Molina authored
198 class TableDefinition
d47357e @kamipo Extract `primary_key` method into `ColumnMethods`
kamipo authored
199 include ColumnMethods
200
a17027d @lifo Merge docrails
lifo authored
201 # An array of ColumnDefinition objects, representing the column changes
202 # that have been defined.
b8a533d @tenderlove @columns list is no longer necessary
tenderlove authored
203 attr_accessor :indexes
99a6f9e @sgrif Add a `foreign_key` option to `references` while creating the table
sgrif authored
204 attr_reader :name, :temporary, :options, :as, :foreign_keys
b3df959 @dhh Refactored the AbstractAdapter to be a lot less scary. Cleaned up the…
dhh authored
205
75a2e4a @ccutrer support creating temporary tables from queries
ccutrer authored
206 def initialize(types, name, temporary, options, as = nil)
76c29a6 @tenderlove Use a hash to look up column definitions
tenderlove authored
207 @columns_hash = {}
ca0af82 @joshuap Automatically create indexes for references/belongs_to statements in …
joshuap authored
208 @indexes = {}
99a6f9e @sgrif Add a `foreign_key` option to `references` while creating the table
sgrif authored
209 @foreign_keys = {}
a80bcc3 @tenderlove mostly decouple TableDefinition from the database connection
tenderlove authored
210 @native = types
14d7dc0 @tenderlove push SQL generation inside the schema creation object
tenderlove authored
211 @temporary = temporary
212 @options = options
75a2e4a @ccutrer support creating temporary tables from queries
ccutrer authored
213 @as = as
14d7dc0 @tenderlove push SQL generation inside the schema creation object
tenderlove authored
214 @name = name
b3df959 @dhh Refactored the AbstractAdapter to be a lot less scary. Cleaned up the…
dhh authored
215 end
216
0a17e8c @senny Merge pull request #21282 from sjain1107/added_docs
senny authored
217 # Returns an array of ColumnDefinition objects for the columns of the table.
b8a533d @tenderlove @columns list is no longer necessary
tenderlove authored
218 def columns; @columns_hash.values; end
219
b9fa354 Add extensive documentation to the ActiveRecord::AbstractAdapter. #2250
Marcel Molina authored
220 # Returns a ColumnDefinition for the column with name +name+.
b3df959 @dhh Refactored the AbstractAdapter to be a lot less scary. Cleaned up the…
dhh authored
221 def [](name)
76c29a6 @tenderlove Use a hash to look up column definitions
tenderlove authored
222 @columns_hash[name.to_s]
b3df959 @dhh Refactored the AbstractAdapter to be a lot less scary. Cleaned up the…
dhh authored
223 end
224
b9fa354 Add extensive documentation to the ActiveRecord::AbstractAdapter. #2250
Marcel Molina authored
225 # Instantiates a new column for the table.
632ab24 Reveal that the type option in migrations can be any supported column…
Marcel Molina authored
226 # The +type+ parameter is normally one of the migrations native types,
227 # which is one of the following:
b9fa354 Add extensive documentation to the ActiveRecord::AbstractAdapter. #2250
Marcel Molina authored
228 # <tt>:primary_key</tt>, <tt>:string</tt>, <tt>:text</tt>,
243a50d @kamipo Missing `:bigint` [ci skip]
kamipo authored
229 # <tt>:integer</tt>, <tt>:bigint</tt>, <tt>:float</tt>, <tt>:decimal</tt>,
58f3cf9 @senny docs, the abstract data type `:timestamp` was removed. See #15184 [ci…
senny authored
230 # <tt>:datetime</tt>, <tt>:time</tt>, <tt>:date</tt>,
231 # <tt>:binary</tt>, <tt>:boolean</tt>.
b9fa354 Add extensive documentation to the ActiveRecord::AbstractAdapter. #2250
Marcel Molina authored
232 #
632ab24 Reveal that the type option in migrations can be any supported column…
Marcel Molina authored
233 # You may use a type not in this list as long as it is supported by your
234 # database (for example, "polygon" in MySQL), but this will not be database
235 # agnostic and should usually be avoided.
236 #
b9fa354 Add extensive documentation to the ActiveRecord::AbstractAdapter. #2250
Marcel Molina authored
237 # Available options are (none of these exists by default):
9450262 Standardize on using hyphens rather than colons to separate option na…
Marcel Molina authored
238 # * <tt>:limit</tt> -
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
239 # Requests a maximum column length. This is number of characters for <tt>:string</tt> and
f049062 @smartinez87 Fixes on schema_definitions docs
smartinez87 authored
240 # <tt>:text</tt> columns and number of bytes for <tt>:binary</tt> and <tt>:integer</tt> columns.
9450262 Standardize on using hyphens rather than colons to separate option na…
Marcel Molina authored
241 # * <tt>:default</tt> -
6019c26 @jeremy change_column accepts :default => nil. Closes #6956.
jeremy authored
242 # The column's default value. Use nil for NULL.
9450262 Standardize on using hyphens rather than colons to separate option na…
Marcel Molina authored
243 # * <tt>:null</tt> -
46f30f9 @lifo Merge documentation changes from docrails.
lifo authored
244 # Allows or disallows +NULL+ values in the column. This option could
b9fa354 Add extensive documentation to the ActiveRecord::AbstractAdapter. #2250
Marcel Molina authored
245 # have been named <tt>:null_allowed</tt>.
9450262 Standardize on using hyphens rather than colons to separate option na…
Marcel Molina authored
246 # * <tt>:precision</tt> -
46f30f9 @lifo Merge documentation changes from docrails.
lifo authored
247 # Specifies the precision for a <tt>:decimal</tt> column.
9450262 Standardize on using hyphens rather than colons to separate option na…
Marcel Molina authored
248 # * <tt>:scale</tt> -
46f30f9 @lifo Merge documentation changes from docrails.
lifo authored
249 # Specifies the scale for a <tt>:decimal</tt> column.
9a0d35e @schuetzm Make `:index` in migrations work with all column types
schuetzm authored
250 # * <tt>:index</tt> -
251 # Create an index for the column. Can be either <tt>true</tt> or an options hash.
2a12b56 @jeremy r4704@asus: jeremy | 2006-06-27 12:00:19 -0700
jeremy authored
252 #
4513f35 @prathamesh-sonpatki Change wording of explanation about precision & scale of decimal numb…
prathamesh-sonpatki authored
253 # Note: The precision is the total number of significant digits
254 # and the scale is the number of digits that can be stored following
a17027d @lifo Merge docrails
lifo authored
255 # the decimal point. For example, the number 123.45 has a precision of 5
256 # and a scale of 2. A decimal with a precision of 5 and a scale of 2 can
257 # range from -999.99 to 999.99.
258 #
2a12b56 @jeremy r4704@asus: jeremy | 2006-06-27 12:00:19 -0700
jeremy authored
259 # Please be aware of different RDBMS implementations behavior with
260 # <tt>:decimal</tt> columns:
261 # * The SQL standard says the default scale should be 0, <tt>:scale</tt> <=
262 # <tt>:precision</tt>, and makes no comments about the requirements of
263 # <tt>:precision</tt>.
46f30f9 @lifo Merge documentation changes from docrails.
lifo authored
264 # * MySQL: <tt>:precision</tt> [1..63], <tt>:scale</tt> [0..30].
2a12b56 @jeremy r4704@asus: jeremy | 2006-06-27 12:00:19 -0700
jeremy authored
265 # Default is (10,0).
46f30f9 @lifo Merge documentation changes from docrails.
lifo authored
266 # * PostgreSQL: <tt>:precision</tt> [1..infinity],
2a12b56 @jeremy r4704@asus: jeremy | 2006-06-27 12:00:19 -0700
jeremy authored
267 # <tt>:scale</tt> [0..infinity]. No default.
46f30f9 @lifo Merge documentation changes from docrails.
lifo authored
268 # * SQLite2: Any <tt>:precision</tt> and <tt>:scale</tt> may be used.
2a12b56 @jeremy r4704@asus: jeremy | 2006-06-27 12:00:19 -0700
jeremy authored
269 # Internal storage as strings. No default.
6019c26 @jeremy change_column accepts :default => nil. Closes #6956.
jeremy authored
270 # * SQLite3: No restrictions on <tt>:precision</tt> and <tt>:scale</tt>,
2a12b56 @jeremy r4704@asus: jeremy | 2006-06-27 12:00:19 -0700
jeremy authored
271 # but the maximum supported <tt>:precision</tt> is 16. No default.
46f30f9 @lifo Merge documentation changes from docrails.
lifo authored
272 # * Oracle: <tt>:precision</tt> [1..38], <tt>:scale</tt> [-84..127].
2a12b56 @jeremy r4704@asus: jeremy | 2006-06-27 12:00:19 -0700
jeremy authored
273 # Default is (38,0).
46f30f9 @lifo Merge documentation changes from docrails.
lifo authored
274 # * DB2: <tt>:precision</tt> [1..63], <tt>:scale</tt> [0..62].
2a12b56 @jeremy r4704@asus: jeremy | 2006-06-27 12:00:19 -0700
jeremy authored
275 # Default unknown.
46f30f9 @lifo Merge documentation changes from docrails.
lifo authored
276 # * SqlServer?: <tt>:precision</tt> [1..38], <tt>:scale</tt> [0..38].
2a12b56 @jeremy r4704@asus: jeremy | 2006-06-27 12:00:19 -0700
jeremy authored
277 # Default (38,0).
b9fa354 Add extensive documentation to the ActiveRecord::AbstractAdapter. #2250
Marcel Molina authored
278 #
279 # This method returns <tt>self</tt>.
280 #
4cbbebb @dhh Added short-hand declaration style to migrations (inspiration from Se…
dhh authored
281 # == Examples
f049062 @smartinez87 Fixes on schema_definitions docs
smartinez87 authored
282 # # Assuming +td+ is an instance of TableDefinition
5392407 @technoweenie Don't use keywords as local vars in documentation. Closes #5291. [je…
technoweenie authored
283 # td.column(:granted, :boolean)
98dc582 @lifo Merge docrails.
lifo authored
284 # # granted BOOLEAN
b9fa354 Add extensive documentation to the ActiveRecord::AbstractAdapter. #2250
Marcel Molina authored
285 #
9edd4e1 @AvnerCohen Migration of docs to 1.9 hash syntax
AvnerCohen authored
286 # td.column(:picture, :binary, limit: 2.megabytes)
98dc582 @lifo Merge docrails.
lifo authored
287 # # => picture BLOB(2097152)
b9fa354 Add extensive documentation to the ActiveRecord::AbstractAdapter. #2250
Marcel Molina authored
288 #
9edd4e1 @AvnerCohen Migration of docs to 1.9 hash syntax
AvnerCohen authored
289 # td.column(:sales_stage, :string, limit: 20, default: 'new', null: false)
98dc582 @lifo Merge docrails.
lifo authored
290 # # => sales_stage VARCHAR(20) DEFAULT 'new' NOT NULL
2a12b56 @jeremy r4704@asus: jeremy | 2006-06-27 12:00:19 -0700
jeremy authored
291 #
9edd4e1 @AvnerCohen Migration of docs to 1.9 hash syntax
AvnerCohen authored
292 # td.column(:bill_gates_money, :decimal, precision: 15, scale: 2)
98dc582 @lifo Merge docrails.
lifo authored
293 # # => bill_gates_money DECIMAL(15,2)
2a12b56 @jeremy r4704@asus: jeremy | 2006-06-27 12:00:19 -0700
jeremy authored
294 #
9edd4e1 @AvnerCohen Migration of docs to 1.9 hash syntax
AvnerCohen authored
295 # td.column(:sensor_reading, :decimal, precision: 30, scale: 20)
98dc582 @lifo Merge docrails.
lifo authored
296 # # => sensor_reading DECIMAL(30,20)
2a12b56 @jeremy r4704@asus: jeremy | 2006-06-27 12:00:19 -0700
jeremy authored
297 #
298 # # While <tt>:scale</tt> defaults to zero on most databases, it
299 # # probably wouldn't hurt to include it.
9edd4e1 @AvnerCohen Migration of docs to 1.9 hash syntax
AvnerCohen authored
300 # td.column(:huge_integer, :decimal, precision: 30)
98dc582 @lifo Merge docrails.
lifo authored
301 # # => huge_integer DECIMAL(30)
4cbbebb @dhh Added short-hand declaration style to migrations (inspiration from Se…
dhh authored
302 #
a17027d @lifo Merge docrails
lifo authored
303 # # Defines a column with a database-specific type.
304 # td.column(:foo, 'polygon')
305 # # => foo polygon
306 #
4cbbebb @dhh Added short-hand declaration style to migrations (inspiration from Se…
dhh authored
307 # == Short-hand examples
308 #
98dc582 @lifo Merge docrails.
lifo authored
309 # Instead of calling +column+ directly, you can also work with the short-hand definitions for the default types.
4cbbebb @dhh Added short-hand declaration style to migrations (inspiration from Se…
dhh authored
310 # They use the type as the method name instead of as a parameter and allow for multiple columns to be defined
311 # in a single statement.
312 #
313 # What can be written like this with the regular calls to column:
314 #
27ca915 @fxn revises a RDoc example to make it idiomatic
fxn authored
315 # create_table :products do |t|
9a0d35e @schuetzm Make `:index` in migrations work with all column types
schuetzm authored
316 # t.column :shop_id, :integer
317 # t.column :creator_id, :integer
318 # t.column :item_number, :string
319 # t.column :name, :string, default: "Untitled"
320 # t.column :value, :string, default: "Untitled"
321 # t.column :created_at, :datetime
322 # t.column :updated_at, :datetime
4cbbebb @dhh Added short-hand declaration style to migrations (inspiration from Se…
dhh authored
323 # end
9a0d35e @schuetzm Make `:index` in migrations work with all column types
schuetzm authored
324 # add_index :products, :item_number
4cbbebb @dhh Added short-hand declaration style to migrations (inspiration from Se…
dhh authored
325 #
27ca915 @fxn revises a RDoc example to make it idiomatic
fxn authored
326 # can also be written as follows using the short-hand:
4cbbebb @dhh Added short-hand declaration style to migrations (inspiration from Se…
dhh authored
327 #
328 # create_table :products do |t|
329 # t.integer :shop_id, :creator_id
9a0d35e @schuetzm Make `:index` in migrations work with all column types
schuetzm authored
330 # t.string :item_number, index: true
9edd4e1 @AvnerCohen Migration of docs to 1.9 hash syntax
AvnerCohen authored
331 # t.string :name, :value, default: "Untitled"
d56be86 @senny synchronize code and docs for `timestamps` and `add_timestamps`.
senny authored
332 # t.timestamps null: false
4cbbebb @dhh Added short-hand declaration style to migrations (inspiration from Se…
dhh authored
333 # end
334 #
46f30f9 @lifo Merge documentation changes from docrails.
lifo authored
335 # There's a short-hand method for each of the type values declared at the top. And then there's
d53c2e4 @smartinez87 fixed-fonts on SchemaDefinitions
smartinez87 authored
336 # TableDefinition#timestamps that'll add +created_at+ and +updated_at+ as datetimes.
6ddde02 @NZKoz Add t.belongs_to and t.references to sexy migrations [arthurgeek]
NZKoz authored
337 #
338 # TableDefinition#references will add an appropriately-named _id column, plus a corresponding _type
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
339 # column if the <tt>:polymorphic</tt> option is supplied. If <tt>:polymorphic</tt> is a hash of
ca0af82 @joshuap Automatically create indexes for references/belongs_to statements in …
joshuap authored
340 # options, these will be used when creating the <tt>_type</tt> column. The <tt>:index</tt> option
341 # will also create an index, similar to calling <tt>add_index</tt>. So what can be written like this:
6ddde02 @NZKoz Add t.belongs_to and t.references to sexy migrations [arthurgeek]
NZKoz authored
342 #
343 # create_table :taggings do |t|
344 # t.integer :tag_id, :tagger_id, :taggable_id
345 # t.string :tagger_type
9edd4e1 @AvnerCohen Migration of docs to 1.9 hash syntax
AvnerCohen authored
346 # t.string :taggable_type, default: 'Photo'
6ddde02 @NZKoz Add t.belongs_to and t.references to sexy migrations [arthurgeek]
NZKoz authored
347 # end
9edd4e1 @AvnerCohen Migration of docs to 1.9 hash syntax
AvnerCohen authored
348 # add_index :taggings, :tag_id, name: 'index_taggings_on_tag_id'
ca0af82 @joshuap Automatically create indexes for references/belongs_to statements in …
joshuap authored
349 # add_index :taggings, [:tagger_id, :tagger_type]
6ddde02 @NZKoz Add t.belongs_to and t.references to sexy migrations [arthurgeek]
NZKoz authored
350 #
351 # Can also be written as follows using references:
352 #
353 # create_table :taggings do |t|
9edd4e1 @AvnerCohen Migration of docs to 1.9 hash syntax
AvnerCohen authored
354 # t.references :tag, index: { name: 'index_taggings_on_tag_id' }
355 # t.references :tagger, polymorphic: true, index: true
356 # t.references :taggable, polymorphic: { default: 'Photo' }
6ddde02 @NZKoz Add t.belongs_to and t.references to sexy migrations [arthurgeek]
NZKoz authored
357 # end
b3df959 @dhh Refactored the AbstractAdapter to be a lot less scary. Cleaned up the…
dhh authored
358 def column(name, type, options = {})
483a3cf @tenderlove automatically add the column definition to the columns list if creati…
tenderlove authored
359 name = name.to_s
360 type = type.to_sym
c0abead @mudge Fix missing index when using timestamps with index
mudge authored
361 options = options.dup
483a3cf @tenderlove automatically add the column definition to the columns list if creati…
tenderlove authored
362
66cd96d @tenderlove just look up the primary key from the columns hash
tenderlove authored
363 if @columns_hash[name] && @columns_hash[name].primary_key?
e4790a2 @senny raise `ArgumentError` when redefining the primary key column. Closes …
senny authored
364 raise ArgumentError, "you can't redefine the primary key column '#{name}'. To define a custom primary key, pass { id: false } to create_table."
365 end
366
9a0d35e @schuetzm Make `:index` in migrations work with all column types
schuetzm authored
367 index_options = options.delete(:index)
368 index(name, index_options.is_a?(Hash) ? index_options : {}) if index_options
f84cf41 @tenderlove there is no reason to check for an already defined column
tenderlove authored
369 @columns_hash[name] = new_column_definition(name, type, options)
b3df959 @dhh Refactored the AbstractAdapter to be a lot less scary. Cleaned up the…
dhh authored
370 self
371 end
68160b3 @dhh Added migration support for Oracle (closes #2647) [Michael Schoen]
dhh authored
372
c351a82 @sjain1107 Added docs for TableDefinition #coloumns & #remove_column [ci skip]
sjain1107 authored
373 # remove the column +name+ from the table.
374 # remove_column(:account_id)
c5e03e8 @tenderlove keep ivars private, do not manipulate them outside their owner object
tenderlove authored
375 def remove_column(name)
376 @columns_hash.delete name.to_s
377 end
378
ca0af82 @joshuap Automatically create indexes for references/belongs_to statements in …
joshuap authored
379 # Adds index options to the indexes hash, keyed by column name
380 # This is primarily used to track indexes that need to be created after the table
5277d55 @vijaydev remove unnecessary 'examples' noise [ci skip]
vijaydev authored
381 #
9edd4e1 @AvnerCohen Migration of docs to 1.9 hash syntax
AvnerCohen authored
382 # index(:account_id, name: 'index_projects_on_account_id')
ca0af82 @joshuap Automatically create indexes for references/belongs_to statements in …
joshuap authored
383 def index(column_name, options = {})
384 indexes[column_name] = options
385 end
46f30f9 @lifo Merge documentation changes from docrails.
lifo authored
386
99a6f9e @sgrif Add a `foreign_key` option to `references` while creating the table
sgrif authored
387 def foreign_key(table_name, options = {}) # :nodoc:
388 foreign_keys[table_name] = options
389 end
390
46f30f9 @lifo Merge documentation changes from docrails.
lifo authored
391 # Appends <tt>:datetime</tt> columns <tt>:created_at</tt> and
d56be86 @senny synchronize code and docs for `timestamps` and `add_timestamps`.
senny authored
392 # <tt>:updated_at</tt> to the table. See SchemaStatements#add_timestamps
393 #
394 # t.timestamps null: false
683ff23 @reagent Ensure t.timestamps respects options. [#828 state:resolved]
reagent authored
395 def timestamps(*args)
ccf7cd0 revert Default timestamps to non-null
Dave Kroondyk authored
396 options = args.extract_options!
a939506 @rafaelfranca Change the default `null` value for `timestamps` to `false`
rafaelfranca authored
397
398 options[:null] = false if options[:null].nil?
399
683ff23 @reagent Ensure t.timestamps respects options. [#828 state:resolved]
reagent authored
400 column(:created_at, :datetime, options)
401 column(:updated_at, :datetime, options)
4cbbebb @dhh Added short-hand declaration style to migrations (inspiration from Se…
dhh authored
402 end
403
41e5e89 @senny better `add_reference` documentation. [ci skip]
senny authored
404 # Adds a reference.
aa3740e @senny Merge pull request #16231 from Envek/type_in_references
senny authored
405 #
406 # t.references(:user)
41e5e89 @senny better `add_reference` documentation. [ci skip]
senny authored
407 # t.belongs_to(:supplier, foreign_key: true)
6d327db @Envek Allow to specify a type for foreign key column in migrations
Envek authored
408 #
41e5e89 @senny better `add_reference` documentation. [ci skip]
senny authored
409 # See SchemaStatements#add_reference for details of the options you can use.
d26704a @sgrif Refactor a common class to reduce the duplication for `references`
sgrif authored
410 def references(*args, **options)
6ddde02 @NZKoz Add t.belongs_to and t.references to sexy migrations [arthurgeek]
NZKoz authored
411 args.each do |col|
d26704a @sgrif Refactor a common class to reduce the duplication for `references`
sgrif authored
412 ReferenceDefinition.new(col, **options).add_to(self)
6ddde02 @NZKoz Add t.belongs_to and t.references to sexy migrations [arthurgeek]
NZKoz authored
413 end
414 end
415 alias :belongs_to :references
416
f20b2f4 @tenderlove push alter table add column sql in to the schema modification visitor
tenderlove authored
417 def new_column_definition(name, type, options) # :nodoc:
99798b9 @tenderlove we don't need a HWIA and a hash allocated for just one k/v pair
tenderlove authored
418 type = aliased_types(type.to_s, type)
d43edf6 @tenderlove push column initialization down to the factory method
tenderlove authored
419 column = create_column_definition name, type
420 limit = options.fetch(:limit) do
421 native[type][:limit] if native[type].is_a?(Hash)
422 end
423
d25e407 @tenderlove separate primary key from column type
tenderlove authored
424 column.limit = limit
425 column.precision = options[:precision]
426 column.scale = options[:scale]
427 column.default = options[:default]
428 column.null = options[:null]
429 column.first = options[:first]
430 column.after = options[:after]
a327a9d @kamipo Add bigint primary key support for MySQL.
kamipo authored
431 column.auto_increment = options[:auto_increment]
d25e407 @tenderlove separate primary key from column type
tenderlove authored
432 column.primary_key = type == :primary_key || options[:primary_key]
446f252 @kamipo Move the collation handling code from the MySQL adapter to common cla…
kamipo authored
433 column.collation = options[:collation]
d43edf6 @tenderlove push column initialization down to the factory method
tenderlove authored
434 column
483a3cf @tenderlove automatically add the column definition to the columns list if creati…
tenderlove authored
435 end
436
f20b2f4 @tenderlove push alter table add column sql in to the schema modification visitor
tenderlove authored
437 private
438 def create_column_definition(name, type)
439 ColumnDefinition.new name, type
440 end
441
483a3cf @tenderlove automatically add the column definition to the columns list if creati…
tenderlove authored
442 def native
a80bcc3 @tenderlove mostly decouple TableDefinition from the database connection
tenderlove authored
443 @native
483a3cf @tenderlove automatically add the column definition to the columns list if creati…
tenderlove authored
444 end
d0f8c46 @sgrif Remove :timestamp column type
sgrif authored
445
99798b9 @tenderlove we don't need a HWIA and a hash allocated for just one k/v pair
tenderlove authored
446 def aliased_types(name, fallback)
447 'timestamp' == name ? :datetime : fallback
d0f8c46 @sgrif Remove :timestamp column type
sgrif authored
448 end
b3df959 @dhh Refactored the AbstractAdapter to be a lot less scary. Cleaned up the…
dhh authored
449 end
96980bd @dhh Added change_table for migrations (Jeff Dean) [#71 state:resolved]
dhh authored
450
f20b2f4 @tenderlove push alter table add column sql in to the schema modification visitor
tenderlove authored
451 class AlterTable # :nodoc:
739a720 @tenderlove allow multiple add columns
tenderlove authored
452 attr_reader :adds
1c170fd @senny fk: generalize using `AlterTable` and `SchemaCreation`.
senny authored
453 attr_reader :foreign_key_adds
454 attr_reader :foreign_key_drops
f20b2f4 @tenderlove push alter table add column sql in to the schema modification visitor
tenderlove authored
455
456 def initialize(td)
739a720 @tenderlove allow multiple add columns
tenderlove authored
457 @td = td
458 @adds = []
1c170fd @senny fk: generalize using `AlterTable` and `SchemaCreation`.
senny authored
459 @foreign_key_adds = []
460 @foreign_key_drops = []
f20b2f4 @tenderlove push alter table add column sql in to the schema modification visitor
tenderlove authored
461 end
462
463 def name; @td.name; end
464
1c170fd @senny fk: generalize using `AlterTable` and `SchemaCreation`.
senny authored
465 def add_foreign_key(to_table, options)
466 @foreign_key_adds << ForeignKeyDefinition.new(name, to_table, options)
467 end
468
469 def drop_foreign_key(name)
470 @foreign_key_drops << name
471 end
472
f20b2f4 @tenderlove push alter table add column sql in to the schema modification visitor
tenderlove authored
473 def add_column(name, type, options)
474 name = name.to_s
475 type = type.to_sym
cd00526 @kamipo Change the `visit_AddColumn` visiblity for the internal API
kamipo authored
476 @adds << AddColumnDefinition.new(@td.new_column_definition(name, type, options))
f20b2f4 @tenderlove push alter table add column sql in to the schema modification visitor
tenderlove authored
477 end
478 end
479
d191db7 @fxn standarizes the use of the article "an" for "SQL" and "SQLite"
fxn authored
480 # Represents an SQL table in an abstract way for updating a table.
96980bd @dhh Added change_table for migrations (Jeff Dean) [#71 state:resolved]
dhh authored
481 # Also see TableDefinition and SchemaStatements#create_table
482 #
483 # Available transformations are:
484 #
485 # change_table :table do |t|
d47357e @kamipo Extract `primary_key` method into `ColumnMethods`
kamipo authored
486 # t.primary_key
96980bd @dhh Added change_table for migrations (Jeff Dean) [#71 state:resolved]
dhh authored
487 # t.column
488 # t.index
1a782b2 @CvX Add rename_index to change_table.
CvX authored
489 # t.rename_index
96980bd @dhh Added change_table for migrations (Jeff Dean) [#71 state:resolved]
dhh authored
490 # t.timestamps
491 # t.change
492 # t.change_default
493 # t.rename
494 # t.references
495 # t.belongs_to
496 # t.string
497 # t.text
498 # t.integer
8fade12 @kamipo Extract the short-hand methods into `ColumnMethods`
kamipo authored
499 # t.bigint
96980bd @dhh Added change_table for migrations (Jeff Dean) [#71 state:resolved]
dhh authored
500 # t.float
501 # t.decimal
502 # t.datetime
503 # t.timestamp
504 # t.time
505 # t.date
506 # t.binary
507 # t.boolean
508 # t.remove
509 # t.remove_references
510 # t.remove_belongs_to
511 # t.remove_index
512 # t.remove_timestamps
513 # end
514 #
515 class Table
d47357e @kamipo Extract `primary_key` method into `ColumnMethods`
kamipo authored
516 include ColumnMethods
517
76e7305 @ccutrer add a Table#name accessor like TableDefinition#name
ccutrer authored
518 attr_reader :name
519
96980bd @dhh Added change_table for migrations (Jeff Dean) [#71 state:resolved]
dhh authored
520 def initialize(table_name, base)
76e7305 @ccutrer add a Table#name accessor like TableDefinition#name
ccutrer authored
521 @name = table_name
96980bd @dhh Added change_table for migrations (Jeff Dean) [#71 state:resolved]
dhh authored
522 @base = base
523 end
524
525 # Adds a new column to the named table.
5277d55 @vijaydev remove unnecessary 'examples' noise [ci skip]
vijaydev authored
526 #
96980bd @dhh Added change_table for migrations (Jeff Dean) [#71 state:resolved]
dhh authored
527 # t.column(:name, :string)
e2d8553 @senny reduce duplication in `ConnectionAdapters::Table` docs. [ci skip]
senny authored
528 #
529 # See TableDefinition#column for details of the options you can use.
96980bd @dhh Added change_table for migrations (Jeff Dean) [#71 state:resolved]
dhh authored
530 def column(column_name, type, options = {})
76e7305 @ccutrer add a Table#name accessor like TableDefinition#name
ccutrer authored
531 @base.add_column(name, column_name, type, options)
96980bd @dhh Added change_table for migrations (Jeff Dean) [#71 state:resolved]
dhh authored
532 end
533
e2d8553 @senny reduce duplication in `ConnectionAdapters::Table` docs. [ci skip]
senny authored
534 # Checks to see if a column exists.
535 #
d62b8f8 @JuanitoFatas Add more documents for AR connection_adapters abstract schema_definit…
JuanitoFatas authored
536 # t.string(:name) unless t.column_exists?(:name, :string)
537 #
e2d8553 @senny reduce duplication in `ConnectionAdapters::Table` docs. [ci skip]
senny authored
538 # See SchemaStatements#column_exists?
3cc83a8 @grantneufeld Changed the default value for the options argument on ActiveRecord::C…
grantneufeld authored
539 def column_exists?(column_name, type = nil, options = {})
76e7305 @ccutrer add a Table#name accessor like TableDefinition#name
ccutrer authored
540 @base.column_exists?(name, column_name, type, options)
11ff3da @pixeltrix Add column and index query methods to ActiveRecord::Schema
pixeltrix authored
541 end
542
46f30f9 @lifo Merge documentation changes from docrails.
lifo authored
543 # Adds a new index to the table. +column_name+ can be a single Symbol, or
e2d8553 @senny reduce duplication in `ConnectionAdapters::Table` docs. [ci skip]
senny authored
544 # an Array of Symbols.
96980bd @dhh Added change_table for migrations (Jeff Dean) [#71 state:resolved]
dhh authored
545 #
546 # t.index(:name)
9edd4e1 @AvnerCohen Migration of docs to 1.9 hash syntax
AvnerCohen authored
547 # t.index([:branch_id, :party_id], unique: true)
548 # t.index([:branch_id, :party_id], unique: true, name: 'by_branch_party')
e2d8553 @senny reduce duplication in `ConnectionAdapters::Table` docs. [ci skip]
senny authored
549 #
550 # See SchemaStatements#add_index for details of the options you can use.
96980bd @dhh Added change_table for migrations (Jeff Dean) [#71 state:resolved]
dhh authored
551 def index(column_name, options = {})
76e7305 @ccutrer add a Table#name accessor like TableDefinition#name
ccutrer authored
552 @base.add_index(name, column_name, options)
96980bd @dhh Added change_table for migrations (Jeff Dean) [#71 state:resolved]
dhh authored
553 end
554
e2d8553 @senny reduce duplication in `ConnectionAdapters::Table` docs. [ci skip]
senny authored
555 # Checks to see if an index exists.
556 #
d62b8f8 @JuanitoFatas Add more documents for AR connection_adapters abstract schema_definit…
JuanitoFatas authored
557 # unless t.index_exists?(:branch_id)
558 # t.index(:branch_id)
559 # end
560 #
e2d8553 @senny reduce duplication in `ConnectionAdapters::Table` docs. [ci skip]
senny authored
561 # See SchemaStatements#index_exists?
11ff3da @pixeltrix Add column and index query methods to ActiveRecord::Schema
pixeltrix authored
562 def index_exists?(column_name, options = {})
76e7305 @ccutrer add a Table#name accessor like TableDefinition#name
ccutrer authored
563 @base.index_exists?(name, column_name, options)
11ff3da @pixeltrix Add column and index query methods to ActiveRecord::Schema
pixeltrix authored
564 end
565
1a782b2 @CvX Add rename_index to change_table.
CvX authored
566 # Renames the given index on the table.
567 #
568 # t.rename_index(:user_id, :account_id)
e2d8553 @senny reduce duplication in `ConnectionAdapters::Table` docs. [ci skip]
senny authored
569 #
570 # See SchemaStatements#rename_index
1a782b2 @CvX Add rename_index to change_table.
CvX authored
571 def rename_index(index_name, new_index_name)
76e7305 @ccutrer add a Table#name accessor like TableDefinition#name
ccutrer authored
572 @base.rename_index(name, index_name, new_index_name)
1a782b2 @CvX Add rename_index to change_table.
CvX authored
573 end
574
e2d8553 @senny reduce duplication in `ConnectionAdapters::Table` docs. [ci skip]
senny authored
575 # Adds timestamps (+created_at+ and +updated_at+) columns to the table.
576 #
577 # t.timestamps(null: false)
5277d55 @vijaydev remove unnecessary 'examples' noise [ci skip]
vijaydev authored
578 #
e2d8553 @senny reduce duplication in `ConnectionAdapters::Table` docs. [ci skip]
senny authored
579 # See SchemaStatements#add_timestamps
ea3ba34 @sgrif Change the default `null` value for timestamps
sgrif authored
580 def timestamps(options = {})
76e7305 @ccutrer add a Table#name accessor like TableDefinition#name
ccutrer authored
581 @base.add_timestamps(name, options)
96980bd @dhh Added change_table for migrations (Jeff Dean) [#71 state:resolved]
dhh authored
582 end
583
584 # Changes the column's definition according to the new options.
5277d55 @vijaydev remove unnecessary 'examples' noise [ci skip]
vijaydev authored
585 #
9edd4e1 @AvnerCohen Migration of docs to 1.9 hash syntax
AvnerCohen authored
586 # t.change(:name, :string, limit: 80)
96980bd @dhh Added change_table for migrations (Jeff Dean) [#71 state:resolved]
dhh authored
587 # t.change(:description, :text)
e2d8553 @senny reduce duplication in `ConnectionAdapters::Table` docs. [ci skip]
senny authored
588 #
589 # See TableDefinition#column for details of the options you can use.
96980bd @dhh Added change_table for migrations (Jeff Dean) [#71 state:resolved]
dhh authored
590 def change(column_name, type, options = {})
76e7305 @ccutrer add a Table#name accessor like TableDefinition#name
ccutrer authored
591 @base.change_column(name, column_name, type, options)
96980bd @dhh Added change_table for migrations (Jeff Dean) [#71 state:resolved]
dhh authored
592 end
593
e2d8553 @senny reduce duplication in `ConnectionAdapters::Table` docs. [ci skip]
senny authored
594 # Sets a new default value for a column.
5277d55 @vijaydev remove unnecessary 'examples' noise [ci skip]
vijaydev authored
595 #
96980bd @dhh Added change_table for migrations (Jeff Dean) [#71 state:resolved]
dhh authored
596 # t.change_default(:qualification, 'new')
597 # t.change_default(:authorized, 1)
a412872 @sikachu Add reversible syntax for change_column_default
sikachu authored
598 # t.change_default(:status, from: nil, to: "draft")
e2d8553 @senny reduce duplication in `ConnectionAdapters::Table` docs. [ci skip]
senny authored
599 #
600 # See SchemaStatements#change_column_default
a412872 @sikachu Add reversible syntax for change_column_default
sikachu authored
601 def change_default(column_name, default_or_changes)
602 @base.change_column_default(name, column_name, default_or_changes)
96980bd @dhh Added change_table for migrations (Jeff Dean) [#71 state:resolved]
dhh authored
603 end
604
605 # Removes the column(s) from the table definition.
5277d55 @vijaydev remove unnecessary 'examples' noise [ci skip]
vijaydev authored
606 #
96980bd @dhh Added change_table for migrations (Jeff Dean) [#71 state:resolved]
dhh authored
607 # t.remove(:qualification)
608 # t.remove(:qualification, :experience)
e2d8553 @senny reduce duplication in `ConnectionAdapters::Table` docs. [ci skip]
senny authored
609 #
610 # See SchemaStatements#remove_columns
96980bd @dhh Added change_table for migrations (Jeff Dean) [#71 state:resolved]
dhh authored
611 def remove(*column_names)
76e7305 @ccutrer add a Table#name accessor like TableDefinition#name
ccutrer authored
612 @base.remove_columns(name, *column_names)
96980bd @dhh Added change_table for migrations (Jeff Dean) [#71 state:resolved]
dhh authored
613 end
614
46f30f9 @lifo Merge documentation changes from docrails.
lifo authored
615 # Removes the given index from the table.
96980bd @dhh Added change_table for migrations (Jeff Dean) [#71 state:resolved]
dhh authored
616 #
e2d8553 @senny reduce duplication in `ConnectionAdapters::Table` docs. [ci skip]
senny authored
617 # t.remove_index(:branch_id)
618 # t.remove_index(column: [:branch_id, :party_id])
619 # t.remove_index(name: :by_branch_party)
620 #
621 # See SchemaStatements#remove_index
96980bd @dhh Added change_table for migrations (Jeff Dean) [#71 state:resolved]
dhh authored
622 def remove_index(options = {})
76e7305 @ccutrer add a Table#name accessor like TableDefinition#name
ccutrer authored
623 @base.remove_index(name, options)
96980bd @dhh Added change_table for migrations (Jeff Dean) [#71 state:resolved]
dhh authored
624 end
625
d53c2e4 @smartinez87 fixed-fonts on SchemaDefinitions
smartinez87 authored
626 # Removes the timestamp columns (+created_at+ and +updated_at+) from the table.
5277d55 @vijaydev remove unnecessary 'examples' noise [ci skip]
vijaydev authored
627 #
96980bd @dhh Added change_table for migrations (Jeff Dean) [#71 state:resolved]
dhh authored
628 # t.remove_timestamps
e2d8553 @senny reduce duplication in `ConnectionAdapters::Table` docs. [ci skip]
senny authored
629 #
630 # See SchemaStatements#remove_timestamps
b64fb30 @CLUSTERfoo Failure to rollback t.timestamps when within a change_table migration
CLUSTERfoo authored
631 def remove_timestamps(options = {})
632 @base.remove_timestamps(name, options)
96980bd @dhh Added change_table for migrations (Jeff Dean) [#71 state:resolved]
dhh authored
633 end
634
635 # Renames a column.
5277d55 @vijaydev remove unnecessary 'examples' noise [ci skip]
vijaydev authored
636 #
96980bd @dhh Added change_table for migrations (Jeff Dean) [#71 state:resolved]
dhh authored
637 # t.rename(:description, :name)
e2d8553 @senny reduce duplication in `ConnectionAdapters::Table` docs. [ci skip]
senny authored
638 #
639 # See SchemaStatements#rename_column
96980bd @dhh Added change_table for migrations (Jeff Dean) [#71 state:resolved]
dhh authored
640 def rename(column_name, new_column_name)
76e7305 @ccutrer add a Table#name accessor like TableDefinition#name
ccutrer authored
641 @base.rename_column(name, column_name, new_column_name)
96980bd @dhh Added change_table for migrations (Jeff Dean) [#71 state:resolved]
dhh authored
642 end
643
41e5e89 @senny better `add_reference` documentation. [ci skip]
senny authored
644 # Adds a reference.
5277d55 @vijaydev remove unnecessary 'examples' noise [ci skip]
vijaydev authored
645 #
68f0ef9 @lexmag Refactor references schema definitions
lexmag authored
646 # t.references(:user)
e2d8553 @senny reduce duplication in `ConnectionAdapters::Table` docs. [ci skip]
senny authored
647 # t.belongs_to(:supplier, foreign_key: true)
68f0ef9 @lexmag Refactor references schema definitions
lexmag authored
648 #
41e5e89 @senny better `add_reference` documentation. [ci skip]
senny authored
649 # See SchemaStatements#add_reference for details of the options you can use.
96980bd @dhh Added change_table for migrations (Jeff Dean) [#71 state:resolved]
dhh authored
650 def references(*args)
651 options = args.extract_options!
68f0ef9 @lexmag Refactor references schema definitions
lexmag authored
652 args.each do |ref_name|
76e7305 @ccutrer add a Table#name accessor like TableDefinition#name
ccutrer authored
653 @base.add_reference(name, ref_name, options)
96980bd @dhh Added change_table for migrations (Jeff Dean) [#71 state:resolved]
dhh authored
654 end
655 end
656 alias :belongs_to :references
657
46f30f9 @lifo Merge documentation changes from docrails.
lifo authored
658 # Removes a reference. Optionally removes a +type+ column.
5277d55 @vijaydev remove unnecessary 'examples' noise [ci skip]
vijaydev authored
659 #
68f0ef9 @lexmag Refactor references schema definitions
lexmag authored
660 # t.remove_references(:user)
661 # t.remove_belongs_to(:supplier, polymorphic: true)
662 #
aa3740e @senny Merge pull request #16231 from Envek/type_in_references
senny authored
663 # See SchemaStatements#remove_reference
96980bd @dhh Added change_table for migrations (Jeff Dean) [#71 state:resolved]
dhh authored
664 def remove_references(*args)
665 options = args.extract_options!
68f0ef9 @lexmag Refactor references schema definitions
lexmag authored
666 args.each do |ref_name|
76e7305 @ccutrer add a Table#name accessor like TableDefinition#name
ccutrer authored
667 @base.remove_reference(name, ref_name, options)
96980bd @dhh Added change_table for migrations (Jeff Dean) [#71 state:resolved]
dhh authored
668 end
669 end
68f0ef9 @lexmag Refactor references schema definitions
lexmag authored
670 alias :remove_belongs_to :remove_references
96980bd @dhh Added change_table for migrations (Jeff Dean) [#71 state:resolved]
dhh authored
671
d62b8f8 @JuanitoFatas Add more documents for AR connection_adapters abstract schema_definit…
JuanitoFatas authored
672 # Adds a foreign key.
673 #
674 # t.foreign_key(:authors)
675 #
676 # See SchemaStatements#add_foreign_key
d26704a @sgrif Refactor a common class to reduce the duplication for `references`
sgrif authored
677 def foreign_key(*args) # :nodoc:
678 @base.add_foreign_key(name, *args)
679 end
680
d62b8f8 @JuanitoFatas Add more documents for AR connection_adapters abstract schema_definit…
JuanitoFatas authored
681 # Checks to see if a foreign key exists.
682 #
683 # t.foreign_key(:authors) unless t.foreign_key_exists?(:authors)
684 #
685 # See SchemaStatements#foreign_key_exists?
6298ac7 @estum Add `foreign_key_exists?` method.
estum authored
686 def foreign_key_exists?(*args) # :nodoc:
687 @base.foreign_key_exists?(name, *args)
688 end
d26704a @sgrif Refactor a common class to reduce the duplication for `references`
sgrif authored
689
96980bd @dhh Added change_table for migrations (Jeff Dean) [#71 state:resolved]
dhh authored
690 private
691 def native
692 @base.native_database_types
693 end
694 end
b3df959 @dhh Refactored the AbstractAdapter to be a lot less scary. Cleaned up the…
dhh authored
695 end
0d07152 Make schema definition column lookups agnostic w/r/t/ whether a key o…
Marcel Molina authored
696 end
Something went wrong with that request. Please try again.