Odd rake db:migrate output

2019-04-28 06:31发布

问题:

Why does rake db:migrate run Execute db:schema:dump my output is all screwed up (showing SQL).

Looks like this:

  ActiveRecord::SchemaMigration Load (0.5ms)  SELECT "schema_migrations".* FROM "schema_migrations"
   (3.7ms)  SELECT t2.oid::regclass::text AS to_table, a1.attname AS column, a2.attname AS primary_key, c.conname AS name, c.confupdtype AS on_update, c.confdeltype AS on_delete
FROM pg_constraint c
JOIN pg_class t1 ON c.conrelid = t1.oid
JOIN pg_class t2 ON c.confrelid = t2.oid
JOIN pg_attribute a1 ON a1.attnum = c.conkey[1] AND a1.attrelid = t1.oid
JOIN pg_attribute a2 ON a2.attnum = c.confkey[1] AND a2.attrelid = t2.oid
JOIN pg_namespace t3 ON c.connamespace = t3.oid
WHERE c.contype = 'f'
  AND t1.relname = 'accounts'
  AND t3.nspname = ANY (current_schemas(false))
ORDER BY c.conname

   (3.2ms)  SELECT t2.oid::regclass::text AS to_table, a1.attname AS column, a2.attname AS primary_key, c.conname AS name, c.confupdtype AS on_update, c.confdeltype AS on_delete
FROM pg_constraint c
JOIN pg_class t1 ON c.conrelid = t1.oid
JOIN pg_class t2 ON c.confrelid = t2.oid
JOIN pg_attribute a1 ON a1.attnum = c.conkey[1] AND a1.attrelid = t1.oid
JOIN pg_attribute a2 ON a2.attnum = c.confkey[1] AND a2.attrelid = t2.oid
JOIN pg_namespace t3 ON c.connamespace = t3.oid
WHERE c.contype = 'f'
  AND t1.relname = 'deliveries'
  AND t3.nspname = ANY (current_schemas(false))
ORDER BY c.conname

   (3.2ms)  SELECT t2.oid::regclass::text AS to_table, a1.attname AS column, a2.attname AS primary_key, c.conname AS name, c.confupdtype AS on_update, c.confdeltype AS on_delete
FROM pg_constraint c
JOIN pg_class t1 ON c.conrelid = t1.oid
JOIN pg_class t2 ON c.confrelid = t2.oid
JOIN pg_attribute a1 ON a1.attnum = c.conkey[1] AND a1.attrelid = t1.oid
JOIN pg_attribute a2 ON a2.attnum = c.confkey[1] AND a2.attrelid = t2.oid
JOIN pg_namespace t3 ON c.connamespace = t3.oid
WHERE c.contype = 'f'
  AND t1.relname = 'posts'
  AND t3.nspname = ANY (current_schemas(false))
ORDER BY c.conname

Trace shows this:

** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke db:load_config (first_time)
** Execute db:load_config
** Execute db:migrate
** Invoke db:_dump (first_time)
** Execute db:_dump
** Invoke db:schema:dump (first_time)
** Invoke environment 
** Invoke db:load_config 
** Execute db:schema:dump

This started after upgrading a rails 4.1.6 to rails 4.2.0.

Why is this occurring?

回答1:

The reason I was getting this weird cryptic SQL'ish output is because I had Heroku's rails_12factor gem in my gemfile and not grouped to :production.

The solution was to do:

group :production do
  gem 'rails_12factor'
end

and run bundle



回答2:

I had this same issue after upgrading to Rails 4.2.1, preventing me from deploying with migrations via Codeship, which expects a very particular output from a rake db:migrate to consider a push successful.

Setting config.log_level = :info in config/environments/production.rb fixed the issue for me, presumably because the log level was too verbose at the :debug level.



回答3:

This discussion solved the "odd" output for me:
Just set dump_schema_after_migration to false in your environment configuration like this:

/config/environments/development.rb

Application.configure do
  ...
  dump_schema_after_migration = false
  ...
end

EDIT
This is not what you want to do in most cases because you need to have the schema.rb in your version control.