Skip to content

Commit

Permalink
Merge pull request #11 from Slania/issue_fixes
Browse files Browse the repository at this point in the history
Fixes issues #7, #9 and #10
  • Loading branch information
packrat386 authored Mar 16, 2017
2 parents 8c723be + 8193223 commit 5926d71
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 38 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ before_install:
# install postgresql v9.5
- if [[ "$PG_VERSION" = "9.5" ]]; then echo "installing pg9.5"; sudo /etc/init.d/postgresql stop; sudo apt-get -y autoremove; sudo apt-key adv --keyserver keys.gnupg.net --recv-keys 7FCC7D46ACCC4CF8; sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main 9.5" >> /etc/apt/sources.list.d/postgresql.list'; sudo apt-get update; sudo apt-get -y install postgresql-9.5; sudo cp /etc/postgresql/9.4/main/pg_hba.conf /etc/postgresql/9.5/main/pg_hba.conf; sudo sed -i 's/5433/5432/' /etc/postgresql/9.5/main/postgresql.conf; sudo /etc/init.d/postgresql restart; else echo "not installing pg9.5"; fi
# setup travis user
- if [[ "$PG_VERSION" = "9.5" ]]; then echo "setting up users for pg9.5"; PGUSER=postgres createuser --superuser travis || true; fi
- if [[ "$PG_VERSION" = "9.5" ]]; then echo "setting up users for pg9.5"; PGUSER=postgres createuser --superuser travis || echo role travis already exists.; fi
# setup pg_dump
- sudo ln -sfn /usr/lib/postgresql/$PG_VERSION/bin/pg_dump /usr/bin/pg_dump
# start up the specific version of PG
Expand Down
82 changes: 45 additions & 37 deletions lib/prodder/prodder.rake
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'shellwords'

module Prodder
# The list of default rake tasks which prodder will be removing or replacing.
# @see databases.rake (currently at lib/active_record/railties/databases.rake)
Expand Down Expand Up @@ -212,71 +214,77 @@ namespace :db do

desc "Load initial seeds from db/seeds.sql"
task :seed => dependencies do
if File.exist?('db/seeds.sql')
config = ActiveRecord::Base.configurations[ENV['RAILS_ENV'] || Rails.env]
config["username"] = config["superuser"] if config["superuser"] && File.exist?('db/permissions.sql')
set_psql_env config
puts "Loading db/seeds.sql into database '#{config['database']}'"
`psql --no-psqlrc -f db/seeds.sql #{Shellwords.escape(config['database'])}`
raise 'Error loading db/seeds.sql' if $?.exitstatus != 0
else
unless File.exist?('db/seeds.sql')
puts 'db/seeds.sql not found: no seeds to load.'
return
end

config = ActiveRecord::Base.configurations[ENV['RAILS_ENV'] || Rails.env]
config["username"] = config["superuser"] if config["superuser"] && File.exist?('db/permissions.sql')
set_psql_env config
puts "Loading db/seeds.sql into database '#{config['database']}'"
`psql --no-psqlrc -f db/seeds.sql #{Shellwords.escape(config['database'])}`
raise 'Error loading db/seeds.sql' if $?.exitstatus != 0
end

desc "Load quality_checks (indexes, triggers, foreign keys) from db/quality_checks.sql"
task :quality_check => dependencies do
if File.exist?('db/quality_checks.sql')
config = ActiveRecord::Base.configurations[ENV['RAILS_ENV'] || Rails.env]
config["username"] = config["superuser"] if config["superuser"] && File.exist?('db/permissions.sql')
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'])}`
raise 'Error loading db/quality_checks.sql' if $?.exitstatus != 0
else
unless File.exist?('db/quality_checks.sql')
puts 'db/quality_checks.sql not found: no quality_checks to load.'
return
end

config = ActiveRecord::Base.configurations[ENV['RAILS_ENV'] || Rails.env]
config["username"] = config["superuser"] if config["superuser"] && File.exist?('db/permissions.sql')
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'])}`
raise 'Error loading db/quality_checks.sql' if $?.exitstatus != 0
end

desc "Load permissions (DB object level access control, group role memberships) from db/permissions.sql"
task :permission => dependencies do
if File.exist?('db/permissions.sql')
config = ActiveRecord::Base.configurations[ENV['RAILS_ENV'] || Rails.env]
config["username"] = config["superuser"] if config["superuser"]
set_psql_env config
puts "Loading db/permissions.sql into database '#{config['database']}'"
disconnect
ActiveRecord::Base.establish_connection((ENV['RAILS_ENV'] || Rails.env).intern)
is_super = ActiveRecord::Base.connection.execute(<<-SQL).first['is_super']
select 1 as is_super from pg_roles where rolname = '#{config['username']}' and rolsuper
SQL
unless is_super
puts "Restoring permissions as config/database.yml non-superuser: #{config['username']}, expect errors, or rerun after granting superuser"
end
`psql --no-psqlrc -f db/permissions.sql #{Shellwords.escape(config['database'])}`

raise 'Error loading db/permissions.sql' if $?.exitstatus != 0
else
unless File.exist?('db/permissions.sql')
puts 'db/permissions.sql not found: no permissions to load.'
return
end

config = ActiveRecord::Base.configurations[ENV['RAILS_ENV'] || Rails.env]
config["username"] = config["superuser"] if config["superuser"]
set_psql_env config
puts "Loading db/permissions.sql into database '#{config['database']}'"
disconnect
ActiveRecord::Base.establish_connection((ENV['RAILS_ENV'] || Rails.env).intern)
result = ActiveRecord::Base.connection.execute(<<-SQL).first
select 1 as is_super from pg_roles where rolname = '#{config['username']}' and rolsuper
SQL
unless result && result['is_super']
puts "Restoring permissions as config/database.yml non-superuser: '#{config['username']}', expect errors, or rerun after granting superuser"
end
`psql --no-psqlrc -f db/permissions.sql #{Shellwords.escape(config['database'])}`
raise 'Error loading db/permissions.sql' if $?.exitstatus != 0
end

desc "Load database settings"
task :settings => dependencies do
unless File.exist?('db/settings.sql')
puts 'db/settings.sql not found: no settings to load.'
return
end

config = ActiveRecord::Base.configurations[ENV['RAILS_ENV'] || Rails.env]
config["username"] = config["superuser"] if config["superuser"] && File.exist?('db/permissions.sql')
set_psql_env config
puts "Loading db/settings.sql into database '#{config['database']}'"
disconnect
ActiveRecord::Base.establish_connection((ENV['RAILS_ENV'] || Rails.env).intern)
is_super = ActiveRecord::Base.connection.execute(<<-SQL).first['is_super']
result = ActiveRecord::Base.connection.execute(<<-SQL).first
select 1 as is_super from pg_roles where rolname = '#{config['username']}' and rolsuper
SQL
unless is_super
puts "Restoring settings as config/database.yml non-superuser: #{config['username']}, expect errors, or rerun after granting superuser"
unless result && result['is_super']
puts "Restoring settings as config/database.yml non-superuser: '#{config['username']}', expect errors, or rerun after granting superuser"
end
`psql --no-psqlrc -f db/settings.sql #{Shellwords.escape(config['database'])}`

raise 'Error loading db/settings.sql' if $?.exitstatus != 0
end

Expand Down

0 comments on commit 5926d71

Please sign in to comment.