เขียน migration ให้เป็น idempotent
13 ตุลาคม 2563
เมื่อต้องเขียน migration แบบทำลายข้อมูล ให้ใช้ if_exists: true ตอนที่จะลบตาราง และตรวจสอบก่อนว่าตารางมีอยู่จริงเมื่อต้องการเพิ่มหรือลบคอลัมน์ ด้วย ActiveRecord::Base.connection.table_exists?
กรณีที่ 1:
def up
drop_table :kittens, if_exists: true
end
กรณีที่ 2:
def up
return unless ActiveRecord::Base.connection.table_exists?(:kittens)
add_column :kittens, :kind, :integer
end