2016-03-14 34 views
13

当我在我的Rails项目(3.2.22.2)上运行rake db:migrate时,我得到了pg_dump: invalid option -- i。以下是完整的跟踪:“pg_dump:无效选项 - 我”在迁移

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/[email protected]/gems/activerecord-3.2.22.2/lib/active_record/railties/databases.rake:429:in `block (3 levels) in <top (required)>' 
/Users/jasonswett/.rvm/gems/[email protected]/gems/activerecord-3.2.22.2/lib/active_record/railties/databases.rake:202:in `block (2 levels) in <top (required)>' 
/Users/jasonswett/.rvm/gems/[email protected]/gems/activerecord-3.2.22.2/lib/active_record/railties/databases.rake:196:in `block (2 levels) in <top (required)>' 
/Users/jasonswett/.rvm/gems/[email protected]/bin/ruby_executable_hooks:15:in `eval' 
/Users/jasonswett/.rvm/gems/[email protected]/bin/ruby_executable_hooks:15:in `<main>' 
Tasks: TOP => db:structure:dump 
(See full trace by running task with --trace) 

我注意到有一个bugfix in Rails属于这个问题。该错误修复似乎没有被应用到Rails版本< 4,因为它不是一个安全修复程序,这是有道理的。

我不明白的是我现在应该做的。如果3.2.x有修复,我还没有找到它。我猜如果没有3.2.x的修复,我想这意味着我必须升级到Rails 4.x,这看起来有点激烈。我怀疑这确实是唯一的解决方案。为什么这个问题最近才出现?

任何建议表示赞赏。

回答

12

不太可能有修复,因为它不是安全问题。即使是这样,我不确定他们是否正在修补3.x。

的问题是在db:结构:倾倒在这里的任务:

https://github.com/rails/rails/blob/v3.2.22.2/activerecord/lib/active_record/railties/databases.rake#L428

最简单的办法是复制这个任务(413 - 448),并把它变成自己的lib/tasks目录中,包裹一个namespace db围绕它,调整pg_dump命令(删除-i),你的任务应该覆盖内置的任务。

+0

嗯,所以基本上写'我rake db:migrate'的版本(通过复制Rails源代码)与-i取出? –

+1

对于它的工作,我不得不在开始处添加'Rake :: Task [“db:structure:dump”]。clear'并更改[this line](https://github.com/rails/rails/blob/ v3.2.22.2/activerecord/lib/active_record/railties/databases.rake#L447)到'Rake :: Task [“db:structure:dump”]。reenable' –

1

我最近在试图运行rake db:migrate后,在Rails 4.2.1中出现了这个错误。

我能够通过升级到Rails 4.2.6来克服它,并让bundle update做它的工作颠覆所有相关的宝石。

希望对他人有用。

2

此错误是因为'-i'方法在9.5.X和更高版本中被删除。 修复了Rails-v'4.2.5'中的错误,您可以将Rails更新为此版本或更高版本。如果你需要快速的方法,我认为你会满意它(这只是黑客,不使用它,如果你有点怀疑,或者你不同意它!):

1)找到这个文件: 'postgresql_database_tasks.rb'(在我的情况确实如此):

/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)打开它,从字符串查找和下面编辑行与删除 '-i':

command = "pg_dump -s -x -O -f #{Shellwords.escape(filename)} #{search_path} #{Shellwords.escape(configuration['database'])}" 

3)保存这个文件并重新开始你的耙子任务! 那!

+2

黑客主动记录不是一个好选择 – dft

0

或者,您可以编辑您的本地pg_dump以删除-i选项。尽管个别的安装可能会有所不同,下面是我所做的编辑:

通过所有它的参数的命令迭代(36行):

for (my $i = 0; $i <= $#ARGV; ++$i) { 

查找条件(39行):

if ($ARGV[$i] eq '--cluster') { 

添加以下(第57行):

} elsif ($ARGV[$i] eq '-i') { 
    splice @ARGV, $i, 1; 
} 
11

我遇到了这个问题以及Rails 3.2.22。它看起来像fixed4.2.5,但对于我们的情况,升级Rails并不是很实用。

在考虑了一些选项之后,我结束了重写默认rake任务db:structure:dump的路径,该任务在db:migrate之后被调用。

我创建了一个文件tasks/database.rake,并从不同的ActiveRecord方法中剽窃了一些零碎,创建了一个新的db:structure:dump任务。当执行db:migrate等时,现在调用这个新任务而不是默认值。

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 

此代码是为我们的项目而创建的,所以如果你有设置像db_dir任何其他自定义配置,则需要相应地调整。