Connecting Ruby on Rails to an External Database - sql-server

I have an MSSQL external DB and I need to connect to it.
I made a new entry in the database.yml
external_database:
adapter: sqlserver
host: xxx.xxx.xxx.xxx
database: Trader
username: name
password: pw
timeout : 5000
and made a model file in app/models/database.rb
class Database < ActiveRecord::Base
establish_connection(:external_database)
self.table_name = 'dbo.Trade'
end
What models/controllers do I need to access something in my database? I have no idea even if I'm connected properly to the database.
There is this case where a guy uses PHP, but this seems to eliminate the point of Ruby.
This link just shows errors after completion in the view where you do the actual value retrieving.

Related

Connecting Jmeter to an Oracle database with two hosts and service name

I am trying to connect Jmeter to a geo redundant database with two hosts and I am struggling with finding the right Database Url format.
This is how my connection string looks like:
jdbc:oracle:thin:#(DESCRIPTION=(ENABLE=BROKEN)(FAILOVER=on)(CONNECT_TIMEOUT=5sec)(TRANSPORT_CONNECT_TIMEOUT=3sec)(RETRY_COUNT=3)(LOAD_BALANCE=on)(ADDRESS_LIST=(LOAD_BALANCE=on)(ADDRESS=(PROTOCOL=TCP)(HOST=HostName)(PORT=port)))(ADDRESS_LIST=(LOAD_BALANCE=on)(ADDRESS=(PROTOCOL=TCP)(HOST=HostName2)(PORT=port)))(CONNECT_DATA=(SERVICE_NAME=ServiceName)))
Database Connection Configuration is as following:
JDBC Driver Class: oracle.jdbc.OracleDriver Username: username
Password: password
For the Database URL I tried different formats and I keep getting the error:
Cannot load JDBC driver class 'oracle.jdbc.OracleDriver'
Note that the ojdbc.jar file is in the /lib folder as per the Jmeter documentation. Also, the ports are the same for both hosts.
Any suggestion is welcome. :)
I don't think you will be able to establish the connection to Oracle RAC using JMeter's JDBC Connection Configuration as it doesn't allow full flexibility therefore you will not be able to properly instantiate the PoolDataSourceFactory
So I would recommend switching to JSR223 Test Elements and Groovy language where you will have the full freedom when it comes to setting up the connection, executing queries, accessing results, etc. The relevant code would be something like:
def prop = new Properties()
prop.put('oracle.jdbc.thinForceDNSLoadBalancing','true')
PoolDataSource pds = PoolDataSourceFactory.getPoolDataSource()
pds.setConnectionProperties(prop)
pds.setConnectionFactoryClassName('oracle.jdbc.pool.OracleDataSource'); pds.setUser('johndoe')
pds.setPassword('secret')
String dbURL =
'jdbc:oracle:thin:#(DESCRIPTION=(ENABLE=BROKEN)(FAILOVER=on)(CONNECT_TIMEOUT=5sec)' +
'(TRANSPORT_CONNECT_TIMEOUT=3sec)(RETRY_COUNT=3)(LOAD_BALANCE=on)(ADDRESS_LIST=(LOAD_BALANCE=on)' +
'(ADDRESS=(PROTOCOL=TCP)(HOST=HostName)(PORT=port)))(ADDRESS_LIST=(LOAD_BALANCE=on)' +
'(ADDRESS=(PROTOCOL=TCP)(HOST=HostName2)(PORT=port)))(CONNECT_DATA=(SERVICE_NAME=ServiceName)))'
pds.setURL(dbURL)
More information: Configuring Fast Connection Failover for JDBC Clients
It appears to be working with a connection string containing only host 1.
The Database URL is in the form:
jdbc:oracle:thin:#<hostname>:<port>/<serviceName>
Additionally, I got the error because the .jar file's path was not added to the classpath (click on Test Plan, in the bottom select browse next to Add directory or jar to classpath and select your odbc jar).
Another thing that was wrong was the Validation query, it should be "select 1 from dual" and also the query should not contain any semicolon at the end.
I hope this help people with the same issue.

Why i can not able to access my rails application db after establishing new connection to wordpress db manually?

I have a rails 4.2 application with postgres database which is running. I have requirement to connect with other databases (word_press_sites) on demand basis.
I have tried the below code this into rails console:
ActiveRecord::Base.establish_connection(
adapter: "mysql2",
host: ENV["HOST"],
username: ENV["USERNAME"],
password: ENV["PASSWORD"],
database: ENV["DB_NAME"]
)
above code results,
No error, connected to the word_press database_tables.
1) If am trying to access the table by calling User.all in my rails application db i can not able to access which is throwing an error.
After opening the rails console you can check
ActiveRecord::Base.connection.current_database
This will give the current db info,Then try the script you have given
ActiveRecord::Base.establish_connection(
adapter: "mysql2",
host: ENV["HOST"],
username: ENV["USERNAME"],
password: ENV["PASSWORD"],
database: ENV["DB_NAME"]
)
Now try the same command
ActiveRecord::Base.connection.current_database
This will give the newly connected ActiveRecord connection
Which means Active record is shifted into newly connected wordpress database so that you can not able to access the table in your rails application database
If you want to revert back to your rails app db you can do this
ActiveRecord::Base.remove_connection( ActiveRecord::Base)
Let me know if you need more information

What Registration Properties are needed to get Database Workbench up and running?

I am a Database Workbench fan from way back, but bizarrely have not used it for quite awhile.
I downloaded a trial version and am trying to "Register Server" as a first step.
The problem is I don't know what to use for which properties. I need to know:
Alias
Host
Instance
With "Use SQL Server Authentication" checked:
Username
Password
I've tried to guess my way through what is needed where, but nothing has worked.
I can connect to the database in (C#) code with this connection string:
"SERVER=PlatypusSQL42;DATABASE=duckbilldata;UID=youinnocentdog;PWD=contrasena;Connection Timeout=0";
And so I have tried these values:
Alias: DBWBSQLServer
Host: PlatypusSQL42
Instance: duckbilldata
Username: youinnocentdog
Password:contrasena
..and this:
Alias: PlatypusSQL42
Host: duckbilldata
Instance:
Username: youinnocentdog
Password:contrasena
...but with both of them, I get, "[DBNETLIB][ConnectionOpen (Connect()).]Specified SQL server not found."
What values are needed?
The instance is not the database in the server you are connecting to. You can actually have more than one Sql Server running in the same operating system. You're using the default instance, so don't use that field or leave it blank.
Host: PlatypusSQL42
Username: youinnocentdog
Password:contrasena

How to connect to MS SQL in Rails

I am attempting to migrate legacy data from a MS SQL database into my Rails Application. I have added configuration in freetds which is connecting properly. In my Gemfile, I have added the tiny_tds and activerecord-sqlserver-adapter, respectively.
I have created a file to house the classes from the legacy database to translate to ActiveRecord:
class LegacyUser < ActiveRecord::Base
establish_connection :legacy
set_table_name 'users'
end
.
.
.
database.yml
legacy:
adapter: sqlserver
mode: odbc
dsn: legacy_db_name
host: db_host_name
database: legacy_db_name
port: 1433
username: username
password: password
Then I have rake tasks to convert the data:
legacy.rake
desc 'migrate users'
task :users => :environment do
require 'lib/tasks/legacy_classes'
LegacyUser.each do |user|
begin
new_user = User.new
new_user.attributes = {
:firstname => user.firstname,
:lastname => user.lastname,
:email => user.email,
:created_at => Time.now,
:updated_at => Time.now
}
new_user.save!
puts "User #{user.id} successfully migrated"
rescue
puts "Error migrating #{user.id}"
end
end
At this point I am just trying to get the rake task to 'connect' to the legacy database.
When I try 'rake users', I get:
rake aborted!
database configuration does not specify adapter
It looks to me like I have clearly specified the adapter. What is the proper way to configure this?
Also as a side question, in my 'classes' file for legacy database tables, should all of those tables mirror the 'new' Rails database schema? Ideally I want to be able to simply connect to various tables in the legacy database and fit them into the new database schema where needed. The associations in the old do not match the new, nor do the naming conventions.
Any help is appreciated. Thanks.
Update
Still experiencing this error. Sadly, the only threads I could find that have had the same error were odd spacing issues in the database.yml file. So I actually took the time to go through and make sure all of the spacing matched my other configurations. Given that it is a vague error I am not really even sure what to check past the instructions for setting up activerecord-sqlserver-adapter.
Got an answer to this here: https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/issues/250
'You can try connecting to your DB like so, this works for us on that branch on Rails 4:'
class LegacyTable < ActiveRecord::Base
establish_connection({
:adapter => "sqlserver",
:host => "host",
:username => "user",
:password => "pass",
:database => "dbname"
})
This solved my issue. Hope it helps someone in the future.

failure - test failed: the network adapter could not establish the connection

I installed oracle 11g in my machine and created tablespaces and user.
Evereything ran succesfully. Now im trying to create a new conenction thru sqlDeveloper but i get this error. I checked the SID name and changed them according to tnsnames.ora. what else might go wrong?
Don't use the tnsnames.ora and set the connection type to "Basic".
Enter your connection details (SID, Port, host, username and password) accordingly.
Use connection type basic, and dont use hyphen "-" in the connection name, while use underscore "_". and then try again to connect
If the database is on your machine, and you're still talking about 11g, then you can simply do this:
Connection Type: Basic
Hostname: localhost (you said it the db was on YOUR machine, yes?)
Port: 1521 (this is the default unless you changed it)
SID: orcl for regular db or xe for Express edition
Service Name: use this if 12c with Pluggable Database
Or course for username and password:
username: system (a default ADMIN account, less dangerous than SYS)
password: whatever you provided when creating your database
If you are going to login as SYS, you must change the Role from default to SYSDBA.
If you have TNSNames.ora file, we should find it, and you can set your Connection Type to TNS, and simply pick your database from the dropdown.
As you can imagine, when you have MANY databases, the TNS path is much easier, but you will need to maintain this file. Otherwise, Basic is the easiest way to go.

Resources