GithubHelp home page GithubHelp logo

phase-3-active-record-writing-migrations-lab's Introduction

Writing Migrations

Learning Goals

  • Write your own migrations
  • Run a migration to create a table
  • Run a migration to add a column to a table
  • Run a migration to change something in the table

Instructions

In this lesson, you'll get practice writing migration code and creating new migrations. Note that in order for the tests to work, the migrations must be created with the file names as described below, using numbers instead of timestamps.

Make sure to also follow proper naming conventions and use snake_case for the file names, and PascalCase for the class names. The names must match exactly in order for the migrations to work.

Creating a Table

The first thing we will do is create a table. In db/migrate/01_create_students.rb, write the code to create a table with Active Record. We've created a class for you called CreateStudents.

Define a method called change and use the Active Record create_table method within that method to create the table. The table should have a :name column with a type string. Check the Active Record Migration docs for more help with the syntax.

After you finish defining the change method, run the migrations by running:

$ bundle exec rake db:migrate

Check the status of your migration and verify the schema was updated correctly to include a students table with a name column before proceeding.

Adding a Column

The next thing we will do is add a couple of columns to the students table we just created. To do this, we will create a second migration file. We cannot add these columns to the existing file. Let's call our new file 02_add_grade_and_birthdate_to_students.rb. It should live in db/migrate just like the first migration.

Note: While we generally recommend using rake db:create_migration to create the migration files, for this lab you'll need to create the file name manually to ensure that the tests are able to find a file with the correct name.

This new migration will look similar to the previous one. We will need a class that inherits from ActiveRecord::Migration, and we will need to define a change method. Sticking to conventions, name the class AddGradeAndBirthdateToStudents, since that is what we're doing (and that is the camel case version of the filename, minus the numbers in front).

Inside #change, instead of #create_table, we will use the #add_column Active Record method. It takes three necessary arguments: add_column(table_name, column_name, type). Check the Active Record Migration docs for more help with the syntax.

Let's add two columns. You'll need to call #add_column twice: once for each column you're adding. Add a grade column that is an integer, and a birthdate column that is a string.

After you finish defining the change method, run the migrations by running:

$ bundle exec rake db:migrate

Check the status of your migration and verify the schema was updated correctly before proceeding. The students table should now have name, grade, and birthdate columns.

Changing a Column

Imagine you're creating an incredible web app to send out a birthday greeting on each student's birthday. While building this, you realize you accidentally stored your birthdate data as a string. It would be much easier to work with if the column type was datetime instead. Let's fix that.

Finally, we will change a column type, string to datetime. Same as before, you'll have to create another migration file. This time call it 03_change_datatype_for_birthdate.rb. Once again, name the class the same name as the file but with capital letters instead of underscores: ChangeDatatypeForBirthdate.

This migration will have the same setup as the last. Write a #change method, as usual. This time, in the #change method, be sure to use the #change_column method. It takes three necessary arguments: change_column(table_name, column_name, type). Check the Active Record Migration docs for more help with the syntax.

After you finish defining the #change method, run the migrations by running:

$ bundle exec rake db:migrate

Check the status of your migration and verify the schema was updated correctly before proceeding. The data type for the birthdate should now be datetime.

Resources

phase-3-active-record-writing-migrations-lab's People

Contributors

andrevollrath avatar annjohn avatar bhollan avatar brennenawana avatar curiositypaths avatar drakeltheryuujin avatar dstoll243 avatar franknowinski avatar ihollander avatar jmburges avatar joshuabamboo avatar lizbur10 avatar maxwellbenton avatar octosteve avatar pletcher avatar rrcobb avatar ruchiramani avatar samnags avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

phase-3-active-record-writing-migrations-lab's Issues

'02_add_grade_and_birthdate_to_students.rb' Section Needs A Better Hint For Version Needed For Class Inheritance Section

Canvas Link

https://learning.flatironschool.com/courses/5286/assignments/172870?module_item_id=376761

Concern

This lab needs a hint that the Version for the 'ActiveRecord::Migration' inherit section needs to be specified since the solution branch version clearly has this present, but no where on this lab mentions this issue:
https://github.com/learn-co-curriculum/phase-3-active-record-writing-migrations-lab/blob/solution/db/migrate/02_add_grade_and_birthdate_to_students.rb

Related Section:

This new migration will look similar to the previous one. We will need a class that inherits from ActiveRecord::Migration, and we will need to define a change method. Sticking to conventions, name the class AddGradeAndBirthdateToStudents, since that is what we're doing (and that is the camel case version of the filename, minus the numbers in front).

The problem with not including this is that if you try to use the inheritance section without specifying a version, the following error occurs:

Related Code:

class AddGradeAndBirthdateToStudents < ActiveRecord::Migration
  def change
    # Add a grade column that is an integer,
    add_column :students, :grade, :integer

    # and a birthdate column that is a string.
    add_column :students, :birthdate, :string
  end
end

Related Error:

samuelbanya@Samuels-MBP ~/hub/Development/code/phase-3/phase-3-active-record-writing-migrations-lab $ bundle exec rake db:migrate
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

Directly inheriting from ActiveRecord::Migration is not supported. Please specify the Rails release the migration was written for:

  class AddGradeAndBirthdateToStudents < ActiveRecord::Migration[4.2]
/Users/samuelbanya/.gem/gems/activerecord-6.1.4/lib/active_record/migration.rb:559:in `inherited'
/Users/samuelbanya/hub/Development/code/phase-3/phase-3-active-record-writing-migrations-lab/db/migrate/02_add_grade_and_birthdate_to_students.rb:1:in `<top (required)>'
/Users/samuelbanya/.gem/gems/activesupport-6.1.4/lib/active_support/dependencies.rb:332:in `require'
/Users/samuelbanya/.gem/gems/activesupport-6.1.4/lib/active_support/dependencies.rb:332:in `block in require'
/Users/samuelbanya/.gem/gems/activesupport-6.1.4/lib/active_support/dependencies.rb:299:in `load_dependency'
/Users/samuelbanya/.gem/gems/activesupport-6.1.4/lib/active_support/dependencies.rb:332:in `require'
/Users/samuelbanya/.gem/gems/activerecord-6.1.4/lib/active_record/migration.rb:1045:in `load_migration'
/Users/samuelbanya/.gem/gems/activerecord-6.1.4/lib/active_record/migration.rb:1041:in `migration'
/Users/samuelbanya/.gem/gems/activerecord-6.1.4/lib/active_record/migration.rb:1037:in `disable_ddl_transaction'
/Users/samuelbanya/.gem/gems/activerecord-6.1.4/lib/active_record/migration.rb:1387:in `use_transaction?'
/Users/samuelbanya/.gem/gems/activerecord-6.1.4/lib/active_record/migration.rb:1379:in `ddl_transaction'
/Users/samuelbanya/.gem/gems/activerecord-6.1.4/lib/active_record/migration.rb:1328:in `execute_migration_in_transaction'
/Users/samuelbanya/.gem/gems/activerecord-6.1.4/lib/active_record/migration.rb:1302:in `each'
/Users/samuelbanya/.gem/gems/activerecord-6.1.4/lib/active_record/migration.rb:1302:in `migrate_without_lock'
/Users/samuelbanya/.gem/gems/activerecord-6.1.4/lib/active_record/migration.rb:1253:in `migrate'
/Users/samuelbanya/.gem/gems/activerecord-6.1.4/lib/active_record/migration.rb:1086:in `up'
/Users/samuelbanya/.gem/gems/activerecord-6.1.4/lib/active_record/migration.rb:1061:in `migrate'
/Users/samuelbanya/.gem/gems/activerecord-6.1.4/lib/active_record/tasks/database_tasks.rb:237:in `migrate'
/Users/samuelbanya/.gem/gems/activerecord-6.1.4/lib/active_record/railties/databases.rake:92:in `block (3 levels) in <top (required)>'
/Users/samuelbanya/.gem/gems/activerecord-6.1.4/lib/active_record/railties/databases.rake:90:in `each'
/Users/samuelbanya/.gem/gems/activerecord-6.1.4/lib/active_record/railties/databases.rake:90:in `block (2 levels) in <top (required)>'
/Users/samuelbanya/.gem/gems/rake-13.0.6/exe/rake:27:in `<top (required)>'
/Users/samuelbanya/.gem/gems/bundler-2.2.23/lib/bundler/cli/exec.rb:63:in `load'
/Users/samuelbanya/.gem/gems/bundler-2.2.23/lib/bundler/cli/exec.rb:63:in `kernel_load'
/Users/samuelbanya/.gem/gems/bundler-2.2.23/lib/bundler/cli/exec.rb:28:in `run'
/Users/samuelbanya/.gem/gems/bundler-2.2.23/lib/bundler/cli.rb:474:in `exec'
/Users/samuelbanya/.gem/gems/bundler-2.2.23/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
/Users/samuelbanya/.gem/gems/bundler-2.2.23/lib/bundler/vendor/thor/lib/thor/invocation.rb:127:in `invoke_command'
/Users/samuelbanya/.gem/gems/bundler-2.2.23/lib/bundler/vendor/thor/lib/thor.rb:392:in `dispatch'
/Users/samuelbanya/.gem/gems/bundler-2.2.23/lib/bundler/cli.rb:30:in `dispatch'
/Users/samuelbanya/.gem/gems/bundler-2.2.23/lib/bundler/vendor/thor/lib/thor/base.rb:485:in `start'
/Users/samuelbanya/.gem/gems/bundler-2.2.23/lib/bundler/cli.rb:24:in `start'
/Users/samuelbanya/.gem/gems/bundler-2.2.23/exe/bundle:49:in `block in <top (required)>'
/Users/samuelbanya/.gem/gems/bundler-2.2.23/lib/bundler/friendly_errors.rb:128:in `with_friendly_errors'
/Users/samuelbanya/.gem/gems/bundler-2.2.23/exe/bundle:37:in `<top (required)>'
/Users/samuelbanya/.rvm/gems/ruby-2.7.4/bin/bundle:25:in `load'
/Users/samuelbanya/.rvm/gems/ruby-2.7.4/bin/bundle:25:in `<main>'
/Users/samuelbanya/.rvm/gems/ruby-2.7.4/bin/ruby_executable_hooks:22:in `eval'
/Users/samuelbanya/.rvm/gems/ruby-2.7.4/bin/ruby_executable_hooks:22:in `<main>'

Caused by:
StandardError: Directly inheriting from ActiveRecord::Migration is not supported. Please specify the Rails release the migration was written for:

Additional Context

No response

Suggested Changes

This is my suggested change:

This new migration will look similar to the previous one. We will need a class that inherits from ActiveRecord::Migration, and we will need to define a change method.  NOTE: You will need to also specify the related version being used with brackets after the inheritance statement.

Sticking to conventions, name the class AddGradeAndBirthdateToStudents, since that is what we're doing (and that is the camel case version of the filename, minus the numbers in front).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.