简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM