Problem with migration
stestaub opened this issue · 2 comments
When I migrate the database but already added the acts_as... I get a database error that the relation does not exist.
PGError: ERROR: relation "actors" does not exist
WHERE a.attrelid = '"actors"'::regclass
SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnull
FROM pg_attribute a LEFT JOIN pg_attrdef d
ON a.attrelid = d.adrelid AND a.attnum = d.adnum
WHERE a.attrelid = '"actors"'::regclass
AND a.attnum > 0 AND NOT a.attisdropped
ORDER BY a.attnum
where actors is my superclass
As long as I first migrate the database and then add the acts_as... in code it works fine. But In my scenario where I develop on local machine and then deploy it to production I will always have to do it in two steps. First deploy the version with only the migrations and then the version with the code changes.
Also if I would setup a new staging server or new development environment, it will always fail on migrating the database.
Did I miss something in my setup or in the migration to avoid this error?
Ok, I found out that it has something to do with active_admin. It tries to create the routes and loads the User class that is subclass of actor. When it tries to load the columns it fails with the said error. It's may not realy a 'acts_as_relation' issue.
I will give update when i figured out how to solve this.
Ok I finally got it, it is because every rake tast calls the Application.initialize! what causes the evaluation of routes.rb. As my User model contains a acts_as_actor and I'm using devise, it crashed because the relation to actor did not exist yet.
As a workaround for anyone having also this issue:
Check if it is a migration task before defining the routes that will cause the error
in routes.rb for my case:
ActiveAdmin.routes(self) if !$ARGV.nil? && $ARGV.find_all { |x| x =~ /migrate|rollback|precompile/i}.empty?
and
devise_for :admin_users, ActiveAdmin::Devise.config if !$ARGV.nil? && $ARGV.find_all { |x| x =~ /migrate|rollback|precompile/i}.empty?
devise_for :users, :controllers => { :registrations => :settings } if !$ARGV.nil? && $ARGV.find_all { |x| x =~ /migrate|rollback|precompile/i}.empty?
For more information about this issue see:
activeadmin/activeadmin#292