rails 3 establish_connection doesn't work for switching databases - ruby-on-rails

I need to use two different db in my app. I found a simple example:
http://pullmonkey.com/2008/4/21/ruby-on-rails-multiple-database-connections/
using establish_connection, but it doesn't seem to work. To test switching databases, I have:
class User < ActiveRecord::Base
establish_connection :test
# attr_accessible :title, :body
end
But I get the error: "Uncaught exception: database configuration does not specify adapter".
Here is my database.yaml:
development:
adapter: mysql2
encoding: utf8
database: foo
username: bar
host: foo.com
password: foobar
test:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
production:
adapter: sqlite3
database: db/production.sqlite3
pool: 5
timeout: 5000
I am running in development mode for this test. Any ideas why it can't switch? Thanks.

First the "pulmonkeyexample" is to use a database label when you have multiple databases and other than the three standard ones - "development, test and production". When you start a rails application, you can use "-e" option to tell rails which mode to start the application in - test, dev or prod. Depending on the mode in which the app is starting, Rails will automatically use corresponding database connections from the "database.yml" file. So remove that line from your model. On second thoughts, even if you write yet it should work provided there is a test database with the user table.

Related

ActiveRecord connection requires reset?

An application in production mode is returning content as expected. It is assumed it is connecting to the configured database. The database.yml file is as follows
default: &default
adapter: postgresql
user: deploy
password: 888xxx888xxx
schema_search_path: public
# For details on connection pooling, see rails configuration guide
# http://guides.rubyonrails.org/configuring.html#database-pooling
pool: 5
timeout: 10000
development:
<<: *default
database: fort_development
<<: *default
database: fort_test
production:
<<: *default
database: fort_production
However, when connecting to the database via the console, whether in development (where the db does not exist) or in production, the console is connecting to the wrong database. It is also referencing a username which was removed from the database.yml file (and app subsequently restarted).
note: at one time the string myapp was referenced in the yml file for the schema_search_path attribute, however it was a commented out line (There is no certainty as to whether it was ever activated.'
> bundle exec rails c
irb(main):001:0> ActiveRecord::Base.connection_config
=> {:adapter=>"postgresql", :user=>"deploy", :password=>"888xxx888xxx", :schema_search_path=>"public", :pool=>5, :timeout=>10000, :database=>"myapp", :username=>"deploy", :host=>"127.0.0.1"}
> bundle exec rails c production
irb(main):001:0> ActiveRecord::Base.connection_config
=> {:adapter=>"postgresql", :user=>"deploy", :password=>"888xxx888xxx", :schema_search_path=>"public", :pool=>5, :timeout=>10000, :database=>"myapp", :username=>"deploy", :host=>"127.0.0.1"}
The console and the app are not in synch. What can make the console work with different values and how can it be reset to work with the proper configuration data?
For anyone stumbling upon this, there is another place where rake can take instructions for connecting via ActiveRecord.
.rbenv-vars
this file within the directory for the application was mistakenly set as:
DATABASE_URL=postgresql://deploy:PASSWORD#127.0.0.1/myapp
Another potential sources of problems (not clear why as the files properly parses in yaml) is to have the database.yml file only reference the production environment
production:
adapter: postgresql
database: fort_production
password: [...]
pool: 5
schema_search_path: public
timeout: 10000
user: deploy

How to setup a MySQL DB with rails in google could SQL

I have a rails app setup on a google cloud instance. I want to have the db in a SQL instance for the extra performance. But I cant see how to do this for a rails app.
I understand you create the SQL instance, start it, install mysql, on it but then how can I have the db and tables added? Creating them manually isn't going to be the solution because normally with rails apps you run rake db:create and rake db:migrate create the DB with tables and columns but this just makes a development.sqlite3 file not a mysql db..
I haven't deployed a rails app before so I think I'm missing something.
Here is my config/databast.yml file
default: &default
adapter: sqlite3
pool: 5
timeout: 5000
development:
<<: *default
database: db/development.sqlite3
mysql_settings: &mysql_settings
adapter: mysql2
encoding: utf8
pool: 5
username: root
password: root
host: 130.211.71.150
database: dbname
test:
<<: *default
database: db/test.sqlite3
production:
<<: *default
database: db/production.sqlite3
I cant find out what needs to be done to have the db be created and tables and columns migrated into the mysql DB.
In your gem file you can put the sqlite gem under a development/test block and you can add the mysql gem in a production block.
In your database.yml file you can keep the development settings you have currently but then add another setting for production settings. Here you can include your mysql db settings (including the host and port of your SQL instance node)
When you launch your app, you can then launch it locally in development mode to use sqlite for development, but when deploying you can launch in production mode to utilize the mysql specific settings. From there you should be able to use db:create db:migrate etc to connect to that host and setup your Db.
Here is a nice article describes this process.
https://www.digitalocean.com/community/tutorials/scaling-ruby-on-rails-setting-up-a-dedicated-mysql-server-part-2
As a team, we chose to use mysql for local development as it more closely mimics what your prod environment will be like.

Is it possible to configure (Thinking) Sphinx on a separate database from the Rails app?

Is it possible to have my app use (in development mode) a SQLite3 adapter, while Sphinx uses Postgres?
I tried to do it by adding the file config/thinking_sphinx.yml below, but I get an error message telling me that SQLite3 is not supported. My config/database.yml has SQLite3 in development, of course.
default: &default
encoding: utf8
host: localhost
adapter: postgresql
port: 5432
pool: 5
timeout: 5000
development:
<<: *default
database: ts_development
test:
<<: *default
database: ts_test
production:
<<: *default
database: ts_production
If you want to use Sphinx, then you need to use either PostgreSQL or MySQL for that environment. So, technically yes, you can use SQLite in development, that will mean you can't use Sphinx or Thinking Sphinx in development, which is I think is a very bad move: I would recommend using the same database in all environments generally (whether or not Sphinx is involved), and especially so in this case, otherwise you won't be able to do anything search-related in your development environment.

Establish connection does not work with Ruby console

I have configured database.yml to include "logging_development" as a label for signifying another database. One of the models is using "establish_connection" to connect to the database using this "label".
My model looks like this:
class AdHistory < ActiveRecord::Base
establish_connection "logging_#{RAILS_ENV}"
The Rails server works fine when it starts and establishes connection of the model with the concerned database. But when I start ruby console and try to use the model, it uses "development" label in database.yml to establish connections. I have looked into the issue but unable to find a solution. Here is a sample of the database.yml file:
development:
adapter: mysql
encoding: utf8
reconnect: true
database: ad_production
pool: 5
username: root
password: ********
socket: /tmp/mysql.sock
logging_development:
adapter: mysql
encoding: utf8
reconnect: true
database: ad_logging
pool: 5
username: root
password: ********
socket: /tmp/mysql.sock
I am using Ruby 1.8.7 and Rails 2.3.8
Have you used the RAILS_ENV=logging_development when running the rails console? If not you should, because the default Rails environment is development. So, try to load the console using this to set the RAILS_ENV variable:
RAILS_ENV=logging_development scripts/rails console
I found the answer to my question. The problem was that I was using Multi_DB gem which was intercepting all the SQL Queries that were being executed and re-directing those queries to the slave database. That was why those queries were not being executed against the database I had chosen in the "database.yml". Once the multi_db connection is nullified, the console behaves as expected. One of the problems of using "multi_db" gem is that it only accepts names of connections as "_slave_database_". So, any deviation from the expected name for multi_db gem in database.yml file will lead to errors, and the queries will not get executed against the selected database.

How to Use Multiple Databases in a Rails App Using the database.yml

I've read up on the documentation on how to do this, but in practice, I am having problems.
In my app, I have 2 different databases as described below in my database.yml file.
sqlite_test:
adapter: sqlite3
database: db/sqlite_test.sqlite3
table: plots
pool: 5
timeout: 5000
development:
adapter: mysql2
encoding: utf8
reconnect: false
database: test
pool: 5
username: myname
password: mypassword
host: localhost
My application is a dynamic plotter that will plot the data in a (basic) database without having knowledge of whats in the database, or how its structured. Both of these databases contain different data. The SQLite database I created in a separate Rails app.
The current app I'm using is built around the MYSQL database, which I build externally.
I copied the SQLite database into the /db directory. So in my main model, when I say:
class Plot < ActiveRecord::Base
establish_connection :development
set_table_name "stock_test"
set_primary_key :id
Everything works out just fine and dandy. However, when I change it to:
establish_connection :sqlite_test
set_table_name "plots"
and try to access that database via the Rails console, I get an error saying:
>>ActiveRecord::AdapterNotSpecified: database configuration does not specify adapter
I don't know why that is, since the database.yml file clearly does specify an adapter?
When I do it by hand in my model though, everything works exactly as it should.
class Plot < ActiveRecord::Base
establish_connection(:adapter => "sqlite3", :database => "db/sqlite_test.sqlite3", :pool => 5 )
Why does it all work when I manually specify whats in the database.yml, but not when I just use the database.yml reference?
Thanks!
I tried to replicate and got it to work. It has to do with naming conventions:
You're in development mode and AR will look for the development tag, which in your case does not exist for sqlite.
Here is my database.yml:
development:
adapter: mysql2
database: se_development
username: root
pool: 5
timeout: 5000
sqlite_development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
Here is the model:
class Plot < ActiveRecord::Base
establish_connection 'sqlite_' + Rails.env
end
Worked for me. Hope this helps.
does this not work if you have two development: with different
key/valuee?
you have to add two development key like
development
development_sec

Resources