Ruby on rails - Get data froma a remote DB without an API - ruby-on-rails

I'm trying to get data in a remote database from my app. I can do it with
ActiveRecord::Base.establish_connection({
'adapter'=>'postgresql',
'host'=> host_db,
'port'=> port,
'username'=> username_db,
'password'=> password_db,
'database'=> name_db
})
data = ActiveRecord::Base.connection.execute(tiraSQL)
ActiveRecord::Base.establish_connection ENV['DATABASE']
But after the last line ActiveRecord::Base.establish_connection ENV['DATABASE'] all my tables become empty.
Is there any way that after the line I mentioned above the previos data in my model persists in a test enviorement (rspec)?
or
i can get the data from a database in other host but whitout changing my current ActiveRecord::Base connection.
Thnx in advance guys

Rails only keeps one database connection open at a time on ActiveRecord like that... So when you establish the connection, you are completely overwriting the old connection (to your local database).
You'll probably want to save the old connection somewhere, and return it after you connect to the new database... Then store the new connection somewhere. eg
old_connection = ActiveRecord::Base.connection
new_connection = ActiveRecord::Base.establish_connection({
'adapter'=>'postgresql',
'host'=> host_db,
'port'=> port,
'username'=> username_db,
'password'=> password_db,
'database'=> name_db
})
data = new_connection.execute(tiraSQL)
ActiveRecord::Base.connection = old_connection
Note: I have not personally tested this code - you may well need to do something else to make it actually work.

Related

Ruby Sequel multiple database issues

I have a rails application (with puma) and few postgresql databases. As a DB driver gem Sequel is used. Here is the code for multiple DB connections when application starting:
require "sequel"
module MyApp
class Databases
class << self
attr_accessor :first_db, :second_db
def start_connections
#first_db = Sequel.connect(ENV.fetch("FIRST_DB_CREDENTIONALS"))
#second_db = Sequel.connect(ENV.fetch("SECOND_DB_CREDENTIONALS"))
end
def disconnect_all
first_db.disconnect
second_db.disconnect
end
end
end
end
MyApp::Databases.start_connections
But, from time to time, some requests (which fetches records from first or second DB) to application fails with error: "PG::ConnectionBad: PQconsumeInput() server closed the connection unexpectedly. This probably means the server terminated abnormally before or while processing the request: ..."
How to fix this error? Is there a problem with connection timeout settings or what?
After lookup at documentation, I found this moment and add following code to my puma config:
before_fork do
MyApp::Databases.disconnect_all
end
But problem still persists. Also tried to close connection manually on end of each request, but the error, mentioned above, raises from time to time.

typeorm SQLITE_BUSY:database is locked

I'm using multiple processes to write and read to an sqlite DB and am running into a busy error:Error: SQLITE_BUSY: database is locked.
Then I try to set the enableWAL parameter to true and the busyErrorRetry parameter to 300 milliseconds. Finally,When the problem occurs again,SQLITE_BUSY error will not be reported at this time,But the sqlite database seems to be locked.I don't know if retries are too frequent, which aggravates the problem.
I think the retry parameter busyErrorRetry is not a good behavior.I want to use the PRAGMA busy_timeout = parameter, but typeomr does not support it.

Tenanti "Undefined variable: Database"

I'm developing an application for multi tenant purpose, for this, I've used Orchestral/Tenanti but I'm experiencing the following problem.
I already performed some of the required steps for Multi Database Connection Setup.
Configuration Tenant Driver for Multiple Database: on my tenanti.php
Setting Default Database Connection: on my Tenant.php Middleware
Definition of my Tenants Connection: on my config/database.php
The problem becomes on the Database Connection Resolver step. In my Middleware I'm defining the connection to be used (I don't post the code because is not relevant for Tenanti use) which is correctly setted.
My resolver code is:
public function boot()
{
Tenanti::connection('tenants', function (User $entity, array $template) {
$template['database'] = "db_{$entity->getKey()}";
return $template;
});
}
This code is located on my AppServiceProvider. But my database to be used, is not correctly setted from my Resolver, throwing the following error:
ErrorException in MySqlConnector.php line 110:
Undefined variable: database
Clearly the error means that database is no setted.
Someone can help? Thanks in advance.
After many tests, I didn't find the solution using Tenanti, instead of that, I used Config class to set the database to use, and maintained my tenants configuration on database.php.
Config::set('database.connections.tenants.database', 'legal_'.$request->route()->parameter('account'));
Config::set('database.default', 'tenants');

DBHandle function always fails in PB12.net

I have migrated a project from PowerBuilder 12 classic to Powerbuilder 12.net using PFC. Even though in PB12 the connection to the database is successful that is not true for PB12.net.
I have been debugging for the problem and the DBHandle function in of_IsConnected of pfc_n_tr returns false. In PB12 classic this returns true. I have made a database profile which connects successfully though.
This is the code which checks for a successful connection:
if this.DBHandle() = 0 then
return false
else
return true
end if
I added connect using sqlca; to see the problem before the check but I got:
Transaction already connected in SQLErrText.
What might be the problem?
So the problem was in DB parameters connection for sql server 2008.
I changed the DBMS variable to "SNC SQL Native Client(OLE DB)" and added the Provider='SQLNCLI10' to the DBParm string.

What is net.rim.device.api.database.DatabaseIOException: File system error (12)

I don't know how to fix this...
URI dbURI = URI.create("file:///SDCard/Databases/MyDatabase.db");
Database database = DatabaseFactory.open(dbURI);
It created DB without any problem, throws exception while trying to open DB.
I figured it out. If I try to open a DB connection before closing the previous connection it throws this exception. So, after all DB operations I call database.close()
make new folder with application name in SDCard/Databases/ and put your database in it.
After that you have to just pass database name in URI.create().
I hope it will be helpful to your .:)

Resources