Module: Fx::Statements::Function

Defined in:
lib/fx/statements/function.rb

Overview

Methods that are made available in migrations for managing Fx functions.

Instance Method Summary collapse

Instance Method Details

#create_function(name, on: '', version: 1, sql_definition: nil) ⇒ Object

Create a new database function.

Examples:

Create from ‘db/functions/uppercase_users_name_v02.sql`

create_function(:uppercase_users_name, version: 2)

Create from provided SQL string

create_function(:uppercase_users_name, sql_definition: <<-SQL)
  CREATE OR REPLACE FUNCTION uppercase_users_name()
  RETURNS trigger AS $$
  BEGIN
    NEW.upper_name = UPPER(NEW.name);
    RETURN NEW;
  END;
  $$ LANGUAGE plpgsql;
SQL

Parameters:

  • name (String, Symbol)

    The name of the database function.

  • version (Fixnum) (defaults to: 1)

    The version number of the function, used to find the definition file in ‘db/functions`. This defaults to `1` if not provided.

  • sql_definition (String) (defaults to: nil)

    The SQL query for the function schema. If both ‘sql_defintion` and `version` are provided, `sql_definition` takes prescedence.

Returns:

  • The database response from executing the create statement.



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/fx/statements/function.rb', line 34

def create_function(name, on: '', version: 1, sql_definition: nil)
  if version.nil? && sql_definition.nil?
    raise(
      ArgumentError,
      'version or sql_definition must be specified'
    )
  end
  sql_definition = sql_definition.strip_heredoc if sql_definition
  sql_definition ||= Fx::Definition.new(name: name, version: version).to_sql

  Fx.database.create_function(sql_definition)
end

#drop_function(name, *_opts, revert_to_version: nil, **_options) ⇒ Object

Drop a database function by name.

Examples:

Drop a function, rolling back to version 2 on rollback

drop_function(:uppercase_users_name, revert_to_version: 2)

Parameters:

  • name (String, Symbol)

    The name of the database function.

  • revert_to_version (Fixnum) (defaults to: nil)

    Used to reverse the ‘drop_function` command on `rake db:rollback`. The provided version will be passed as the `version` argument to #create_function.

Returns:

  • The database response from executing the drop statement.



58
59
60
# File 'lib/fx/statements/function.rb', line 58

def drop_function(name, *_opts, revert_to_version: nil, **_options)
  Fx.database.drop_function(name)
end

#update_function(name, version: nil, sql_definition: nil, revert_to_version: nil) ⇒ Object

Update a database function.

Examples:

Update function to a given version

update_function(
  :uppercase_users_name,
  version: 3,
  revert_to_version: 2,
)

Update function from provided SQL string

update_function(:uppercase_users_name, sql_definition: <<-SQL)
  CREATE OR REPLACE FUNCTION uppercase_users_name()
  RETURNS trigger AS $$
  BEGIN
    NEW.upper_name = UPPER(NEW.name);
    RETURN NEW;
  END;
  $$ LANGUAGE plpgsql;
SQL

Parameters:

  • name (String, Symbol)

    The name of the database function.

  • version (Fixnum) (defaults to: nil)

    The version number of the function, used to find the definition file in ‘db/functions`. This defaults to `1` if not provided.

  • sql_definition (String) (defaults to: nil)

    The SQL query for the function schema. If both ‘sql_defintion` and `version` are provided, `sql_definition` takes prescedence.

Returns:

  • The database response from executing the create statement.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/fx/statements/function.rb', line 91

def update_function(name, version: nil, sql_definition: nil, revert_to_version: nil)
  if version.nil? && sql_definition.nil?
    raise(
      ArgumentError,
      'version or sql_definition must be specified'
    )
  end

  sql_definition = sql_definition.strip_heredoc if sql_definition
  sql_definition ||= Fx::Definition.new(
    name: name,
    version: version,
  ).to_sql

  Fx.database.update_function(name, sql_definition)
end