Multiple postgresql Database at runtime in ruby on rails - ruby-on-rails

When customers is created in my app ruby on rails,I want to create database for each customers without break/closing my exiting/running application database.When particular customer is login then I want to CRUD operations on both databases.
My app Database name is myapp.
E.g when customer 1 is created in myapp I want create One database for customer_1.
when customer 2 is created in myapp I want create One database for customer_2.
When customer 1 is login in myapp , when he does any crud operations then I want save all data in Myapp database as well as customer_1 database.

try sequel
Sequel is a simple, flexible, and powerful SQL database access toolkit
for Ruby.
Sequel provides thread safety, connection pooling and a concise DSL
for constructing SQL queries and table schemas.
i think this tool can help you create many database set on you app

You can try to use establish_connection (using before_filter in application_controller) http://www.runtime-era.com/2012/11/dynamic-activerecord-database.html

first make another label and put there all setting of databasee in database.yml then in respected model from where you want to connect different database create two different methods and assign their database connection by establish_connection(database.yml files label name here such as "development") and another method put another db name, which location you want to switch database then call these methods.

Related

I want to migrate users from one database to the next

I am attempting to create a custom command that will migrate user data from one database to the next, This database is not normalized, very legacy, very badly made.
All of that is fine, but I am unsure, in a custom laravel command of how to connect to a different database and do select commands and get back collections of data, like you would if you were doing User::all().
I wont have models for this other database, the application that uses said database is written in php 5.2.
How can I create a custom command that does select statements to another database? All the answers I find are for creating migrations to another database.
You can define multiple database connection configurations in your config/database.php file, then use DB::connection('connection_name_here') to run queries on specific connections.
For example:
DB::connection('first_db')->select("...");
DB::connection('second_db')->select("...");
More information on multiple connections can be found in Laravel's documentation: https://laravel.com/docs/5.7/database#using-multiple-database-connections

Integrate new rails app with existing external MS SQL Server database

I need to create a new rails application that will use an existing external Microsoft SQL server database as its primary database. This DB is running on an external server that i'll need to connect to. Here where i work there's a separate DB Admins section so im not sure what steps i need to perform. I think the easy part is connect to the server and set the app to work with that DB. But i get confused after that.
Should i create a db dump and migration?
Should i add models to represent existing database structure?
In case i need to create new models and tables in the db...
Should i generate its models from the rails generator and run migrations on the db or contact the DB admins to create the tables, and then specify manually the models inside the app? <- this really confuses me
Thank you in advance and sorry for my english! :)
EDIT!! -> The Rails Way
I've found another great way to integrate multiple database data... You can define a model and specify in which database it's located (so db connection is never closed)... This is definitely The Rails Way and should be followed...
Here's a nice tut:
Multiple Databases in Rails
TL;DR
Define your secondary_db in database.yml
Define your model and specify its database and table name
Class ExternalDatabaseModel < ActiveRecord::Base
establish_connection(:secondary_db)
self.table_name = "the_table"
self.primary_key = 'someWeirdIDColumn'
end
Then use as any other rails model, specify relationships and more :)
Hope it helps to somebody!
I would suggest you take a look at tiny tds gem: https://github.com/rails-sqlserver/tiny_tds. It's really use to config and use... you can use it to connect on your external sql server db and make the queries you want. Don't know which type of system you're working, but if you're constantly retrieving data, making external requests may get your system slower. Depending on your application, you could consider synchronizing the data, making a local database redundant. Hope it helps you, good luck!
EDIT: about the models part: if you're going to retrieve the data from that sql server every time you need, than you don't need to store it locally. But if you're doing that redundancy, you should create the models with the attributes you'll need, than when you retrieve from the server, you save it normally, just attributing the values to your model and calling save! method.

Create a sub database within rails application to store data in sub database

I have a rails application requirement which I have User table in main database and need to create UserJobs related records(sub tables) with in a sub database , i.e I need to create a different database for each user and store their Jobs in a different database.
How can I do this manually from rails application.
Apartment gem could come in handy. It lets you create multiple schemas in database with the ability of supporting multi-tenancy.
Apartment Gem would do all the things for you which you want to do as mentioned in your query.

rails application + multiple database instance

I need an idea to develop an webapplication using ruby on rails that can connect to to multiple database instances residing in single server..
I want to develop a webapplication and host it.. and connecting to database server...
now on different users on registering to the site, seperate database instance should be created for different users, each user details will be stored in their own database...
Perticular user on logging in he should be connected to his database instance.. same case for other users..
please suggest a solution for this approach in both development and production..
It looks like you want to implement (some parts of) SaaS approach. From the user side it looks like he gets clean working app for him only. But apps are the same in a point of basic functionality and you want it to be the same app with different db instances. This approach has one significant minus - you can't customize your client's applications separately.
But you can redefine this class method of your models like this (only example):
self.table_name
"#{current_user.name}_database.table"
end

Migration to create different databases structures in Rails

I have 3 different organizations types and 3 different databases structures: one for each type. When org1 registers I'm creating a new database instance for org1, and when org2 registers I'm creating a new db instance, and same for org3.
Now, how do I run migrations for three different db:structure from my application soon after I create a new database instance for organizations.
I am creating a db instance from application by:
ActiveRecord::Base.connection.execute(sql)
How can I do this? Is there another approach I should be using?
The command you are looking for is establish_connection. You might
want to look at the rails rdoc. Search for "Connection to multiple
databases in different models" in
http://api.rubyonrails.com/classes/ActiveRecord/Base.html.
Connections are usually created through ActiveRecord::Base.establish_connection and retrieved by ActiveRecord::Base.connection. All classes inheriting from ActiveRecord::Base will use this connection. But you can also set a class-specific connection. For example, if Course is an ActiveRecord::Base, but resides in a different database, you can just say Course.establish_connection and Course and all of its subclasses will use this connection instead.

Resources