The test database is entirely managed by the testing tools and should never be

The test database is entirely managed by the testing

This preview shows page 140 - 142 out of 517 pages.

. The test database is entirely managed by the testing tools and should never be modified manually: it is wiped clean and repopulated at the beginning of every testing run, as we’ll see in Chapter 8 . An empty database was created for us by the rails new command in the file db/development.sqlite3 , as specified in config/database.yml . We need to create a table for movie information. We could use the sqlite3 command-line tool or a SQLite GUI tool to do this manually, but how would we later create the table in our production database when we deploy? Typing the same commands a second time isn’t DRY, and the exact commands might be hard to remember. Further, if the production database is something other than SQLite3 (as is almost certainly the case), the specific commands might be different. And in the future, if we add more tables or make other changes to the database, we’ll face the same problem. A better alternative is a migration —a portable script for changing the database schema (layout of tables and columns) in a consistent and repeatable way, just as Bundler uses the Gemfile to identify and install necessary gems (libraries) in a consistent and repeatable way. Changing the schema using migrations is a four-step process: 1. Create a migration describing what changes to make. As with rails new , Rails provides a migration generator that gives you the boilerplate code, plus various helper methods to describe the migration. 2. Apply the migration to the development database. Rails defines a rake task for this. 3. Assuming the migration succeeded, update the test database’s schema by running rake db:test:prepare . 4. Run your tests, and if all is well, apply the migration to the production database and deploy the new code to production. The process for applying migrations in production depends on the deployment environment; Appendix A.8 covers how to do it using Heroku, the cloud computing deployment environment used for the examples in this book. We’ll use the first 3 steps of this process to add a new table that stores each movie’s title, rating, description, and release date, to match Chapter 2 . Each migration needs a name, and since this migration will create the movies table, we choose the name CreateMovies. Run the command rails generate migration create_movies , and if successful, you will find a new file under db/migrate
Image of page 140
whose name begins with the creation time and date and ends with the name you supplied, for example, 20111201180638_create_movies.rb . (This naming scheme lets Rails apply migrations in the order they were created, since the file names will sort in date order.) Edit this file to make it look like Figure 4.3 . As you can see, migrations illustrate an idiomatic use of blocks: the ActiveRecord::Migration#create_table method takes a block of 1 argument and yields to that block an object representing the table being created. The methods string , datetime , and so on are provided by this table object, and calling them results in creating columns in the newly-created database table; for example, t.string ’title’ creates a column named
Image of page 141
Image of page 142

You've reached the end of your free preview.

Want to read all 517 pages?

  • Spring '19
  • Dr.Marcos

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes
A+ icon
Ask Expert Tutors