Skip to content

Commit

Permalink
Add postgres as main database
Browse files Browse the repository at this point in the history
- Update README with instructions to config application and setup database;
- Add money gem to handle financial values;
- Add DATABASE_URL as required environment variable;
- Add fake data for development/test purposes;
- Configure sequel to handle database migrations;
- Add golang as development dependency to running local services;
- Add Procfile to start/stop related services;
- Create custom tasks to handle postgres database;
  • Loading branch information
ricardopacheco committed Feb 16, 2024
1 parent 3366193 commit dfa934b
Show file tree
Hide file tree
Showing 18 changed files with 296 additions and 6 deletions.
1 change: 1 addition & 0 deletions .env.development.template
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
APP_ENV=development
DATABASE_URL=postgresql://postgres:@127.0.0.1/auction_fun_core_development?pool=10
1 change: 1 addition & 0 deletions .env.test.template
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
APP_ENV=test
DATABASE_URL=postgresql://postgres:@127.0.0.1:5432/auction_fun_core_test?pool=10
2 changes: 2 additions & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
ruby 3.3.0
postgres 16.1
golang 1.21.5
11 changes: 9 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,29 @@ source "https://rubygems.org"
gemspec

gem "activesupport", "7.1.3"
gem "dotenv", "3.0.2"
gem "dry-events", "1.0.1"
gem "dry-matcher", "1.0.0"
gem "dry-monads", "1.6.0"
gem "dry-system", "1.0.1"
gem "dry-validation", "1.10.0"
gem "dotenv", "3.0.2"
gem "money", "6.16.0"
gem "pg", "1.5.5"
gem "rake", "13.1.0"
gem "rom", "5.3.0"
gem "rom-sql", "3.6.2"
gem "yard", "0.9.34"
gem "zeitwerk", "2.6.13"

group :development, :test do
gem "faker", "3.2.3"
gem "pry", "0.14.2"
gem "rspec", "3.13.0"
gem "standard", "1.33.0"
gem "pry", "0.14.2"
end

group :test do
gem "database_cleaner-sequel", "2.0.2"
gem "rom-factory", "0.12.0"
gem "simplecov", "0.22.0"
end
57 changes: 57 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ GEM
coderay (1.1.3)
concurrent-ruby (1.2.3)
connection_pool (2.4.1)
database_cleaner-core (2.0.1)
database_cleaner-sequel (2.0.2)
database_cleaner-core (~> 2.0.0)
sequel
diff-lcs (1.5.1)
docile (1.4.0)
dotenv (3.0.2)
Expand Down Expand Up @@ -59,6 +63,11 @@ GEM
dry-logic (>= 1.4, < 2)
dry-types (>= 1.7, < 2)
zeitwerk (~> 2.6)
dry-struct (1.6.0)
dry-core (~> 1.0, < 2)
dry-types (>= 1.7, < 2)
ice_nine (~> 0.11)
zeitwerk (~> 2.6)
dry-system (1.0.1)
dry-auto_inject (~> 1.0, < 2)
dry-configurable (~> 1.0, < 2)
Expand All @@ -77,18 +86,24 @@ GEM
dry-initializer (~> 3.0)
dry-schema (>= 1.12, < 2)
zeitwerk (~> 2.6)
faker (3.2.3)
i18n (>= 1.8.11, < 2)
i18n (1.14.1)
concurrent-ruby (~> 1.0)
ice_nine (0.11.2)
json (2.7.1)
language_server-protocol (3.17.0.3)
lint_roller (1.1.0)
method_source (1.0.0)
minitest (5.22.2)
money (6.16.0)
i18n (>= 0.6.4, <= 2)
mutex_m (0.2.0)
parallel (1.24.0)
parser (3.3.0.5)
ast (~> 2.4.1)
racc
pg (1.5.5)
pry (0.14.2)
coderay (~> 1.1)
method_source (~> 1.0)
Expand All @@ -97,6 +112,38 @@ GEM
rake (13.1.0)
regexp_parser (2.9.0)
rexml (3.2.6)
rom (5.3.0)
rom-changeset (~> 5.3, >= 5.3.0)
rom-core (~> 5.3, >= 5.3.0)
rom-repository (~> 5.3, >= 5.3.0)
rom-changeset (5.3.0)
dry-core (~> 1.0)
rom-core (~> 5.3)
transproc (~> 1.0, >= 1.1.0)
rom-core (5.3.0)
concurrent-ruby (~> 1.1)
dry-configurable (~> 1.0)
dry-core (~> 1.0)
dry-inflector (~> 1.0)
dry-initializer (~> 3.0, >= 3.0.1)
dry-struct (~> 1.0)
dry-types (~> 1.6)
transproc (~> 1.0, >= 1.1.0)
rom-factory (0.12.0)
dry-configurable (~> 1.0)
dry-core (~> 1.0)
dry-struct (~> 1.6)
faker (>= 2.0, < 4)
rom-core (~> 5.3)
rom-repository (5.3.0)
dry-core (~> 1.0)
dry-initializer (~> 3.0, >= 3.0.1)
rom-core (~> 5.3, >= 5.3.0)
rom-sql (3.6.2)
dry-core (~> 1.0)
dry-types (~> 1.0)
rom (~> 5.2, >= 5.2.1)
sequel (>= 4.49)
rspec (3.13.0)
rspec-core (~> 3.13.0)
rspec-expectations (~> 3.13.0)
Expand Down Expand Up @@ -128,6 +175,8 @@ GEM
rubocop-ast (>= 1.30.0, < 2.0)
ruby-progressbar (1.13.0)
ruby2_keywords (0.0.5)
sequel (5.77.0)
bigdecimal
simplecov (0.22.0)
docile (~> 1.1)
simplecov-html (~> 0.11)
Expand All @@ -146,6 +195,7 @@ GEM
standard-performance (1.3.1)
lint_roller (~> 1.1)
rubocop-performance (~> 1.20.2)
transproc (1.1.1)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
unicode-display_width (2.5.0)
Expand All @@ -159,14 +209,21 @@ PLATFORMS
DEPENDENCIES
activesupport (= 7.1.3)
auction_fun_core!
database_cleaner-sequel (= 2.0.2)
dotenv (= 3.0.2)
dry-events (= 1.0.1)
dry-matcher (= 1.0.0)
dry-monads (= 1.6.0)
dry-system (= 1.0.1)
dry-validation (= 1.10.0)
faker (= 3.2.3)
money (= 6.16.0)
pg (= 1.5.5)
pry (= 0.14.2)
rake (= 13.1.0)
rom (= 5.3.0)
rom-factory (= 0.12.0)
rom-sql (= 3.6.2)
rspec (= 3.13.0)
simplecov (= 0.22.0)
standard (= 1.33.0)
Expand Down
1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
database: postgres
59 changes: 56 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,66 @@ is considered the value `development` by default.

#### Ruby

#### Database (PostgreSQL)

```sh
user@host:~$ sudo apt install build-essential libssl-dev libreadline-dev zlib1g-dev libcurl4-openssl-dev uuid-dev
user@host:~$ asdf plugin add postgres
user@host:~$ asdf install postgres 16.1
user@host:~$ rm -rf $HOME/.asdf/installs/postgres/16.1/data
user@host:~$ initdb -D $HOME/.asdf/installs/postgres/16.1/data -U postgres
```

```sh
user@host:~$ sudo apt install autoconf patch build-essential rustc libssl-dev libyaml-dev libreadline6-dev zlib1g-dev libgmp-dev libncurses5-dev libffi-dev libgdbm6 libgdbm-dev libdb-dev uuid-dev
user@host:~$ asdf plugin add ruby
user@host:~$ asdf install ruby 3.3.0
user@host:~$ gem install pg -v 1.4.5 --verbose -- --with-pg-config=$HOME/.asdf/installs/postgres/14.2/bin/pg_config # Fix pg_config
user@host:~$ bundle install
user@host:~$ gem install pg -v 1.5.5 --verbose -- --with-pg-config=$HOME/.asdf/installs/postgres/16.1/bin/pg_config # Fix pg_config
user@host:~$ bin/setup
```

#### Overmind (Procfile manager)

```sh
user@host:~$ asdf install golang latest
user@host:~$ go install github.com/DarthSim/overmind/v2
user@host:~$ asdf reshim
```

#### Create database for development environment

> **[postgres]** in rake commands is a name of user for postgres. Change if needed
In current tab:

```sh
user@host:~$ overmind s -l database
```

### Interactive prompt
Open a new tab and create development database:

```sh
user@host:~$ bundle exec rake 'auction_fun_core:db:create_database[postgres]'
user@host:~$ bundle exec rake 'auction_fun_core:db:migrate'
```

Now come back to overmind tab, kill the current database process using **Ctrl+c**. After that:

```sh
user@host:~$ overmind start
```

This will start all required services needed to run core application.

In new tab, you could run seed data for development with

```sh
user@host:~$ bundle exec rake 'auction_fun_core:db:seed'
```

## Interactive prompt

To experiment with that code, run `bin/console` for an interactive prompt.

## Test

Expand All @@ -66,6 +117,8 @@ Configure the `.env.test` file with the values according to your machine or netw
Run the test suite with the coverage report using the command:

```sh
user@host:~$ APP_ENV=test bundle exec rake auction_fun_core:db:create_database[userdb]
user@host:~$ APP_ENV=test bundle exec rake auction_fun_core:db:migrate
user@host:~$ CI=true APP_ENV=test bundle exec rspec .
```

Expand Down
3 changes: 3 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
# frozen_string_literal: true

require_relative 'config/application'
require "bundler/gem_tasks"
require "rspec/core/rake_task"

RSpec::Core::RakeTask.new(:spec)

require "standard/rake"

Dir.glob("#{File.expand_path(__dir__)}/lib/tasks/**/*.rake").each { |f| load f }

task default: %i[spec standard]
2 changes: 1 addition & 1 deletion config/boot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
unless defined?(Dotenv)
require 'dotenv'
Dotenv.load(".env.#{ENV.fetch("APP_ENV", nil)}")
# Dotenv.require_keys('DATABASE_URL', 'REDIS_URL', 'DEFAULT_CURRENCY')
Dotenv.require_keys('DATABASE_URL')
end
13 changes: 13 additions & 0 deletions db/migrate/20240216200926_enable_postgres_extensions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

ROM::SQL.migration do
up do
run 'CREATE EXTENSION "unaccent"'
run 'CREATE EXTENSION "hstore"'
end

down do
run 'DROP EXTENSION IF EXISTS "unaccent"'
run 'DROP EXTENSION IF EXISTS "hstore"'
end
end
3 changes: 3 additions & 0 deletions db/seeds.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# frozen_string_literal: true

# This file should contain all the record creation needed to seed the development database with its default values.
72 changes: 72 additions & 0 deletions lib/tasks/database.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# frozen_string_literal: true

require 'rom-sql'
require 'rom/sql/rake_task'
require 'pry'
require 'uri'

namespace :auction_fun_core do
namespace :db do
uri = URI.parse(ENV.fetch('DATABASE_URL'))
@database_name = (uri.path || '').split('/').last
@database_host = uri.host

desc 'Perform migration reset (full erase and migration up)'
task :rom_configuration do
Rake::Task['auction_fun_core:db:setup'].invoke
end

desc 'Prepare database for running migrations'
task :setup do
AuctionFunCore::Application.start(:db)
ROM::SQL::RakeSupport.env = ROM.container(AuctionFunCore::Application[:db_config])
end

desc 'Perform migration reset (full erase and migration up)'
task reset: :rom_configuration do
ROM::SQL::RakeSupport.run_migrations(target: 0)
ROM::SQL::RakeSupport.run_migrations
puts '<= db:reset executed'
end

desc 'Create a postgres database'
task :create_database, [:userdb] do |_t, args|
command = "CREATE DATABASE #{@database_name} LOCALE 'en_US.utf8' ENCODING UTF8 TEMPLATE template0;"

sh %(psql -h #{@database_host} -U #{args[:userdb]} -c "#{command}")
end

desc 'Drop a postgres database'
task :drop_database, [:userdb] do |_t, args|
sh %(psql -h #{@database_host} -U #{args[:userdb]} -c "DROP DATABASE #{@database_name};")
end

desc 'Migrate the database (options [version_number])]'
task :migrate, [:version] => :rom_configuration do |_, args|
version = args[:version]

if version.nil?
ROM::SQL::RakeSupport.run_migrations
puts '<= db:migrate executed'
else
ROM::SQL::RakeSupport.run_migrations(target: version.to_i)
puts "<= db:migrate version=[#{version}] executed"
end
end

desc 'Perform migration down (removes all tables)'
task clean: :rom_configuration do
ROM::SQL::RakeSupport.run_migrations(target: 0)
puts '<= db:clean executed'
end

desc 'Seed data'
task :seed_database do
Rake::Task['auction_fun_core:db:setup'].invoke
seed_file = AuctionFunCore::Application.root.join('db', 'seeds.rb')
raise 'You tried to load seed data, but no seed loader is specified' unless seed_file

load(seed_file)
end
end
end
9 changes: 9 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,21 @@
require_relative '../config/application'
require 'pry'
require 'dotenv'
require 'rom-factory'
require 'database_cleaner/sequel'

AuctionFunCore::Application.start(:core)

Factory = ROM::Factory.configure do |config|
config.rom = AuctionFunCore::Application[:container]
end

Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |file| require file }

RSpec.configure do |config|
config.add_setting :rom
config.rom = Factory.rom

# Enable flags like --only-failures and --next-failure
config.example_status_persistence_file_path = ".rspec_status"

Expand Down
6 changes: 6 additions & 0 deletions spec/support/faker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

if defined?(Faker)
I18n.enforce_available_locales = false
Faker::Config.locale = 'pt-BR'
end
3 changes: 3 additions & 0 deletions system/providers/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
end

start do
target.start(:settings)
target.start(:persistence)

Dry::Schema.load_extensions(:hints)
Dry::Schema.load_extensions(:info)
Dry::Schema.load_extensions(:monads)
Expand Down
Loading

0 comments on commit dfa934b

Please sign in to comment.