Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails schema.rb does not include new custom Postgres function

I have just created a new custom Postgres function via usual migration.

class CreateBestBowlingFigureFunction < ActiveRecord::Migration
  def change
    execute "CREATE OR REPLACE FUNCTION ......"
  end
end

After the migration, this new function is not available in schema.rb.

As per the official doc, I use the command db:schema:load to create the schema before running my tests.

So, what is the best practice to create custom functions before running the tests?

like image 666
Lenin Raj Rajasekaran Avatar asked Dec 16 '14 14:12

Lenin Raj Rajasekaran


1 Answers

schema.rb does not handle (see section 6.2 of the Rails 3.2.x guides and section 7.2 of the Rails 4 guides) views or custom functions. We have a view in our application and the schema does not work for it.

We use structure.sql instead as this properly sets up our view and, my sense is the same would apply here for custom functions. To use structure.sql instead of schema.rb:

This is set in config/application.rb by the config.active_record.schema_format setting, which may be either :sql or :ruby.

You can also use a combination of schema.rb (for regular tables & index) and structure.sql (for custom functions). To setup this combo for test environments:

bundle exec rake db:schema:load
bundle exec rake db:structure:load

In this setup, note that the structure.sql has to be maintained manually while schema.rb will be maintained by Rails for you.

like image 130
craig.kaminsky Avatar answered Nov 04 '22 06:11

craig.kaminsky