https://github.com/blocknotes/active_storage_db
An ActiveStorage service plugin to store files in a PostgreSQL / MySQL / MSSQL database
https://github.com/blocknotes/active_storage_db
activestorage activestorage-service mysql postgres postgresql rails ruby
Last synced: 3 months ago
JSON representation
An ActiveStorage service plugin to store files in a PostgreSQL / MySQL / MSSQL database
- Host: GitHub
- URL: https://github.com/blocknotes/active_storage_db
- Owner: blocknotes
- License: mit
- Created: 2020-08-08T13:02:53.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2025-04-05T22:39:08.000Z (3 months ago)
- Last Synced: 2025-04-05T23:24:08.527Z (3 months ago)
- Topics: activestorage, activestorage-service, mysql, postgres, postgresql, rails, ruby
- Language: Ruby
- Homepage:
- Size: 264 KB
- Stars: 40
- Watchers: 3
- Forks: 6
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: MIT-LICENSE
Awesome Lists containing this project
README
# Active Storage DB
[](https://badge.fury.io/rb/active_storage_db)
[](https://rubygems.org/gems/active_storage_db)
[](https://codeclimate.com/github/blocknotes/active_storage_db/maintainability)[](https://github.com/blocknotes/active_storage_db/actions/workflows/linters.yml)
[](https://github.com/blocknotes/active_storage_db/actions/workflows/specs_postgres_ruby34.yml)
[](https://github.com/blocknotes/active_storage_db/actions/workflows/specs_mysql_ruby34.yml)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*:```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:
```yml
attachments:
database: attachments
migrations_paths: config/attachments_migrate
# other connection details ...
```2. 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](https://guides.rubyonrails.org/engines.html#overriding-models-and-controllers)):
```rb
# 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
```3. Move the _ActiveStorageDB_ migrations to the specified migrations path
4. Execute the _rails db:migrate_ task## Misc
Some utility tasks are available:
```sh
# 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](http://blocknot.es), thanks also to the good guys that opened issues and pull requests from time to time.
For development information please check [this document](extra/development.md).
## 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](https://www.blocknot.es/about-me).
## License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).