Transfering data between two rails servers - ruby-on-rails

What I'm trying to do is to transfer my Company model that has a lot of associations, depots, products, users, owners, etc, to another database(server). I've tried cloning the company but it doesn't get the associations. How exactly can I get the Company data and it's children to the other database? I don't want to dump my data and restore it, I want to establish a connection between the two databases and be able to transfer what data I have from the first server to the second.

I'd suggest instead do this on the data base level rather than the application level. This would result in a more reliably faithful copy of the data.
For Postgres, you can do the following (found at https://www.google.com.tw/search?q=postgres+backup+and+restore&ie=utf-8&oe=utf-8):
Backup a local postgres database and restore to remote server using single command:
$ pg_dump dbname | psql -h hostname dbname. ...

Related

Running the same Rails code on multiple laptops and saving the data from all of them to the same Database

I am trying to run the same rails code on multiple laptops on localhost:3000. How can I save the data submitted from the different laptops to save in a single database. Or, they would share the same database and if someone has submitted data, it would automatically be on the other persons laptop. Also consider that there won't be any internet connection, but we can connect them with Ethernet cables. Also, we are using sqllite3 . Thanks!
Probably you would like to use mysql in that case - it is designed to work this way, you just specify remote host instead of 'localhost' in config.
However, you can put sqlite database in ftp/sftp/webdav server, mount as local folder (kinda complicated on windows tho), and try to use it, which will work REALLY slow.

Does anyone know how to create a new database (through code) on heroku?

I'm confronted with a large number of clients all doing large number of transactions on distinct record sets.
I have say 100 clients each with 500k product records.
Previously I had separated their data with different schemas. It worked fine for a small time then we noticed that when one client was hammering the database all others would grind to a halt.
We then moved to separate instances of Heroku. Each client had their own application and thus their own database. The problem with this is that on-boarding a new client means we have to fire up a new Heroku app and typically that task falls to a developer.
Ok.. so that's the background.
Now my question. I want to create a new database on Heroku for each client. (all in one "application"). When we on-board a new client my app will create the database and tie the username to that database.
I can create a database readily enough with something like this.
heroku addons:add heroku-postgresql:#{db_plan}
But what I don't know is how to get the username and password for this new database, so I can store it against the new user.
QUESTION: How does one get the user name and password for a database they created on heroku?
the heroku toolbelt has a pg:credentials option, I've never tried what you're attempting, but it's possible you can obtain that information here.
Usage: heroku pg
List databases for an app
Additional commands, type "heroku help COMMAND" for more details:
pg:credentials DATABASE # Display the DATABASE credentials.
pg:info [DATABASE] #
pg:kill procpid [DATABASE] # kill a query
pg:killall [DATABASE] # terminates ALL connections
pg:promote DATABASE # Sets DATABASE as your DATABASE_URL
pg:ps [DATABASE] # view active queries with execution time
pg:psql [DATABASE] #
pg:pull <REMOTE_SOURCE_DATABASE> <LOCAL_TARGET_DATABASE> # Pull from REMOTE_SOURCE_DATABASE to LOCAL_TARGET_DATABASE
pg:push <LOCAL_SOURCE_DATABASE> <REMOTE_TARGET_DATABASE> # Push from LOCAL_SOURCE_DATABASE to REMOTE_TARGET_DATABASE
pg:reset DATABASE # Delete all data in DATABASE
pg:unfollow REPLICA # stop a replica from following and make it a read/write database
pg:wait [DATABASE] # monitor database creation, exit when complete
and furthermore...
Usage: heroku pg:credentials DATABASE
Display the DATABASE credentials.
--reset # Reset credentials on the specified database.
Local testing seem to indicate that it does, indeed, give you the database user and password.

Stored Proc access multiple databases on same server FAILS

I am trying to run stored procedure from a limited permission login that has been granted execute permissions for said stored procedure. The stored procedure access 2 databases that exist on the same server. When I execute the stored procedure I receive an error that states:
The server principal "LimitedUser" is not able to access the database "Database2" under the current security context.
Some background:
I have recently been tasked with the goal of migrating our 2 different database servers into a single database. I have backed up and exported the necessary databases and restored them into the new server. The older databases are MS sql server 2000 (for Database 2), and MS sql server 2005 (for database 1 - where the aforementioned stored proc is located)
I have found some leads that seem to suggest that because I imported the databases, the owners were different and that would cause a problem. So i ran "exec sp_changedbowner 'sa'" on the 2 databases to ensure they had the same owner. I still got the same error when running the stored proc from the LimitedUser. A lot of other examples on various forum sites deal with databases that are on different servers...and having to utilize open query commands. I do not believe this is necessary.
When I run it as a user who has more admin permissions, the stored proc runs just fine. So my question is, what permissions should I be setting to allow this action from LimitedUser?
Thanks!
LimitedUser needs permissions on Database2 to do whatever the stored procedure is doing in that database, ownership chaining will only work within the same database (unless you enable the server option Cross Database Ownership Chaining, which I don't recommend as it breaks down the database container as a security boundary).
So, for example, you have db1 and db2, there is a stored proc in db1 that executes select * from db2.dbo.table1
For this you need LimitedUser to have:
execute permissions in the db1 database for the procedure
select permissions on table1 in db2

How to update a database on a remote machine in Rails

I have a Rails 3.1 app, which lets end user to add/update customer's information list. Now I have a request to sync this cust_info table with a db on a remote machine. So whenever a change has been made in Rails, it automatically update the remote db. What's the best way to approach this?
I'm thinking of setting up a remote db access in config/database.yml and create a model to establish a connection to that db. But in my controller, when a customer's info gets saved, how do I use that remote db connection to update one table? Should I write a raw sql query?
Thanks.

Connecting directly to Database in Rails (Heroku?)

I am currently writing RoR applications and deploying using Heroku.
Is there any way to connect directly to the DB using a connection string?
I guess what I am asking here is can I connect directly to the DB, is there a connection string, how can I get the connection string, etc. I want to be able to perform querys on the DB outside of the terminal I am developing. My current solution is using
heroku db:pull 'anotherPOSTGRESQLdatabasesCONNECTIONstring'
and then performing queries on that database, but this is not a valid solution, because I am developing this application for users who are not code savvy, and they should be able to perform queries on the database without me or them using the terminal.
I think what you might be asking is available here: https://postgres.heroku.com/
Click "Try For Free", Select the option where you already have a heroku account, login with the correct credentials.
Now you can choose the database you want to perform actions on, and create "Data Clips" that your clients can run to get data reports etc (data clips can be used to run arbitrary select SQL commands).

Resources