Skip to content

Instantly share code, notes, and snippets.

@lkfken
Created June 21, 2017 00:04
Show Gist options
  • Save lkfken/149b107ba448f3a4b905bd89f196523a to your computer and use it in GitHub Desktop.
Save lkfken/149b107ba448f3a4b905bd89f196523a to your computer and use it in GitHub Desktop.
Ruby Sequel Blob datatype
Sequel.migration do
up do
create_table(:office_files) do
primary_key :id
column :uuid, 'uniqueidentifier'
String :name
Date :created_at
File :file # datatype File = blob
end
end
down do
drop_table(:office_files)
end
end
class OfficeFile < Sequel::Model(DB)
plugin :uuid, :field => :uuid
plugin :timestamps
end
desc 'upload a file'
task :upload do
record = OfficeFile.new
record.name = 'some_file.docx'
record.file = File.read('some_file.docx', :binmode => true) # important: binmode must be true
record.save
end
desc 'download a file'
task :download do
record = OfficeFile.where(:name => 'some_file.docx').first
bin_stream = record.file
File.open('./tmp/download.docx', 'w') { |f| f.write(bin_stream) }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment