Skip to content

Commit 55cf91a

Browse files
FIX: Add default precision to db (#1166)
Generate migration file to set the default precision for datetime columns in the database Co-authored-by: Rob Young <rhyoung214@gmail.com>
1 parent 2f080af commit 55cf91a

2 files changed

Lines changed: 43 additions & 11 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# frozen_string_literal: true
2+
3+
class AddPrecisionToTimestamps < ActiveRecord::Migration[7.1]
4+
SKIP_TABLES = [:schema_migrations, :ar_internal_metadata].freeze
5+
6+
def up
7+
migrate_precision(precision: 6)
8+
end
9+
10+
def down
11+
migrate_precision(precision: nil)
12+
end
13+
14+
def migrate_precision(precision:)
15+
table_names = ActiveRecord::Base.connection.tables.map(&:to_sym)
16+
table_names.each do |table|
17+
next if SKIP_TABLES.include?(table)
18+
19+
ActiveRecord::Base.connection.columns(table).each do |column|
20+
next unless datetime_column?(column)
21+
22+
change_column(table, column.name, :datetime, precision:)
23+
end
24+
end
25+
end
26+
27+
private
28+
29+
def datetime_column?(column)
30+
column.sql_type_metadata.type == :datetime
31+
end
32+
end

db/schema.rb

Lines changed: 11 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)