Introduction to migration

Introduction to migration

What is migration?

text Migration is a convenient way to make continuous changes to your database schema easy. Since the migration uses a Ruby DSL, you don't have to write raw SQL and make changes to the schema independent of the database type.

I see, when you add a table or when you add an attribute to a table You need to create SQL, right?

CREATE TABLE HOGE

You can write raw SQL and make changes to the database like this, but you don't have to do that. It also does not depend on the database type. I used it somehow, but it's amazing.

Migration example

Here is an example of adding a table called products.

It contains a string column called name and a text column called description. The primary key is implicitly added with the name id. id is the default primary key in the Active Record model.

ruby.rb



class CreateProducts < ActiveRecord::Migration[5.0]
  def change
    create_table :products do |t|
      t.string :name
      t.text :description
 
      t.timestamps
    end
  end
end

Performing this migration will generate a table. You can also drop this table by rolling back.

Create migration

rails generate migration AddPartNumberToProducts part_number:string

Running the above code will generate a migration file like the one below. The content is to add a column to products.

ruby.rb


class AddPartNumberToProducts < ActiveRecord::Migration[5.0]
  def change
    add_column :products, :part_number, :string
  end
end

Reflected in DB

Reflect the previous migration file in the DB.

#Run
rails db:migrate
#roll back
rails db:rollback

You can execute migration or rollback as described above.

Roughly, I summarized the migration. I used to use it somehow, but I've gradually come to understand what it's like.

That's all for today ** 91 days to become a full-fledged engineer **

Recommended Posts

Introduction to migration
Introduction to Ruby 2
Introduction to SWING
Introduction to web3j
Introduction to Micronaut 1 ~ Introduction ~
[Java] Introduction to Java
Introduction to java
Introduction to Doma
Introduction to JAR files
Introduction to Ratpack (8)-Session
Introduction to RSpec 1. Test, RSpec
Introduction to bit operation
Introduction to Ratpack (6) --Promise
Introduction to PlayFramework 2.7 ① Overview
Introduction to Android Layout
Introduction to Practical Programming
Introduction to javadoc command
Introduction to jar command
Introduction to Ratpack (2)-Architecture
Introduction to lambda expression
Introduction to java command
Introduction to RSpec 2. RSpec setup
Introduction to Keycloak development
Introduction to javac command
Introduction to Design Patterns (Builder)
Migration from Cobol to JAVA
Introduction to RSpec 5. Controller specs
Introduction to RSpec 6. System specifications
Introduction to Android application development
Introduction to RSpec 3. Model specs
Introduction to Metabase ~ Environment Construction ~
Introduction to Ratpack (7) --Guice & Spring
(Dot installation) Introduction to Java8_Impression
Introduction to Design Patterns (Composite)
Introduction to Micronaut 2 ~ Unit test ~
Introduction to JUnit (study memo)
Introduction to Spring Boot ① ~ DI ~
[Java] Introduction to lambda expressions
Introduction to Apache Beam (2) ~ ParDo ~
[Ruby] Introduction to Ruby Error statement
Introduction to EHRbase 2-REST API
Introduction to design patterns Prototype
GitHub Actions Introduction to self-made actions
[Java] Introduction to Stream API
Introduction to Design Patterns (Iterator)
Introduction to Spring Boot Part 1
Introduction to Ratpack (1) --What is Ratpack?
XVim2 introduction memo to Xcode12.3
How to rollback migration files
to_ ○
Introduction to RSpec-Everyday Rails Summary-
Introduction to Design Patterns (Strategy)
[Introduction to rock-paper-scissors games] Java
Introduction to Linux Container / Docker (Part 1)
Introduction to swift practice output Chapter5
protocol buffeer migration from 2.x to 3.x
[Introduction to Java] About lambda expressions
Introduction to algorithms in java-cumulative sum
[Introduction to Java] About Stream API
Introduction to Functional Programming (Java, Javascript)
Introduction to Ruby processing system self-made