【问题标题】:rake db:schema:dump produces an empty schemarake db:schema:dump 产生一个空模式
【发布时间】:2017-07-01 10:53:04
【问题描述】:

我有一个项目目前使用旧版本的 Ruby on Rails 环境并使用 PostgreSQL 数据库。版本是(是的,我知道......别笑,我现在必须支持这个旧版本):

ruby 1.8.7
rails 2.3.11
rake 10.5.0
psql 9.5.7

如果重要的话,这一切都安装在 Ubuntu 16.04 系统上。这一切都在我拥有的旧系统上运行,最终死机。于是我在新机器上设置了rvm和这些开发工具版本,并复制了数据库。

在新机器上,一切似乎都运行良好除了我尝试过的几个 rake 数据库任务。例如,如果我运行rake db:schema:dump,我不会得到错误输出,并且会得到一个包含以下内容的db/schema.rb 文件:

# This file is auto-generated from the current state of the database. Instead of editing this file,
# please use the migrations feature of Active Record to incrementally modify your database, and
# then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your database schema. If you need
# to create the application database on another system, you should be using db:schema:load, not running
# all the migrations from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 0) do

end

我尝试运行rake db:structure:dump,但出现错误:

/usr/lib/postgresql/9.5/bin/pg_dump: invalid option -- 'i'
Try "pg_dump --help" for more information.
rake aborted!
Error dumping database
/home/mark/.rvm/gems/ruby-1.8.7-p374@caplus/gems/rails-2.3.11/lib/tasks/databases.rake:287
/home/mark/.rvm/gems/ruby-1.8.7-p374@caplus/bin/ruby_executable_hooks:15
Tasks: TOP => db:structure:dump
(See full trace by running task with --trace)

我主要担心的是缺乏转储架构的能力。我不明白它是如何运行没有错误的,但没有生成架构。正如我最初提到的,该应用程序通过script/server 运行良好。我还从控制台运行ActiveRecord::SchemaDumper.dump,得到完全相同的结果(我猜这是意料之中的,因为这可能是 rake 任务运行的内容)。但是从控制台,我可以很好地检查任何模型和数据。都在那里。

关于为什么架构文件为空的任何想法?我希望有人以前见过这种现象。我已经对rake db:schema:dump 的故障模式进行了大量搜索,但在任何地方都找不到提到的这种特殊症状。

【问题讨论】:

    标签: ruby-on-rails rake


    【解决方案1】:

    我做了一些挖掘并确定PostgresqlAdapter 的tables 方法返回一个空列表,即使它与数据库有有效连接。适配器源在这里:

    gems/activerecord-2.3.11/lib/active_record/connection_adapters/postgresql_adapter.rb
    

    方法如下:

      # Returns the list of all tables in the schema search path or a specified schema.
      def tables(name = nil)
        schemas = schema_search_path.split(/,/).map { |p| quote(p) }.join(',')
        query(<<-SQL, name).map { |row| row[0] }
          SELECT tablename
            FROM pg_tables
           WHERE schemaname IN (#{schemas})
        SQL
      end
    

    schema_search_path 返回的字符串类似于"\"$user\", public"。当它被处理时,前面的空白与“public”一起携带,因此查询正在检查模式名称为"\"$user\"" 或" public" 的模式,因此它与"public" 的模式名称不匹配。

    我只是像这样破解了代码,现在我的rake db:schema:dump 工作正常:

        schemas = schema_search_path.split(/,/).map { |p| quote(p.strip) }.join(',')
    

    我怀疑有更好的方法来真正解决问题,但这对我来说很可靠。在这一点上更不用说,我使用的版本太旧了,也许没人关心。我确实查看了tables 的后续实现,它们有点不同。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-25
      • 1970-01-01
      • 2016-10-02
      • 1970-01-01
      • 2013-01-01
      • 1970-01-01
      • 2014-04-16
      相关资源
      最近更新 更多