Skip to content

Commit

Permalink
Merge pull request #41324 from eileencodes/fix-timestamp-type-for-sql…
Browse files Browse the repository at this point in the history
…ite3

Fix timestamp type for sqlite3
  • Loading branch information
eileencodes committed Feb 4, 2021
1 parent d4907b7 commit d1fd637
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ def #{option_name}=(value)
end
CODE
end

def aliased_types(name, fallback)
"timestamp" == name ? :datetime : fallback
end
end

AddColumnDefinition = Struct.new(:column) # :nodoc:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ def change_column_null(table_name, column_name, null, default = nil) #:nodoc:
def change_column(table_name, column_name, type, **options) #:nodoc:
alter_table(table_name) do |definition|
definition[column_name].instance_eval do
self.type = type
self.type = aliased_types(type.to_s, type)
self.options.merge!(options)
end
end
Expand Down
22 changes: 22 additions & 0 deletions activerecord/test/cases/migration/change_schema_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,28 @@ def test_add_column_with_timestamp_type
end
end

def test_change_column_with_timestamp_type
connection.create_table :testings do |t|
t.column :foo, :datetime, null: false
end

connection.change_column :testings, :foo, :timestamp

column = connection.columns(:testings).find { |c| c.name == "foo" }

assert_equal :datetime, column.type

if current_adapter?(:PostgreSQLAdapter)
assert_equal "timestamp without time zone", column.sql_type
elsif current_adapter?(:Mysql2Adapter)
assert_equal "timestamp", column.sql_type
elsif current_adapter?(:OracleAdapter)
assert_equal "TIMESTAMP(6)", column.sql_type
else
assert_equal connection.type_to_sql("datetime"), column.sql_type
end
end

def test_change_column_quotes_column_names
connection.create_table :testings do |t|
t.column :select, :string
Expand Down

0 comments on commit d1fd637

Please sign in to comment.