Skip to content

Commit aa58bfa

Browse files
authored
Merge pull request #57 from blocknotes/dev/docker-setup
Docker dev setup
2 parents d261cf9 + 473ce4a commit aa58bfa

16 files changed

Lines changed: 302 additions & 63 deletions

.rubocop.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,11 @@ RSpec/NestedGroups:
4444
Style/ClassAndModuleChildren:
4545
Exclude:
4646
- 'lib/active_storage/service/db_service.rb'
47+
48+
Style/FrozenStringLiteralComment:
49+
Exclude:
50+
- spec/dummy/db/**/schema*.rb
51+
52+
Style/NumericLiterals:
53+
Exclude:
54+
- spec/dummy/db/**/schema*.rb

Gemfile

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,43 @@
33
source 'https://rubygems.org'
44
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
55

6-
gemspec
6+
if ENV['DEVEL'] == '1'
7+
rails_ver = ENV.fetch('RAILS_VERSION')
8+
gem 'rails', rails_ver
9+
10+
gem 'activeadmin', ENV.fetch('ACTIVEADMIN_VERSION')
11+
gem 'active_storage_db', path: './'
12+
gem 'appraisal', '~> 2.4'
13+
gem 'factory_bot_rails', '~> 6.1'
14+
15+
if rails_ver.start_with?('7.0')
16+
gem 'concurrent-ruby', '1.3.4'
17+
end
18+
else
19+
gemspec
20+
end
721

822
if ENV['DB_TEST'] == 'mssql'
923
gem 'activerecord-sqlserver-adapter', '7.0.3.0'
1024
gem 'tiny_tds'
1125
end
1226
gem 'mysql2' if ENV['DB_TEST'] == 'mysql'
1327
gem 'pg' if ['postgres', 'postgresql'].include? ENV['DB_TEST']
28+
gem 'sqlite3' if ENV['DB_TEST'] == 'sqlite'
1429

30+
gem 'bigdecimal'
1531
gem 'image_processing', '>= 1.2'
16-
17-
gem 'webrick'
18-
19-
gem 'simplecov'
20-
gem 'simplecov-lcov'
32+
gem 'mutex_m'
33+
gem 'puma'
34+
gem 'sprockets-rails'
2135

2236
# Testing
2337
gem 'capybara'
2438
gem 'rspec_junit_formatter'
2539
gem 'rspec-rails'
2640
gem 'selenium-webdriver'
41+
gem 'simplecov'
42+
gem 'simplecov-lcov'
2743

2844
# Linters
2945
gem 'brakeman'

Makefile

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
help:
2+
@echo "Main targets: up / down / console / shell"
3+
4+
# Docker commands
5+
down:
6+
docker compose down
7+
8+
up:
9+
docker compose up
10+
11+
attach:
12+
docker compose attach app
13+
14+
up_attach:
15+
docker compose up -d && docker compose attach app
16+
17+
cleanup:
18+
docker container rm -f active_storage_db_app && docker image rm -f active_storage_db-app
19+
20+
# Rails specific commands
21+
db_reset:
22+
docker compose exec app bin/rails db_reset
23+
24+
console:
25+
docker compose exec -e "PAGER=more" app bin/rails console
26+
27+
routes:
28+
docker compose exec app bin/rails routes
29+
30+
specs:
31+
docker compose exec app bin/rspec --fail-fast
32+
33+
# Other commands
34+
bundle:
35+
docker compose exec app bundle
36+
37+
shell:
38+
docker compose exec -e "PAGER=more" app bash
39+
40+
lint:
41+
docker compose exec app bin/rubocop

README.md

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@
88
[![specs Postgres](https://github.com/blocknotes/active_storage_db/actions/workflows/specs_postgres_71.yml/badge.svg)](https://github.com/blocknotes/active_storage_db/actions/workflows/specs_postgres_71.yml)
99
[![specs MySQL](https://github.com/blocknotes/active_storage_db/actions/workflows/specs_mysql_71.yml/badge.svg)](https://github.com/blocknotes/active_storage_db/actions/workflows/specs_mysql_71.yml)
1010

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

13-
Main features:
14-
- attachment data stored in a binary field (or blob);
15-
- all service methods implemented;
16-
- supports Rails _6_ and _7_.
14+
Attachment data get stored in a binary field (or blob).
1715

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

@@ -69,17 +67,18 @@ bin/rails 'asdb:search[some_filename]'
6967
bin/rails 'asdb:download[123,/tmp]'
7068
```
7169

70+
## Development
71+
72+
Project created by [Mattia Roccoberton](http://blocknot.es), thanks also to the good guys that opened issues and pull requests from time to time.
73+
74+
For development information please check [this document](extra/development.md).
75+
7276
## Do you like it? Star it!
7377

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

7680
Or consider offering me a coffee, it's a small thing but it is greatly appreciated: [about me](https://www.blocknot.es/about-me).
7781

78-
## Development
79-
80-
- Author: [Mattia Roccoberton](https://blocknot.es/)
81-
- Inspired by [activestorage-database-service](https://github.com/TitovDigital/activestorage-database-service) project
82-
8382
## License
8483

8584
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).

docker-compose.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
services:
2+
app:
3+
container_name: 'active_storage_db_app'
4+
build:
5+
context: .
6+
dockerfile: ./extra/Dockerfile
7+
args:
8+
# Debian-based Ruby image:
9+
RUBY_IMAGE: ruby:3.4-slim
10+
UID: ${UID}
11+
environment:
12+
ACTIVEADMIN_VERSION: ~> 3.3
13+
DB_TEST: sqlite
14+
RAILS_VERSION: ~> 8.0
15+
user: "${UID}:${GID}"
16+
ports:
17+
- '3000:3000'
18+
working_dir: '/app'
19+
volumes:
20+
- '.:/app'
21+
stdin_open: true
22+
tty: true
23+
entrypoint:
24+
- /bin/sh
25+
- ./extra/entrypoint.sh

extra/Dockerfile

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
ARG RUBY_IMAGE=ruby:3
2+
FROM ${RUBY_IMAGE}
3+
4+
ARG UID
5+
6+
ENV DEVEL=1
7+
ENV LANG=C.UTF-8
8+
ENV RAILS_ENV=development
9+
10+
RUN apt-get update -qq
11+
RUN DEBIAN_FRONTEND=noninteractive apt-get install -yqq --no-install-recommends build-essential libvips42 libyaml-dev nano
12+
13+
RUN gem install bundler
14+
RUN echo 'gem: --no-document' > /etc/gemrc
15+
16+
RUN useradd -u ${UID} --shell /bin/bash app
17+
RUN mkdir -p /home/app && chown -R app:app /home/app
18+
RUN chown -R app /usr/local/bundle
19+
20+
USER ${UID}
21+
COPY . /app

extra/README.md

Lines changed: 0 additions & 44 deletions
This file was deleted.

extra/dev_setup.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/sh
2+
3+
export DEVEL=1
4+
5+
export RAILS_VERSION=8.0.2
6+
export ACTIVEADMIN_VERSION=3.3.0
7+
8+
export RAILS_ENV=development
9+
10+
export DB_TEST=sqlite

extra/development.md

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# Development
2+
3+
There 3 ways to interact with this project:
4+
5+
1) Using Docker:
6+
7+
```sh
8+
# Run rails server on the dummy app (=> http://localhost:3000 to access to ActiveAdmin):
9+
make up
10+
# Enter in a Rails console (with the dummy app started):
11+
make console
12+
# Enter in a shell (with the dummy app started):
13+
make shell
14+
# Run the linter on the project (with the dummy app started):
15+
make lint
16+
# Run the test suite (with the dummy app started):
17+
make specs
18+
# Remove container and image:
19+
make cleanup
20+
# To try different versions of Ruby/Rails/ActiveAdmin edit docker-compose.yml
21+
# For more commands please check the Makefile
22+
```
23+
24+
2) Using Appraisal:
25+
26+
```sh
27+
export RAILS_ENV=development
28+
# Install dependencies:
29+
bin/appraisal
30+
# Run server (or any command):
31+
bin/appraisal rails s
32+
# Or with a specific configuration:
33+
bin/appraisal rails80-activeadmin rails s
34+
```
35+
36+
3) With a local setup:
37+
38+
```sh
39+
# Dev setup (set the required envs):
40+
source extra/dev_setup.sh
41+
# Install dependencies:
42+
bundle update
43+
# Prepare the app
44+
bin/rails db:reset
45+
# Run server (or any command):
46+
bin/rails s
47+
# To try different versions of Rails/ActiveAdmin edit extra/dev_setup.sh
48+
```
49+
50+
Sample code to attach a file:
51+
52+
```ruby
53+
# Create a test post in the dummy app
54+
post = Post.create!(title: "test1")
55+
post.some_file.attach(io: Rails.root.join("../../README.md").open, filename: "README.md")
56+
```
57+
58+
## Releases
59+
60+
```sh
61+
# Update lib/active_storage_db/version.rb with the new version
62+
# Update the gemfiles:
63+
bin/appraisal
64+
```
65+
66+
## Old dev setup
67+
68+
Local tests:
69+
70+
```sh
71+
# Running tests on Rails 6.1:
72+
DB_TEST=postgres bin/appraisal rails-6_1-postgres rails db:test:prepare
73+
DB_TEST=postgres bin/appraisal rails-6_1-postgres rspec
74+
75+
# Running tests on Rails 7.0:
76+
DB_TEST=postgres bin/appraisal rails-7_0-postgres rails db:test:prepare
77+
DB_TEST=postgres bin/appraisal rails-7_0-postgres rspec
78+
79+
# Running tests on Rails 7.1:
80+
DB_TEST=postgres bin/appraisal rails-7_1-postgres rails db:test:prepare
81+
DB_TEST=postgres bin/appraisal rails-7_1-postgres rspec
82+
83+
# Eventually recreate the DB:
84+
DB_TEST=postgres bin/appraisal rails-7_0-postgres rails db:drop db:create db:migrate
85+
```
86+
87+
Using docker:
88+
89+
```sh
90+
# Run a specific test configuration
91+
docker compose up --abort-on-container-exit -- tests_30_mysql
92+
docker compose up --abort-on-container-exit -- tests_30_postgres
93+
# Cleanup (also removing local images):
94+
docker compose down --rmi local
95+
```

extra/entrypoint.sh

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1-
#!/bin/sh
2-
set -e
3-
exec "$@"
1+
echo "> Install dependencies"
2+
rm -f Gemfile.lock && bundle install
3+
4+
echo "> Run pending migrations"
5+
cd spec/dummy && bundle exec rails db:migrate
6+
7+
echo "> Start Rails server"
8+
cd /app && rm -f spec/dummy/tmp/pids/server.pid && bundle exec rails s -b 0.0.0.0

0 commit comments

Comments
 (0)