Skip to content

Commit

Permalink
Fix Hanami multi-environment database setup
Browse files Browse the repository at this point in the history
- Create development and test databases in setup.sh
- Remove HANAMI_ENV from development image so tests run in the test
  environment / database
- Fix database_cleaner support file to load gem
- Temporarily remove "clean_with" from cleaner integration because we
  cannot truncate the startup/fixture data (without dealing with
  referential integrity and reloading it with some kind of migration)
  • Loading branch information
botimer committed Nov 7, 2023
1 parent 8ad5031 commit d2fb084
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 5 deletions.
3 changes: 3 additions & 0 deletions db/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ echo " host : $host"
echo " port : $port"
echo " database : $database"

mariadb -u root --host=$host --port=$port -e "CREATE DATABASE ${database} DEFAULT CHARACTER SET utf8"
mariadb -u root --host=$host --port=$port -e "GRANT ALL ON ${database}.* TO ${user}@'%' IDENTIFIED by '${password}'"

mariadb --user=$user --host=$host --port=$port --password=$password $database < "$directory/tables.sql"
mariadb --user=$user --host=$host --port=$port --password=$password $database < "$directory/root.sql"
mariadb --user=$user --host=$host --port=$port --password=$password $database < "$directory/keys.sql"
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ services:
command:
- /bin/bash
- "-c"
- "/sql/setup.sh -u lauth -p lauth -h db.lauth.local lauth"
- "/sql/setup.sh -u lauth -p lauth -h db.lauth.local lauth_development && /sql/setup.sh -u lauth -p lauth -h db.lauth.local lauth_test"

volumes:
mariadb_data:
Expand Down
2 changes: 1 addition & 1 deletion lauth/.env.development
Original file line number Diff line number Diff line change
@@ -1 +1 @@
DATABASE_URL=mysql2://lauth:lauth@db.lauth.local:3306/lauth
DATABASE_URL=mysql2://lauth:lauth@db.lauth.local:3306/lauth_development
2 changes: 0 additions & 2 deletions lauth/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,6 @@ FROM dev-gems AS development
COPY . .
RUN chown -R ${UID}:${GID} /gems && chown -R ${UID}:${GID} /lauth/lauth

ENV HANAMI_ENV=development

EXPOSE 2300
USER $UNAME
CMD ["hanami", "server"]
6 changes: 5 additions & 1 deletion lauth/spec/support/database_cleaner.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
require "database_cleaner-sequel"

Hanami.app.prepare(:persistence)
DatabaseCleaner[:sequel, db: Hanami.app["persistence.db"]]

RSpec.configure do |config|
config.before(:suite) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
# We can't truncate without more thought because of fixture data and
# referential integrity in our database.
# DatabaseCleaner.clean_with(:truncation)
end

config.around(:each, type: :database) do |example|
Expand Down

0 comments on commit d2fb084

Please sign in to comment.