From cb3538cde63e3da14c5e40eb8fb292d43f29b28a Mon Sep 17 00:00:00 2001 From: Peter Goldstein Date: Thu, 21 Apr 2022 16:44:05 -0700 Subject: [PATCH] Get Postgres running --- .github/workflows/tests.yml | 21 ++++++++++++++++++++- .rubocop_todo.yml | 2 +- Gemfile.lock | 2 ++ rating.gemspec | 1 + spec/common_helper.rb | 2 +- spec/support/database.rb | 33 +++++++++++++++++++++++++++++++++ spec/support/mysql.rb | 10 ---------- 7 files changed, 58 insertions(+), 13 deletions(-) create mode 100644 spec/support/database.rb delete mode 100644 spec/support/mysql.rb diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index e065ec6..da3bc68 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -14,7 +14,9 @@ jobs: - '2.7' - '3.0' - '3.1' - + db: + - mysql + - postgres services: mysql: env: @@ -26,7 +28,24 @@ jobs: ports: - 3306:3306 + postgres: + image: postgres + env: + POSTGRES_DB: rating_user + POSTGRES_USER: rating_user + POSTGRES_PASSWORD: rating_password + options: >- + --health-cmd pg_isready + --health-interval 10s + --health-timeout 5s + --health-retries 5 + ports: + - 5432:5432 + env: + DB: ${{ matrix.db }} + POSTGRES_USER: rating_user + POSTGRES_PASSWORD: rating_password steps: - name: Checkout uses: actions/checkout@v3 diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index ac2d604..ada1cbb 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -51,7 +51,7 @@ Metrics/ParameterLists: # AllowedIdentifiers: capture3, iso8601, rfc1123_date, rfc822, rfc2822, rfc3339 Naming/VariableNumber: Exclude: - - 'spec/support/mysql.rb' + - 'spec/support/database.rb' # Offense count: 9 # This cop supports safe auto-correction (--auto-correct). diff --git a/Gemfile.lock b/Gemfile.lock index a506804..dea80bf 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -71,6 +71,7 @@ GEM parallel (1.22.1) parser (3.1.2.0) ast (~> 2.4.1) + pg (1.3.5) racc (1.6.0) rack (2.2.3) rack-test (1.1.0) @@ -154,6 +155,7 @@ DEPENDENCIES debug factory_bot_rails mysql2 + pg rating! rspec-rails rubocop-performance diff --git a/rating.gemspec b/rating.gemspec index 4e3872a..dc000a9 100644 --- a/rating.gemspec +++ b/rating.gemspec @@ -22,6 +22,7 @@ Gem::Specification.new do |spec| spec.add_development_dependency 'debug' spec.add_development_dependency 'factory_bot_rails' spec.add_development_dependency 'mysql2' + spec.add_development_dependency 'pg' spec.add_development_dependency 'rspec-rails' spec.add_development_dependency 'rubocop-performance' spec.add_development_dependency 'rubocop-rails' diff --git a/spec/common_helper.rb b/spec/common_helper.rb index 347e720..8f88c0e 100644 --- a/spec/common_helper.rb +++ b/spec/common_helper.rb @@ -9,7 +9,7 @@ require 'debug' require 'rating' require 'support/common' -require 'support/mysql' +require 'support/database' require 'support/database_cleaner' require 'support/factory_bot' require 'support/migrate' diff --git a/spec/support/database.rb b/spec/support/database.rb new file mode 100644 index 0000000..d58e10a --- /dev/null +++ b/spec/support/database.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +require 'mysql2' +require 'pg' + +ENV['DB'] ||= 'mysql' + +conn_params = { + database: :rating_test, + host: '127.0.0.1', +} + +case ENV.fetch('DB', nil) +when 'mysql' + client = Mysql2::Client.new(host: '127.0.0.1', username: :root) + conn_params[:adapter] = :mysql2 + conn_params[:username] = :root +when 'postgres' + ENV['POSTGRES_USER'] ||= 'rating_user' + client = PG::Connection.new(host: '127.0.0.1', user: ENV.fetch('POSTGRES_USER', nil), + password: ENV.fetch('POSTGRES_PASSWORD', nil) + ) + conn_params.merge!( + adapter: :postgresql, + username: ENV.fetch('POSTGRES_USER', nil), + password: ENV.fetch('POSTGRES_PASSWORD', nil) + ) +end + +client.query('DROP DATABASE IF EXISTS rating_test;') +client.query('CREATE DATABASE rating_test;') + +ActiveRecord::Base.establish_connection(conn_params) diff --git a/spec/support/mysql.rb b/spec/support/mysql.rb deleted file mode 100644 index 7efd9e1..0000000 --- a/spec/support/mysql.rb +++ /dev/null @@ -1,10 +0,0 @@ -# frozen_string_literal: true - -require 'mysql2' - -client = Mysql2::Client.new(host: '127.0.0.1', username: :root) - -client.query('DROP DATABASE IF EXISTS rating_test;') -client.query('CREATE DATABASE IF NOT EXISTS rating_test;') - -ActiveRecord::Base.establish_connection(adapter: :mysql2, database: :rating_test, username: :root, host: '127.0.0.1')