Coder Social home page Coder Social logo

writing-migrations's Introduction

Let's Write Some Migrations

Objectives

  • 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

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.

After you finish defining the change method, run the migrations by running rake db:migrate in your terminal.

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.

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.

Let's add a :grade column and a :birthdate column. The :grade column type should be integer and the :birthdate column type should be string.

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. Be sure to use the change_column method. It takes three necessary arguments: change_column(table_name, column_name, type).

Active Record 5.x Migration Syntax

NOTE: As of Active Record 5.x, we can no longer inherit directly from ActiveRecord::Migration and must instead specify which version of Active Record / Rails the migration was written for. If we were writing a migration for Active Record 5.1, we would inherit from ActiveRecord::Migration[5.1]. Don't worry too much about this until you get to the Rails section. Until then, if you encounter an error like this...

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

  class CreateDogs < ActiveRecord::Migration[5.2]

...simply add [5.2] to the end of ActiveRecord::Migration, exactly as the error message instructs.

View Writing Our Own Migrations on Learn.co and start learning to code for free.

writing-migrations's People

Contributors

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

Watchers

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

writing-migrations's Issues

Spec Helper bad relative - mixed lab goals

Spec helper requires a relative that does not exist. Lab has students writing migrations, but tests are trying to test with a Student class that does not exist (the one required by spec helper).

Migrations are also already written in completely already, ie solution is given immediately.

writing migration

Error when I run learn

~/.../code/writing-migrations-onl01-seng-pt-050420 // ♥ > learn Traceback (most recent call last): 9: from /Users/ian/.rvm/gems/ruby-2.6.1/bin/learn-test:23:in

'
8: from /Users/ian/.rvm/gems/ruby-2.6.1/bin/learn-test:23:in load' 7: from /Users/ian/.rvm/gems/ruby-2.6.1/gems/learn-test-2.6.1/bin/learn-test:4:in <top (required)>'
6: from /Users/ian/.rvm/gems/ruby-2.6.1/gems/learn-test-2.6.1/bin/learn-test:4:in require_relative' 5: from /Users/ian/.rvm/gems/ruby-2.6.1/gems/learn-test-2.6.1/lib/learn_test.rb:2:in <top (required)>'
4: from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in require' 3: from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in require'
2: from /Users/ian/.rvm/gems/ruby-2.6.1/gems/oj-2.18.5/lib/oj.rb:43:in <top (required)>' 1: from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in require'
/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require': incompatible library version - /Users/ian/.rvm/gems/ruby-2.6.1/gems/oj-2.18.5/lib/oj/oj.bundle (LoadError)

Cannot install sqlite3 for this lab

I get this error when running bundle install, and cannot complete this lab:

"An error occurred while installing sqlite3 (1.3.13), and Bundler cannot
continue.
Make sure that gem install sqlite3 -v '1.3.13' --source 'https://rubygems.org/'
succeeds before bundling."

I tried running gem install sqlite3 -v '1.3.13' --source 'https://rubygems.org/' which did not help.

I also googled the issue and tried a suggested solution of putting sqlite3 in the gem file this way:

"group :development, :test do
gem 'sqlite3'
end"

Unfortunately this didn't resolve the issue either.

Empty migration files cause Rake to throw an error

|| rake aborted!
|| NameError: uninitialized constant AddGradeAndBirthdateToStudents

If you run rake db:migrate after writing the code in 01_create_students.rb, as the instructions in the lesson guide you to, you will receive the above error.

This error seems to happen because Rake sees the other migration files and expects them to have classes declared (Add_Grade_And_Birthdate_To_Students).

Either declare the classes in them, or don't include the files and ask the student to create the next two migration files.

JSON gem issues

The json (1.8.3) in the Gemfile.lock causes issues. Deleting Gemfile.lock then running bundle resolves the install issue.

missing info

The read me is missing the names of the 2&3 class for the migration db/migrate dir (yes you can get it from the error ) but it should be in the read me. What the name of the class is? that could be anything in the world as it is up to each dev to name it how he wants.

Already existing files cause migration issues + rake tasks

@AnnJohn @pletcher
The lab has the student run the migrations, then write three migration files, then re-run. Problem is the lab clones with all three files, so students edit them and run the migrations, but they don't update since migrations were already run. Student workaround is either delete files and recreate or rake:db:reset (if we tell them about it). I'd suggest removing the migration files.

Also the rake tasks is missing rake:db:create.

If you're okay with suggestions, I'm happy to make them myself if you want to tag me.

Default code raised Loaderror

After copying the repo locally, the default code from the lab created a load error when trying to use "rake -T" or "rake db:migrate"

my operating system - ubuntu (linux)

when running rake -T i received the following
// ♥ rake -T
rake aborted!
LoadError: cannot load such file -- sinatra/activerecord/rake
/home/alex/code/labs/writing-migrations-v-000/Rakefile:5:in require' /home/alex/code/labs/writing-migrations-v-000/Rakefile:5:in <top (required)>'
/home/alex/.rvm/gems/ruby-2.3.1/bin/ruby_executable_hooks:15:in eval' /home/alex/.rvm/gems/ruby-2.3.1/bin/ruby_executable_hooks:15:in

'
(See full trace by running task with --trace)

I opened a question for the lab. The instructor was able to fix the problem by removing the following code from the default Rakefile:
#require 'sinatra/activerecord/rake'
When the above code was commented out, rake worked properly.

This issue originated from the labs default code. Not sure if changes are required for lab.

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.