db-postgres icon indicating copy to clipboard operation
db-postgres copied to clipboard

Issue getting TIMESTAMP and TIMESTAMPTZ columns from database

Open cyclotron3k opened this issue 4 years ago • 0 comments

If Postgres returns a timestamp with microsecond precision (e.g. 2022-11-03 14:31:05.112156) or timezone information (1999-12-31 23:59:59+00) it causes the following lines in db-postgres-0.7.0/lib/db/postgres/native/types.rb:105 to blow up:

parts = string.split(/[\-\s:]/)
return Time.utc(*parts)

Note: Postgres will truncate trailing zeroes, so if you insert a timestamp with 0 milliseconds, Postgres will return a timestamp without a decimal point and this issue will not be triggered.

Full test case:

Sync do
	session = client.session

	session.clause("CREATE TABLE IF NOT EXISTS")
		.identifier(:users)
		.clause("(")
			.identifier(:a_timestamp).clause("TIMESTAMP NOT NULL")
		.clause(")")
		.call

	session.clause("INSERT INTO")
		.identifier(:users)
		.clause("VALUES (NOW())")
		.call

	result = session.clause("SELECT * FROM").identifier(:users).call

	puts "Result: #{result.to_a}"
	puts "Done"

rescue => e
	puts "#{e.class}: #{e.message}"
	puts e.backtrace
ensure
	session.close
end

Output:

ArgumentError: invalid value for Integer(): "05.112156"
/usr/local/bundle/gems/db-postgres-0.7.0/lib/db/postgres/native/types.rb:105:in `utc'
/usr/local/bundle/gems/db-postgres-0.7.0/lib/db/postgres/native/types.rb:105:in `parse'
/usr/local/bundle/gems/db-postgres-0.7.0/lib/db/postgres/native/result.rb:87:in `block in cast!'
/usr/local/bundle/gems/db-postgres-0.7.0/lib/db/postgres/native/result.rb:85:in `times'
/usr/local/bundle/gems/db-postgres-0.7.0/lib/db/postgres/native/result.rb:85:in `cast!'
/usr/local/bundle/gems/db-postgres-0.7.0/lib/db/postgres/native/result.rb:96:in `block in each'
/usr/local/bundle/gems/db-postgres-0.7.0/lib/db/postgres/native/result.rb:95:in `times'
/usr/local/bundle/gems/db-postgres-0.7.0/lib/db/postgres/native/result.rb:95:in `each'
/usr/local/bundle/gems/db-postgres-0.7.0/lib/db/postgres/native/result.rb:115:in `to_a'
test.rb:36:in `block in <main>'
/usr/local/bundle/gems/async-2.2.1/lib/async/task.rb:107:in `block in run'
/usr/local/bundle/gems/async-2.2.1/lib/async/task.rb:243:in `block in schedule'

async 2.2.1 db-postgres 0.7.0 Ruby 3.1 Postgres 13 Debian GNU/Linux 11

cyclotron3k avatar Nov 03 '22 15:11 cyclotron3k