Skip to content

Commit 7192691

Browse files
committed
Don't append limit to primary key column definition. Freeze some constants.
1 parent c329794 commit 7192691

File tree

4 files changed

+41
-33
lines changed

4 files changed

+41
-33
lines changed

activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ def dump_schema_information #:nodoc:
254254

255255
def type_to_sql(type, limit = nil, precision = nil, scale = nil) #:nodoc:
256256
if native = native_database_types[type]
257-
column_type_sql = native.is_a?(Hash) ? native[:name] : native
257+
column_type_sql = (native.is_a?(Hash) ? native[:name] : native).dup
258258

259259
if type == :decimal # ignore limit, use precision and scale
260260
scale ||= native[:scale]
@@ -269,7 +269,7 @@ def type_to_sql(type, limit = nil, precision = nil, scale = nil) #:nodoc:
269269
raise ArgumentError, "Error adding decimal column: precision cannot be empty if scale if specified"
270270
end
271271

272-
elsif limit ||= native.is_a?(Hash) && native[:limit]
272+
elsif (type != :primary_key) && (limit ||= native.is_a?(Hash) && native[:limit])
273273
column_type_sql << "(#{limit})"
274274
end
275275

activerecord/lib/active_record/connection_adapters/mysql_adapter.rb

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -151,13 +151,30 @@ class MysqlAdapter < AbstractAdapter
151151
@@emulate_booleans = true
152152
cattr_accessor :emulate_booleans
153153

154+
ADAPTER_NAME = 'MySQL'.freeze
155+
154156
LOST_CONNECTION_ERROR_MESSAGES = [
155157
"Server shutdown in progress",
156158
"Broken pipe",
157159
"Lost connection to MySQL server during query",
158160
"MySQL server has gone away"
159161
]
160162

163+
NATIVE_DATABASE_TYPES = {
164+
:primary_key => "int(11) DEFAULT NULL auto_increment PRIMARY KEY".freeze,
165+
:string => { :name => "varchar", :limit => 255 },
166+
:text => { :name => "text" },
167+
:integer => { :name => "int"},
168+
:float => { :name => "float" },
169+
:decimal => { :name => "decimal" },
170+
:datetime => { :name => "datetime" },
171+
:timestamp => { :name => "datetime" },
172+
:time => { :name => "time" },
173+
:date => { :name => "date" },
174+
:binary => { :name => "blob" },
175+
:boolean => { :name => "tinyint", :limit => 1 }
176+
}
177+
161178
def initialize(connection, logger, connection_options, config)
162179
super(connection, logger)
163180
@connection_options, @config = connection_options, config
@@ -166,28 +183,15 @@ def initialize(connection, logger, connection_options, config)
166183
end
167184

168185
def adapter_name #:nodoc:
169-
'MySQL'
186+
ADAPTER_NAME
170187
end
171188

172189
def supports_migrations? #:nodoc:
173190
true
174191
end
175192

176193
def native_database_types #:nodoc:
177-
{
178-
:primary_key => "int(11) DEFAULT NULL auto_increment PRIMARY KEY",
179-
:string => { :name => "varchar", :limit => 255 },
180-
:text => { :name => "text" },
181-
:integer => { :name => "int", :limit => 11 },
182-
:float => { :name => "float" },
183-
:decimal => { :name => "decimal" },
184-
:datetime => { :name => "datetime" },
185-
:timestamp => { :name => "datetime" },
186-
:time => { :name => "time" },
187-
:date => { :name => "date" },
188-
:binary => { :name => "blob" },
189-
:boolean => { :name => "tinyint", :limit => 1 }
190-
}
194+
NATIVE_DATABASE_TYPES
191195
end
192196

193197

activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,26 @@ module ConnectionAdapters
224224
# * <tt>:min_messages</tt> -- An optional client min messages that is used in a SET client_min_messages TO <min_messages> call on the connection.
225225
# * <tt>:allow_concurrency</tt> -- If true, use async query methods so Ruby threads don't deadlock; otherwise, use blocking query methods.
226226
class PostgreSQLAdapter < AbstractAdapter
227+
ADAPTER_NAME = 'PostgreSQL'.freeze
228+
229+
NATIVE_DATABASE_TYPES = {
230+
:primary_key => "serial primary key".freeze,
231+
:string => { :name => "character varying", :limit => 255 },
232+
:text => { :name => "text" },
233+
:integer => { :name => "integer" },
234+
:float => { :name => "float" },
235+
:decimal => { :name => "decimal" },
236+
:datetime => { :name => "timestamp" },
237+
:timestamp => { :name => "timestamp" },
238+
:time => { :name => "time" },
239+
:date => { :name => "date" },
240+
:binary => { :name => "bytea" },
241+
:boolean => { :name => "boolean" }
242+
}
243+
227244
# Returns 'PostgreSQL' as adapter name for identification purposes.
228245
def adapter_name
229-
'PostgreSQL'
246+
ADAPTER_NAME
230247
end
231248

232249
# Initializes and connects a PostgreSQL adapter.
@@ -268,20 +285,7 @@ def disconnect!
268285
end
269286

270287
def native_database_types #:nodoc:
271-
{
272-
:primary_key => "serial primary key",
273-
:string => { :name => "character varying", :limit => 255 },
274-
:text => { :name => "text" },
275-
:integer => { :name => "integer" },
276-
:float => { :name => "float" },
277-
:decimal => { :name => "decimal" },
278-
:datetime => { :name => "timestamp" },
279-
:timestamp => { :name => "timestamp" },
280-
:time => { :name => "time" },
281-
:date => { :name => "date" },
282-
:binary => { :name => "bytea" },
283-
:boolean => { :name => "boolean" }
284-
}
288+
NATIVE_DATABASE_TYPES
285289
end
286290

287291
# Does PostgreSQL support migrations?

activerecord/lib/active_record/fixtures.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ def initialize(connection, table_name, class_name, fixture_path, file_filter = D
543543
@connection, @table_name, @fixture_path, @file_filter = connection, table_name, fixture_path, file_filter
544544
@class_name = class_name ||
545545
(ActiveRecord::Base.pluralize_table_names ? @table_name.singularize.camelize : @table_name.camelize)
546-
@table_name = ActiveRecord::Base.table_name_prefix + @table_name + ActiveRecord::Base.table_name_suffix
546+
@table_name = "#{ActiveRecord::Base.table_name_prefix}#{@table_name}#{ActiveRecord::Base.table_name_suffix}"
547547
@table_name = class_name.table_name if class_name.respond_to?(:table_name)
548548
@connection = class_name.connection if class_name.respond_to?(:connection)
549549
read_fixture_files

0 commit comments

Comments
 (0)