rails 4 app with 2 databases: MySQL and Cassandra - where to begin? - ruby-on-rails

Hi have an existing rails 4 app which is using a MySQL database.
I now need to add code that creates dynamically a new table for every object and its attributes.
I decided to use a No SQL solution for this. Initially I wanted to use Mongo DB since I was familair with this. However, my company supports only Cassandra/redis/solr. So I have to pick one of these.
I chose Cassandra but Im not finding any tutorials or examples on how to use it with an existing rails app with a MySQL DB.
I found this ORM for rails-Cassandra but no idea how to make it work with an existing Active record - mysql system.
Any ideas would be very appreciated.
Apologize this post has no code. But I couldnt find any answers for this issue on SO so please excuse if any mistakes.
thanks

ActiveRecord and Cequel will be independent.
As shown in their docs you extend a model's functionality by including the Cequel::Record module.
include Cequel::Record
In order to use it in rails you (obviously) need to add the proper gem in your Gemfile
gem 'cequel'
and create the configuration for the project
bundle exec rake cequel:keyspace:create
After that, you will have a config/cequel.yml from where you will define your settings (as you did in config/database.yml for your MySQL)
MySQL models will still use the ActiveRecord
class Foo < ActiveRecord::Base
...
end
while Cassandra ones will use the Cequel
class Bar
include Cequel::Record
...
end

Related

Converting a single Tenant Application to a multitenant one using Apartment gem

I've built a single tenant rails application with all core features ready to ship. But I now want to make it multi-tenant using the apartment gem. Most tutorials I've found all show how to start from scratch. But I find no pointers on how to convert an existing project to have multi-tenancy built in. From my research I found that all models need a tenant ID added in the migration. How do I add this to all existing models easily? Does installing the gem and running the generator suffice? I'm running a rails 5 API application with close to 30 models and using graphql ruby in an Ubuntu 18.04 environment.
Any ideas on how to do this?
Thanks #lacostenycoder for pointing me in the right direction. Here's what I did.
Short answer: Yes, installing the gem, running the generator and creating your tenant model should suffice. Apartment reads your schema and creates schemas for the tenants.
Long answer, my experience:
I Backed up existing data from my database just in-case.
Installed apartment gem
If you are using any PostgreSQL database extensions like I was (pgcrypto, uuid- ssop)you need to understand that the extensions are not automatically loaded into the newly created schema.(look here for more details) do the following:
Follow instructions the github readme to create a shared_extensions schema
Create your first tenant. If it works, all is well. But...
If that doesn’t work, I mean when you create a tenant you get errors like ActiveRecord::StatementInvalid: PG::UndefinedFunction: ERROR: function gen_random_uuid() does not exist
or ActiveRecord::StatementInvalid: PG::UndefinedFunction: ERROR: function uuid_generate_v4() does not exist (more details here, here and here) like it was my case when creating my first tenant, I discovered that once the extensions are enabled on the public schema, they cannot be installed in the shared schema shared_extensions in this case. So you have to change them from the public schema to the shared_extensions schema. get more details here
If you are using uuid-ossp ALTER EXTENSION "uuid-ossp" SET SCHEMA shared_extensions in your rails dbconsole
If you are using pgcrypto ALTER EXTENSION "pgcrypto" SET SCHEMA shared_extensions in your rails dbconsole
The apartment gem will create all models for your tenant once you have it set up correctly and create your first tenant.
For more information on check out some of these github issues on apartment. Here and here.
Took me a day to figure it out and gather that info. I hope it saves you time and headache!
There doesn't appear to be any reason why you can't add this to an existing Rails app. I have not done this myself, nor does there appear to be any specific tutorials on how to migrate an existing app. Follow the gem setup instructions as shown on the README and also see the wiki for additional configurations and instructions. Use TDD wherever possible and expect to make changes to your application to get everything working correctly. Depending on how complex your existing app is will determine how much work this move will be.
You'll likely want to use lvh.me:3000 instead of localhost:3000 as root domain and port in your browser and perhaps use rails s -b lvh.me when starting your local server.
If you want to safely hack through this branch I recommend that you make a full backup of your local development database before you get started so that you can rollback to your stable current version in the event things go horribly wrong. If you're using Postgres for example there's pg_dump and pg_restore
This approach combined with good TDD should get you where you need to be.

Ruby on rails Database setup using scaffold generator or Manually

I am new user to ruby on rails. I have some question please give the answer as early as possible
What is the Best way to Create a Database For Your Projects in Rails?
1.Scaffold generator
2.Manually Design Database​
After installing the Rails then create a project using rails new project_name then cd project_name then run rake db:create it will create a database.
You can modify database name like by default rails uses a database e.g name_development or name_production you can modify this on go to project/config/database.yml.
The rails new project_name command creates default database adapter e.g sqlite3 you can define this which adapter he use by default while rails project create.
The sqlite is a development database you should change database adapter while going to production like postgresql or mysql or mongodb...
If you need to use mysql by default then run below command
rails new project_name -d mysql
If you need to use postgresql by default then run below command
rails new project_name -d postgres
It will create a database adapter by default which you need.
Note: You can use both for a command like rake new project_name or rails new project_name
You need to the read the Rails Guides carefully for understand basic Rails. This tutorial assumes you have basic Rails knowledge.
Also here are the Rails Commands
Ruby on rails Database setup using scaffold generator or Manually
The scaffold works after create a database.
You can see the active record basics and for basic association you can see the Michael Hartley tutorial this is good tutorial for new RoR programmer.
After all you need to design manually using reference or defining by index foreign key
First of all decide what kind of database are you going to use. (Msql2 or Postgres are mostly used)
Then design the schema on a paper or anywhere else.
And whenever you're sure. Now everything comes down to 2 different things.
You want things to be created automatically. for example: Views, CRUD for database operations in controllers.
You want to control things on your own. Now create model from rails commands, write associations, and begin writing your code and define routes as well.
NOTE: Point 2 is implemented when you're going to create a custom application and you may need operations other than basic crud.
Now, you can tell us. What actually you want to achieve and people will help you.

Beginner Ruby on Rails: Migrating an existing psql database

So basically I am making a new sample application that will run using an external Postgres database and a model, both created by another programmer who has given me this project.
I am supposed to create a rails app that will implement the model and database in order to add some mapping functionality. The problem is that I truly do not know how to migrate an existing database using the "db_name.sqldump" file they provided me. I have installed psql, created a new rails app and added the "pg" gem, but I haven't been able to create a table with all the model information.
I am not sure if I am being clear but I truly am pretty much lost here with RoR. Any input would help.
Assuming you've a pg_dump file, import it from the command line using either of psql or pg_restore.
http://www.postgresql.org/docs/current/interactive/app-pgrestore.html
Follow this railscast by Ryan Bates - http://railscasts.com/episodes/342-migrating-to-postgresql
To convert your sqldump to a sqlite db, Follow these guides : -
script to convert mysql dump sql file into format that can be imported into sqlite3 db
http://code-n-fold.com/2011/11/28/convert-mysql-dump-to-sqlite-format/

Ruby Gem Development - How to use ActiveRecord?

I'm currently trying to develop my first ruby gem and I'm already stuck. I used the "bundle gem" command to create the basic structure and read some tutorials but what I can't find is how to integrate ActiveRecord.
Where do I create my migrations?
Do I create the "db/migrations" folder within the lib folder or at the root?
And do I have to do anything in the Rakefile (I found some questions where the answer was something like "you have to create your own [my_gem]:db:migrate" or something like that.)
All I need is a way to create a gem, which defines ActiveRecord models (including the migrations of course), which can then be used by a rails app.
Any help on that one would be greatly appreciated!
Greetings, Flo
When building a gem to integrate with a rails project, you want to build a railtie engine. If you are using rails 3.0.x, use enginex, if you are using rails 3.1 you should be use the new generator:
rails g plugin new your-plugin-name
Then, inside your gem, you can just define models, inside the app/models/ folder and they will automatically be picked up.
Migrations is somewhat harder: for rails 3.1 it is ok if you define them in the correct folder, in rails 3.0 you will have to manually generate a task to copy the migrations to your code-base. Check this link where I answered that very question.
For more information on rails engines check this and this article.
getting the functionality of ActiveRecord can be done by:
require "rubygems"
require "active_record"
class User < ActiveRecord::Base
end
This should work.

Does Ruby on Rails have a way to import an existing database schema?

I know I can do a db:create and create database tables or db:migrate to a new version, however what I want to do is import the schema from an existing database (all tables) and generate models for each one.
Does ActiveRecord have the ability to do this? Thanks!
Nope. ActiveRecord is an ORM, so it doesn't have anything to do with generating database schemas or generating models. Rails doesn't have any concept of doing this, either.
You'll have to write the database migrations and do this yourself.
There's kind of a way. Create a new Rails 2.2 app (it can't be 2.3 or 3.0, the gem hasn't been updated since 2007). Then install and use the Magic Model Generator gem.
This will look at your existing database and extra all the models from it, like magic. Since the structure of basic ActiveRecord model files is pretty similar in Rails 3, you can just copy them into your current project and tweak as necessary.

Resources