Get Postgres running

main
Peter Goldstein 2022-04-21 16:44:05 -07:00
parent 09d934cfd1
commit cb3538cde6
7 changed files with 58 additions and 13 deletions

View File

@ -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

View File

@ -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).

View File

@ -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

View File

@ -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'

View File

@ -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'

View File

@ -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)

View File

@ -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')