Method: ActiveRecord::Migration#create_view

Defined in:
lib/pg_trunk/operations/views/create_view.rb

#create_view(name, **options) {|v| ... } ⇒ void

This method returns an undefined value.

Create a view

The operation creates the view using its sql_definition:

create_view("views.admin_users", sql_definition: "SELECT id, name FROM users WHERE admin;\n")

For compatibility to the scenic gem, we also support adding a definition via its version:

create_view "admin_users", version: 1

It is expected, that a db/views/admin_users_v01.sql to contain the SQL snippet.

You can also set a comment describing the view, and the check option (either :local or :cascaded):

create_view "admin_users" do |v|
  v.sql_definition "SELECT id, name FROM users WHERE admin;"
  v.check :local
  v.comment "Admin users only"
end

With the replace_existing: true option the operation would use CREATE OR REPLACE VIEW command, so it can be used to "update" (or reload) the existing view.

create_view "admin_users", version: 1, replace_existing: true

This option makes an operation irreversible due to uncertainty of the previous state of the database.

Parameters:

  • (nil) The qualified name of the view

  • a customizable set of options

Options Hash (**options):

  • :replace_existing (Boolean) — default: false

    If the view should overwrite an existing one

  • :sql_definition (#to_s) — default: nil

    The snippet containing the query

  • :version (#to_i) — default: nil

    The alternative way to set sql_definition by referencing to a file containing the snippet

  • :check (#to_s) — default: nil

    Controls the behavior of automatically updatable views Supported values: :local, :cascaded

  • :comment (#to_s) — default: nil

    The comment describing the view

Yields:

  • (v)

    the block with the view's definition

Yield Parameters:

  • Object

    receiver of methods specifying the view



55
# File 'lib/pg_trunk/operations/views/create_view.rb', line 55

def create_view(name, **options, &block); end