I started building my app in PHP but was convinced by some developer friends to change to RoR instead. Since I already had my database structure designed and created in mysql, I never used any rails migrations to create tables; I just created the appropriate models to match the existing database schema.
So, now I am at the point where I want to test deployment and, of course, I have no migrations to rake to recreate the db on, for example, Heroku.
I know that I can simply go back and recreate the database by creating migrations but my app has dozens of tables with hundreds of fields in total.
Is there any way to create a set of migrations based on my existing db schema, or will I just have to knuckle under and build the migrations one by one to recreate the structure via rails' migrations.
Actually, there are some rake tasks to do the work:
rake db:schema:dump # Create a db/schema.rb file that can be portably used against any DB supported by AR
rake db:schema:load # Load a schema.rb file into the database
You can run: heroku run rake db:schema:load.
Related
I have a ruby on rails application already running in production. The database has records which I do not want to loose. I had to add and run new migrations to add some new columns to existing tables. The migrations run successfully and the schema.rb file reflects the changes but the changes do not appear in the database or existing table structure.
Based on research online, rake db:schema:load updates the db based on the schema.rb file. But this resets the database.
It is crucial that I do not loose the data in the tables.Is there any way to solve this? I am fairly new to ruby on rails.
I was able to fix it. I checked the status of migrations in the production environment rake db:migrate:status RAILS_ENV=production and realised they were down. I then run rake db:migrate RAILS_ENV=production and that did it. Changes now reflect in the db. Thanks #muistooshort for the nudge in the right direction
I am a completely new at this; I used the psql console to create tables for the db, but I think I'm doing it wrong since when I run rake db:migrate, everything gets wiped. So my question is how do I get it to stop dropping all my tables upon a reset without typing all my tables again? I think I'm supposed to use models, is this correct?
EDIT - So I have wasted all my time making tables using the psql console?
You need to create tables "the Rails way" with migrations. You should read the link provided, but essentially you create migrations to track the changes in your database schema over time. You have migrations for creating tables, adding new columns, changing column definitions, etc. You can generate a migration to create a table from the command line like so:
rails generate model Fruit title:string amount:integer
rake db:migrate
This will not only create the table fruits with the specified name, it will also create a model Fruit with which you can query the table. You're also free to edit the migration created before running rake db:migrate.
You should create migration files to build your database. That is what you are doing when you run rake db:migrate: you are taking your migration files and applying them to your database. Look at this guide for more information about Rails migrations.
By running $ rake -T you can check every rake task. Everything is being wiped because other tasks are run along with certain rake tasks. Try
$ rake db:drop
$ rake db:create
$ rake db:migrate
Your database should be created properly, as long as you gave database creation permissions to your Postgres role.
Is there a way to re-create all the migrations for a Rails 3 application from the models or schema file?
No. You can't "re-create" migrations if you didn't keep them under source control. However, you can dump the current schema to Ruby with:
# create db/schema.rb
rake db:schema:dump
or to SQL with:
# create db/structure.sql
rake db:structure:dump
This will enable you to recreate the current structure of the database on demand, but your historical migrations are still lost unless you kept them under source control or backed them up somewhere.
How do I undo bundle exec rake db:setup?
I ran it in the wrong rails app. I ran it in the blogger when I should have ran it in the blogger_advanced app.
You can do rake db:drop
.
It will drop all tables (thats any tables created by setup, any migrations run by setup and any seeds created by setup)
You may use rake db:rollback and it'll rollback migrations one by one, but if yours apps have same named models (User for example) it'll be lost. So you need to dump this tables first and recreate them later using database administrator tool.
And also it'll broke your schema_migrations table and you'll need to refill it by hands from your migration file names.
So if there is not much useful info, it's better to use #Alexphys approach.
In my endless stupidness I changed the mysql db using mysql and not migrations, so now the db is out of sync with migration.
my question is, if it's possible to generate the missing migrations (step) and a new schema.db without loosing data and the changes* in the db?
*changes like adding tables, columns.
thx
As far as recreating your migrations, you're out of luck but you can recreate the schema
rake db:schema:dump
If you go this route, when creating a new db (for a new environment etc...) you'll want to do
RAILS_ENV=some_env rake db:schema:load # specify the env if not development
instead of
rake db:migrate
since your migrations do not align with the current schema.
Be careful when running schema:load as it recreates the db from scratch. i.e. you'll lose all data.
It will be good to create the missing migrations. You can fix your local dev database by adding the timestamps to the schema_migrations table manually. That is the consequence of changing your schema by hand.
Also make sure that rake db:migrate:reset (drop all tables and migrate from scratch) will produce the same db/schema.rb as rake db:schema:dump would. Any change in the database schema must be automated by a migration.
The problem with rake db:schema:load is that it will forcefully create the tables.