Building test rails database with custom rake task

rails
ruby

#1

I’m in the process of upgrading an app of ours from Rails 3 to Rails 4 and I’m hitting a problem which is causing the database to fail to load. An example stack trace, which is occurring in the Database section of the build:

export RAILS_ENV="test"
export RACK_ENV="test"
bundle exec rake db:create db:schema:load --trace
** Invoke db:create (first_time)
** Invoke db:load_config (first_time)
** Execute db:load_config
** Execute db:create
** Invoke db:schema:load (first_time)
** Invoke environment (first_time)
** Execute environment
rake aborted!
ActiveRecord::StatementInvalid: PG::Error: ERROR:  relation "base_posts" does not exist
LINE 5:                WHERE a.attrelid = '"base_posts"'::regclass
                                          ^
:               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 = '"base_posts"'::regclass
                 AND a.attnum > 0 AND NOT a.attisdropped
               ORDER BY a.attnum
/home/ubuntu/copyin-website/vendor/bundle/ruby/1.9.1/gems/activerecord-4.0.13/lib/active_record/connection_adapters/postgresql_adapter.rb:798:in `async_exec'

The problem is that due to the gems we’re using (and how they’re configured), rake db:schema:load doesn’t work. I’ve looked into this and I think it’s pretty much unresolvable at the moment; though may be once we’ve completed our upgrade.

I’m able to reproduce this error locally, but I have found that running rake db:test:prepare creates the database successfully.

So, I’d like to replace the rake db:create db:schema:load step to rake db:test:prepare. Is that possible?


#2

For anyone who’s interested, in order to do what I’m asking, check out the section on database overrides in the example circle.yml: https://circleci.com/docs/config-sample

I wanted something like:

## Customize database setup
database:
  override:
    # replace CircleCI's generated database.yml is required if we want to run something other than db:schema:load
    - cp config/database.yml.ci config/database.yml
    # Run some custom command
    - bundle exec rake db:test:prepare

This wasn’t ultimately the root of my problem; I finally found out the cause as being a horrible munge of FriendlyId, Rails and STI all not working well together.

Long story short, don’t extend FriendlyId in STI subclasses.


#3