Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"pg_dump: invalid option -- i" when migrating

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.17.1.1 is running in BACKPORTED mode. [ http://git.io/vJf3J ]
[DEPRECATION] `last_comment` is deprecated.  Please use `last_description` instead.
[DEPRECATION] `last_comment` is deprecated.  Please use `last_description` instead.
[DEPRECATION] `last_comment` is deprecated.  Please use `last_description` instead.
[DEPRECATION] `last_comment` is deprecated.  Please use `last_description` instead.
[DEPRECATION] `last_comment` is deprecated.  Please use `last_description` instead.
pg_dump: invalid option -- i
Try "pg_dump --help" for more information.
rake aborted!
Error dumping database
/Users/jasonswett/.rvm/gems/ruby-2.1.4@bm43/gems/activerecord-3.2.22.2/lib/active_record/railties/databases.rake:429:in `block (3 levels) in <top (required)>'
/Users/jasonswett/.rvm/gems/ruby-2.1.4@bm43/gems/activerecord-3.2.22.2/lib/active_record/railties/databases.rake:202:in `block (2 levels) in <top (required)>'
/Users/jasonswett/.rvm/gems/ruby-2.1.4@bm43/gems/activerecord-3.2.22.2/lib/active_record/railties/databases.rake:196:in `block (2 levels) in <top (required)>'
/Users/jasonswett/.rvm/gems/ruby-2.1.4@bm43/bin/ruby_executable_hooks:15:in `eval'
/Users/jasonswett/.rvm/gems/ruby-2.1.4@bm43/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => db:structure:dump
(See full trace by running task with --trace)

I notice that there's a bugfix in Rails pertaining to this issue. The bugfix seems not to have been applied to Rails versions < 4 since it's not a security fix, which makes sense.

What I don't understand is what I'm supposed to do now. If there's a fix for 3.2.x, I haven't been able to find it yet. I guess if there's no fix for 3.2.x, I guess that means I have to upgrade to Rails 4.x, which seems a bit drastic. I doubt that's really the only solution. And why did the issue only recently pop up out of nowhere?

Any suggestions are appreciated.

like image 456
Jason Swett Avatar asked Mar 14 '16 23:03

Jason Swett


4 Answers

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.

like image 135
Philip Hallstrom Avatar answered Nov 14 '22 21:11

Philip Hallstrom


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.

like image 45
ewH Avatar answered Nov 14 '22 19:11

ewH


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!

like image 25
mmike Avatar answered Nov 14 '22 20:11

mmike


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.

like image 32
fagiani Avatar answered Nov 14 '22 19:11

fagiani