Skip to content

Commit

Permalink
published 0.5.15 - fix datetime precision #95
Browse files Browse the repository at this point in the history
  • Loading branch information
PNixx committed Jul 21, 2023
1 parent 2d2e8a3 commit c09f082
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ class DateTime < Type::DateTime # :nodoc:
def serialize(value)
value = super
return unless value
return value.strftime('%Y-%m-%d %H:%M:%S') unless value.acts_like?(:time)

value.to_time.strftime('%Y-%m-%d %H:%M:%S')
value.strftime('%Y-%m-%d %H:%M:%S' + (@precision.present? && @precision > 0 ? ".%#{@precision}N" : ''))
end

def type_cast_from_database(value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def initialize_type_map(m) # :nodoc:
super
register_class_with_limit m, %r(String), Type::String
register_class_with_limit m, 'Date', Clickhouse::OID::Date
register_class_with_limit m, 'DateTime', Clickhouse::OID::DateTime
register_class_with_precision m, %r(datetime)i, Clickhouse::OID::DateTime

register_class_with_limit m, %r(Int8), Type::Integer
register_class_with_limit m, %r(Int16), Type::Integer
Expand Down
2 changes: 1 addition & 1 deletion lib/clickhouse-activerecord/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module ClickhouseActiverecord
VERSION = '0.5.14'
VERSION = '0.5.15'
end
26 changes: 26 additions & 0 deletions spec/cases/model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,32 @@
end
end

context 'DateTime64' do

let!(:model) do
Class.new(ActiveRecord::Base) do
self.table_name = 'some'
end
end

before do
migrations_dir = File.join(FIXTURES_PATH, 'migrations', 'dsl_table_with_datetime_creation')
quietly { ClickhouseActiverecord::MigrationContext.new(migrations_dir, model.connection.schema_migration).up }
end

describe '#create' do
it 'create a new record' do
time = DateTime.parse('2023-07-21 08:00:00.123')
model.create!(datetime: time, datetime64: time)
row = model.first
expect(row.datetime).to_not eq(row.datetime64)
expect(row.datetime.strftime('%Y-%m-%d %H:%M:%S')).to eq('2023-07-21 08:00:00')
expect(row.datetime64.strftime('%Y-%m-%d %H:%M:%S.%3N')).to eq('2023-07-21 08:00:00.123')
end
end

end

context 'array' do

let!(:model) do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class CreateSomeTable < ActiveRecord::Migration[5.0]
def up
create_table :some, id: false do |t|
create_table :some, id: false, force: true do |t|
t.datetime :datetime, null: false
t.datetime :datetime64, precision: 3, null: true
end
Expand Down

0 comments on commit c09f082

Please sign in to comment.