“pg_dump: invalid option — i” when migrating

后端 未结 5 966
闹比i
闹比i 2021-01-11 10:01

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         


        
5条回答
  •  无人及你
    2021-01-11 10:20

    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.

提交回复
热议问题