Skip to content

Commit

Permalink
Merge pull request #35 from hkim3162/rails_upgrade_prodder_changes
Browse files Browse the repository at this point in the history
Rails 6.1.x Prodder Upgrade Changes
  • Loading branch information
mokpro authored Feb 10, 2023
2 parents 3bb323d + 336cc07 commit 2178135
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 54 deletions.
55 changes: 55 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
name: CI

on:
workflow_dispatch:
push:
branches:
- main
pull_request:

permissions:
contents: read

jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
ruby-version: [2.6, 2.7, 3.0]
services:
postgres:
image: postgres:12.1-alpine
ports:
- 5432:5432
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
steps:
- name: Checkout Project
uses: actions/checkout@v3

- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby-version }}
bundler-cache: true

- name: Install Library Dependencies
run: sudo apt-get install libpq-dev

- name: Setup Database
run: |
cp config/database.yml.github-actions config/database.yml
env:
RAILS_ENV: test
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres

- name: Test with RSpec
env:
RAILS_ENV: "test"
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
run: |
bundle exec rspec
41 changes: 0 additions & 41 deletions .travis.yml

This file was deleted.

8 changes: 8 additions & 0 deletions config/database.yml.github-actions
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
test:
adapter: postgresql
host: localhost
encoding: unicode
database: github-actions
pool: 20
username: <%= ENV["POSTGRES_USER"] %>
password: <%= ENV["POSTGRES_PASSWORD"] %>
23 changes: 11 additions & 12 deletions lib/prodder/prodder.rake
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ namespace :db do
end
as("superuser", in: environments) do
ActiveRecord::Tasks::DatabaseTasks.create_current
ActiveRecord::Base.configurations.each do |env, config|
Rails.configuration.database_configuration.each do |env, config|
if environments.include?(env) && config["migration_user"] && config['database']
set_psql_env config
`psql --no-psqlrc --command "ALTER DATABASE #{config['database']} OWNER TO #{config['migration_user']}" #{Shellwords.escape(config['database'])}`
Expand All @@ -182,7 +182,7 @@ namespace :db do
task :all => dependencies do
as("superuser") do
ActiveRecord::Tasks::DatabaseTasks.create_all
ActiveRecord::Base.configurations.each do |env, config|
Rails.configuration.database_configuration.each do |env, config|
if config["migration_user"] && config['database']
set_psql_env config
`psql --no-psqlrc --command "ALTER DATABASE #{config['database']} OWNER TO #{config['migration_user']}" #{Shellwords.escape(config['database'])}`
Expand All @@ -204,7 +204,7 @@ namespace :db do
desc "Load db/structure.sql into the current environment's database"
task :load => dependencies do
as("superuser", in: ENV['RAILS_ENV'] || Rails.env) do
config = ActiveRecord::Base.configurations[ENV['RAILS_ENV'] || Rails.env]
config = Rails.configuration.database_configuration[ENV['RAILS_ENV'] || Rails.env]
set_psql_env config
puts "Loading db/structure.sql into database '#{config['database']}'"
`psql --no-psqlrc -f db/structure.sql #{Shellwords.escape(config['database'])}`
Expand All @@ -217,7 +217,7 @@ namespace :db do
task :seed => dependencies do
if File.exist?('db/seeds.sql')
as("superuser", in: ENV['RAILS_ENV'] || Rails.env) do
config = ActiveRecord::Base.configurations[ENV['RAILS_ENV'] || Rails.env]
config = Rails.configuration.database_configuration[ENV['RAILS_ENV'] || Rails.env]
set_psql_env config
puts "Loading db/seeds.sql into database '#{config['database']}'"
`psql --no-psqlrc -f db/seeds.sql #{Shellwords.escape(config['database'])}`
Expand All @@ -232,7 +232,7 @@ namespace :db do
task :quality_check => dependencies do
if File.exist?('db/quality_checks.sql')
as("superuser", in: ENV['RAILS_ENV'] || Rails.env) do
config = ActiveRecord::Base.configurations[ENV['RAILS_ENV'] || Rails.env]
config = Rails.configuration.database_configuration[ENV['RAILS_ENV'] || Rails.env]
set_psql_env config
puts "Loading db/quality_checks.sql into database '#{config['database']}'"
`psql --no-psqlrc -f db/quality_checks.sql #{Shellwords.escape(config['database'])}`
Expand All @@ -247,7 +247,7 @@ namespace :db do
task :permission => dependencies do
if File.exist?('db/permissions.sql')
as("superuser", in: ENV['RAILS_ENV'] || Rails.env) do
config = ActiveRecord::Base.configurations[ENV['RAILS_ENV'] || Rails.env]
config = Rails.configuration.database_configuration[ENV['RAILS_ENV'] || Rails.env]
set_psql_env config
puts "Loading db/permissions.sql into database '#{config['database']}'"
result = ActiveRecord::Base.connection.execute(<<-SQL).first
Expand All @@ -268,7 +268,7 @@ namespace :db do
task :settings => dependencies do
if File.exist?('db/settings.sql')
as("superuser", in: ENV['RAILS_ENV'] || Rails.env) do
config = ActiveRecord::Base.configurations[ENV['RAILS_ENV'] || Rails.env]
config = Rails.configuration.database_configuration[ENV['RAILS_ENV'] || Rails.env]
set_psql_env config
puts "Loading db/settings.sql into database '#{config['database']}'"
result = ActiveRecord::Base.connection.execute(<<-SQL).first
Expand Down Expand Up @@ -362,20 +362,19 @@ namespace :db do

def as(user, opts = {}, &block)
if File.exist?('db/permissions.sql')
# `ActiveRecord::Base.configurations` in Rails 6 now returns an object instead of a hash
config, config_was = ActiveRecord::Base.configurations.deep_dup.to_h, ActiveRecord::Base.configurations.deep_dup
config, config_was = Rails.configuration.database_configuration.deep_dup.to_h, Rails.configuration.database_configuration.deep_dup
in_env = Array(opts[:in]) || config.keys
if config.all? { |env, config_hash| in_env.include?(env) ? config_hash[user] : true }
disconnect
config.each { |env, config_hash| config_hash["username"] = config_hash[user] if in_env.include?(env) }
ActiveRecord::Base.configurations = config
Rails.configuration.database_configuration = config
end
else
puts "No permissions file (db/permissions.sql) found, running everything in context of user"
end
yield
ensure
ActiveRecord::Base.configurations = config_was if config_was
Rails.configuration.database_configuration = config_was if config_was
in_env.each { |env| ActiveRecord::Base.establish_connection(env.intern) } if in_env
end

Expand All @@ -394,5 +393,5 @@ end

# Yes, I really want migrations to run against the test DB.
Rake::Task['db:migrate'].actions.unshift(proc {
ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations[ENV['RAILS_ENV'] || Rails.env])
ActiveRecord::Base.establish_connection(Rails.configuration.database_configuration[(ENV['RAILS_ENV'] || Rails.env).to_sym])
})
2 changes: 1 addition & 1 deletion lib/prodder/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Prodder
VERSION = "1.7.7"
VERSION = "1.8.0"
end

0 comments on commit 2178135

Please sign in to comment.