Class: DynamicMigrations::Postgres::Server::Database::Schema::Function

Inherits:
DynamicMigrations::Postgres::Server::Database::Source show all
Defined in:
lib/dynamic_migrations/postgres/server/database/schema/function.rb

Overview

This class represents a postgres function.

Defined Under Namespace

Classes: ExpectedDefinitionError, ExpectedSchemaError, UnnormalizableDefinitionError

Instance Attribute Summary collapse

Attributes inherited from DynamicMigrations::Postgres::Server::Database::Source

#source

Instance Method Summary collapse

Methods inherited from DynamicMigrations::Postgres::Server::Database::Source

#assert_is_a_symbol!, #from_configuration?, #from_database?

Constructor Details

#initialize(source, schema, name, definition, description: nil) ⇒ Function

initialize a new object to represent a postgres function



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/dynamic_migrations/postgres/server/database/schema/function.rb', line 26

def initialize source, schema, name, definition, description: nil
  super source

  @triggers = []

  raise ExpectedSchemaError, schema unless schema.is_a? Schema
  @schema = schema

  raise ExpectedSymbolError, name unless name.is_a? Symbol
  @name = name

  unless definition.is_a?(String) && definition.strip != "" && definition.strip.end_with?("END;", "END")
    raise ExpectedDefinitionError, "Definition must be a string, and end with `END;`. Definition provided:\n#{definition}"
  end
  @definition = definition.strip.freeze

  unless description.nil?
    raise ExpectedStringError, description unless description.is_a? String
    @description = description.strip.freeze
    @description = nil if description == ""
  end
end

Instance Attribute Details

#definitionObject (readonly)

Returns the value of attribute definition.



21
22
23
# File 'lib/dynamic_migrations/postgres/server/database/schema/function.rb', line 21

def definition
  @definition
end

#descriptionObject (readonly)

Returns the value of attribute description.



22
23
24
# File 'lib/dynamic_migrations/postgres/server/database/schema/function.rb', line 22

def description
  @description
end

#nameObject (readonly)

Returns the value of attribute name.



20
21
22
# File 'lib/dynamic_migrations/postgres/server/database/schema/function.rb', line 20

def name
  @name
end

#schemaObject (readonly)

Returns the value of attribute schema.



19
20
21
# File 'lib/dynamic_migrations/postgres/server/database/schema/function.rb', line 19

def schema
  @schema
end

#triggersObject (readonly)

Returns the value of attribute triggers.



23
24
25
# File 'lib/dynamic_migrations/postgres/server/database/schema/function.rb', line 23

def triggers
  @triggers
end

Instance Method Details

#add_trigger(trigger) ⇒ Object

for tracking all the triggers which are associated with this function



55
56
57
58
59
60
61
# File 'lib/dynamic_migrations/postgres/server/database/schema/function.rb', line 55

def add_trigger trigger
  # this should never happen, but adding it just in case
  unless trigger.source == source
    raise "Internal error - trigger source `#{trigger.source}` does not match function source `#{source}`"
  end
  @triggers << trigger
end

#differences_descriptions(other_function) ⇒ Object



63
64
65
66
67
# File 'lib/dynamic_migrations/postgres/server/database/schema/function.rb', line 63

def differences_descriptions other_function
  method_differences_descriptions other_function, [
    :normalized_definition
  ]
end

#has_description?Boolean

returns true if this function has a description, otehrwise false

Returns:

  • (Boolean)


50
51
52
# File 'lib/dynamic_migrations/postgres/server/database/schema/function.rb', line 50

def has_description?
  !@description.nil?
end

#normalized_definitionObject

temporarily create a function in postgres and fetch the actual normalized definition directly from the database



71
72
73
74
75
76
77
78
# File 'lib/dynamic_migrations/postgres/server/database/schema/function.rb', line 71

def normalized_definition
  # no need to normalize definitions which originated from the database
  if from_database?
    definition
  else
    @normalized_definition ||= fetch_normalized_definition
  end
end