activerecord 3.2.13 → 3.2.14.rc1
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of activerecord might be problematic. Click here for more details.
- data/CHANGELOG.md +148 -2
- data/lib/active_record/associations/association.rb +9 -3
- data/lib/active_record/associations/belongs_to_association.rb +1 -1
- data/lib/active_record/associations/belongs_to_polymorphic_association.rb +2 -1
- data/lib/active_record/associations/builder/belongs_to.rb +4 -1
- data/lib/active_record/associations/builder/belongs_to.rb.orig +95 -0
- data/lib/active_record/associations/collection_association.rb +1 -1
- data/lib/active_record/associations/has_many_association.rb +1 -2
- data/lib/active_record/associations/has_many_association.rb.orig +116 -0
- data/lib/active_record/associations/join_dependency.rb +1 -1
- data/lib/active_record/associations/join_dependency/join_association.rb +6 -1
- data/lib/active_record/associations/preloader/through_association.rb +1 -2
- data/lib/active_record/associations/through_association.rb +1 -1
- data/lib/active_record/autosave_association.rb +7 -12
- data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +2 -2
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb.orig +619 -0
- data/lib/active_record/connection_adapters/connection_specification.rb.orig +124 -0
- data/lib/active_record/connection_adapters/mysql2_adapter.rb +5 -3
- data/lib/active_record/connection_adapters/postgresql/cast.rb.orig +136 -0
- data/lib/active_record/connection_adapters/postgresql/schema_statements.rb.orig +485 -0
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +9 -3
- data/lib/active_record/core.rb.orig +452 -0
- data/lib/active_record/explain_subscriber.rb +1 -1
- data/lib/active_record/model_schema.rb +4 -2
- data/lib/active_record/nested_attributes.rb +41 -17
- data/lib/active_record/railtie.rb +6 -7
- data/lib/active_record/railties/databases.rake +2 -1
- data/lib/active_record/relation/calculations.rb +5 -6
- data/lib/active_record/relation/calculations.rb.orig +378 -0
- data/lib/active_record/relation/finder_methods.rb +1 -0
- data/lib/active_record/relation/finder_methods.rb.orig +405 -0
- data/lib/active_record/relation/spawn_methods.rb +34 -3
- data/lib/active_record/store.rb +1 -1
- data/lib/active_record/version.rb +2 -2
- data/lib/rails/generators/active_record/observer/observer_generator.rb.orig +15 -0
- metadata +117 -70
- checksums.yaml +0 -7
@@ -109,7 +109,7 @@ module ActiveRecord
|
|
109
109
|
case associations
|
110
110
|
when Symbol, String
|
111
111
|
reflection = parent.reflections[associations.to_s.intern] or
|
112
|
-
raise ConfigurationError, "Association named '#{ associations }' was not found; perhaps you misspelled it?"
|
112
|
+
raise ConfigurationError, "Association named '#{ associations }' was not found on #{parent.active_record.name}; perhaps you misspelled it?"
|
113
113
|
unless join_association = find_join_association(reflection, parent)
|
114
114
|
@reflections << reflection
|
115
115
|
join_association = build_join_association(reflection, parent)
|
@@ -55,7 +55,12 @@ module ActiveRecord
|
|
55
55
|
|
56
56
|
def find_parent_in(other_join_dependency)
|
57
57
|
other_join_dependency.join_parts.detect do |join_part|
|
58
|
-
parent
|
58
|
+
case parent
|
59
|
+
when JoinBase
|
60
|
+
parent.active_record == join_part.active_record
|
61
|
+
else
|
62
|
+
parent == join_part
|
63
|
+
end
|
59
64
|
end
|
60
65
|
end
|
61
66
|
|
@@ -56,8 +56,7 @@ module ActiveRecord
|
|
56
56
|
through_options[:include] = options[:include] || options[:source]
|
57
57
|
through_options[:conditions] = options[:conditions]
|
58
58
|
end
|
59
|
-
|
60
|
-
through_options[:order] = options[:order]
|
59
|
+
through_options[:order] = options[:order] if options.has_key?(:order)
|
61
60
|
end
|
62
61
|
|
63
62
|
through_options
|
@@ -62,7 +62,7 @@ module ActiveRecord
|
|
62
62
|
# properly support stale-checking for nested associations.
|
63
63
|
def stale_state
|
64
64
|
if through_reflection.macro == :belongs_to
|
65
|
-
owner[through_reflection.foreign_key].to_s
|
65
|
+
owner[through_reflection.foreign_key] && owner[through_reflection.foreign_key].to_s
|
66
66
|
end
|
67
67
|
end
|
68
68
|
|
@@ -332,16 +332,16 @@ module ActiveRecord
|
|
332
332
|
|
333
333
|
if records = associated_records_to_validate_or_save(association, @new_record_before_save, autosave)
|
334
334
|
begin
|
335
|
-
|
335
|
+
if autosave
|
336
|
+
records_to_destroy = records.select(&:marked_for_destruction?)
|
337
|
+
records_to_destroy.each { |record| association.proxy.destroy(record) }
|
338
|
+
records -= records_to_destroy
|
339
|
+
end
|
336
340
|
|
337
341
|
records.each do |record|
|
338
|
-
next if record.destroyed?
|
339
|
-
|
340
342
|
saved = true
|
341
343
|
|
342
|
-
if autosave && record.
|
343
|
-
records_to_destroy << record
|
344
|
-
elsif autosave != false && (@new_record_before_save || record.new_record?)
|
344
|
+
if autosave != false && (@new_record_before_save || record.new_record?)
|
345
345
|
if autosave
|
346
346
|
saved = association.insert_record(record, false)
|
347
347
|
else
|
@@ -353,19 +353,14 @@ module ActiveRecord
|
|
353
353
|
|
354
354
|
raise ActiveRecord::Rollback unless saved
|
355
355
|
end
|
356
|
-
|
357
|
-
records_to_destroy.each do |record|
|
358
|
-
association.proxy.destroy(record)
|
359
|
-
end
|
360
356
|
rescue
|
361
357
|
records.each {|x| IdentityMap.remove(x) } if IdentityMap.enabled?
|
362
358
|
raise
|
363
359
|
end
|
364
|
-
|
365
360
|
end
|
366
361
|
|
367
362
|
# reconstruct the scope now that we know the owner's id
|
368
|
-
association.
|
363
|
+
association.reset_scope if association.respond_to?(:reset_scope)
|
369
364
|
end
|
370
365
|
end
|
371
366
|
|
@@ -42,8 +42,8 @@ module ActiveRecord
|
|
42
42
|
# Represents the schema of an SQL table in an abstract way. This class
|
43
43
|
# provides methods for manipulating the schema representation.
|
44
44
|
#
|
45
|
-
# Inside migration files, the +t+ object in +create_table+
|
46
|
-
#
|
45
|
+
# Inside migration files, the +t+ object in +create_table+
|
46
|
+
# is actually of this type:
|
47
47
|
#
|
48
48
|
# class SomeMigration < ActiveRecord::Migration
|
49
49
|
# def up
|
@@ -0,0 +1,619 @@
|
|
1
|
+
require 'active_support/core_ext/array/wrap'
|
2
|
+
require 'active_support/deprecation/reporting'
|
3
|
+
|
4
|
+
module ActiveRecord
|
5
|
+
module ConnectionAdapters # :nodoc:
|
6
|
+
module SchemaStatements
|
7
|
+
# Returns a Hash of mappings from the abstract data types to the native
|
8
|
+
# database types. See TableDefinition#column for details on the recognized
|
9
|
+
# abstract data types.
|
10
|
+
def native_database_types
|
11
|
+
{}
|
12
|
+
end
|
13
|
+
|
14
|
+
# Truncates a table alias according to the limits of the current adapter.
|
15
|
+
def table_alias_for(table_name)
|
16
|
+
table_name[0...table_alias_length].gsub(/\./, '_')
|
17
|
+
end
|
18
|
+
|
19
|
+
# Checks to see if the table +table_name+ exists on the database.
|
20
|
+
#
|
21
|
+
# === Example
|
22
|
+
# table_exists?(:developers)
|
23
|
+
def table_exists?(table_name)
|
24
|
+
tables.include?(table_name.to_s)
|
25
|
+
end
|
26
|
+
|
27
|
+
# Returns an array of indexes for the given table.
|
28
|
+
# def indexes(table_name, name = nil) end
|
29
|
+
|
30
|
+
# Checks to see if an index exists on a table for a given index definition.
|
31
|
+
#
|
32
|
+
# === Examples
|
33
|
+
# # Check an index exists
|
34
|
+
# index_exists?(:suppliers, :company_id)
|
35
|
+
#
|
36
|
+
# # Check an index on multiple columns exists
|
37
|
+
# index_exists?(:suppliers, [:company_id, :company_type])
|
38
|
+
#
|
39
|
+
# # Check a unique index exists
|
40
|
+
# index_exists?(:suppliers, :company_id, :unique => true)
|
41
|
+
#
|
42
|
+
# # Check an index with a custom name exists
|
43
|
+
# index_exists?(:suppliers, :company_id, :name => "idx_company_id"
|
44
|
+
def index_exists?(table_name, column_name, options = {})
|
45
|
+
column_names = Array.wrap(column_name)
|
46
|
+
index_name = options.key?(:name) ? options[:name].to_s : index_name(table_name, :column => column_names)
|
47
|
+
if options[:unique]
|
48
|
+
indexes(table_name).any?{ |i| i.unique && i.name == index_name }
|
49
|
+
else
|
50
|
+
indexes(table_name).any?{ |i| i.name == index_name }
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
# Returns an array of Column objects for the table specified by +table_name+.
|
55
|
+
# See the concrete implementation for details on the expected parameter values.
|
56
|
+
def columns(table_name, name = nil) end
|
57
|
+
|
58
|
+
# Checks to see if a column exists in a given table.
|
59
|
+
#
|
60
|
+
# === Examples
|
61
|
+
# # Check a column exists
|
62
|
+
# column_exists?(:suppliers, :name)
|
63
|
+
#
|
64
|
+
# # Check a column exists of a particular type
|
65
|
+
# column_exists?(:suppliers, :name, :string)
|
66
|
+
#
|
67
|
+
# # Check a column exists with a specific definition
|
68
|
+
# column_exists?(:suppliers, :name, :string, :limit => 100)
|
69
|
+
def column_exists?(table_name, column_name, type = nil, options = {})
|
70
|
+
columns(table_name).any?{ |c| c.name == column_name.to_s &&
|
71
|
+
(!type || c.type == type) &&
|
72
|
+
(!options[:limit] || c.limit == options[:limit]) &&
|
73
|
+
(!options[:precision] || c.precision == options[:precision]) &&
|
74
|
+
(!options[:scale] || c.scale == options[:scale]) }
|
75
|
+
end
|
76
|
+
|
77
|
+
# Creates a new table with the name +table_name+. +table_name+ may either
|
78
|
+
# be a String or a Symbol.
|
79
|
+
#
|
80
|
+
# There are two ways to work with +create_table+. You can use the block
|
81
|
+
# form or the regular form, like this:
|
82
|
+
#
|
83
|
+
# === Block form
|
84
|
+
# # create_table() passes a TableDefinition object to the block.
|
85
|
+
# # This form will not only create the table, but also columns for the
|
86
|
+
# # table.
|
87
|
+
#
|
88
|
+
# create_table(:suppliers) do |t|
|
89
|
+
# t.column :name, :string, :limit => 60
|
90
|
+
# # Other fields here
|
91
|
+
# end
|
92
|
+
#
|
93
|
+
# === Block form, with shorthand
|
94
|
+
# # You can also use the column types as method calls, rather than calling the column method.
|
95
|
+
# create_table(:suppliers) do |t|
|
96
|
+
# t.string :name, :limit => 60
|
97
|
+
# # Other fields here
|
98
|
+
# end
|
99
|
+
#
|
100
|
+
# === Regular form
|
101
|
+
# # Creates a table called 'suppliers' with no columns.
|
102
|
+
# create_table(:suppliers)
|
103
|
+
# # Add a column to 'suppliers'.
|
104
|
+
# add_column(:suppliers, :name, :string, {:limit => 60})
|
105
|
+
#
|
106
|
+
# The +options+ hash can include the following keys:
|
107
|
+
# [<tt>:id</tt>]
|
108
|
+
# Whether to automatically add a primary key column. Defaults to true.
|
109
|
+
# Join tables for +has_and_belongs_to_many+ should set it to false.
|
110
|
+
# [<tt>:primary_key</tt>]
|
111
|
+
# The name of the primary key, if one is to be added automatically.
|
112
|
+
# Defaults to +id+. If <tt>:id</tt> is false this option is ignored.
|
113
|
+
#
|
114
|
+
# Also note that this just sets the primary key in the table. You additionally
|
115
|
+
# need to configure the primary key in the model via +self.primary_key=+.
|
116
|
+
# Models do NOT auto-detect the primary key from their table definition.
|
117
|
+
#
|
118
|
+
# [<tt>:options</tt>]
|
119
|
+
# Any extra options you want appended to the table definition.
|
120
|
+
# [<tt>:temporary</tt>]
|
121
|
+
# Make a temporary table.
|
122
|
+
# [<tt>:force</tt>]
|
123
|
+
# Set to true to drop the table before creating it.
|
124
|
+
# Defaults to false.
|
125
|
+
#
|
126
|
+
# ===== Examples
|
127
|
+
# ====== Add a backend specific option to the generated SQL (MySQL)
|
128
|
+
# create_table(:suppliers, :options => 'ENGINE=InnoDB DEFAULT CHARSET=utf8')
|
129
|
+
# generates:
|
130
|
+
# CREATE TABLE suppliers (
|
131
|
+
# id int(11) DEFAULT NULL auto_increment PRIMARY KEY
|
132
|
+
# ) ENGINE=InnoDB DEFAULT CHARSET=utf8
|
133
|
+
#
|
134
|
+
# ====== Rename the primary key column
|
135
|
+
# create_table(:objects, :primary_key => 'guid') do |t|
|
136
|
+
# t.column :name, :string, :limit => 80
|
137
|
+
# end
|
138
|
+
# generates:
|
139
|
+
# CREATE TABLE objects (
|
140
|
+
# guid int(11) DEFAULT NULL auto_increment PRIMARY KEY,
|
141
|
+
# name varchar(80)
|
142
|
+
# )
|
143
|
+
#
|
144
|
+
# ====== Do not add a primary key column
|
145
|
+
# create_table(:categories_suppliers, :id => false) do |t|
|
146
|
+
# t.column :category_id, :integer
|
147
|
+
# t.column :supplier_id, :integer
|
148
|
+
# end
|
149
|
+
# generates:
|
150
|
+
# CREATE TABLE categories_suppliers (
|
151
|
+
# category_id int,
|
152
|
+
# supplier_id int
|
153
|
+
# )
|
154
|
+
#
|
155
|
+
# See also TableDefinition#column for details on how to create columns.
|
156
|
+
def create_table(table_name, options = {})
|
157
|
+
td = table_definition
|
158
|
+
td.primary_key(options[:primary_key] || Base.get_primary_key(table_name.to_s.singularize)) unless options[:id] == false
|
159
|
+
|
160
|
+
yield td if block_given?
|
161
|
+
|
162
|
+
if options[:force] && table_exists?(table_name)
|
163
|
+
drop_table(table_name, options)
|
164
|
+
end
|
165
|
+
|
166
|
+
create_sql = "CREATE#{' TEMPORARY' if options[:temporary]} TABLE "
|
167
|
+
create_sql << "#{quote_table_name(table_name)} ("
|
168
|
+
create_sql << td.to_sql
|
169
|
+
create_sql << ") #{options[:options]}"
|
170
|
+
execute create_sql
|
171
|
+
end
|
172
|
+
|
173
|
+
# A block for changing columns in +table+.
|
174
|
+
#
|
175
|
+
# === Example
|
176
|
+
# # change_table() yields a Table instance
|
177
|
+
# change_table(:suppliers) do |t|
|
178
|
+
# t.column :name, :string, :limit => 60
|
179
|
+
# # Other column alterations here
|
180
|
+
# end
|
181
|
+
#
|
182
|
+
# The +options+ hash can include the following keys:
|
183
|
+
# [<tt>:bulk</tt>]
|
184
|
+
# Set this to true to make this a bulk alter query, such as
|
185
|
+
# ALTER TABLE `users` ADD COLUMN age INT(11), ADD COLUMN birthdate DATETIME ...
|
186
|
+
#
|
187
|
+
# Defaults to false.
|
188
|
+
#
|
189
|
+
# ===== Examples
|
190
|
+
# ====== Add a column
|
191
|
+
# change_table(:suppliers) do |t|
|
192
|
+
# t.column :name, :string, :limit => 60
|
193
|
+
# end
|
194
|
+
#
|
195
|
+
# ====== Add 2 integer columns
|
196
|
+
# change_table(:suppliers) do |t|
|
197
|
+
# t.integer :width, :height, :null => false, :default => 0
|
198
|
+
# end
|
199
|
+
#
|
200
|
+
# ====== Add created_at/updated_at columns
|
201
|
+
# change_table(:suppliers) do |t|
|
202
|
+
# t.timestamps
|
203
|
+
# end
|
204
|
+
#
|
205
|
+
# ====== Add a foreign key column
|
206
|
+
# change_table(:suppliers) do |t|
|
207
|
+
# t.references :company
|
208
|
+
# end
|
209
|
+
#
|
210
|
+
# Creates a <tt>company_id(integer)</tt> column
|
211
|
+
#
|
212
|
+
# ====== Add a polymorphic foreign key column
|
213
|
+
# change_table(:suppliers) do |t|
|
214
|
+
# t.belongs_to :company, :polymorphic => true
|
215
|
+
# end
|
216
|
+
#
|
217
|
+
# Creates <tt>company_type(varchar)</tt> and <tt>company_id(integer)</tt> columns
|
218
|
+
#
|
219
|
+
# ====== Remove a column
|
220
|
+
# change_table(:suppliers) do |t|
|
221
|
+
# t.remove :company
|
222
|
+
# end
|
223
|
+
#
|
224
|
+
# ====== Remove several columns
|
225
|
+
# change_table(:suppliers) do |t|
|
226
|
+
# t.remove :company_id
|
227
|
+
# t.remove :width, :height
|
228
|
+
# end
|
229
|
+
#
|
230
|
+
# ====== Remove an index
|
231
|
+
# change_table(:suppliers) do |t|
|
232
|
+
# t.remove_index :company_id
|
233
|
+
# end
|
234
|
+
#
|
235
|
+
# See also Table for details on
|
236
|
+
# all of the various column transformation
|
237
|
+
def change_table(table_name, options = {})
|
238
|
+
if supports_bulk_alter? && options[:bulk]
|
239
|
+
recorder = ActiveRecord::Migration::CommandRecorder.new(self)
|
240
|
+
yield Table.new(table_name, recorder)
|
241
|
+
bulk_change_table(table_name, recorder.commands)
|
242
|
+
else
|
243
|
+
yield Table.new(table_name, self)
|
244
|
+
end
|
245
|
+
end
|
246
|
+
|
247
|
+
# Renames a table.
|
248
|
+
# ===== Example
|
249
|
+
# rename_table('octopuses', 'octopi')
|
250
|
+
def rename_table(table_name, new_name)
|
251
|
+
raise NotImplementedError, "rename_table is not implemented"
|
252
|
+
end
|
253
|
+
|
254
|
+
# Drops a table from the database.
|
255
|
+
def drop_table(table_name, options = {})
|
256
|
+
execute "DROP TABLE #{quote_table_name(table_name)}"
|
257
|
+
end
|
258
|
+
|
259
|
+
# Adds a new column to the named table.
|
260
|
+
# See TableDefinition#column for details of the options you can use.
|
261
|
+
def add_column(table_name, column_name, type, options = {})
|
262
|
+
add_column_sql = "ALTER TABLE #{quote_table_name(table_name)} ADD #{quote_column_name(column_name)} #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}"
|
263
|
+
add_column_options!(add_column_sql, options)
|
264
|
+
execute(add_column_sql)
|
265
|
+
end
|
266
|
+
|
267
|
+
# Removes the column(s) from the table definition.
|
268
|
+
# ===== Examples
|
269
|
+
# remove_column(:suppliers, :qualification)
|
270
|
+
# remove_columns(:suppliers, :qualification, :experience)
|
271
|
+
def remove_column(table_name, *column_names)
|
272
|
+
if column_names.flatten!
|
273
|
+
message = 'Passing array to remove_columns is deprecated, please use ' +
|
274
|
+
'multiple arguments, like: `remove_columns(:posts, :foo, :bar)`'
|
275
|
+
ActiveSupport::Deprecation.warn message, caller
|
276
|
+
end
|
277
|
+
|
278
|
+
columns_for_remove(table_name, *column_names).each do |column_name|
|
279
|
+
execute "ALTER TABLE #{quote_table_name(table_name)} DROP #{column_name}"
|
280
|
+
end
|
281
|
+
end
|
282
|
+
alias :remove_columns :remove_column
|
283
|
+
|
284
|
+
# Changes the column's definition according to the new options.
|
285
|
+
# See TableDefinition#column for details of the options you can use.
|
286
|
+
# ===== Examples
|
287
|
+
# change_column(:suppliers, :name, :string, :limit => 80)
|
288
|
+
# change_column(:accounts, :description, :text)
|
289
|
+
def change_column(table_name, column_name, type, options = {})
|
290
|
+
raise NotImplementedError, "change_column is not implemented"
|
291
|
+
end
|
292
|
+
|
293
|
+
# Sets a new default value for a column.
|
294
|
+
# ===== Examples
|
295
|
+
# change_column_default(:suppliers, :qualification, 'new')
|
296
|
+
# change_column_default(:accounts, :authorized, 1)
|
297
|
+
# change_column_default(:users, :email, nil)
|
298
|
+
def change_column_default(table_name, column_name, default)
|
299
|
+
raise NotImplementedError, "change_column_default is not implemented"
|
300
|
+
end
|
301
|
+
|
302
|
+
# Renames a column.
|
303
|
+
# ===== Example
|
304
|
+
# rename_column(:suppliers, :description, :name)
|
305
|
+
def rename_column(table_name, column_name, new_column_name)
|
306
|
+
raise NotImplementedError, "rename_column is not implemented"
|
307
|
+
end
|
308
|
+
|
309
|
+
# Adds a new index to the table. +column_name+ can be a single Symbol, or
|
310
|
+
# an Array of Symbols.
|
311
|
+
#
|
312
|
+
# The index will be named after the table and the column name(s), unless
|
313
|
+
# you pass <tt>:name</tt> as an option.
|
314
|
+
#
|
315
|
+
# ===== Examples
|
316
|
+
#
|
317
|
+
# ====== Creating a simple index
|
318
|
+
# add_index(:suppliers, :name)
|
319
|
+
# generates
|
320
|
+
# CREATE INDEX suppliers_name_index ON suppliers(name)
|
321
|
+
#
|
322
|
+
# ====== Creating a unique index
|
323
|
+
# add_index(:accounts, [:branch_id, :party_id], :unique => true)
|
324
|
+
# generates
|
325
|
+
# CREATE UNIQUE INDEX accounts_branch_id_party_id_index ON accounts(branch_id, party_id)
|
326
|
+
#
|
327
|
+
# ====== Creating a named index
|
328
|
+
# add_index(:accounts, [:branch_id, :party_id], :unique => true, :name => 'by_branch_party')
|
329
|
+
# generates
|
330
|
+
# CREATE UNIQUE INDEX by_branch_party ON accounts(branch_id, party_id)
|
331
|
+
#
|
332
|
+
# ====== Creating an index with specific key length
|
333
|
+
# add_index(:accounts, :name, :name => 'by_name', :length => 10)
|
334
|
+
# generates
|
335
|
+
# CREATE INDEX by_name ON accounts(name(10))
|
336
|
+
#
|
337
|
+
# add_index(:accounts, [:name, :surname], :name => 'by_name_surname', :length => {:name => 10, :surname => 15})
|
338
|
+
# generates
|
339
|
+
# CREATE INDEX by_name_surname ON accounts(name(10), surname(15))
|
340
|
+
#
|
341
|
+
# Note: SQLite doesn't support index length
|
342
|
+
#
|
343
|
+
# ====== Creating an index with a sort order (desc or asc, asc is the default)
|
344
|
+
# add_index(:accounts, [:branch_id, :party_id, :surname], :order => {:branch_id => :desc, :part_id => :asc})
|
345
|
+
# generates
|
346
|
+
# CREATE INDEX by_branch_desc_party ON accounts(branch_id DESC, party_id ASC, surname)
|
347
|
+
#
|
348
|
+
# Note: mysql doesn't yet support index order (it accepts the syntax but ignores it)
|
349
|
+
#
|
350
|
+
def add_index(table_name, column_name, options = {})
|
351
|
+
index_name, index_type, index_columns = add_index_options(table_name, column_name, options)
|
352
|
+
execute "CREATE #{index_type} INDEX #{quote_column_name(index_name)} ON #{quote_table_name(table_name)} (#{index_columns})"
|
353
|
+
end
|
354
|
+
|
355
|
+
# Remove the given index from the table.
|
356
|
+
#
|
357
|
+
# Remove the index_accounts_on_column in the accounts table.
|
358
|
+
# remove_index :accounts, :column
|
359
|
+
# Remove the index named index_accounts_on_branch_id in the accounts table.
|
360
|
+
# remove_index :accounts, :column => :branch_id
|
361
|
+
# Remove the index named index_accounts_on_branch_id_and_party_id in the accounts table.
|
362
|
+
# remove_index :accounts, :column => [:branch_id, :party_id]
|
363
|
+
# Remove the index named by_branch_party in the accounts table.
|
364
|
+
# remove_index :accounts, :name => :by_branch_party
|
365
|
+
def remove_index(table_name, options = {})
|
366
|
+
remove_index!(table_name, index_name_for_remove(table_name, options))
|
367
|
+
end
|
368
|
+
|
369
|
+
def remove_index!(table_name, index_name) #:nodoc:
|
370
|
+
execute "DROP INDEX #{quote_column_name(index_name)} ON #{quote_table_name(table_name)}"
|
371
|
+
end
|
372
|
+
|
373
|
+
# Rename an index.
|
374
|
+
#
|
375
|
+
# Rename the index_people_on_last_name index to index_users_on_last_name
|
376
|
+
# rename_index :people, 'index_people_on_last_name', 'index_users_on_last_name'
|
377
|
+
def rename_index(table_name, old_name, new_name)
|
378
|
+
# this is a naive implementation; some DBs may support this more efficiently (Postgres, for instance)
|
379
|
+
old_index_def = indexes(table_name).detect { |i| i.name == old_name }
|
380
|
+
return unless old_index_def
|
381
|
+
remove_index(table_name, :name => old_name)
|
382
|
+
add_index(table_name, old_index_def.columns, :name => new_name, :unique => old_index_def.unique)
|
383
|
+
end
|
384
|
+
|
385
|
+
def index_name(table_name, options) #:nodoc:
|
386
|
+
if Hash === options # legacy support
|
387
|
+
if options[:column]
|
388
|
+
"index_#{table_name}_on_#{Array.wrap(options[:column]) * '_and_'}"
|
389
|
+
elsif options[:name]
|
390
|
+
options[:name]
|
391
|
+
else
|
392
|
+
raise ArgumentError, "You must specify the index name"
|
393
|
+
end
|
394
|
+
else
|
395
|
+
index_name(table_name, :column => options)
|
396
|
+
end
|
397
|
+
end
|
398
|
+
|
399
|
+
# Verify the existence of an index with a given name.
|
400
|
+
#
|
401
|
+
# The default argument is returned if the underlying implementation does not define the indexes method,
|
402
|
+
# as there's no way to determine the correct answer in that case.
|
403
|
+
def index_name_exists?(table_name, index_name, default)
|
404
|
+
return default unless respond_to?(:indexes)
|
405
|
+
index_name = index_name.to_s
|
406
|
+
indexes(table_name).detect { |i| i.name == index_name }
|
407
|
+
end
|
408
|
+
|
409
|
+
# Returns a string of <tt>CREATE TABLE</tt> SQL statement(s) for recreating the
|
410
|
+
# entire structure of the database.
|
411
|
+
def structure_dump
|
412
|
+
end
|
413
|
+
|
414
|
+
def dump_schema_information #:nodoc:
|
415
|
+
sm_table = ActiveRecord::Migrator.schema_migrations_table_name
|
416
|
+
migrated = select_values("SELECT version FROM #{sm_table} ORDER BY version")
|
417
|
+
migrated.map { |v| "INSERT INTO #{sm_table} (version) VALUES ('#{v}');" }.join("\n\n")
|
418
|
+
end
|
419
|
+
|
420
|
+
# Should not be called normally, but this operation is non-destructive.
|
421
|
+
# The migrations module handles this automatically.
|
422
|
+
def initialize_schema_migrations_table
|
423
|
+
sm_table = ActiveRecord::Migrator.schema_migrations_table_name
|
424
|
+
|
425
|
+
unless table_exists?(sm_table)
|
426
|
+
create_table(sm_table, :id => false) do |schema_migrations_table|
|
427
|
+
schema_migrations_table.column :version, :string, :null => false
|
428
|
+
end
|
429
|
+
add_index sm_table, :version, :unique => true,
|
430
|
+
:name => "#{Base.table_name_prefix}unique_schema_migrations#{Base.table_name_suffix}"
|
431
|
+
|
432
|
+
# Backwards-compatibility: if we find schema_info, assume we've
|
433
|
+
# migrated up to that point:
|
434
|
+
si_table = Base.table_name_prefix + 'schema_info' + Base.table_name_suffix
|
435
|
+
|
436
|
+
if table_exists?(si_table)
|
437
|
+
ActiveSupport::Deprecation.warn "Usage of the schema table `#{si_table}` is deprecated. Please switch to using `schema_migrations` table"
|
438
|
+
|
439
|
+
old_version = select_value("SELECT version FROM #{quote_table_name(si_table)}").to_i
|
440
|
+
assume_migrated_upto_version(old_version)
|
441
|
+
drop_table(si_table)
|
442
|
+
end
|
443
|
+
end
|
444
|
+
end
|
445
|
+
|
446
|
+
def assume_migrated_upto_version(version, migrations_paths = ActiveRecord::Migrator.migrations_paths)
|
447
|
+
migrations_paths = Array.wrap(migrations_paths)
|
448
|
+
version = version.to_i
|
449
|
+
sm_table = quote_table_name(ActiveRecord::Migrator.schema_migrations_table_name)
|
450
|
+
|
451
|
+
migrated = select_values("SELECT version FROM #{sm_table}").map { |v| v.to_i }
|
452
|
+
paths = migrations_paths.map {|p| "#{p}/[0-9]*_*.rb" }
|
453
|
+
versions = Dir[*paths].map do |filename|
|
454
|
+
filename.split('/').last.split('_').first.to_i
|
455
|
+
end
|
456
|
+
|
457
|
+
unless migrated.include?(version)
|
458
|
+
execute "INSERT INTO #{sm_table} (version) VALUES ('#{version}')"
|
459
|
+
end
|
460
|
+
|
461
|
+
inserted = Set.new
|
462
|
+
(versions - migrated).each do |v|
|
463
|
+
if inserted.include?(v)
|
464
|
+
raise "Duplicate migration #{v}. Please renumber your migrations to resolve the conflict."
|
465
|
+
elsif v < version
|
466
|
+
execute "INSERT INTO #{sm_table} (version) VALUES ('#{v}')"
|
467
|
+
inserted << v
|
468
|
+
end
|
469
|
+
end
|
470
|
+
end
|
471
|
+
|
472
|
+
def type_to_sql(type, limit = nil, precision = nil, scale = nil) #:nodoc:
|
473
|
+
if native = native_database_types[type.to_sym]
|
474
|
+
column_type_sql = (native.is_a?(Hash) ? native[:name] : native).dup
|
475
|
+
|
476
|
+
if type == :decimal # ignore limit, use precision and scale
|
477
|
+
scale ||= native[:scale]
|
478
|
+
|
479
|
+
if precision ||= native[:precision]
|
480
|
+
if scale
|
481
|
+
column_type_sql << "(#{precision},#{scale})"
|
482
|
+
else
|
483
|
+
column_type_sql << "(#{precision})"
|
484
|
+
end
|
485
|
+
elsif scale
|
486
|
+
raise ArgumentError, "Error adding decimal column: precision cannot be empty if scale if specified"
|
487
|
+
end
|
488
|
+
|
489
|
+
elsif (type != :primary_key) && (limit ||= native.is_a?(Hash) && native[:limit])
|
490
|
+
column_type_sql << "(#{limit})"
|
491
|
+
end
|
492
|
+
|
493
|
+
column_type_sql
|
494
|
+
else
|
495
|
+
type
|
496
|
+
end
|
497
|
+
end
|
498
|
+
|
499
|
+
def add_column_options!(sql, options) #:nodoc:
|
500
|
+
sql << " DEFAULT #{quote(options[:default], options[:column])}" if options_include_default?(options)
|
501
|
+
# must explicitly check for :null to allow change_column to work on migrations
|
502
|
+
if options[:null] == false
|
503
|
+
sql << " NOT NULL"
|
504
|
+
end
|
505
|
+
end
|
506
|
+
|
507
|
+
# SELECT DISTINCT clause for a given set of columns and a given ORDER BY clause.
|
508
|
+
#
|
509
|
+
<<<<<<< HEAD
|
510
|
+
# distinct("posts.id", "posts.created_at desc")
|
511
|
+
=======
|
512
|
+
# distinct("posts.id", ["posts.created_at desc"])
|
513
|
+
#
|
514
|
+
>>>>>>> 1b02299... Merge pull request #6792 from Empact/postgres-distinct
|
515
|
+
def distinct(columns, order_by)
|
516
|
+
"DISTINCT #{columns_for_distinct(columns, order_by)}"
|
517
|
+
end
|
518
|
+
|
519
|
+
# Given a set of columns and an ORDER BY clause, returns the columns for a SELECT DISTINCT.
|
520
|
+
# Both PostgreSQL and Oracle overrides this for custom DISTINCT syntax - they
|
521
|
+
# require the order columns appear in the SELECT.
|
522
|
+
#
|
523
|
+
# columns_for_distinct("posts.id", ["posts.created_at desc"])
|
524
|
+
def columns_for_distinct(columns, orders)
|
525
|
+
columns
|
526
|
+
end
|
527
|
+
|
528
|
+
# Adds timestamps (created_at and updated_at) columns to the named table.
|
529
|
+
# ===== Examples
|
530
|
+
# add_timestamps(:suppliers)
|
531
|
+
def add_timestamps(table_name)
|
532
|
+
add_column table_name, :created_at, :datetime
|
533
|
+
add_column table_name, :updated_at, :datetime
|
534
|
+
end
|
535
|
+
|
536
|
+
# Removes the timestamp columns (created_at and updated_at) from the table definition.
|
537
|
+
# ===== Examples
|
538
|
+
# remove_timestamps(:suppliers)
|
539
|
+
def remove_timestamps(table_name)
|
540
|
+
remove_column table_name, :updated_at
|
541
|
+
remove_column table_name, :created_at
|
542
|
+
end
|
543
|
+
|
544
|
+
protected
|
545
|
+
def add_index_sort_order(option_strings, column_names, options = {})
|
546
|
+
if options.is_a?(Hash) && order = options[:order]
|
547
|
+
case order
|
548
|
+
when Hash
|
549
|
+
column_names.each {|name| option_strings[name] += " #{order[name].to_s.upcase}" if order.has_key?(name)}
|
550
|
+
when String
|
551
|
+
column_names.each {|name| option_strings[name] += " #{order.upcase}"}
|
552
|
+
end
|
553
|
+
end
|
554
|
+
|
555
|
+
return option_strings
|
556
|
+
end
|
557
|
+
|
558
|
+
# Overridden by the mysql adapter for supporting index lengths
|
559
|
+
def quoted_columns_for_index(column_names, options = {})
|
560
|
+
option_strings = Hash[column_names.map {|name| [name, '']}]
|
561
|
+
|
562
|
+
# add index sort order if supported
|
563
|
+
if supports_index_sort_order?
|
564
|
+
option_strings = add_index_sort_order(option_strings, column_names, options)
|
565
|
+
end
|
566
|
+
|
567
|
+
column_names.map {|name| quote_column_name(name) + option_strings[name]}
|
568
|
+
end
|
569
|
+
|
570
|
+
def options_include_default?(options)
|
571
|
+
options.include?(:default) && !(options[:null] == false && options[:default].nil?)
|
572
|
+
end
|
573
|
+
|
574
|
+
def add_index_options(table_name, column_name, options = {})
|
575
|
+
column_names = Array.wrap(column_name)
|
576
|
+
index_name = index_name(table_name, :column => column_names)
|
577
|
+
|
578
|
+
if Hash === options # legacy support, since this param was a string
|
579
|
+
index_type = options[:unique] ? "UNIQUE" : ""
|
580
|
+
index_name = options[:name].to_s if options.key?(:name)
|
581
|
+
else
|
582
|
+
index_type = options
|
583
|
+
end
|
584
|
+
|
585
|
+
if index_name.length > index_name_length
|
586
|
+
raise ArgumentError, "Index name '#{index_name}' on table '#{table_name}' is too long; the limit is #{index_name_length} characters"
|
587
|
+
end
|
588
|
+
if index_name_exists?(table_name, index_name, false)
|
589
|
+
raise ArgumentError, "Index name '#{index_name}' on table '#{table_name}' already exists"
|
590
|
+
end
|
591
|
+
index_columns = quoted_columns_for_index(column_names, options).join(", ")
|
592
|
+
|
593
|
+
[index_name, index_type, index_columns]
|
594
|
+
end
|
595
|
+
|
596
|
+
def index_name_for_remove(table_name, options = {})
|
597
|
+
index_name = index_name(table_name, options)
|
598
|
+
|
599
|
+
unless index_name_exists?(table_name, index_name, true)
|
600
|
+
raise ArgumentError, "Index name '#{index_name}' on table '#{table_name}' does not exist"
|
601
|
+
end
|
602
|
+
|
603
|
+
index_name
|
604
|
+
end
|
605
|
+
|
606
|
+
def columns_for_remove(table_name, *column_names)
|
607
|
+
column_names = column_names.flatten
|
608
|
+
|
609
|
+
raise ArgumentError.new("You must specify at least one column name. Example: remove_column(:people, :first_name)") if column_names.blank?
|
610
|
+
column_names.map {|column_name| quote_column_name(column_name) }
|
611
|
+
end
|
612
|
+
|
613
|
+
private
|
614
|
+
def table_definition
|
615
|
+
TableDefinition.new(self)
|
616
|
+
end
|
617
|
+
end
|
618
|
+
end
|
619
|
+
end
|