Connect to existing Postgresql database with ActiveRecord without rails - ruby

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' # <-- ???
)

Related

ruby-oci8: ORA-12514: TNS:listener does not currently know of service requested in connect descriptor

I tried to connect to Oracle DB with the latest oracle_enhanced, ruby-oci8 gems on a Windows 7 x64 PC and it failed as follows:
$ ruby oracle_db_connection.rb
oci8.c:654:in oci8lib_230.so: ORA-12514: TNS:listener does not currently know of service requested in connect descriptor (OCIError)
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/ruby-oci8-2.2.2-x64-mingw32/lib/oci8/oci8.rb:142:in `initialize'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-oracle_enhanced-adapter-1.6.7/lib/active_record/connection_adapters/oracle_enhanced/oci_connection.rb:333:in `new'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-oracle_enhanced-adapter-1.6.7/lib/active_record/connection_adapters/oracle_enhanced/oci_connection.rb:333:in `new_connection'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-oracle_enhanced-adapter-1.6.7/lib/active_record/connection_adapters/oracle_enhanced/oci_connection.rb:393:in `initialize'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-oracle_enhanced-adapter-1.6.7/lib/active_record/connection_adapters/oracle_enhanced/oci_connection.rb:26:in `new'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-oracle_enhanced-adapter-1.6.7/lib/active_record/connection_adapters/oracle_enhanced/oci_connection.rb:26:in `initialize'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-oracle_enhanced-adapter-1.6.7/lib/active_record/connection_adapters/oracle_enhanced/connection.rb:9:in `new'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-oracle_enhanced-adapter-1.6.7/lib/active_record/connection_adapters/oracle_enhanced/connection.rb:9:in `create'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-oracle_enhanced-adapter-1.6.7/lib/active_record/connection_adapters/oracle_enhanced_adapter.rb:156:in `oracle_enhanced_connection'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:438:in `new_connection'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:448:in `checkout_new_connection'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:422:in `acquire_connection'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:349:in `block in checkout'
from C:/Ruby23-x64/lib/ruby/2.3.0/monitor.rb:214:in `mon_synchronize'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:348:in `checkout'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:263:in `block in connection'
from C:/Ruby23-x64/lib/ruby/2.3.0/monitor.rb:214:in `mon_synchronize'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:262:in `connection'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:571:in `retrieve_connection'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_handling.rb:113:in `retrieve_connection'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_handling.rb:87:in `connection'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/model_schema.rb:230:in `table_exists?'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/attribute_methods/primary_key.rb:97:in `get_primary_key'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/attribute_methods/primary_key.rb:85:in `reset_primary_key'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/attribute_methods/primary_key.rb:73:in `primary_key'
from C:in `primary_key'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/relation/finder_methods.rb:493:in `find_nth_with_limit'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/relation/finder_methods.rb:484:in `find_nth'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/relation/finder_methods.rb:127:in `first'
from C:in `first'
from oracle_db_connection.rb:30:in `<main>'
I installed Oracle instant client by downloading and unzipped it to
**C:\Program Files\Oracle\instantclient_12_1**
I added the environment variable TNS_ADMIN pointing to C:\Program Files\Oracle\instantclient_12_1.
I added TNS_ADMIN to the Path.
When running the simple ruby script:
require 'active_record'
ActiveRecord::Base.establish_connection(
adapter: 'oracle_enhanced',
database: 'my_db_sid',
host: 'my_db_host',
port: 1531,
username: 'username',
password: 'password'
)
class Customer < ActiveRecord::Base
self.table_name = 'CUSTOMER'
end
puts "First: #{Customer.first}"
If I remove the call to the Customer table, I can inspect connection object:
connection = ActiveRecord::Base.establish_connection(
adapter: 'oracle_enhanced',
database: 'my_db_sid',
host: 'my_db_host',
port: 1531,
username: 'username',
password: 'password'
)
puts connection.inspect
#class Customer < ActiveRecord::Base
# self.table_name = 'CUSTOMER'
#end
#
#puts "First: #{Customer.first}"
Any idea on what is wrong here ? The script worked previousely fine on other PCs but with other DBs and environement. So the used settings should be fine.
Thank you.
Environment:
Windows 7 x 64 bits Ruby 2.3
activerecord-oracle_enhanced-adapter (1.6.7)
ruby-oci8 (2.2.2 x64-mingw32)
You need to use some different format of database section. Here's my config:
adapter: "oracle_enhanced"
username: "db_user"
database: "//my.host/my.sid"
password: "passw0rd"
I figured out how to connect to one of 2 databases. They have different settings, at least in Sqldeveloper v 4.1.3.20.
Here are the settings that worked for me (please note, it is out of Rails context, just a ruby file):
require 'active_record'
ActiveRecord::Base.establish_connection(
adapter: 'oracle_enhanced',
host: 'as specified in the field Host name of SQL developer',
port: 'as specified in the field Port of SQL developer',
database: 'as specified in the fiels Service name of SQL developer',
username: 'username',
password: 'userpassword'
)
class Department < ActiveRecord::Base
self.table_name = 'DEPARTMENTS'
end
puts "First found department: #{Department.first.inspect}"
The second database has different settings in SQL developer:
- host: some_host_url
- port: some port number
- SID: some SID value
The last soltion was to add the following settings:
HOST = "your host"
PORT = XXXX #your port number
SID = 'your SID'
ActiveRecord::Base.establish_connection(
adapter: 'oracle_enhanced',
database: "(DESCRIPTION=
(ADDRESS_LIST=(ADDRESS=(PROTOCOL=tcp)(HOST=#{HOST})(PORT=#{PORT})))
(CONNECT_DATA=(SID=#{SID}))
)",
username: 'user',
password: 'secret'
)
Hope this helps.

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

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 :)

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.

Trying to use multiple databases - getting ActiveRecord Connection Not Established

I'm trying to set up ActiveRecord so I can use multiple database connections in my application. I am not using Rails.
To do this, I set up an abstract class for each of my databases:
class BuoyDatabase < ActiveRecord::Base
self.abstract_class = true
establish_connection $database_config['buoy_database']
end
Then I inherit from the OneDatabase class:
class Buoy < BuoyDatabase
has_many :buoyDatas, :foreign_key => 'buoy_id'
end
class BuoyData < BuoyDatabase
belongs_to :buoy
end
I can successfully create and read instances of both Buoy and BuoyData, but I can't get any related records:
Buoy.find_by_id(...).buoyDatas # gives ActiveRecord::ConnectionNotEstablished
Details of the error:
ActiveRecord::ConnectionNotEstablished: ActiveRecord::ConnectionNotEstablished
from /Users/admin/.rvm/rubies/ruby-1.9.3-head/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:409:in `retrieve_connection'
from /Users/admin/.rvm/rubies/ruby-1.9.3-head/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/connection_specification.rb:107:in `retrieve_connection'
from /Users/admin/.rvm/rubies/ruby-1.9.3-head/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/connection_specification.rb:89:in `connection'
from /Users/admin/.rvm/rubies/ruby-1.9.3-head/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/associations/alias_tracker.rb:69:in `connection'
from /Users/admin/.rvm/rubies/ruby-1.9.3-head/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/associations/alias_tracker.rb:54:in `initial_count_for'
from /Users/admin/.rvm/rubies/ruby-1.9.3-head/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/associations/alias_tracker.rb:12:in `block in initialize'
from /Users/admin/.rvm/rubies/ruby-1.9.3-head/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/associations/alias_tracker.rb:29:in `yield'
from /Users/admin/.rvm/rubies/ruby-1.9.3-head/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/associations/alias_tracker.rb:29:in `default'
from /Users/admin/.rvm/rubies/ruby-1.9.3-head/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/associations/alias_tracker.rb:29:in `aliased_name_for'
from /Users/admin/.rvm/rubies/ruby-1.9.3-head/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/associations/alias_tracker.rb:17:in `aliased_table_for'
from /Users/admin/.rvm/rubies/ruby-1.9.3-head/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/associations/join_helper.rb:15:in `block in construct_tables'
from /Users/admin/.rvm/rubies/ruby-1.9.3-head/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/associations/join_helper.rb:14:in `each'
from /Users/admin/.rvm/rubies/ruby-1.9.3-head/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/associations/join_helper.rb:14:in `construct_tables'
from /Users/admin/.rvm/rubies/ruby-1.9.3-head/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/associations/association_scope.rb:53:in `add_constraints'
from /Users/admin/.rvm/rubies/ruby-1.9.3-head/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/associations/association_scope.rb:33:in `scope'
from /Users/admin/.rvm/rubies/ruby-1.9.3-head/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/associations/association.rb:99:in `association_scope'
from /Users/admin/.rvm/rubies/ruby-1.9.3-head/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/associations/association.rb:88:in `scoped'
from /Users/admin/.rvm/rubies/ruby-1.9.3-head/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/associations/collection_association.rb:367:in `find_target'
from /Users/admin/.rvm/rubies/ruby-1.9.3-head/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/associations/collection_association.rb:324:in `load_target'
from /Users/admin/.rvm/rubies/ruby-1.9.3-head/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/associations/collection_proxy.rb:51:in `load_target'
from /Users/admin/.rvm/rubies/ruby-1.9.3-head/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/associations/collection_proxy.rb:89:in `method_missing'
The workaround is to establish a connection on ActiveRecord::Base itself. It doesn't matter to which database the connection is made. This connection is only needed so that ActiveRecord can get the meta information necessary to handle associations and whatnot. You need not use the connection at all, once established.
This issue is a duplicate of ActiveRecord 3.1.0 multiple databases and the workaround is first described here: https://stackoverflow.com/a/7406374/29729

(Ruby) ActiveRecord with PG try to connect with local user to remote server

I made a simple-coding-ruby-program for get backups from remote server, and I made a db in postgresql for save information and schedule backups.
The connect with DB was done with ActiveRecord, I configured for access a internal DB (in other server), but when I try to connect to remote db, i get this message:
/usr/lib/ruby/1.8/active_record/connection_adapters/postgresql_adapter.rb:968:in `initialize': FATAL: no existe el rol <<mi_user_name>> (PGError)
from /usr/lib/ruby/1.8/active_record/connection_adapters/postgresql_adapter.rb:968:in `connect'
from /usr/lib/ruby/1.8/active_record/connection_adapters/postgresql_adapter.rb:968:in `connect'
from /usr/lib/ruby/1.8/active_record/connection_adapters/postgresql_adapter.rb:217:in `initialize'
from /usr/lib/ruby/1.8/active_record/connection_adapters/postgresql_adapter.rb:37:in `new'
from /usr/lib/ruby/1.8/active_record/connection_adapters/postgresql_adapter.rb:37:in `postgresql_connection'
from /usr/lib/ruby/1.8/active_record/connection_adapters/abstract/connection_pool.rb:223:in `send'
from /usr/lib/ruby/1.8/active_record/connection_adapters/abstract/connection_pool.rb:223:in `new_connection'
from /usr/lib/ruby/1.8/active_record/connection_adapters/abstract/connection_pool.rb:245:in `checkout_new_connection'
from /usr/lib/ruby/1.8/active_record/connection_adapters/abstract/connection_pool.rb:188:in `checkout'
from /usr/lib/ruby/1.8/active_record/connection_adapters/abstract/connection_pool.rb:184:in `loop'
from /usr/lib/ruby/1.8/active_record/connection_adapters/abstract/connection_pool.rb:184:in `checkout'
from /usr/lib/ruby/1.8/monitor.rb:242:in `synchronize'
from /usr/lib/ruby/1.8/active_record/connection_adapters/abstract/connection_pool.rb:183:in `checkout'
from /usr/lib/ruby/1.8/active_record/connection_adapters/abstract/connection_pool.rb:98:in `connection'
from /usr/lib/ruby/1.8/active_record/connection_adapters/abstract/connection_pool.rb:326:in `retrieve_connection'
from /usr/lib/ruby/1.8/active_record/connection_adapters/abstract/connection_specification.rb:123:in `retrieve_connection'
from /usr/lib/ruby/1.8/active_record/connection_adapters/abstract/connection_specification.rb:115:in `connection'
from /usr/lib/ruby/1.8/active_record/base.rb:3113:in `quoted_table_name'
from /usr/lib/ruby/1.8/active_record/base.rb:1684:in `construct_finder_sql'
from /usr/lib/ruby/1.8/active_record/base.rb:1548:in `find_every'
from /usr/lib/ruby/1.8/active_record/base.rb:1505:in `find_initial'
from /usr/lib/ruby/1.8/active_record/base.rb:613:in `find'
from main.rb:84:in `main'
from main.rb:126
PG try to connect to remote db with my local user, in the declaration of ActiveRecord I set the parameters:
require 'active_record'
ActiveRecord::Base.establish_connection(
:adapter => "postgresql",
:host => "xxx.xxx.x.101",
:port => 5432,
:database => "VB_DB",
:user => "pg_user",
:password => "blahblah"
)
I working with Ruby 1.8.7.
Any idea about this?
thank for reading, and help me.
Grettings.
EDIT
I found the solution:
ActiveRecord::Base.establish_connection(
:adapter => "postgresql",
:host => "xxx.xxx.x.101",
:port => 5432,
:database => "VB_DB",
:username => "pg_user",
:password => "blahblah"
)
I used :user and must be :username.
Shame on me.
From the error message, it appears that Postgres doesn't think your user exists in the database.
A good first step to troubleshoot this is to try connecting without involving Ruby at all. Since the 'psql' command-line client uses the same underlying library (assuming you're using the 'pg' gem to connect), in general if you can connect using that, you should be okay to connect from Ruby as well.
Try this:
psql -h xxx.xxx.x.101 -U pg_user -W -l
If this shows you the same error (no existe el rol <<mi_user_name>>), then you'll need to confirm that your user exists, and create it.
If it shows you a list of databases, we'll have to try something else.

Resources