ぼざクリタグ広場 https://hub.nizika.monster
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

28 lines
614 B

  1. class AddVersionNoToPosts < ActiveRecord::Migration[8.0]
  2. def up
  3. add_column :posts, :version_no, :integer
  4. execute <<~SQL
  5. UPDATE
  6. posts
  7. SET
  8. version_no = (
  9. SELECT
  10. MAX(version_no)
  11. FROM
  12. post_versions
  13. WHERE
  14. post_id = posts.id)
  15. SQL
  16. change_column_null :posts, :version_no, false
  17. add_check_constraint :posts, 'version_no > 0', name: 'chk_posts_version_no_positive'
  18. end
  19. def down
  20. remove_check_constraint :posts, name: 'chk_posts_version_no_positive'
  21. remove_column :posts, :version_no
  22. end
  23. end