Rebuild lost database in Ruby on Rails app - ruby-on-rails

I have an existing ruby on rails app that was on a VPS. I pulled the app off the VPS but never got the database data backed up. I was looking through the source code and see that there is no schema.rb file but I do have all the migration files.
My question is, will there be a way to rebuild the entire database the way it was before?

rake db:migrate should give you back the table schema, but you don't get any of the data.

Related

Rails: Migration and database differences between development and production

I recently deployed a website to heroku and I am having problems with the database. I am using ruby on rails and when I first created my local databases I used SQLite. When deploying to heroku I had to change my databases to Postgres and in the process several migration files got removed and changed into a single migration. Now, when I run the postgres database on my localhost, I am still seeing all of the columns in my database, but when I visit the page on heroku one of the columns is missing.
I have checked both my local console and the heroku console and the databases are now different. The local console includes all of the columns in the database and the heroku console is missing one of the columns in the database, but is generating all of the other columns correctly. I have tried running a rake task on heroku and have pushed the most recent changes to heroku.
I tried to to add an additional migration to add the missing column to the database, but whenever I try to rake the migration it tells me that attribute already exists. Any help I can get is appreciated!
The best way to set up your database on heroku is with
rake db:schema:load
From the guides on migrations
Migrations, mighty as they may be, are not the authoritative source
for your database schema. That role falls to either db/schema.rb or an
SQL file which Active Record generates by examining the database. They
are not designed to be edited, they just represent the current state
of the database.
There is no need (and it is error prone) to deploy a new instance of
an app by replaying the entire migration history. It is much simpler
and faster to just load into the database a description of the current
schema.

Can I deploy to Heroku without destroying existing database? Ruby on Rails

I have made quite a large upgrade to my app. I have older version deployed on Heroku at the moment. Problem is that I have added/removed quite few migrations in the process of making my app more modular. I do not want to lose my registered user table that is already up on Heroku while deploying the update. Are there any tips someone can offer on how to preserve my user table while upgrading the app? I do have backup add-on installed but I have no clue what to do with that file.
You should then setup your migrations more carefully since that one, which is last presented on the heroku server. So, when:
you are adding a column, just setup defualt value
you remove the column, you shell export data to (for example) YAML, and store it in the tmp/.
you are reconstructing parts of the colunms by adding and remove some of them, in migration carefully copy required data from older column (prepared to deletion) to newly created ones.
If you're that concerned about deploying I think you might need to declare migration bankruptcy.
Create a new migration that drops the current schema and copy the new database schema into it
Dump out the contents of your database as CSV ( https://coderwall.com/p/jwtxjg/simple-export-to-csv-with-postgres ) or using Rails to dump in some other way (YAML etc.)
Write a Rake task to parse the data dump into your new schema
Set up a new database on Heroku Postgres (this does not delete the old data)
Do a dry run locally to make sure it all works
Promote the new database to DATABASE_URL
Deploy, migrate, run the Rake task
Not very nice and not something you can roll back from easily if it goes wrong.

Rails -- working with an existing db, cannot run migrations without losing data

I am a beginner working with Rails 4.1. I am trying to integrate an existing sqlite3 db with my app in the development environment.
To try to get this to work, I've followed the steps listed at Joel Berghoff's blog (for MySQL):
Reference the db in config/database.yml
Run “rake db:schema:dump”
Convert schema.rb into db/migrate/001_create_database.rb
The issue I am facing is, whenever I run "rake db:migrate" the entire db refreshes and I lose all the pre-populated data. I got around this for awhile by running migrations first, then replacing the blank db that was generated with my pre-populated copy -- this allowed me to play around with my models in the rails console and see the data. However when I try to boot up the server on my local machine, I get a message that migrations are pending.
I am not quite sure what to do here...I've read that I should be seeding the db from "rake db:seed", but my existing db is quite large -- almost 1mm records, and when I attempted this (in albeit clumsy fashion) it ran for over 3 hours before I gave up. Any guidance on how to proceed is much appreciated!
Migrates should be used to create and changes the tables and fields, not load data, as you can see here Ruby on Rails Guides
If you want to import data you could do it on the seeds, but in your specific case it seems to me that you should create a dump from your origin database and load it on the target database.
Here is a tutorial sqlite3: how to import/export data from/to a file
sqllite3 and MySQL are different things. My guess is you think you are connected to sqllite db but in reality you are connected to an empty MySQL db.
Look into ActiveRecord migrations.
Here is a rails doc on ActiveRecord Migrations: http://guides.rubyonrails.org/migrations.html

What if we mistakenly deleted some migration file..?

I have deleted some migration file mistakenly. Those files are already migrated.
If I am creating migration with same name, and fire rake db:migrate command it is showing me message that Table already exist. Is there any way to regain those files?
please help..
You will find information about previous migration in config/schema.rb
There is no need of keeping old migration files if all databases (developers, staging and production) have been migrated. Even if you did not delete the old files, they would not prevent you from getting a error message if you try to create a duplicate table.
I am relatively new to RoR. However, I'm a bit concerned that losing this file will present some problems if you aspire to host your app on a server other than the one it is currently hosted on. For example, if you had a mind to host the app in Heroku, you would need to run your migrations in that environment.
If you are not terribly far along with your application, it would be best to start over, take care not to delete your migrations, and put your app under version control so that you will have a fallback should you run into this problem in the future.

How to load schema and data for my rails app

How to load schema and data for my production rails app into a different machine.
I would like to converge all migrations done so far into a single migration file and point the new instance to a snapshot of the prod db. How to solve
Just copy and paste the schema and do rake db:schema:load. You can also paste the schema.rb contents into a migration, but make sure to delete all other migrations. However keep in mind that there is really no reason to do so, just leave the old migrations as they are.
To import test data use this gem: https://github.com/ludicast/yaml_db .

Resources