Skip to content

Commit

Permalink
Abstract high precision timestamp in rb schema
Browse files Browse the repository at this point in the history
  • Loading branch information
nvasilevski committed Feb 10, 2025
1 parent ce96809 commit 7183946
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,13 @@ def schema_default(column)
end

def schema_expression(column)
"-> { #{column.default_function.inspect} }" if column.default_function
return unless column.default_function

if column.default_function == @connection.high_precision_current_timestamp
"-> { high_precision_current_timestamp } "
else
"-> { #{column.default_function.inspect} }"
end
end

def schema_collation(column)
Expand Down
7 changes: 7 additions & 0 deletions activerecord/test/cases/schema_dumper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -931,12 +931,14 @@ class SchemaDumperDefaultsTest < ActiveRecord::TestCase

setup do
@connection = ActiveRecord::Base.lease_connection
high_precision_timestamp_function = @connection.high_precision_current_timestamp
@connection.create_table :dump_defaults, force: true do |t|
t.string :string_with_default, default: "Hello!"
t.date :date_with_default, default: "2014-06-05"
t.datetime :datetime_with_default, default: "2014-06-05 07:17:04"
t.time :time_with_default, default: "07:17:04"
t.decimal :decimal_with_default, default: "1234567890.0123456789", precision: 20, scale: 10
t.timestamp :high_precision_published_at, default: -> { high_precision_timestamp_function }

if supports_text_column_with_default?
t.text :text_with_default, default: "John' Doe"
Expand Down Expand Up @@ -965,6 +967,11 @@ class SchemaDumperDefaultsTest < ActiveRecord::TestCase
@connection.drop_table "dump_defaults", if_exists: true
end

def test_schema_dump_abstracts_high_precision_current_timestamp
output = dump_table_schema("dump_defaults")
assert_match %r{t\.datetime\s+"high_precision_published_at",\s+default: -> \{ high_precision_current_timestamp \}}, output
end

def test_schema_dump_defaults_with_universally_supported_types
output = dump_table_schema("dump_defaults")

Expand Down
1 change: 1 addition & 0 deletions activerecord/test/schema/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
t.integer :wheels_count, default: 0, null: false
t.datetime :wheels_owned_at
t.timestamp :manufactured_at, default: -> { "CURRENT_TIMESTAMP" }
t.datetime :manufactured_precicely_at, default: -> { high_precision_current_timestamp }
end

create_table :articles, force: true do |t|
Expand Down

0 comments on commit 7183946

Please sign in to comment.