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.
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.
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.
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.
class AddPartNumberToProducts < ActiveRecord::Migration[5.0] def change add_column :products, :part_number, :string end end
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 **