Module: Fx::Statements::Trigger

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

Overview

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

Constant Summary collapse

DEFINTION_TYPE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

'trigger'

Instance Method Summary collapse

Instance Method Details

#create_trigger(name, version: nil, on: nil, sql_definition: nil) ⇒ Object

Create a new database trigger.

Examples:

Create trigger from ‘db/triggers/uppercase_users_name_v01.sql`

create_trigger(:uppercase_users_name, version: 1)

Create trigger from provided SQL string

create_trigger(:uppercase_users_name, sql_definition: <<-SQL)
  CREATE TRIGGER uppercase_users_name
      BEFORE INSERT ON users
      FOR EACH ROW
      EXECUTE PROCEDURE uppercase_users_name();
 SQL

Parameters:

  • name (String, Symbol)

    The name of the database trigger.

  • version (Fixnum) (defaults to: nil)

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

  • sql_definition (String) (defaults to: nil)

    The SQL query for the function. An error will be raised if ‘sql_definition` and `version` are both set, as they are mutually exclusive.

Returns:

  • The database response from executing the create statement.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/fx/statements/trigger.rb', line 32

def create_trigger(name, version: nil, on: nil, sql_definition: nil)
  if version.present? && sql_definition.present?
    raise(
      ArgumentError,
      'sql_definition and version cannot both be set'
    )
  end

  if version.nil?
    version = 1
  end

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

  Fx.database.create_trigger(sql_definition)
end

#drop_trigger(name, *_opts, on: nil, revert_to_version: nil) ⇒ Object

Drop a database trigger by name.

Examples:

Drop a trigger, rolling back to version 3 on rollback

drop_trigger(:log_inserts, on: :users, revert_to_version: 3)

Parameters:

  • name (String, Symbol)

    The name of the database trigger.

  • on (String, Symbol) (defaults to: nil)

    The name of the table the database trigger is associated with.

  • revert_to_version (Fixnum) (defaults to: nil)

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

Returns:

  • The database response from executing the drop statement.



67
68
69
# File 'lib/fx/statements/trigger.rb', line 67

def drop_trigger(name, *_opts, on: nil, revert_to_version: nil)
  Fx.database.drop_trigger(name, on: on)
end

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

Update a database trigger to a new version.

The existing trigger is dropped and recreated using the supplied ‘on` and `version` parameter.

Examples:

Update trigger to a given version

update_trigger(
  :log_inserts,
  on: :users,
  version: 3,
  revert_to_version: 2,
)

Update trigger from provided SQL string

update_trigger(:uppercase_users_name, sql_definition: <<-SQL)
  CREATE TRIGGER uppercase_users_name
      BEFORE INSERT ON users
      FOR EACH ROW
      EXECUTE PROCEDURE uppercase_users_name();
 SQL

Parameters:

  • name (String, Symbol)

    The name of the database trigger.

  • version (Fixnum) (defaults to: nil)

    The version number of the trigger.

  • on (String, Symbol) (defaults to: nil)

    The name of the table the database trigger is associated with.

  • sql_definition (String) (defaults to: nil)

    The SQL query for the function. An error will be raised if ‘sql_definition` and `version` are both set, as they are mutually exclusive.

  • revert_to_version (Fixnum) (defaults to: nil)

    The version number to rollback to on ‘rake db rollback`

Returns:

  • The database response from executing the create statement.

Raises:

  • (ArgumentError)


103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/fx/statements/trigger.rb', line 103

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

  if version.present? && sql_definition.present?
    raise(
      ArgumentError,
      'sql_definition and version cannot both be set'
    )
  end

  raise ArgumentError, 'on is required' if on.nil?

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

  Fx.database.update_trigger(
    name,
    on: on,
    sql_definition: sql_definition
  )
end