运行JRuby / Rails app" off-premises"使用Heroku Postgres

时间:2014-11-13 22:54:06

标签: postgresql activerecord heroku jruby jrubyonrails

我在自己的应用服务器上运行JRuby / Rails应用。我现在不想运行自己的Postgres盒子,想要使用Heroku。因此,我开发了一些Heroku Postgres实例并且可以通过Sequel很好地连接它们,但我很难与#34; native" ActiveRecord访问。

我已将ENV变量中的连接URL设置为

WAREHOUSE_PG_URL='jdbc:postgresql://<instance>:<port>/<dbname>?user=<username>&password=<password>&ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory'

然后,在我的database.yml中,我有:

production:
  adapter: postgresql
  url: <%= ENV['RAILS_PG_URL'] %>

这看起来非常麻烦。我尝试设置DATABASE_URL,但是IndexError: string not matched爆炸了,如果我刚刚做了

production:  <%= ENV['RAILS_PG_URL'] %>

爆炸:ActiveRecord::ConnectionNotEstablished: jdbc adapter requires :driver and :url (got :driver = )

迁移给我带来了奇怪的错误:

% rake db:migrate
file:/Users/me/.rbenv/versions/jruby-1.7.15/lib/jruby.jar!/jruby/kernel19/kernel.rb:28 warning: unsupported exec option: close_others
rake aborted!
NoMethodError: undefined method `empty?' for nil:NilClass
/Users/me/code/Exchange/vendor/bundle/jruby/1.9/gems/activerecord-4.1.7/lib/active_record/tasks/postgresql_database_tasks.rb:54:in `structure_dump'
/Users/me/code/Exchange/vendor/bundle/jruby/1.9/gems/activerecord-4.1.7/lib/active_record/tasks/database_tasks.rb:150:in `structure_dump'
/Users/me/code/Exchange/vendor/bundle/jruby/1.9/gems/activerecord-4.1.7/lib/active_record/railties/databases.rake:270:in `(root)'
/Users/me/code/Exchange/vendor/bundle/jruby/1.9/gems/activerecord-4.1.7/lib/active_record/railties/databases.rake:43:in `(root)'
/Users/me/code/Exchange/vendor/bundle/jruby/1.9/gems/activerecord-4.1.7/lib/active_record/railties/databases.rake:37:in `(root)'
Tasks: TOP => db:structure:dump
(See full trace by running task with --trace)

更新:这是围绕该方法的实际代码:

  def structure_dump(filename)
    set_psql_env
    search_path = configuration['schema_search_path']
    unless search_path.blank?
      search_path = search_path.split(",").map{|search_path_part| "--schema=#{Shellwords.escape(search_path_part.strip)}" }.join(" ")
    end

    command = "pg_dump -i -s -x -O -f #{Shellwords.escape(filename)} #{search_path} #{Shellwords.escape(configuration['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" }
  end

第54行是command = "pg_dump.....

更新2:我也想抛出一个&#34;我很生气我必须使用“不要费心检查证书是否合格&#39;在这个设置&#34;在那里,也是。

Update3:我只是密切关注这一点并意识到它有多奇怪:

% rake db:migrate
file:/Users/me/.rbenv/versions/jruby-1.7.15/lib/jruby.jar!/jruby/kernel19/kernel.rb:28 warning: unsupported exec option: close_others
E, [2014-11-13T15:45:49.482000 #86686] ERROR -- : ActiveRecord::JDBCError: org.postgresql.util.PSQLException: ERROR: relation "users" does not exist
  Position: 293:         SELECT a.attname, format_type(a.atttypid, a.atttypmod),
               pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod
          FROM pg_attribute a LEFT JOIN pg_attrdef d
            ON a.attrelid = d.adrelid AND a.attnum = d.adnum
         WHERE a.attrelid = '"users"'::regclass
           AND a.attnum > 0 AND NOT a.attisdropped
         ORDER BY a.attnum

I, [2014-11-13T15:45:51.671000 #86686]  INFO -- : Migrating to CreateMarkets (20140312080145)

CreateMarkets是我的第一次迁移 - 我无法弄清楚该代码来自何处,它引发了之前发生的异常。此外,该例外并未阻止其他迁移进入......

1 个答案:

答案 0 :(得分:0)

看起来我能够通过将所有数据库连接详细信息添加到database.yml来解决此问题:

production:
  adapter: postgresql
  url: <%= ENV['RAILS_PG_URL'] %>
  database: <%= ENV['RAILS_PG_DATABASE'] %>
  port: <%= ENV['RAILS_PG_PORT'] %>
  host: <%= ENV['RAILS_PG_HOST'] %>
  username: <%= ENV['RAILS_PG_USER'] %>
  password: <%= ENV['RAILS_PG_PASSWORD'] %>
  ssl: true

因此,ActiveRecord JDBC Adapter使用url进行连接(使用所有ssl-cert-unchecking和所有这些),而&#34; regular&#34;在数据库维护过程中发生的事情(pg_dump)需要构建自己的一组ENV变量来控制该命令的执行。整齐。

我没有&#34;恋爱&#34;有了它,但似乎没问题。