Class: Fx::Adapters::Postgres

Inherits:
Object
  • Object
show all
Defined in:
lib/fx/adapters/postgres.rb,
lib/fx/adapters/postgres/triggers.rb,
lib/fx/adapters/postgres/functions.rb,
lib/fx/adapters/postgres/connection.rb

Overview

Creates an instance of the F(x) Postgres adapter.

This is the default adapter for F(x). Configuring it via Fx.configure is not required, but the example below shows how one would explicitly set it.

Examples:

Fx.configure do |config|
  config.adapter = Fx::Adapters::Postgres.new
end

Instance Method Summary collapse

Constructor Details

#initialize(connectable = ActiveRecord::Base) ⇒ Postgres

Creates an instance of the F(x) Postgres adapter.

This is the default adapter for F(x). Configuring it via Fx.configure is not required, but the example below shows how one would explicitly set it.

Examples:

Fx.configure do |config|
  config.adapter = Fx::Adapters::Postgres.new
end

Parameters:

  • connectable (#connection) (defaults to: ActiveRecord::Base)

    An object that returns the connection for F(x) to use. Defaults to ActiveRecord::Base.



39
40
41
# File 'lib/fx/adapters/postgres.rb', line 39

def initialize(connectable = ActiveRecord::Base)
  @connectable = connectable
end

Instance Method Details

#create_function(sql_definition) ⇒ void

This method returns an undefined value.

Creates a function in the database.

This is typically called in a migration via Statements::Function#create_function.

Parameters:

  • sql_definition

    The SQL schema for the function.



71
72
73
# File 'lib/fx/adapters/postgres.rb', line 71

def create_function(sql_definition)
  execute(sql_definition)
end

#create_trigger(sql_definition) ⇒ void

This method returns an undefined value.

Creates a trigger in the database.

This is typically called in a migration via Statements::Trigger#create_trigger.

Parameters:

  • sql_definition

    The SQL schema for the trigger.



83
84
85
# File 'lib/fx/adapters/postgres.rb', line 83

def create_trigger(sql_definition)
  execute(sql_definition)
end

#drop_function(name) ⇒ void

This method returns an undefined value.

Drops the function from the database

This is typically called in a migration via Statements::Function#drop_function.

Parameters:

  • name

    The name of the function to drop



127
128
129
130
131
132
133
# File 'lib/fx/adapters/postgres.rb', line 127

def drop_function(name)
  if connection.support_drop_function_without_args
    execute("DROP FUNCTION #{name};")
  else
    execute("DROP FUNCTION #{name}();")
  end
end

#drop_trigger(name, on:) ⇒ void

This method returns an undefined value.

Drops the trigger from the database

This is typically called in a migration via Statements::Trigger#drop_trigger.

Parameters:

  • name

    The name of the trigger to drop

  • on

    The associated table for the trigger to drop



144
145
146
# File 'lib/fx/adapters/postgres.rb', line 144

def drop_trigger(name, on:)
  execute("DROP TRIGGER #{name} ON #{on};")
end

#functionsArray<Fx::Function>

Returns an array of functions in the database.

This collection of functions is used by the [Fx::SchemaDumper] to populate the schema.rb file.

Returns:

  • (Array<Fx::Function>)


49
50
51
# File 'lib/fx/adapters/postgres.rb', line 49

def functions
  Functions.all(connection)
end

#triggersArray<Fx::Trigger>

Returns an array of triggers in the database.

This collection of triggers is used by the [Fx::SchemaDumper] to populate the schema.rb file.

Returns:

  • (Array<Fx::Trigger>)


59
60
61
# File 'lib/fx/adapters/postgres.rb', line 59

def triggers
  Triggers.all(connection)
end

#update_function(name, sql_definition) ⇒ void

This method returns an undefined value.

Updates a function in the database.

This is typically called in a migration via Statements::Function#update_function.

Parameters:

  • name

    The name of the function.

  • sql_definition

    The SQL schema for the function.



96
97
98
99
# File 'lib/fx/adapters/postgres.rb', line 96

def update_function(name, sql_definition)
  drop_function(name)
  create_function(sql_definition)
end

#update_trigger(name, on:, sql_definition:) ⇒ void

This method returns an undefined value.

Updates a trigger in the database.

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

This is typically called in a migration via Statements::Function#update_trigger.

Parameters:

  • name

    The name of the trigger.

  • on

    The associated table for the trigger to drop

  • sql_definition

    The SQL schema for the function.



114
115
116
117
# File 'lib/fx/adapters/postgres.rb', line 114

def update_trigger(name, on:, sql_definition:)
  drop_trigger(name, on: on)
  create_trigger(sql_definition)
end