Skip to content

An ActiveStorage service plugin to store files in a PostgreSQL / MySQL / MSSQL database

License

Notifications You must be signed in to change notification settings

blocknotes/active_storage_db

Folders and files

NameName
Last commit message
Last commit date
Mar 27, 2025
Sep 6, 2022
Mar 31, 2025
Aug 11, 2020
Sep 6, 2022
Mar 31, 2025
Mar 27, 2025
Mar 9, 2025
Mar 31, 2025
Mar 31, 2025
Mar 3, 2022
Mar 23, 2025
Mar 3, 2022
Oct 21, 2023
Aug 11, 2020
Mar 27, 2025
Mar 27, 2025
Mar 27, 2025
Aug 8, 2020
Mar 31, 2025
Mar 24, 2025
Mar 23, 2025
Nov 1, 2023

Repository files navigation

Active Storage DB

gem version gem downloads maintainability

linters Specs Postgres Ruby 3.4 Specs MySQL Ruby 3.4

An Active Storage service upload/download plugin that stores files in a PostgreSQL or MySQL database. Experimental support also for MSSQL and SQLite.

Attachment data get stored in a binary field (or blob).

Useful also with platforms like Heroku (due to their ephemeral file system).

Installation

  • Setup Active Storage in your Rails application
  • Add to your Gemfile gem 'active_storage_db' (and execute: bundle)
  • Install the gem migrations: bin/rails active_storage_db:install:migrations (and execute: bin/rails db:migrate)
  • Add to your config/routes.rb: mount ActiveStorageDB::Engine => '/active_storage_db'
  • Change Active Storage service in config/environments/development.rb to: config.active_storage.service = :db
  • Add to config/storage.yml:
db:
  service: DB

Customizations

To setup a separate database connection for the ActiveStorageDB migrations and files data:

  1. Add a different database configuration per environment to config/database.yml, e.g:
attachments:
  database: attachments
  migrations_paths: config/attachments_migrate
  # other connection details ...
  1. Extend the ActiveStorage base record class providing the connects_to options (updating config/application.rb / using an initializer for ActiveStorageDB / overriding the base model like in the Rails guide):
# e.g. app/overrides/models/active_storage_db/application_record_override.rb
ActiveStorageDB::ApplicationRecord.class_eval do
  connects_to database: { reading: :attachments, writing: :attachments }
end
  1. Move the ActiveStorageDB migrations to the specified migrations path
  2. Execute the rails db:migrate task

Misc

Some utility tasks are available:

# list attachments ordered by blob id desc (with limit 100):
bin/rails 'asdb:list'
# search attachments by filename (or part of it)
bin/rails 'asdb:search[some_filename]'
# download attachment by blob id (retrieved with list or search tasks) - the second argument is the destination:
bin/rails 'asdb:download[123,/tmp]'

Development

Project created by Mattia Roccoberton, thanks also to the good guys that opened issues and pull requests from time to time.

For development information please check this document.

Do you like it? Star it!

If you use this component just star it. A developer is more motivated to improve a project when there is some interest.

Or consider offering me a coffee, it's a small thing but it is greatly appreciated: about me.

License

The gem is available as open source under the terms of the MIT License.