Reputation: 58244
I have a project that currently uses older versions of the Ruby on Rails environment and uses PostgreSQL database. The versions are (yes, I know... don't laugh, I have to support this legacy version for now):
ruby 1.8.7
rails 2.3.11
rake 10.5.0
psql 9.5.7
If it matters, this is all installed on an Ubuntu 16.04 system. It was all working on an older system I had, which finally died. So I set up rvm
and these development tool versions on the new machine and copied over the database.
On the new machine, everything seems to work fine except a couple of rake database tasks I've tried. If I run, for example, rake db:schema:dump
, I get no error output, and I get a db/schema.rb
file with the following contents:
# 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
I tried running rake db:structure:dump
and got an error:
/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)
My primary concern is the lack of ability to dump the schema. I don't understand how it runs with no errors, but generates no schema. As I mentioned initially, the app runs fine via script/server
. I also ran ActiveRecord::SchemaDumper.dump
from the console with the exact same result (which I guess would be expected, since that's what the rake task probably runs). But from the console, I can examine any of the models and data just fine. It's all there.
Any thoughts on why the schema file is empty? I'm hoping someone has seen this phenomenon before. I've done lots of searching for modes of failure of rake db:schema:dump
and couldn't find this particular symptom mentioned anywhere.
Upvotes: 0
Views: 1664
Reputation: 58244
I did a bit of digging and determined that the tables
method of the PostgresqlAdapter
was returning an empty list even though it had a valid connection to the database. The adapter source is here:
gems/activerecord-2.3.11/lib/active_record/connection_adapters/postgresql_adapter.rb
The method looks like this:
# 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
The string returned by schema_search_path
looks like, "\"$user\", public"
. When it's processed, the preceding blank is carried along with "public", so the query is checking for schemas with a schema name which is either "\"$user\""
or " public"
so it doesn't match the schema name of "public"
.
I just hacked the code a little like so, and now my rake db:schema:dump
works fine:
schemas = schema_search_path.split(/,/).map { |p| quote(p.strip) }.join(',')
I suspect there's a better way to really fix the problem, but this works reliably for me. Not to mention at this point, the version I'm using is so old perhaps no one else cares. I did look at later implementations of tables
and they're a bit different.
Upvotes: 1