ぼざクリタグ広場 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.
 
 
 
 
 
 

81 lines
2.4 KiB

  1. class CreatePostVersions < ActiveRecord::Migration[8.0]
  2. def change
  3. create_table :post_versions do |t|
  4. t.references :post, null: false, foreign_key: true
  5. t.integer :version_no, null: false
  6. t.string :event_type, null: false
  7. t.string :title
  8. t.string :url, limit: 768, null: false
  9. t.string :thumbnail_base, limit: 2000
  10. t.text :tags, null: false
  11. t.references :parent, foreign_key: { to_table: :posts }
  12. t.datetime :original_created_from
  13. t.datetime :original_created_before
  14. t.datetime :created_at, null: false
  15. t.references :created_by_user, foreign_key: { to_table: :users }
  16. t.index [:post_id, :version_no], unique: true
  17. t.check_constraint 'version_no > 0'
  18. t.check_constraint "event_type IN ('create', 'update', 'discard', 'restore')"
  19. end
  20. reversible do |dir|
  21. dir.up do
  22. execute <<~SQL
  23. INSERT INTO
  24. post_versions(
  25. post_id
  26. , version_no
  27. , event_type
  28. , title
  29. , url
  30. , thumbnail_base
  31. , tags
  32. , parent_id
  33. , original_created_from
  34. , original_created_before
  35. , created_at
  36. , created_by_user_id)
  37. SELECT
  38. posts.id
  39. , 1
  40. , 'create'
  41. , posts.title
  42. , posts.url
  43. , posts.thumbnail_base
  44. , COALESCE(tag_snapshots.tags, '')
  45. , posts.parent_id
  46. , posts.original_created_from
  47. , posts.original_created_before
  48. , posts.created_at
  49. , posts.uploaded_user_id
  50. FROM
  51. posts
  52. LEFT JOIN
  53. (
  54. SELECT
  55. post_tags.post_id
  56. , GROUP_CONCAT(tag_names.name ORDER BY tag_names.name SEPARATOR ' ') AS tags
  57. FROM
  58. post_tags
  59. INNER JOIN
  60. tags
  61. ON
  62. tags.id = post_tags.tag_id
  63. INNER JOIN
  64. tag_names
  65. ON
  66. tag_names.id = tags.tag_name_id
  67. WHERE
  68. post_tags.discarded_at IS NULL
  69. GROUP BY
  70. post_tags.post_id
  71. ) tag_snapshots
  72. ON
  73. tag_snapshots.post_id = posts.id
  74. SQL
  75. end
  76. end
  77. end
  78. end