Files
codeocean/db/migrate/20140918063522_add_hashed_content_to_files.rb
Sebastian Serth 6c44ffbd5c Fix missing/incorrect Model specifications in migrations
We need to define the required models within the migration (not below) to work reliably.
2024-07-04 11:02:10 +02:00

28 lines
576 B
Ruby

# frozen_string_literal: true
class AddHashedContentToFiles < ActiveRecord::Migration[4.2]
class CodeOcean::File < ApplicationRecord
before_validation :hash_content, if: :content_present?
private
def content_present?
content? || native_file?
end
def hash_content
self.hashed_content = Digest::MD5.new.hexdigest(read || '')
end
end
def change
add_column :files, :hashed_content, :string
reversible do |direction|
direction.up do
CodeOcean::File.unscope(:order).find_each(&:save)
end
end
end
end