class Fx::Adapters::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.

@param [#connection] connectable An object that returns the connection

for F(x) to use. Defaults to `ActiveRecord::Base`.

@example

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

Attributes

connectable[R]

Public Class Methods

new(connectable = ActiveRecord::Base) click to toggle source

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.

@param [#connection] connectable An object that returns the connection

for F(x) to use. Defaults to `ActiveRecord::Base`.

@example

Fx.configure do |config|
  config.adapter = Fx::Adapters::Postgres.new
end
# File lib/fx/adapters/postgres.rb, line 39
def initialize(connectable = ActiveRecord::Base)
  @connectable = connectable
end

Public Instance Methods

create_function(sql_definition) click to toggle source

Creates a function in the database.

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

@param sql_definition The SQL schema for the function.

@return [void]

# File lib/fx/adapters/postgres.rb, line 71
def create_function(sql_definition)
  execute sql_definition
end
create_trigger(sql_definition) click to toggle source

Creates a trigger in the database.

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

@param sql_definition The SQL schema for the trigger.

@return [void]

# File lib/fx/adapters/postgres.rb, line 83
def create_trigger(sql_definition)
  execute sql_definition
end
drop_function(name) click to toggle source

Drops the function from the database

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

@param name The name of the function to drop

@return [void]

# File lib/fx/adapters/postgres.rb, line 127
def drop_function(name)
  if support_drop_function_without_args
    execute "DROP FUNCTION #{name};"
  else
    execute "DROP FUNCTION #{name}();"
  end
end
drop_trigger(name, on:) click to toggle source

Drops the trigger from the database

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

@param name The name of the trigger to drop @param on The associated table for the trigger to drop

@return [void]

# File lib/fx/adapters/postgres.rb, line 144
def drop_trigger(name, on:)
  execute "DROP TRIGGER #{name} ON #{on};"
end
functions() click to toggle source

Returns an array of functions in the database.

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

@return [Array<Fx::Function>]

# File lib/fx/adapters/postgres.rb, line 49
def functions
  Functions.all(connection)
end
triggers() click to toggle source

Returns an array of triggers in the database.

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

@return [Array<Fx::Trigger>]

# File lib/fx/adapters/postgres.rb, line 59
def triggers
  Triggers.all(connection)
end
update_function(name, sql_definition) click to toggle source

Updates a function in the database.

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

@param name The name of the function. @param sql_definition The SQL schema for the function.

@return [void]

# 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:) click to toggle source

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 {Fx::Statements::Function#update_trigger}.

@param name The name of the trigger. @param on The associated table for the trigger to drop @param sql_definition The SQL schema for the function.

@return [void]

# 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

Private Instance Methods

connection() click to toggle source
# File lib/fx/adapters/postgres.rb, line 154
def connection
  Connection.new(connectable.connection)
end
support_drop_function_without_args() click to toggle source
# File lib/fx/adapters/postgres.rb, line 158
def support_drop_function_without_args
  # https://www.postgresql.org/docs/9.6/sql-dropfunction.html
  # https://www.postgresql.org/docs/10/sql-dropfunction.html

  pg_connection = connectable.connection.raw_connection
  pg_connection.server_version >= 10_00_00
end