Skip to content

Commit 7265111

Browse files
authored
Merge pull request rails#41324 from eileencodes/fix-timestamp-type-for-sqlite3
Fix timestamp type for sqlite3
2 parents d30dad4 + 02bcf89 commit 7265111

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ def #{option_name}=(value)
7171
end
7272
CODE
7373
end
74+
75+
def aliased_types(name, fallback)
76+
"timestamp" == name ? :datetime : fallback
77+
end
7478
end
7579

7680
AddColumnDefinition = Struct.new(:column) # :nodoc:

activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ def change_column_null(table_name, column_name, null, default = nil) #:nodoc:
271271
def change_column(table_name, column_name, type, **options) #:nodoc:
272272
alter_table(table_name) do |definition|
273273
definition[column_name].instance_eval do
274-
self.type = type
274+
self.type = aliased_types(type.to_s, type)
275275
self.options.merge!(options)
276276
end
277277
end

activerecord/test/cases/migration/change_schema_test.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,28 @@ def test_add_column_with_timestamp_type
290290
end
291291
end
292292

293+
def test_change_column_with_timestamp_type
294+
connection.create_table :testings do |t|
295+
t.column :foo, :datetime, null: false
296+
end
297+
298+
connection.change_column :testings, :foo, :timestamp
299+
300+
column = connection.columns(:testings).find { |c| c.name == "foo" }
301+
302+
assert_equal :datetime, column.type
303+
304+
if current_adapter?(:PostgreSQLAdapter)
305+
assert_equal "timestamp without time zone", column.sql_type
306+
elsif current_adapter?(:Mysql2Adapter)
307+
assert_equal "timestamp", column.sql_type
308+
elsif current_adapter?(:OracleAdapter)
309+
assert_equal "TIMESTAMP(6)", column.sql_type
310+
else
311+
assert_equal connection.type_to_sql("datetime"), column.sql_type
312+
end
313+
end
314+
293315
def test_change_column_quotes_column_names
294316
connection.create_table :testings do |t|
295317
t.column :select, :string

0 commit comments

Comments
 (0)