Partial database recovery on Heroku - ruby-on-rails

Thankfully this is a hypothetical, planning-ahead sort of a question. Can you restore part of a database using Heroku's backup addon, or otherwise? So, for instance, only restore records in all tables which have a client_id of 5?

No, that does not appear to be a feature included in the PG Backups addon provided by Heroku: http://devcenter.heroku.com/articles/pgbackups#restoring_from_a_backup

Related

Heroku and database management for rails

I'm getting really close to putting up my first app. However, I'm worried that because I'm so new, I won't know how to manage my database properly.
What's the best practice for managing a database on heroku? As in, backups for example. What if I really screw up something? Can I edit information like usernames, email addresses for users, etc directly in heroku or do I still do that from my terminal?
As Gene mentioned above, you will use pg backup to back up your db. You can do this manually, but you can also schedule backups.
You can easily access the rails console on heroku. Docs here: https://devcenter.heroku.com/articles/getting-started-with-rails4#console
Gook luck rolling your your first app!

How to gracefully handle "Mysql2::Error: Invalid date" in ActiveRecord?

I'm building a Rails 3.2 app upon a legacy database which also has some broken records in different tables. One of the issues giving the most headache is that it includes invalid dates.
I've setup a sandbox which I manually fixed one time to get my code working. Now it's time for deployment. For this reason, the sandbox is reset every night and copied from the live database, ferret indexes are rebuilt, and migrations are re-applied. We are going to deploy to the sandbox often to get in the last fixes before deploying to the live setup.
As the legacy PHP app and this new Rails app need to run in parallel for a few weeks to months, we cannot simply one-time-fix the dates (Update: just for clarification, that means they run on the same database at the same time). I need a way to automate this, maybe with a migration or rake task (I'd go for the latter).
But the problem is: ActiveRecord chokes on loading such records so I have no way to investigate the record and fix the dates by some hardcoded assumptions made in ruby code.
A second problem is that the legacy database has inconsistencies because the PHP code did not use transactions and some code paths are broken and left orphans and broken table constraints behind. I will deal with that as they occur, most of them is already taken care of in the models. First problem goes with the dates.
How would you usually fix this? Maybe there's even some magic gem out there which supports migrating legacy databases with broken records by intercepting exceptions and running some try-to-fix code...
The migration path uses MySQL, and three production environments (stable with live database, staging with the same database, and sandbox with a database clone reset every night). We decided against doing a one-time data mapping / migration because we cannot replace the complete legacy application in one step (it consists of a CMS with about 50000 articles, hundreds of topics, huge file database with images and downloads, supporting about 10 websites, about 12 years of data and work, messy PHP code from different programming skills, duplicated code from different migration stages, pulling in RSS content from partner sites to mix articles/posts from there into the article timelines in our own application's topics, and a lot more fun stuff...
First step is to migrate the backend application to get a consistent admin and publishing interface. The legacy frontend applications still need to write to the database (comments and other content created by visitors). So the process of fixing the database must be able to run unattended on a regular basis.
We already have fixes in place that gracefully handle broken model dependencies in belongs_to and has_many. Paperclip integration has been designed to work with all the fantastic filename mappings invented. And the airbrake gem reports all application crashes to our redmine installation so we get a quick overview of all the left quirks.
The legacy applications have already been modified to work with the latest MySQL version and has been migrated to a current MySQL database server.
I had the same problem. The solution was to tell mysql2 not to perform casting, like this:
client.query(sql, cast: false).each do |row|
row['some_date'] = Date.parse(row['some_date']) rescue(nil)
end
See mysql2 documentation for details on how to build client object. If required, access rails db config via ActiveRecord::Base.configurations.
Create a data import rake task that does all the conversions and fixes you need (including the data parsing and fixing), and run it every time you get a fresh update from the legacy app. The task can use raw SQL (look-up "execute" and "exec_query" methods), it doesn't have to work with models. This will be your magical "gem" that you were looking for. Obviously, you cannot have a one-fits-all tool for that, as every case of broken data is unique.
But just don't create kludges in your new code base.
Similar to: Rails: How to handle existing invalid dates in database? and also without correct answer so I repost my solution below.
I think the simplest solution that worked for me was to set in database.yml file write cast: false, e.g. for development section
development
<<: *default
adapter: mysql2
(... some other settings ...)
cast: false
I believe it will solve your problem Date.parse()
e.g. Date.parse(foo.created_at)

Migrating database (SQLite) from one rails application to another

I currently have a system in which one rails 2.3.2 has a database with all of my content data on. I have since created another application which is using rails 3.1 and is very similar but with some more features (hence changed database structure (added and removed columns around the place)).
My issue is that I'm not sure how to get the data (I only really want three values from all of the "entries" (I don't work with databases often and so don't really know the lingo)) in one of my models from one database (SQLite production) to the other (also SQLite production).
I looked into db:seed however it turns out that rails 2.3.2 doesn't support db:seed and so I cannot use this.
Any ideas on how I can do this and easily add the missing information to these entries aswell (such as the published_at column which is new in the newer application which needs to be added for each entry)?
Best Regards,
Joe
If you need to migrate the whole application, check this out: Migrating from Rails 2 to Rails 3
If you only need to manage the sqlite databases, there are a lot of tools to do that. Here's the complete list

Moving from SQLite3 to Mongo on Heroku?

I'm currently using SQLite3 with a simple post and image sharing app, similar to the Rails 3 Hartl tutorial (in terms of db structure). But I'd like to move to Mongo for future scalability/learning.
I'm also hosted on Heroku, and am using a 15 GB shared db. I attempted to install MongoHQ and MongoMapper (as per Heroku's instructions) for the transition and this part according to Heroku's support is set up correctly. However, when I turn off the shared db, the app stops working, rather than running off of Mongo.
I'm not sure what do do next, do I have to rewrite my code in mongo or does mongo mapper solve all that? Do I lose my data if I change, if so, how do I copy?
Could any of you please point me to some resources or help me out? Thank you very much!!
MongoDB is not a drop in replacement for a SQL database. There are a couple of things you need to adapt:
The models' code are to be updated to use MongoDB. I can suggest using Mongoid, an ODM, as it will ease your learning path. Mongoid implements Active Record.
The current data saved in your SQL database needs to be migrated - and this is not automatic – to MongoDB schemas. MongoDB do not support migrations as you are used to in SQL world. You will need to write your own scripts for that.
I suggest you write a simple app from scratch using your MongoDB ODM of choice – MongoMapper or Mongoid – so that you get familiar with the basis of MongoDB before attempting to make a migration.

Heroku PostgreSQL

I need to edit data in my tables at Heroku, how can i do that with an IDE for PostgreSQL or some other solution to this? I'm really new to Heroku, so please can you guide me what's the best way around it.
thanks
To the best of my knowledge, Heroku does not offer direct access to the database. Does heroku console cut it for you? You can access the db through your models directly.
Otherwise you may want to look at how to import/export your database.
If you have access to a dedicated database, you will be able to take advantage of the "ingress" feature to get a raw connection to your database, compatible with all programs that need to speak over the Postgres wire protocol (examples include: psql, pgadmin, navicat, or Microsoft Excel with ODBC -- yes, really, and it's not half bad sometimes!)
But on shared databases you are somewhat out of luck at this time unless you somehow bounce your interactions off ruby and the pg gem.
Alternatively (if you're able to) use db:pull to bring the database locally, do the work locally and then db:push it back up.
Or more likely the easiest option is to do it from console (heroku console at prompt)
John.
for my rails app I use the typus gem which provides a very simple to implement fairly nice db admin interface to the application.

Resources