A Migration to the Backend
Tags: beginners, ruby, webdev
Published: 2022-11-22 06:17:46

Last week, our cohort wrapped up the third phase of the software engineering program at Flatiron School. It’s hard to believe how far we’ve come already and how quickly we have progressed. During this phase we began to shift our focus from React.js, and the front-end presentation of content, to laying the basis for our work in the backend. We began by learning the foundations of Ruby and SQL syntax before moving into some libraries we can use to build and manipulate our databases without going through quite as much legwork. 

By the end of the phase, we were able to use Ruby and Active Record to build databases in SQL, access them using custom built methods, and serve them to our front-end using Sinatra. The final result is a tailor-made API, which we used to seed and serve the data to the frontend for our phase-end project. 

In this blog post I want to talk about how to build a SQL database using Ruby and Active-Record. A lot of these steps will be performed automatically when we start using the Rails framework, but there is value in knowing how they work so that when issues arise down the line, we are prepared to do troubleshooting. This walkthrough presumes active record and rake gems are installed and configured appropriately. 

Step one is to create a db (database) if you do not have one in your project already. This is done using the command rake db:create

```
rake db:create
```

this command will create a directory to store your database and migrations. 

The next step is to create a migration to begin 
populating the db. We use the following command to create a db

```
rake db:create_migration NAME=<NAME_OF_MIGRATION>
```

By using the rake commands inherited from Active 
Record/Sinatra, we are able to create files with the appropriate naming schema and basic inheritance already in place. Let's create an example migration to build a new table. 

```
rake db:create_migration NAME=create_students
```

This will create a timestamped migration file in the 'migrate' directory with the named class and active record inheritance filled out automatically. Migrations are run in sequential order to avoid conflicts and enable seamless rollback functionality, so the naming convention here is actually very important. Luckily, it is taken care of for us. Here is the result:


```
class CreateStudents < ActiveRecord::Migration[6.1]
  def change
  end
end

```

In this file we will tell the migration what change we want to take place, in this case we want to create a table with a few columns for data. For this we will use the create table method, which requires one argument, the name of the table. Inside we are going to create columns by using the command t.<data_type> :<column_name>. 

```
class CreateStudents < ActiveRecord::Migration[6.1]
  def change
    create_table :interactions do |t|
      t.string :first_name
      t.string :last_name
      t.integer :grade
      t.integer :graduation_year
      t.boolean :has_graduated
    end
  end
end

```

with the migration prepared, all we have to do is run it, using the following command: 

```
rake db:migrate
```

this will add our change to the schema file and db, or create one to populate if the file doesn't exist yet. 

By using migrations, we are able to rollback any changes using a simple rake command. 

```
rake db:rollback
```

Using this feature, we can fix any mistakes we might have made in our migration before running it again. Changes and corrections can also be made using migrations to patch the existing table instead, but there are trade offs and benefits to each approach, that likely depend on the context of your project.

That was a simple walkthrough of creating a database using ruby and active record. Hopefully it provides some understanding of the general template for creating migrations and what the purpose of each component of the migration is.
I'm thrilled to begin learning Rails to streamline this process and open up all of the other capabilities it brings. For now, I hope this can help any other beginners, taking their very first steps into the world of API creation.