Skip to content

Commit

Permalink
Merge pull request rails#53882 from Shopify/sqlite-use-default-function
Browse files Browse the repository at this point in the history
SQLite3: Use default function as default insert value
  • Loading branch information
kamipo authored Dec 10, 2024
2 parents bcb2fd3 + be09eb5 commit 1201cb1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,11 @@ def returning_column_values(result)
end

def default_insert_value(column)
column.default
if column.default_function
Arel.sql(column.default_function)
else
column.default
end
end
end
end
Expand Down
17 changes: 15 additions & 2 deletions activerecord/test/cases/fixtures_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require "models/admin/account"
require "models/admin/randomly_named_c1"
require "models/admin/user"
require "models/aircraft"
require "models/author"
require "models/binary"
require "models/book"
Expand Down Expand Up @@ -112,8 +113,6 @@ def test_bulk_insert_multiple_table_with_a_multi_statement_query
end

def test_bulk_insert_with_a_multi_statement_query_raises_an_exception_when_any_insert_fails
require "models/aircraft"

assert_equal false, Aircraft.columns_hash["wheels_count"].null
fixtures = {
"aircraft" => [
Expand Down Expand Up @@ -478,6 +477,20 @@ def test_insert_with_datetime
assert first
end

def test_insert_with_default_function
create_fixtures("aircrafts")

aircraft = Aircraft.find_by(name: "boeing-with-no-manufactured-at")
assert_equal Time.now.utc.strftime("%Y-%m-%d %H:%M:%S"), aircraft.manufactured_at.strftime("%Y-%m-%d %H:%M:%S")
end

def test_insert_with_default_value
create_fixtures("aircrafts")

aircraft = Aircraft.find_by(name: "boeing-with-no-wheels")
assert_equal 0, aircraft.wheels_count
end

def test_logger_level_invariant
previous_logger = ActiveRecord::Base.logger
ActiveRecord::Base.logger = ActiveSupport::Logger.new(nil)
Expand Down
7 changes: 7 additions & 0 deletions activerecord/test/fixtures/aircrafts.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
no_wheels:
name: boeing-with-no-wheels
manufactured_at: 2024-01-01

no_manufactured_at:
name: boeing-with-no-manufactured-at
wheels_count: 2

0 comments on commit 1201cb1

Please sign in to comment.