pg_saurus 3.2.2 → 3.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b4147b6ee5df11ef09825b5c6b5d59d0521b357b
4
- data.tar.gz: ee5a976b7fdd83f17492d3a877a11d4ebbc25a62
3
+ metadata.gz: 95526ab7651625ecb8d454074e855cbddb574524
4
+ data.tar.gz: a44975a7e86b74893dbec2ded843a2a82b80780c
5
5
  SHA512:
6
- metadata.gz: 8689fb75e3adea700b5a1ced18232efccac99b3989370fbfbfe8ec3e9ef926f6bb97b8302fd0c59b22abfeeb01a716a521000c978fb6aa672a1517c5019402df
7
- data.tar.gz: 23c7d58b7eeae4ad725c301cf54393eaccf36b1c7ac6ddbd3740846aafa54401e668c06bc5a2c01abd46ec67aefd55bfc8fa1e8019ff8980a7093ed0c83edfaa
6
+ metadata.gz: edde7148883c2b70be2f90ec6894fd4cabb644c6e981af6a224c8842ab9f6b221e2a4e4c624f396d4f54b0249d09fbd2eee79666fd2e815fbdb1d78f910e773b
7
+ data.tar.gz: ab9d11096b351eb872a7c8a16e5569ccce00a657863a4293987d34a5f844c3c19f9df540f2b7466a3a07544d172ed2f5a9299e9ce83a4ee3b3e62f54a14322c5
data/README.markdown CHANGED
@@ -366,8 +366,8 @@ BEGIN
366
366
  END;
367
367
  SQL
368
368
 
369
- # Arguments are: function_name, return_type, function_definition, options (currently, only :schema)
370
- create_function 'pets_not_empty()', :boolean, pets_not_empty_function, schema: 'public'
369
+ # Arguments are: function_name, return_type, function_definition, options (currently, only :schema and :volatility)
370
+ create_function 'pets_not_empty()', :boolean, pets_not_empty_function, schema: 'public', volatility: 'stable'
371
371
  ```
372
372
  Drop a function:
373
373
 
@@ -5,6 +5,7 @@ module PgSaurus::ConnectionAdapters
5
5
  :definition,
6
6
  :function_type,
7
7
  :language,
8
- :oid )
8
+ :oid,
9
+ :volatility )
9
10
  end
10
11
  end
@@ -39,6 +39,7 @@ module PgSaurus::ConnectionAdapters::PostgreSQLAdapter::FunctionMethods
39
39
  name = parse_function_name(function_str)
40
40
  language = parse_function_language(function_str)
41
41
  definition = parse_function_definition(function_str)
42
+ volatility = parse_function_volatility(function_str)
42
43
 
43
44
  if definition
44
45
  buffer << ::PgSaurus::ConnectionAdapters::FunctionDefinition.new(name,
@@ -46,7 +47,8 @@ module PgSaurus::ConnectionAdapters::PostgreSQLAdapter::FunctionMethods
46
47
  definition.strip,
47
48
  function_type,
48
49
  language,
49
- oid)
50
+ oid,
51
+ volatility)
50
52
  end
51
53
  buffer
52
54
  end
@@ -62,11 +64,17 @@ module PgSaurus::ConnectionAdapters::PostgreSQLAdapter::FunctionMethods
62
64
  else
63
65
  'OR REPLACE '
64
66
  end
67
+ volatility = case options[:volatility]
68
+ when :volatile, :stable, :immutable
69
+ "\n #{options[:volatility].to_s.upcase}"
70
+ else
71
+ ""
72
+ end
65
73
 
66
74
  sql = <<-SQL.gsub(/^[ ]{6}/, "")
67
75
  CREATE #{replace}FUNCTION #{function_name}
68
76
  RETURNS #{returning}
69
- LANGUAGE #{language}
77
+ LANGUAGE #{language}#{volatility}
70
78
  AS $function$
71
79
  #{definition.strip}
72
80
  $function$
@@ -97,6 +105,24 @@ module PgSaurus::ConnectionAdapters::PostgreSQLAdapter::FunctionMethods
97
105
  end
98
106
  private :parse_function_language
99
107
 
108
+ # Retrieve the volatility of the function: volatile, stable, or immutable.
109
+ # @return [Symbol]
110
+ def parse_function_volatility(function_str)
111
+ rows = function_str.split("\n")
112
+ lang_index = rows.index { |line| line =~ /LANGUAGE/ }
113
+ def_index = rows.index { |line| line =~ /AS \$function\$/ }
114
+
115
+ if lang_index && def_index && def_index - lang_index == 2
116
+ tokens = rows[def_index - 1].strip.downcase.split(" ")
117
+ token = tokens.find { |t| %w(volatile stable immutable).include? t }
118
+
119
+ token.nil? ? :volatile : token.to_sym
120
+ else
121
+ :volatile
122
+ end
123
+ end
124
+ private :parse_function_volatility
125
+
100
126
  # Retrieve the function definition from the function SQL.
101
127
  def parse_function_definition(function_str)
102
128
  function_str[/#{Regexp.escape("AS $function$\n")}(.*?)#{Regexp.escape("$function$")}/m, 1]
@@ -3,17 +3,19 @@ module PgSaurus::SchemaDumper::FunctionMethods
3
3
 
4
4
  # :nodoc
5
5
  def tables_with_functions(stream)
6
- tables_without_functions(stream)
7
-
6
+ # Functions must be dumped before tables.
7
+ # Some indexes may use defined functions.
8
8
  dump_functions stream
9
9
 
10
+ tables_without_functions(stream)
11
+
10
12
  stream
11
13
  end
12
14
 
13
15
  # Writes out a command to create each detected function.
14
16
  def dump_functions(stream)
15
17
  @connection.functions.each do |function|
16
- statement = " create_function '#{function.name}', :#{function.returning}, <<-FUNCTION_DEFINITION.gsub(/^[\s]{4}/, '')"
18
+ statement = " create_function '#{function.name}', :#{function.returning}, <<-FUNCTION_DEFINITION.gsub(/^[\s]{4}/, ''), volatility: :#{function.volatility}"
17
19
  statement << "\n#{function.definition.split("\n").map{|line| " #{line}" }.join("\n")}"
18
20
  statement << "\n FUNCTION_DEFINITION\n\n"
19
21
 
@@ -1,4 +1,4 @@
1
1
  module PgSaurus
2
2
  # Version of pg_saurus gem.
3
- VERSION = "3.2.2"
3
+ VERSION = "3.3.0"
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pg_saurus
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.2.2
4
+ version: 3.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Potapov Sergey
@@ -13,7 +13,7 @@ authors:
13
13
  autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
- date: 2018-04-24 00:00:00.000000000 Z
16
+ date: 2018-09-06 00:00:00.000000000 Z
17
17
  dependencies:
18
18
  - !ruby/object:Gem::Dependency
19
19
  name: pg