-
Notifications
You must be signed in to change notification settings - Fork 21.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make t.timestamps
with precision by default.
#34970
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,13 +16,55 @@ def self.find(version) | |
V6_0 = Current | ||
|
||
class V5_2 < V6_0 | ||
module TableDefinition | ||
def timestamps(**options) | ||
options[:precision] ||= nil | ||
super | ||
end | ||
end | ||
|
||
module CommandRecorder | ||
def invert_transaction(args, &block) | ||
[:transaction, args, block] | ||
end | ||
end | ||
|
||
def create_table(table_name, **options) | ||
if block_given? | ||
super { |t| yield compatible_table_definition(t) } | ||
else | ||
super | ||
end | ||
end | ||
|
||
def change_table(table_name, **options) | ||
if block_given? | ||
super { |t| yield compatible_table_definition(t) } | ||
else | ||
super | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need these similar overrides? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is to inject https://github.com/rails/rails/pull/34970/files#diff-2a8be25f82da6b3935cc6a41300a1b01R19` If the https://github.com/rails/rails/pull/34970/files#diff-ec048630132cce280a9445022318906eR140 |
||
end | ||
|
||
def create_join_table(table_1, table_2, **options) | ||
if block_given? | ||
super { |t| yield compatible_table_definition(t) } | ||
else | ||
super | ||
end | ||
end | ||
|
||
def add_timestamps(table_name, **options) | ||
options[:precision] ||= nil | ||
super | ||
end | ||
|
||
private | ||
def compatible_table_definition(t) | ||
class << t | ||
prepend TableDefinition | ||
end | ||
t | ||
end | ||
|
||
def command_recorder | ||
recorder = super | ||
|
@@ -87,35 +129,12 @@ def create_table(table_name, options = {}) | |
options[:id] = :integer | ||
end | ||
|
||
if block_given? | ||
super do |t| | ||
yield compatible_table_definition(t) | ||
end | ||
else | ||
super | ||
end | ||
end | ||
|
||
def change_table(table_name, options = {}) | ||
if block_given? | ||
super do |t| | ||
yield compatible_table_definition(t) | ||
end | ||
else | ||
super | ||
end | ||
super | ||
end | ||
|
||
def create_join_table(table_1, table_2, column_options: {}, **options) | ||
column_options.reverse_merge!(type: :integer) | ||
|
||
if block_given? | ||
super do |t| | ||
yield compatible_table_definition(t) | ||
end | ||
else | ||
super | ||
end | ||
super | ||
end | ||
|
||
def add_column(table_name, column_name, type, options = {}) | ||
|
@@ -136,7 +155,7 @@ def compatible_table_definition(t) | |
class << t | ||
prepend TableDefinition | ||
end | ||
t | ||
super | ||
end | ||
end | ||
|
||
|
@@ -154,33 +173,13 @@ def timestamps(**options) | |
end | ||
end | ||
|
||
def create_table(table_name, options = {}) | ||
if block_given? | ||
super do |t| | ||
yield compatible_table_definition(t) | ||
end | ||
else | ||
super | ||
end | ||
end | ||
|
||
def change_table(table_name, options = {}) | ||
if block_given? | ||
super do |t| | ||
yield compatible_table_definition(t) | ||
end | ||
else | ||
super | ||
end | ||
end | ||
|
||
def add_reference(*, **options) | ||
def add_reference(table_name, ref_name, **options) | ||
options[:index] ||= false | ||
super | ||
end | ||
alias :add_belongs_to :add_reference | ||
|
||
def add_timestamps(_, **options) | ||
def add_timestamps(table_name, **options) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would this not cause warnings of unused arguments? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Currently Ruby doesn't raise any warnings of unused method arguments for now. |
||
options[:null] = true if options[:null].nil? | ||
super | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -157,4 +157,55 @@ def test_timestamps_without_null_set_null_to_false_on_add_timestamps | |
assert @connection.column_exists?(:has_timestamps, :created_at, null: false) | ||
assert @connection.column_exists?(:has_timestamps, :updated_at, null: false) | ||
end | ||
|
||
if subsecond_precision_supported? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
def test_timestamps_sets_presicion_on_create_table | ||
ActiveRecord::Schema.define do | ||
create_table :has_timestamps do |t| | ||
t.timestamps | ||
end | ||
end | ||
|
||
assert @connection.column_exists?(:has_timestamps, :created_at, precision: 6, null: false) | ||
assert @connection.column_exists?(:has_timestamps, :updated_at, precision: 6, null: false) | ||
end | ||
|
||
def test_timestamps_sets_presicion_on_change_table | ||
ActiveRecord::Schema.define do | ||
create_table :has_timestamps | ||
|
||
change_table :has_timestamps do |t| | ||
t.timestamps default: Time.now | ||
end | ||
end | ||
|
||
assert @connection.column_exists?(:has_timestamps, :created_at, precision: 6, null: false) | ||
assert @connection.column_exists?(:has_timestamps, :updated_at, precision: 6, null: false) | ||
end | ||
|
||
if ActiveRecord::Base.connection.supports_bulk_alter? | ||
def test_timestamps_sets_presicion_on_change_table_with_bulk | ||
ActiveRecord::Schema.define do | ||
create_table :has_timestamps | ||
|
||
change_table :has_timestamps, bulk: true do |t| | ||
t.timestamps default: Time.now | ||
end | ||
end | ||
|
||
assert @connection.column_exists?(:has_timestamps, :created_at, precision: 6, null: false) | ||
assert @connection.column_exists?(:has_timestamps, :updated_at, precision: 6, null: false) | ||
end | ||
end | ||
|
||
def test_timestamps_sets_presicion_on_add_timestamps | ||
ActiveRecord::Schema.define do | ||
create_table :has_timestamps | ||
add_timestamps :has_timestamps, default: Time.now | ||
end | ||
|
||
assert @connection.column_exists?(:has_timestamps, :created_at, precision: 6, null: false) | ||
assert @connection.column_exists?(:has_timestamps, :updated_at, precision: 6, null: false) | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've changed to apply new default the same with Action Mailbox.