PG::ConnectionBad: PQconsumeInput() SSL error: key values mismatch - ruby

When I open a certificate with OpenSSL::PKCS12, I lose the connection with my database and occur the error: PG::ConnectionBad: PQconsumeInput() SSL error: key values mismatch.
I'm doing this:
myuser#developer:~/myapp$ rails c
2.2.3 :001 > OpenSSL::PKCS12.new File.read('/local/to/mycert.pfx'), 'PASSWORD'
=> #<OpenSSL::PKCS12:0x000000072e6808 #key=#<OpenSSL::PKey::RSA:0x000000072e67e0>, #certificate=#<OpenSSL::X509::Certificate: subject=#<OpenSSL::X509::Name:0x000000072e6100>, issuer=#<OpenSSL::X509::Name:0x000000072e6128>, serial=#<OpenSSL::BN:0x000000072e6150>, not_before=2014-12-18 19:15:55 UTC, not_after=2015-12-18 19:15:55 UTC>,
#ca_certs=[#<OpenSSL::X509::Certificate: subject=#<OpenSSL::X509::Name:0x000000072e5c78>, issuer=#<OpenSSL::X509::Name:0x000000072e5ca0>, serial=#<OpenSSL::BN:0x000000072e5cc8>, not_before=2014-08-04 18:38:36 UTC, not_after=2021-10-11 18:38:36 UTC>, #<OpenSSL::X509::Certificate: subject=#<OpenSSL::X509::Name:0x000000072e5818>, issuer=#<OpenSSL::X509::Name:0x000000072e5840>, serial=#<OpenSSL::BN:0x000000072e5868>, not_before=2011-10-21 12:16:29 UTC, not_after=2021-10-21 12:16:29 UTC>, #<OpenSSL::X509::Certificate: subject=#<OpenSSL::X509::Name:0x000000072e52f0>, issuer=#<OpenSSL::X509::Name:0x000000072e5318>, serial=#<OpenSSL::BN:0x000000072e5340>, not_before=2010-06-21 19:04:57 UTC, not_after=2023-06-21 19:04:57 UTC>]>
After this I lose the connection:
2.2.3 :002 > Company.last
PG::ConnectionBad: PQconsumeInput() SSL error: key values mismatch
: SELECT tablename
FROM pg_tables
WHERE schemaname = ANY (current_schemas(false))
ActiveRecord::StatementInvalid: PG::ConnectionBad: PQconsumeInput() SSL error: key values mismatch
: SELECT tablename
FROM pg_tables
WHERE schemaname = ANY (current_schemas(false))
from /home/myuser/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/connection_adapters/postgresql/database_statements.rb:147:in `async_exec'
from /home/myuser/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/connection_adapters/postgresql/database_statements.rb:147:in `block in query'
from /home/myuser/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/connection_adapters/abstract_adapter.rb:473:in `block in log'
from /home/myuser/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.4/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
from /home/myuser/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/connection_adapters/abstract_adapter.rb:467:in `log'
from /home/myuser/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/connection_adapters/postgresql/database_statements.rb:146:in `query'
from /home/myuser/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/connection_adapters/postgresql/schema_statements.rb:91:in `tables'
from /home/myuser/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/connection_adapters/schema_cache.rb:90:in `prepare_tables'
from /home/myuser/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/connection_adapters/schema_cache.rb:22:in `table_exists?'
from /home/myuser/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/model_schema.rb:230:in `table_exists?'
from /home/myuser/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/attribute_methods/primary_key.rb:97:in `get_primary_key'
from /home/myuser/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/attribute_methods/primary_key.rb:85:in `reset_primary_key'
from /home/myuser/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/attribute_methods/primary_key.rb:73:in `primary_key'
from /home/myuser/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/attribute_methods/primary_key.rb:80:in `quoted_primary_key'
from /home/myuser/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/relation/delegation.rb:48:in `quoted_primary_key'
from /home/myuser/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/relation/query_methods.rb:1078:in `reverse_sql_order'
from /home/myuser/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/relation/query_methods.rb:852:in `reverse_order!'
from /home/myuser/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/relation/query_methods.rb:846:in `reverse_order'
from /home/myuser/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/relation/finder_methods.rb:511:in `find_last'
from /home/myuser/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/relation/finder_methods.rb:160:in `last'
from /home/myuser/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/querying.rb:3:in `last'
from (irb):2
from /home/myuser/.rvm/gems/ruby-2.2.3/gems/railties-4.2.4/lib/rails/commands/console.rb:110:in `start'
from /home/myuser/.rvm/gems/ruby-2.2.3/gems/railties-4.2.4/lib/rails/commands/console.rb:9:in `start'
from /home/myuser/.rvm/gems/ruby-2.2.3/gems/railties-4.2.4/lib/rails/commands/commands_tasks.rb:68:in `console'
from /home/myuser/.rvm/gems/ruby-2.2.3/gems/railties-4.2.4/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /home/myuser/.rvm/gems/ruby-2.2.3/gems/railties-4.2.4/lib/rails/commands.rb:17:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'2.2.3 :003 >
I perceive that this occurs when the method ca_certs is not null.
Ruby version: 2.2.3
Rails version: 4.2.4
gem 'pg', '0.18.3'
Has anyone had this problem?

I encountered this same problem today. After a long search I came across this thread. Seems like a bug in OpenSSL library/extension. Because opening a certificate and talking to a DB happen in a single thread, the connection gets screwed up. I still haven't figured out all details but a workaround is to open the certificate in it's own thread, like:
pkcs = nil
Thread.new { pkcs = OpenSSL::PKCS12.new File.read('/local/to/mycert.pfx'), 'PASSWORD' }.join
Hope that helps :)

Related

Rails 5 serializing a database column: NoMethodError: undefined method `[]=' for nil:NilClass

Can't seem to read a serialized column after upgrading to Rails 5.0 stable.
class PaymentNotification < ActiveRecord::Base
serialize :raw_IPN
...
end
a raw_IPN in the database looks like:
--- !ruby/hash:ActionController::Parameters
address_status: confirmed
subscr_date: 12:42:39 Dec 20, 2015 PST
payer_id: 123
address_street: 1 Main St
mc_amount1: '6.00'
mc_amount3: '12.00'
charset: windows-1252
address_zip: '123'
first_name: Test
reattempt: '1'
address_country_code: US
address_name: Test Buyer
notify_version: '3.8'
subscr_id: I-123123123
custom: '2'
payer_status: verified
business: payments-facilitator#test.com
address_country: United States
address_city: San Jose
verify_sign: 123-12312312321312.2JohI99Xp56cvu9zc
payer_email: payments-buyer#test.com
btn_id: '123'
last_name: Buyer
address_state: CA
receiver_email: test#email.com
recurring: '1'
txn_type: subscr_signup
item_name: asd
mc_currency: EUR
item_number: asd
residence_country: US
test_ipn: '1'
period1: 12 M
period3: 12 M
ipn_track_id: 111
controller: payment_notifications
action: create
The error: (testing in the console)
p = PaymentNotification.last
PaymentNotification Load (0.1ms) SELECT `payment_notifications`.* FROM `payment_notifications` ORDER BY
`payment_notifications`.`id` DESC LIMIT 1
NoMethodError: undefined method `[]=' for nil:NilClass
Stacktrace:
actionpack (5.0.0) lib/action_controller/metal/strong_parameters.rb:414:in `[]='
/home/frexuz/.rbenv/versions/2.3.1/lib/ruby/2.3.0/psych/visitors/to_ruby.rb:362:in `block in revive_hash'
/home/frexuz/.rbenv/versions/2.3.1/lib/ruby/2.3.0/psych/visitors/to_ruby.rb:336:in `revive_hash'
/home/frexuz/.rbenv/versions/2.3.1/lib/ruby/2.3.0/psych/visitors/to_ruby.rb:281:in `visit_Psych_Nodes_Mapping'
/home/frexuz/.rbenv/versions/2.3.1/lib/ruby/2.3.0/psych/visitors/visitor.rb:16:in `visit'
/home/frexuz/.rbenv/versions/2.3.1/lib/ruby/2.3.0/psych/visitors/visitor.rb:6:in `accept'
/home/frexuz/.rbenv/versions/2.3.1/lib/ruby/2.3.0/psych/visitors/to_ruby.rb:32:in `accept'
/home/frexuz/.rbenv/versions/2.3.1/lib/ruby/2.3.0/psych/visitors/to_ruby.rb:311:in `visit_Psych_Nodes_Document'
/home/frexuz/.rbenv/versions/2.3.1/lib/ruby/2.3.0/psych/visitors/visitor.rb:16:in `visit'
/home/frexuz/.rbenv/versions/2.3.1/lib/ruby/2.3.0/psych/visitors/visitor.rb:6:in `accept'
/home/frexuz/.rbenv/versions/2.3.1/lib/ruby/2.3.0/psych/visitors/to_ruby.rb:32:in `accept'
/home/frexuz/.rbenv/versions/2.3.1/lib/ruby/2.3.0/psych/nodes/node.rb:38:in `to_ruby'
/home/frexuz/.rbenv/versions/2.3.1/lib/ruby/2.3.0/psych.rb:253:in `load'
activerecord (5.0.0) lib/active_record/coders/yaml_column.rb:24:in `load'
activerecord (5.0.0) lib/active_record/type/serialized.rb:18:in `deserialize'
activerecord (5.0.0) lib/active_record/attribute.rb:140:in `type_cast'
activerecord (5.0.0) lib/active_record/attribute.rb:38:in `value'
activerecord (5.0.0) lib/active_record/attribute_set.rb:42:in `fetch_value'
activerecord (5.0.0) lib/active_record/attribute_methods/read.rb:66:in `_read_attribute'
activerecord (5.0.0) lib/active_record/attribute_methods/read.rb:36:in `__temp__271677f59405e4'
app/admin/payment_notifications.rb:17:in `block (3 levels) in <top (required)>'
This issue is fixed in the 5-0-stable branch.
Solution:
gem 'rails', github: "rails/rails", branch: '5-0-stable'
and bundle update rails until they release a new gem version.

Ruby and Https: A socket operation was attempted to an unreachable network

I'm trying to download all of my class notes from coursera. I figured that since I'm learning ruby this would be a good practice exercise, downloading all the PDFs they have for future use. Unfortunately though, I'm getting an exception saying ruby can't connect for some reason. Here is my code:
require 'net/http'
module Coursera
class Downloader
attr_accessor :page_url
attr_accessor :destination_directory
attr_accessor :cookie
def initialize(page_url,dest,cookie)
#page_url=page_url
#destination_directory = dest
#cookie=cookie
end
def download
puts #page_url
request = Net::HTTP::Get.new(#page_url)
puts #cookie.encoding
request['Cookie']=#cookie
# the line below is where the exception is thrown
res = Net::HTTP.start(#page_url.hostname, use_ssl=true,#page_url.port) {|http|
http.request(request)
}
html_page = res.body
pattern = /http[^\"]+\.pdf/
i=0
while (match = pattern.match(html_page,i)) != nil do
# 0 is the entire string.
url_string = match[0]
# make sure that 'i' is updated
i = match.begin(0)+1
# we want just the name of the file.
j = url_string.rindex("/")
filename = url_string[j+1..url_string.length]
destination = #destination_directory+"\\"+filename
# I want to download that resource to that file.
uri = URI(url_string)
res = Net::HTTP.get_response(uri)
# write that body to the file
f=File.new(destination,mode="w")
f.print(res.body)
end
end
end
end
page_url_string = 'https://class.coursera.org/datasci-002/lecture'
puts page_url_string.encoding
dest='C:\\Users\\michael\\training material\\data_science'
page_url=URI(page_url_string)
# I copied this from my browsers developer tools, I'm omitting it since
# it's long and has my session key in it
cookie="..."
downloader = Coursera::Downloader.new(page_url,dest,cookie)
downloader.download
At runtime the following is written to console:
Fast Debugger (ruby-debug-ide 0.4.22, debase 0.0.9) listens on 127.0.0.1:65485
UTF-8
https://class.coursera.org/datasci-002/lecture
UTF-8
Uncaught exception: A socket operation was attempted to an unreachable network. - connect(2)
C:/Ruby200-x64/lib/ruby/2.0.0/net/http.rb:878:in `initialize'
C:/Ruby200-x64/lib/ruby/2.0.0/net/http.rb:878:in `open'
C:/Ruby200-x64/lib/ruby/2.0.0/net/http.rb:878:in `block in connect'
C:/Ruby200-x64/lib/ruby/2.0.0/timeout.rb:52:in `timeout'
C:/Ruby200-x64/lib/ruby/2.0.0/net/http.rb:877:in `connect'
C:/Ruby200-x64/lib/ruby/2.0.0/net/http.rb:862:in `do_start'
C:/Ruby200-x64/lib/ruby/2.0.0/net/http.rb:851:in `start'
C:/Ruby200-x64/lib/ruby/2.0.0/net/http.rb:582:in `start'
C:/Users/michael/Documents/Aptana Studio 3 Workspace/practice/CourseraDownloader.rb:20:in `download'
C:/Users/michael/Documents/Aptana Studio 3 Workspace/practice/CourseraDownloader.rb:52:in `<top (required)>'
C:/Ruby200-x64/bin/rdebug-ide:23:in `load'
C:/Ruby200-x64/bin/rdebug-ide:23:in `<main>'
C:/Ruby200-x64/lib/ruby/2.0.0/net/http.rb:878:in `initialize': A socket operation was attempted to an unreachable network. - connect(2) (Errno::ENETUNREACH)
from C:/Ruby200-x64/lib/ruby/2.0.0/net/http.rb:878:in `open'
from C:/Ruby200-x64/lib/ruby/2.0.0/net/http.rb:878:in `block in connect'
from C:/Ruby200-x64/lib/ruby/2.0.0/timeout.rb:52:in `timeout'
from C:/Ruby200-x64/lib/ruby/2.0.0/net/http.rb:877:in `connect'
from C:/Ruby200-x64/lib/ruby/2.0.0/net/http.rb:862:in `do_start'
from C:/Ruby200-x64/lib/ruby/2.0.0/net/http.rb:851:in `start'
from C:/Ruby200-x64/lib/ruby/2.0.0/net/http.rb:582:in `start'
from C:/Users/michael/Documents/Aptana Studio 3 Workspace/practice/CourseraDownloader.rb:20:in `download'
from C:/Users/michael/Documents/Aptana Studio 3 Workspace/practice/CourseraDownloader.rb:52:in `<top (required)>'
from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/ruby-debug-ide-0.4.22/lib/ruby-debug-ide.rb:86:in `debug_load'
from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/ruby-debug-ide-0.4.22/lib/ruby-debug-ide.rb:86:in `debug_program'
from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/ruby-debug-ide-0.4.22/bin/rdebug-ide:110:in `<top (required)>'
from C:/Ruby200-x64/bin/rdebug-ide:23:in `load'
from C:/Ruby200-x64/bin/rdebug-ide:23:in `<main>'
I was following instructions here to write all the HTTP code. As far as I can see I'm following them ver-batim.
I'm using Windows 7, ruby 2.0.0p481, and Aptana Studio 3. When I copy the url into my browser it goes straight to the page without a problem. When I look at the request headers in my browser for that url, I don't see anything else I think I'm missing. I also tried setting the Host and Referer request headers, it made no difference.
I am out of ideas, and have already searched Stack Overflow for similar questions but that didn't help. Please let me know what I'm missing.
So, I had this same error message with a different project and the problem was that my machine literally couldn't connect to the IP / Port. Have you tried connecting with curl? If it works in your browser, it could be using a proxy or something to actually get there. Testing the URL with curl solved the problem for me.

Error while upgrading from Rails 3.1 to Rails 3.2

Upgrade steps were performed as mentioned in http://guides.rubyonrails.org/3_2_release_notes.html
Right now I'm on Ruby 1.8.7 and Rails 3.1.x (using system ruby). I installed RVM with ruby 1.8.7 and added to rails 3.2 and then I get the following error:
[app]$ rails console
Faraday: you may want to install system_timer for reliable timeouts
$HOME/src/qbol/tapp/config/environment.rb:16:in `add': undefined method `>' for nil:NilClass (NoMethodError)
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/activesupport-3.2.17/lib/active_support/tagged_logging.rb:55:in `add'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/activesupport-3.2.17/lib/active_support/tagged_logging.rb:61:in `info'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/activerecord-3.2.17/lib/active_record/railtie.rb:86
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/activesupport-3.2.17/lib/active_support/lazy_load_hooks.rb:36:in `instance_eval'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/activesupport-3.2.17/lib/active_support/lazy_load_hooks.rb:36:in `execute_hook'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/activesupport-3.2.17/lib/active_support/lazy_load_hooks.rb:26:in `on_load'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/activesupport-3.2.17/lib/active_support/lazy_load_hooks.rb:25:in `each'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/activesupport-3.2.17/lib/active_support/lazy_load_hooks.rb:25:in `on_load'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/activerecord-3.2.17/lib/active_record/railtie.rb:80
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/railties-3.2.17/lib/rails/initializable.rb:30:in `instance_exec'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/railties-3.2.17/lib/rails/initializable.rb:30:in `run'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/railties-3.2.17/lib/rails/initializable.rb:55:in `run_initializers'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/railties-3.2.17/lib/rails/initializable.rb:54:in `each'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/railties-3.2.17/lib/rails/initializable.rb:54:in `run_initializers'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/railties-3.2.17/lib/rails/application.rb:136:in `initialize!'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/railties-3.2.17/lib/rails/railtie/configurable.rb:30:in `send'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/railties-3.2.17/lib/rails/railtie/configurable.rb:30:in `method_missing'
from $HOME/src/app/config/environment.rb:48
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/activesupport-3.2.17/lib/active_support/dependencies.rb:251:in `require'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/activesupport-3.2.17/lib/active_support/dependencies.rb:251:in `require'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/activesupport-3.2.17/lib/active_support/dependencies.rb:236:in `load_dependency'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/activesupport-3.2.17/lib/active_support/dependencies.rb:251:in `require'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/railties-3.2.17/lib/rails/application.rb:103:in `require_environment!'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/railties-3.2.17/lib/rails/commands.rb:40
from script/rails:6:in `require'
from script/rails:6
This is on my development box. Any idea why this is happening?
EDIT: Below is the environment.rb. The error is happening on the last line App::Application.initialize!
# Load the rails application
require File.expand_path('../application', __FILE__)
module ActiveSupport
class BufferedLogger
def self.current_user
Thread.current[:user]
end
def self.current_user=(user)
Thread.current[:user] = user
end
def add(severity, message = nil, progname = nil, &block)
return if #level > severity
message = (message || (block && block.call) || progname).to_s
level = {
0 => "DEBUG",
1 => "INFO ",
2 => "WARN ",
3 => "ERROR",
4 => "FATAL"
}[severity] || "UNKNOWN"
user=BufferedLogger.current_user
if(!user.nil?)
idstr = "uid:#{user.id}"
if !user.current_app_user.nil?
idstr.concat(", acid: #{user.current_app_user.account_id}")
end
else
idstr=""
end
message = "[%s: %s #{idstr}] %s" %
["#{level} pid: #{$$}", Time.now.strftime("%y-%m-%d %H:%M:%S"), message]
message = "#{message}\n" unless message[-1] == ?\n
buffer << message
auto_flush
message
end
end
end
# Initialize the rails application
App::Application.initialize!
The error is happening inside ActiveSupport::BufferedLogger#add method call and you appear to be monkey-patching this class. The internals of ActiveSupport::BufferedLogger have likely changing between the versions of Rails you're using.
Try removing all of the ActiveSupport::BufferedLogger code from your environment.rb to work past this error and get your app running. Then, if you still need the monkeypatch (don't know why), you'd have to rewrite it on top of the newer version of the class.

Connect to existing Postgresql database with ActiveRecord without rails

I am not using rails, and I'm trying to use ActiveRecord to make it easier to work with an existing database. This is just a script so I have no database.yml or any other files. I've set up my database connection using
ActiveRecord::Base.establish_connection(
adapter: 'postgresql',
host: 'thehosthere',
database: 'management',
username: 'management_readonly',
password: '',
port: '5432'
)
I'm not really familiar with databases, so I'll just state what I know about this one. There are quite a few schemas. The schema I'm interested in is the management schema that has the table "host". I've created a class Host in the script like this:
class Host < ActiveRecord::Base
self.table_name = "host"
end
I'm then trying to pull all rows that match the criteria with this query and to store it into an array.
servers = Host.where(:realm => 'stage', :status => 'UP').pluck(:hostname)
but I am getting this error every single time.
ruby environments_are_okDev.rb
/usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql_adapter.rb:768:in `exec': PG::UndefinedTable: ERROR: relation "host" does not exist (ActiveRecord::StatementInvalid)
LINE 5: WHERE a.attrelid = '"host"'::regclass
^
: SELECT a.attname, format_type(a.atttypid, a.atttypmod),
pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid,
a.atttypmod
FROM pg_attribute a LEFT JOIN pg_attrdef d
ON a.attrelid = d.adrelid AND a.attnum = d.adnum
WHERE a.attrelid = '"host"'::regclass
AND a.attnum > 0 AND NOT a.attisdropped
ORDER BY a.attnum
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql_adapter.rb:768:in `exec_no_cache'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql/database_statements.rb:138:in `block in exec_query'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract_adapter.rb:425:in `block in log'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activesupport-4.0.0/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract_adapter.rb:420:in `log'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql/database_statements.rb:137:in `exec_query'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql_adapter.rb:915:in `column_definitions'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql/schema_statements.rb:174:in `columns'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/connection_adapters/schema_cache.rb:114:in `block in prepare_default_proc'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/connection_adapters/schema_cache.rb:56:in `yield'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/connection_adapters/schema_cache.rb:56:in `default'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/connection_adapters/schema_cache.rb:56:in `columns'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/model_schema.rb:208:in `columns'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/model_schema.rb:247:in `column_names'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/relation/delegation.rb:60:in `block in method_missing'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/relation.rb:270:in `scoping'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/relation/delegation.rb:60:in `method_missing'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/relation/calculations.rb:152:in `block in pluck'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/relation/calculations.rb:151:in `map!'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/relation/calculations.rb:151:in `pluck'
from environments_are_okDev.rb:51:in `run'
from environments_are_okDev.rb:97:in `<main>'
I don't really understand why this is happening, so I tried to see what the database looks like with this command ActiveRecord::Base.connection.tables but all I got was an empty array. I'm not sure what could be happening. I'm obviously doing something wrong, I just can't figure out what. Keep in mind, this is all in a single file script.
Edit:
So I can see the schemas if I use ActiveRecord::Base.connection.schema_names. This gives me an array => ["db_stats", "management", "management_audit", "public"] Using pgadmin, I know that the table I want is in management, so how do I access that?
Try adding schema_search_path when you call establish_connection:
ActiveRecord::Base.establish_connection(
adapter: 'postgresql',
host: 'thehosthere',
database: 'management',
username: 'management_readonly',
password: '',
port: 5432,
schema_search_path: 'management' # <-- ???
)

Sequel gem: Handling invalid date values gracefully

I'm a ruby noob and I'm trying to process some blog posts using Sequel and the data_objects adapter:
DB = Sequel.connect('do:mysql://user:pass#localhost/database')
db[posts_query].each do |post|
puts post
end
But I get Sequel::InvalidValue exception, complaining about the date column:
/usr/lib/ruby/1.9.1/time.rb:202:in `local': ArgumentError: argument out of range (Sequel::InvalidValue)
from /usr/lib/ruby/1.9.1/time.rb:202:in `make_time'
from /usr/lib/ruby/1.9.1/time.rb:271:in `parse'
from /var/lib/gems/1.9.1/gems/sequel-3.42.0/lib/sequel/core.rb:295:in `string_to_datetime'
I tried to catch the exception:
begin
db[posts_query].each do |post|
puts post
end
rescue Sequel::InvalidValue => e
puts e.inspect
end
but that doesn't help much.
How can I find out which row has the incorrect value?
Also, is there a way to do this iteration, such that I can catch the exception but continue to loop over the remaining rows?
Update:
I switched to the mysql2 adapter and now I can at least see the invalid date:
/var/lib/gems/1.9.1/gems/sequel-3.42.0/lib/sequel/adapters/mysql2.rb:154:in `each': Mysql2::Error: Invalid date: 2008-04-00 00:00:15 (Sequel::DatabaseError)
from /var/lib/gems/1.9.1/gems/sequel-3.42.0/lib/sequel/adapters/mysql2.rb:154:in `block in fetch_rows'
from /var/lib/gems/1.9.1/gems/sequel-3.42.0/lib/sequel/adapters/mysql2.rb:89:in `_execute'
from /var/lib/gems/1.9.1/gems/sequel-3.42.0/lib/sequel/adapters/shared/mysql_prepared_statements.rb:34:in `block in execute'
from /var/lib/gems/1.9.1/gems/sequel-3.42.0/lib/sequel/database/connecting.rb:236:in `block in synchronize'
from /var/lib/gems/1.9.1/gems/sequel-3.42.0/lib/sequel/connection_pool/threaded.rb:104:in `hold'
from /var/lib/gems/1.9.1/gems/sequel-3.42.0/lib/sequel/database/connecting.rb:236:in `synchronize'
from /var/lib/gems/1.9.1/gems/sequel-3.42.0/lib/sequel/adapters/shared/mysql_prepared_statements.rb:34:in `execute'
from /var/lib/gems/1.9.1/gems/sequel-3.42.0/lib/sequel/dataset/actions.rb:778:in `execute'
from /var/lib/gems/1.9.1/gems/sequel-3.42.0/lib/sequel/adapters/mysql2.rb:171:in `execute'
from /var/lib/gems/1.9.1/gems/sequel-3.42.0/lib/sequel/adapters/mysql2.rb:140:in `fetch_rows'
from /var/lib/gems/1.9.1/gems/sequel-3.42.0/lib/sequel/dataset/actions.rb:154:in `each'
from wordpress_importer.rb:112:in `process'
from wordpress_importer.rb:308:in `<main>'
Can you post more of the backtrace? You need to see what is calling string_to_datetime.
Also, I would recommend against using the do/mysql adapter unless you have specific needs that require it. Use the mysql or mysql2 adapter instead. If the error is being caused by bogus datetimes in your MySQL database, then you may want to use the mysql2 adapter or use the mysql adapter and set DB.convert_invalid_date_time = nil.

Resources