迁移时"pg_dump:无效选项 - i"

Jas*_*ett 14 postgresql ruby-on-rails

当我运行rake db:migrate我的Rails项目(3.2.22.2)时,我得到了pg_dump: invalid option -- i.这是完整的痕迹:

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)
Run Code Online (Sandbox Code Playgroud)

我注意到Rails中有关于此问题的错误修正.修复程序似乎没有应用于Rails版本<4,因为它不是安全修复程序,这是有道理的.

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

任何建议表示赞赏.

Phi*_*rom 14

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

问题出在db:structure:dump任务这里:

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),您的任务应该覆盖内置任务.

  • 为此,我必须在开头添加`Rake :: Task [“ db:structure:dump”]。clear`并更改[此行](https://github.com/rails/rails/blob/v3 .2.22.2 / activerecord / lib / active_record / railties / databases.rake#L447)到`Rake :: Task [“ db:structure:dump”]。reenable` (2认同)

ewH*_*ewH 14

我和Rails一起遇到了这个问题3.2.22.它看起来像是固定4.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
Run Code Online (Sandbox Code Playgroud)

此代码专门为我们的项目创建,因此如果您设置了任何其他自定义配置db_dir,则需要相应地进行调整.


mmi*_*ike 8

这个错误是因为在 9.5.X 和更高版本中弃用了“-i”方法。在 Rails -v '4.2.5' 中修复了错误,您可以将 Rails 更新到此版本或更高版本。但是,如果您需要快速的方法,我想您会满意的(这只是 hack,如果您有一点怀疑或不同意,请不要使用它!):

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
Run Code Online (Sandbox Code Playgroud)

2)打开它,找到并编辑下面的行,并从字符串中删除“-i”:

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

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