When I run rake db:migrate
on my Rails project (3.2.22.2) I get pg_dump: invalid option -- i
. Here\'s the full trace:
Celluloid 0.1
Unlikely there is a fix as it's not a security issue. Even if it was, I'm not sure they are patching 3.x anymore.
The problem is in the db:structure:dump task here:
https://github.com/rails/rails/blob/v3.2.22.2/activerecord/lib/active_record/railties/databases.rake#L428
Easiest thing is to copy that task (413 - 448) and put it into your own lib/tasks directory, wrap a namespace db
around it, tweak the pg_dump command (remove -i) and your task should override the built in task.
I ran into this issue as well with Rails 3.2.22
. It looks like this was fixed in 4.2.5
, but for our situation, upgrading Rails was not very practical.
After considering some options, I ended up down the path of overriding the default rake task db:structure:dump
which is getting called after db:migrate
.
I created a file tasks/database.rake
and hacked together bits and pieces from different ActiveRecord
methods to create a new db:structure:dump
task. Now this new task is called instead of the default when db:migrate
, etc is executed.
Rake::Task["db:structure:dump"].clear
namespace :db do
namespace :structure do
desc "Overriding the task db:structure:dump task to remove -i option from pg_dump to make postgres 9.5 compatible"
task dump: [:environment, :load_config] do
config = ActiveRecord::Base.configurations[Rails.env]
set_psql_env(config)
filename = File.join(Rails.root, "db", "structure.sql")
database = config["database"]
command = "pg_dump -s -x -O -f #{Shellwords.escape(filename)} #{Shellwords.escape(database)}"
raise 'Error dumping database' unless Kernel.system(command)
File.open(filename, "a") { |f| f << "SET search_path TO #{ActiveRecord::Base.connection.schema_search_path};\n\n" }
if ActiveRecord::Base.connection.supports_migrations?
File.open(filename, "a") do |f|
f.puts ActiveRecord::Base.connection.dump_schema_information
f.print "\n"
end
end
Rake::Task["db:structure:dump"].reenable
end
end
def set_psql_env(configuration)
ENV['PGHOST'] = configuration['host'] if configuration['host']
ENV['PGPORT'] = configuration['port'].to_s if configuration['port']
ENV['PGPASSWORD'] = configuration['password'].to_s if configuration['password']
ENV['PGUSER'] = configuration['username'].to_s if configuration['username']
end
end
This code was created specifically for our project, so if you have any other custom configurations set like db_dir
, you will need to adjust accordingly.
This error because '-i' method depricated in 9.5.X and higher versions. Bug fixed in Rails -v '4.2.5' and you can update your Rails to this version or upper. But if you need fast method I think you'll satisfied with it (it's just hack, do not use it if you have little bit doubting or you don't agree with it!):
1) find this file: 'postgresql_database_tasks.rb' (in my case it was):
/Users/YourUserName/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/tasks/postgresql_database_tasks.rb
2) Open it, find and edit line below with remove '-i' from string:
command = "pg_dump -s -x -O -f #{Shellwords.escape(filename)} #{search_path} #{Shellwords.escape(configuration['database'])}"
3) save this file and start your rake task again! That it!
Alternatively, you could edit your local pg_dump
to remove the -i
option. Although individual installs may differ, here are the edits I made:
The command iterates through all it's arguments (line 36):
for (my $i = 0; $i <= $#ARGV; ++$i) {
Find the conditional (line 39):
if ($ARGV[$i] eq '--cluster') {
Add the following (line 57):
} elsif ($ARGV[$i] eq '-i') {
splice @ARGV, $i, 1;
}
I've recently got this very error with Rails 4.2.1 after trying to run rake db:migrate
.
I was able to overcome it by upgrading to Rails 4.2.6 and letting bundle update
do it's job bumping all related gems.
Hope that becomes useful to others.