Use two different databases in one project - ruby-on-rails

Is it possible to use two different databases for the same project in Ruby On Rails?
At the moment I use PostgreSQL with Heroku for my project and I would like to use Redis to store some of my models.
Thanks
First step, add redis-rb to your Gemfile:
gem 'redis', '2.1.1'
Then install the gem via Bundler:
bundle install
Lastly, create an initializer in config/initializers/redis.rb and add the following:
$redis = Redis.new(:host => 'localhost', :port => 6379)
Will that have any side effects to my existing database PostgreSQL database?
Or I 'll able to use $redis whenever I want to store something?

If you follow the steps you've written, you shouldn't have any problems with ActiveRecord/Postgres
$redis will work fine for you, as long as the connection works.

If you want to use Redis as a database storage for your model then you should use some of the available gems that lets you easily store objects in Redis. Some of the gems are:
OHM
Redis::Objects

Related

Adding ActiveRecord on top of a Rails + Mongoid install

I have an existing project with Rails 5.2 and Mongoid 7, I had previously disabled all activerecord-related modules.
We are working on some synchronization with business intelligence data warehouses, and I discovered this gem that seems like a good starting point : I'm planning to use ActiveRecord with this adapter to easily implement code that will push data to my Amazon Aurora DB for BI purposes.
I have created a simple model
# model/test.rb
class Test < ActiveRecord::Base
end
I have added a database.yml
# config/database.yml
development:
adapter: aurora_serverless
...
But when I try to do anything with a model it says it could not connect
ActiveRecord::ConnectionNotEstablished: No connection with pool with 'primary' found
Am I missing other things to make ActiveRecord work ? Do I need some additional Railties, etc ? It's as if the database.yml file was not read at all
Here is a summary of the things I ended up doing
add required gems in your Gemfile (as a matter of fact, the gem I added did not even require the mysql2 gem which is quite a relief as mysql2 has dependencies on the local OS libs, and you're quite likely to encounter many bugs)
create a config/database.yml file, and have one key per environment you need, with the AR config you need
in application.rb, add require 'active_record/railtie' (the railtie will initialize connection from the database.yml file)
create some models
Maybe you need extra adapters for local/test/staging/prod environments
Add a container for a SQL DB if you're working with docker-compose

Deploy MySQL database on Heroku with Ruby on Rails

I am trying to deploy a Rails project on Heroku. My Rails application uses mysql2.
I have tried using the taps gem but its not working. I get the following error when I run the command taps server mysql://root#localhost/heroku_ex tempuser tempass:
Failed to connect to database: Sequel::AdapterNotFound -> LoadError: cannot load such file -- mysql
Is there any way to deploy my application on Heroku? I would prefer to only use free add-ons if possible.
There are a few moving parts here.
First of all, Heroku doesn't support MySQL by default. If you want to use MySQL instead of PostgreSQL you will have to provision an add-on that provides it. There are currently at least two add-ons that provide MySQL or MariaDB¹ support with a free tier².
Next, Heroku doesn't run database servers on localhost. How can you handle different database configurations between your development machine and your Heroku server?
One strategy that is endorsed by Heroku is to store configuration in the environment. Following this model lets you alter your application's configuration by modifying environment variables instead of editing files. Luckily, Rails appears to override config/database.yml with configuration from the DATABASE_URL variable by default, so this approach should be a good fit.
Very often database add-ons will automatically set an environment variable for you. For example, the JawsDB Maria add-on sets JAWSDB_MARIA_URL when it is provisioned. This isn't the variable that Rails looks for, so you'll either have to tell Rails to look for JAWSDB_MARIA_URL instead of DATABASE_URL or manually set DATABASE_URL to contain the same URL that JawsDB provides in JAWSDB_MARIA_URL.
¹MariaDB is a fork of MySQL that aims to be fully-compatible.
²Note that the free tier may be quite limited, e.g. by only providing 5MB of storage. You may have to upgrade to a paid database tier as you continue to develop your application.
I have solved this by adding the pg gem in production and mysql2 in development environment.
group :development, :test do
gem 'mysql2'
end
group :production do
gem 'listen', '~> 3.0.5'
gem 'pg'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
end

How to run ruby on rails apps with different versions of rails?

I have about 3 apps on a Windows Server 2012 and all of them are different versions of rails. So far, I can run locally the app with the latest version of rails, but not the other two, which were deployed from Heroku. I wanted to make changes locally before I push them to Heroku but no luck. Is there any reference to that or is this even possible? thanks a lot!
Your problem is that you are trying to use sqlite with Heroku. Heroku requires using 'postgres' as the database
change the gemfile to the following
group :development do
gem 'sqlite3'
end
group :production do
gem 'pg'
end
And then change your database.yml file to this
adapter: postgresql

Static Read Only SQLite3 Database in Heroku

I will try my best to explain my issue.
I have a SQLite3 database file of about 50,000 records that I compiled and use in my app. This database is read only and is only used to query and pull data from. I created a model for it in rails and it looks like this.
class Locations < ActiveRecord::Base
establish_connection(
:adapter => 'sqlite3',
:database => 'db/locs.sqlite3'
)
set_table_name "Locations"
end
Everything works great on the development end, but fails when I deploy to Heroku. I'm assuming the issue is that Heroku runs on Postrgres but I need this database to be separate from the main production database and I don't need something fancy. All I need is for the app to query some records from this file.
Is there a solution out there? Thanks
Allowing SQLite3 read only can be enabled by following the steps from this git repo.
https://github.com/yotsumoto/heroku-buildpack-ruby-with-sqlite3
After running:
$ heroku config:set BUILDPACK_URL=https://github.com/yotsumoto/heroku-buildpack-ruby-with-sqlite3
I was able to git push heroku master with gem 'sqlite3' still in the Gemfile.
I'm really not understanding your question but I'm assuming you want to use sq in dev & testing but want to use postrgres in product, correct? If so, the code below should help. Switching from sqlite to pg is rather seamless so you shouldn't have a problem doing so.
group :development, :test do
gem 'sqlite3'
end
group :production do
gem 'pg', '0.12.2'
end
The simple answer to your question is it's not possible to do what you're trying to do. Heroku don't have necessary prerequisites installed to even let you install the sqlite gem.

Ruby Gemfile & database adapters

I am developing on an open source rails application and as a rails 3 project, it includes a Gemfile. The application is also dependent on a database adapter for active record. I can specify a specific adapter (like gem 'sqlite3') within the Gemfile but this is not what I want to do. I advertise the app as beeing compatible with all the adapters active record is compatible with. Is there any way to specify that the app depends on a single database adapter (out of a selection) but that one is sufficient? Something like
any_of do
gem 'sqlite'
gem 'mysql'
etc.
end
Thanks for any help or suggestions how to approach the problem differently.
I just recently installed something that generated an error, until I realized how they handled this.
group :postgresql do
gem 'pg'
end
group :mysql do
gem 'mysql2'
end
and then when you run bundler:
bundle install --without postgresql

Resources