Module: DynamicMigrations::Postgres::Generator::Function

Included in:
DynamicMigrations::Postgres::Generator
Defined in:
lib/dynamic_migrations/postgres/generator/function.rb

Instance Method Summary collapse

Instance Method Details

#create_function(function, code_comment = nil) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/dynamic_migrations/postgres/generator/function.rb', line 5

def create_function function, code_comment = nil
  options = {}

  if function.description.nil?
    comment_sql = ""
  else
    comment_sql = <<~RUBY
      #{function.name}_comment = <<~COMMENT
        #{indent function.description || ""}
      COMMENT
    RUBY
    options[:comment] = "#{function.name}_comment"
  end

  options_syntax = options.map { |k, v| "#{k}: #{v}" }.join(", ")
  optional_options_syntax = (options_syntax == "") ? "" : ", #{options_syntax}"

  fn_sql = function.definition.strip

  add_fragment schema: function.schema,
    table: optional_function_table(function),
    migration_method: :create_function,
    object: function,
    code_comment: code_comment,
    migration: comment_sql + <<~RUBY
      create_function :#{function.name}#{optional_options_syntax} do
        <<~SQL
          #{indent fn_sql, 2}
        SQL
      end
    RUBY
end

#drop_function(function, code_comment = nil) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/dynamic_migrations/postgres/generator/function.rb', line 55

def drop_function function, code_comment = nil
  add_fragment schema: function.schema,
    table: optional_function_table(function),
    migration_method: :drop_function,
    object: function,
    code_comment: code_comment,
    migration: <<~RUBY
      drop_function :#{function.name}
    RUBY
end

#optional_function_table(function) ⇒ Object

we only provide a table to these migration fragments if the function applies only to one table and that take is in the same schema as the function



94
95
96
97
98
# File 'lib/dynamic_migrations/postgres/generator/function.rb', line 94

def optional_function_table function
  # all the tables which use this function
  tables = function.triggers.map(&:table).uniq
  (tables.count == 1 && tables.first&.schema == function.schema) ? tables.first : nil
end

#remove_function_comment(function, code_comment = nil) ⇒ Object

remove the comment from a function



81
82
83
84
85
86
87
88
89
90
# File 'lib/dynamic_migrations/postgres/generator/function.rb', line 81

def remove_function_comment function, code_comment = nil
  add_fragment schema: function.schema,
    table: optional_function_table(function),
    migration_method: :remove_function_comment,
    object: function,
    code_comment: code_comment,
    migration: <<~RUBY
      remove_function_comment :#{function.name}
    RUBY
end

#set_function_comment(function, code_comment = nil) ⇒ Object

add a comment to a function



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/dynamic_migrations/postgres/generator/function.rb', line 67

def set_function_comment function, code_comment = nil
  add_fragment schema: function.schema,
    table: optional_function_table(function),
    migration_method: :set_function_comment,
    object: function,
    code_comment: code_comment,
    migration: <<~RUBY
      set_function_comment :#{function.name}, <<~COMMENT
        #{indent function.description || ""}
      COMMENT
    RUBY
end

#update_function(function, code_comment = nil) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/dynamic_migrations/postgres/generator/function.rb', line 38

def update_function function, code_comment = nil
  fn_sql = function.definition.strip

  add_fragment schema: function.schema,
    table: optional_function_table(function),
    migration_method: :update_function,
    object: function,
    code_comment: code_comment,
    migration: <<~RUBY
      update_function :#{function.name} do
        <<~SQL
          #{indent fn_sql, 2}
        SQL
      end
    RUBY
end