-
-
Save pboling/7598b4a7aab9dd22585b to your computer and use it in GitHub Desktop.
Are you using PostgreSQL and don't want to make your app run as PostgreSQL super user, then add this custom rake task to your `lib/tasks` folder and be happy.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# PostgreSQL writes two optional commands to the database schema | |
# file, called db/structure.sql, that can only be run as a root | |
# database user. These are not needed actually, so comment them | |
# out automatically | |
# | |
# CREATE EXTENSION IF NOT EXISTS plpgsql WITH SCHEMA pg_catalog; | |
# COMMENT ON EXTENSION plpgsql IS 'PL/pgSQL procedural language'; | |
# | |
namespace :db do | |
namespace :structure do | |
desc 'Comment out the plpgsql lines from structure.sql so that a non-root user can create the test database' | |
task :fix_plpgsql do | |
lines_to_strike = [ | |
'CREATE EXTENSION IF NOT EXISTS plpgsql WITH SCHEMA pg_catalog;', | |
"COMMENT ON EXTENSION plpgsql IS 'PL/pgSQL procedural language';", | |
] | |
schema_file = File.join(File.dirname(__FILE__), 'db', 'structure.sql') | |
if File.exist?(schema_file) and (input = File.open(schema_file)) | |
# Create a temp file, read through the original, commenting out the target lines. | |
lines = Array.new | |
line_count = 0 | |
input.each_line do |line| | |
if line | |
line_count += 1 | |
if lines_to_strike.include?(line.strip) | |
lines << "-- The following was commented out by rake db:structure:fix_plpgsql\n" | |
lines << '-- ' + line | |
else | |
lines << line | |
end | |
end | |
end # each | |
input.close | |
if lines.count > line_count | |
# Lines were commented out, so write the new content to the file | |
File.write(schema_file, lines.join) | |
else | |
# No lines were commented out, so there is no need to rewrite the file | |
STDERR.puts "No changes are needed to #{schema_file}, it's left unchanged." | |
end | |
else | |
STDERR.puts "unable to find or open schema_file: #{schema_file}" | |
end | |
end # task | |
end # namespace | |
end # namespace | |
# Inform Rake that this should be run every time rake db:structure:dump is run | |
Rake::Task['db:structure:dump'].enhance do | |
Rake::Task['db:structure:fix_plpgsql'].invoke | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment