Skip to content

Commit

Permalink
Run db:<tasks> in user contexts (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
Slania authored and mlarraz committed Jun 6, 2018
1 parent 1c0c3bc commit 0439e08
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 45 deletions.
89 changes: 45 additions & 44 deletions lib/prodder/prodder.rake
Original file line number Diff line number Diff line change
Expand Up @@ -203,24 +203,26 @@ namespace :db do
namespace :structure do
desc "Load db/structure.sql into the current environment's database"
task :load => dependencies do
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/structure.sql into database '#{config['database']}'"
`psql --no-psqlrc -f db/structure.sql #{Shellwords.escape(config['database'])}`
raise 'Error loading db/structure.sql' if $?.exitstatus != 0
as("superuser", in: ENV['RAILS_ENV'] || Rails.env) do
config = ActiveRecord::Base.configurations[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'])}`
raise 'Error loading db/structure.sql' if $?.exitstatus != 0
end
end
end

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
as("superuser", in: ENV['RAILS_ENV'] || Rails.env) do
config = ActiveRecord::Base.configurations[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'])}`
raise 'Error loading db/seeds.sql' if $?.exitstatus != 0
end
else
puts 'db/seeds.sql not found: no seeds to load.'
end
Expand All @@ -229,12 +231,13 @@ namespace :db do
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
as("superuser", in: ENV['RAILS_ENV'] || Rails.env) do
config = ActiveRecord::Base.configurations[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'])}`
raise 'Error loading db/quality_checks.sql' if $?.exitstatus != 0
end
else
puts 'db/quality_checks.sql not found: no quality_checks to load.'
end
Expand All @@ -243,20 +246,19 @@ namespace :db do
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)
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 "warning: Restoring permissions as config/database.yml non-superuser: '#{config['username']}', expect errors, or rerun after granting superuser"
as("superuser", in: ENV['RAILS_ENV'] || Rails.env) do
config = ActiveRecord::Base.configurations[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
select 1 as is_super from pg_roles where rolname = '#{config['username']}' and rolsuper
SQL
unless result && result['is_super']
puts "warning: 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
`psql --no-psqlrc -f db/permissions.sql #{Shellwords.escape(config['database'])}`
raise 'Error loading db/permissions.sql' if $?.exitstatus != 0
else
puts 'db/permissions.sql not found: no permissions to load.'
end
Expand All @@ -265,20 +267,19 @@ namespace :db do
desc "Load database settings"
task :settings => dependencies do
if File.exist?('db/settings.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/settings.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 "warning: Restoring settings as config/database.yml non-superuser: '#{config['username']}', expect errors, or rerun after granting superuser"
as("superuser", in: ENV['RAILS_ENV'] || Rails.env) do
config = ActiveRecord::Base.configurations[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
select 1 as is_super from pg_roles where rolname = '#{config['username']}' and rolsuper
SQL
unless result && result['is_super']
puts "warning: 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
`psql --no-psqlrc -f db/settings.sql #{Shellwords.escape(config['database'])}`
raise 'Error loading db/settings.sql' if $?.exitstatus != 0
else
puts 'db/settings.sql not found: no settings to load.'
end
Expand Down
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.5"
VERSION = "1.7.6"
end

0 comments on commit 0439e08

Please sign in to comment.