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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With