20170304202101_add_type_to_media_attachments.rb 914 B

123456789101112131415161718192021222324252627
  1. # frozen_string_literal: true
  2. class AddTypeToMediaAttachments < ActiveRecord::Migration[5.0]
  3. class MigrationMediaAttachment < ApplicationRecord
  4. self.table_name = :media_attachments
  5. enum type: [:image, :gifv, :video]
  6. IMAGE_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif'].freeze
  7. VIDEO_MIME_TYPES = ['video/webm', 'video/mp4'].freeze
  8. end
  9. def up
  10. add_column :media_attachments, :type, :integer, default: 0, null: false
  11. MigrationMediaAttachment.reset_column_information
  12. MigrationMediaAttachment
  13. .where(file_content_type: MigrationMediaAttachment::IMAGE_MIME_TYPES)
  14. .update_all(type: MigrationMediaAttachment.types[:image])
  15. MigrationMediaAttachment
  16. .where(file_content_type: MigrationMediaAttachment::VIDEO_MIME_TYPES)
  17. .update_all(type: MigrationMediaAttachment.types[:video])
  18. end
  19. def down
  20. remove_column :media_attachments, :type
  21. end
  22. end