When I read Rails book, each time they create a new database, always follow a db:migrate.
rails generate scaffold school
rake db:migrate
In console view, I see at first line, Rails create some files, no problem. but in second line, I see that Rails isn't really change anything. I have view some files that Rails nearly create and see no change too.
So, what the purpose of line 2, please tell me.
Thanks :)
The rake migrates the changes into your database. It is which acttually changes the database schema to match your previously generated scaffolded model.
Without it, you wouldn't have a table to write your objects into. Or in case of changed model, the table could differ from your model, leading to error.
When you generate a model (or scaffold one) a migration file is created in your db/migration directory. It is a pure text file, you can create such manually, if you want. This is the tool for the iterative development in rails regarding the database. Each migration adds some change to the system. When you run rake db:migrate your database is updated by the given migrations. This is a handy tool in case of distributed development, when one programmer can check out the code from the repository, and can run the migrations on his own development database.
db:migrate, is the command that tells rails to update the database with new changes. Think of it as this way
when u say rails generate scaffold rails will generate files like a model, controller etc.. and it create a file under db/migrate which has the sql script to update the database.
Ex: if you run rails generate scaffold User name:string, then you will need a table called users in the database with the column 'name', that sql script will generated under db/migrate folder
with db:migrate, command, you are telling rails to migrate new sql scripts to the database, in the above case, it will creates the 'users' table
if you run rake -T, from your rails application root, you could see all the rake tasks
HTH :)
Related
I have created an application with Ruby and Rails. The thing is that when I was develpoing it, I had some problems with the migrations, because I created them but with a wrong syntax. What happened is that I deleted some of the files because sold migrations that didn´t work had the same name than the new ones, but in the middle of that I accidentally deleted some of the migrations (obviously after running rails db:migrate) that the project uses actually. So for instance, i have the Service table, which is related to the Reservation table because Service has reservation_id, but i don´t have the migration file that says AddReservationIdToService.
So now I want to use Heroku for production. the thing is that O have to change to postgresql because Heroku doesn't support sqlite. So i have to run the de:migrate again to create the tables and relationships in the new DB, but I need the files that I explained that I deleted. The question is:
Can I create the migrations manually, so when i run db:migrate for postgres the full structure of the database is created without lacking relations?
You don't really need the migrations to recreate the existing DB -- in fact it's not a good idea to try for a couple of reasons (including the missing migration file problem you encountered). You can simply run:
bin/rails db:schema:load
to populate a new database from the existing schema. If for some reason you haven't got a db/schema.rb checked under version control you can run:
bin/rails db:schema:dump
against the sqlite version to re-create a fresh schema file from the database.
You can also keep your migrations list tidy by occasionally zapping really old migrations, since all the cumulative changes are captured in the schema file.
Yes, you might create another couple of migration files.
Certify you have now the tables you wish locally with your sqlite. Draw these table in a piece of paper (or where it be the best fr you), then check this official API documentation of Rails.
Delete all migrations made before and create another according to the tables you drew.
The workflow is gonna be like:
1) "I need to create a table called Reservation, where is it shown on the documentation?"
2) "I need a table called Service, where is it shown on the documentation?
3) "I need to add a column with a foreign key to service named reservaton_id, how does this documentation says it?
For all this steps above, create the correspondent migration file as you normally have done.
The main difference here is not to run the migration locally. Instead, push your new version app to your heroku remote branch and there you run the migration, like:
heroku run rails db:migrate
Remember to not run this same migration locally because you already have these tables locally.
The last two advise is:
1) If your migration doesn't go as you expect, don't delete the migration file. Instead, run rails db:rollback and try again.
2) Keep tracking your migration files on the same branch of your version control.
I have run rails g model Task description:text. Then I have run rails console and put in a few tasks. I would now like to add more attributes to the create_tasks.rb file. Such as .string :title.
What I tried:
opened the file(create_tasks.rb), put the new line in. Then ran rake
db:migrate then went back into the console and opened the first task
and it doesn't show the title attribute.
also tried creating a new task using the title attribute. Error:
unknown title attribute for Task.
So, how do I update the model?
Welcome to Rails!
Here you can find some tutorials about how to deal with migrations:
http://edgeguides.rubyonrails.org/active_record_migrations.html
https://www.tutorialspoint.com/ruby-on-rails/rails-migrations.htm
Basically, every time you want to modify a migration, you must:
run rake db:rollback
modify the migration
run rake db:migrate
I hope this helps. Good coding!!
You need to rails db:rollback to roll the database back before the latest migration, add the new variables to the migration file, then run rails db:migrate to have the new parts of the migration file included.
If you need to roll back more revisions (if you have created more migrations since you created this model), you can either include the number of roll backs like
rails db:rollback STEP=<enter number of steps>
#e.g. rails db:rollback STEP=2
or, you could also rails db:reset which would remove all databases, recreate them, then remigrate them. Or you could rails db:drop to drop the database, then rails db:create and rails db:migrate to migrate the new database.
Do not edit the schema file. The schema file is automatically updated when you run migrations etc to match the content of your migration files.
I've dropped my table successfully from the database from the console, using
ActiveRecord::Migration.drop_table(:foo),
But for some reason the table data still shows in the schema file.
I would like a method to do this without having to manually remove the data, (Doesn't give a proper rails feeling to it.)
The schema file located in db/schema.rb was most likely generated the very last time you ran rake db:migrate. This is because rake db:migrate migrates your db through all of the migrations in your db/migrate folder, and then it calls the db:schema:dump task, which generates the schema file based on the current state of the database.
For your situation, after you have run ActiveRecord::Migration.drop_table(:foo) in the rails console, the current state of your database (now, no longer having the table foo) is not in sync with the schema.rb file that was generated when you last migrated.
What you want to do at this point is re-dump the schema based on the current state of the database, and you do this by running rake db:schema:dump. This will take the current state of your database (without the table foo) and generate your db/schema.rb file.
For more information on the schema file and its relationship to migrations, I would recommend looking at the Rails Guide on Active Record Migrations: Schema Dumping and You.
While the answers provided here work properly, I wanted something a bit more 'straightforward', I found it here: link
First enter rails console:
$rails console
Then just type:
ActiveRecord::Migration.drop_table(:foo)
Where :foo is the table name. And done, worked for me!
I'm trying to make a new rails project with an existing database.
I create the projects, configure the database.yml and can successfully do a db:schema:dump
This is all based on what I read on this site
http://blog.joelberghoff.com/2013/02/06/ruby-on-rails-tutorial-creating-a-rails-instance-from-an-existing-mysql-db/
And a few others.
After I do the dump, I copied the schema.rb into db/migrate and renamed to 001_createdatabase.rb as indicated in his tutorial.
Everything seems fine. However I cannot run the site as it tells me i have migrations pending. and the db:migrate:status indicates its that 001_createdatabase.rb that I created.
So based on that:
Was creating that 001_createdatabase migration from the schema.rb the right thing to do?
If it was how do I get rails to understand that it doesn't need to run that one as its already done?
Where does rails check to see if it has pending migrations. Obviously it must check the db/migrate folder and compare it to what?
Thanks
It would seem that in order to get credit for having run the migration, you need to actually run the migration. The migration, 001_createdatabase.rb, represents the migration that will take you from having an empty database -- no tables, no data -- to having your first version of the database. When you run rake db:migrate, rails checks to see if every file in the db/migrate directory has been run by looking for the numerical part of the migration file name in the schema_migrations table.
In your case, you've not run the migration, there is no entry in the schema_migrations table, so rails thinks, rightly, there is a migration to run.
To fix the issue, you can put the record that the migration has been run into schema_migrations or actually run the migration. You may need to create the schema_migrations table, rails will create it as needed.
If you want to run the migration, either drop your database (or create a new one -- you can drop the old one when this works), and point your rails application to the new database. run rake db:create and rake db:migrate. When it completes, you should have a new copy of your database, but one that has been created by the rails migration process. The schema_migrations table should have a record with 1 in the version column.
Is there a way to export database structure in the database from the rails application?
I believe there is a way to export Data from the db using rake.
rake db:migrate will create tables from migration files. Is there a command that does opposite way so that migration files will be generated from the db?
I have previously built a rails application during 3.0.x version.
I have many more migration files than the actual number of tables in the database because I have some migration file which adds and removes or change the column structure of the database.
Obviously, I can manually change each migration file and change it into migration file with 3.1 standard, but I just want to know if there is a way to export data base structure.
(Or, deriving from existing schema file)
Use command rake db:schema:dump, it will do it for you
Edit:
You might be interested in this link:
http://www.tutorialspoint.com/ruby-on-rails/rails-and-rake.htm
After rake db:schema:dump you can use a gem called schema_to_scaffold to generate a scaffold script.
it outputs: rails g scaffold users fname:string lname:string bdate:date email:string encrypted_password:string
from your schema.rb our your renamed schema.rb. Check here