fx 0.7.0 → 0.9.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.
Files changed (85) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ci.yml +53 -0
  3. data/.gitignore +0 -1
  4. data/.rspec +1 -1
  5. data/.standard.yml +3 -0
  6. data/CHANGELOG.md +124 -0
  7. data/CONTRIBUTING.md +13 -4
  8. data/Gemfile +13 -2
  9. data/README.md +4 -2
  10. data/Rakefile +2 -1
  11. data/bin/setup +0 -4
  12. data/bin/standardrb +27 -0
  13. data/fx.gemspec +20 -27
  14. data/lib/fx/adapters/postgres/connection.rb +12 -0
  15. data/lib/fx/adapters/postgres/functions.rb +3 -3
  16. data/lib/fx/adapters/postgres/triggers.rb +3 -3
  17. data/lib/fx/adapters/postgres.rb +6 -14
  18. data/lib/fx/command_recorder.rb +87 -6
  19. data/lib/fx/configuration.rb +0 -25
  20. data/lib/fx/definition.rb +16 -6
  21. data/lib/fx/function.rb +7 -7
  22. data/lib/fx/schema_dumper.rb +45 -5
  23. data/lib/fx/statements.rb +228 -6
  24. data/lib/fx/trigger.rb +3 -3
  25. data/lib/fx/version.rb +1 -1
  26. data/lib/fx.rb +30 -12
  27. data/lib/generators/fx/function/function_generator.rb +8 -8
  28. data/lib/generators/fx/trigger/trigger_generator.rb +6 -10
  29. data/spec/acceptance/user_manages_functions_spec.rb +5 -5
  30. data/spec/acceptance/user_manages_triggers_spec.rb +8 -8
  31. data/spec/acceptance_helper.rb +6 -4
  32. data/spec/dummy/Rakefile +4 -4
  33. data/spec/dummy/bin/bundle +2 -2
  34. data/spec/dummy/bin/rails +3 -3
  35. data/spec/dummy/bin/rake +2 -2
  36. data/spec/dummy/config/application.rb +6 -0
  37. data/spec/dummy/config/database.yml +2 -0
  38. data/spec/dummy/config.ru +1 -1
  39. data/spec/features/functions/migrations_spec.rb +4 -4
  40. data/spec/features/functions/revert_spec.rb +7 -7
  41. data/spec/features/triggers/migrations_spec.rb +6 -6
  42. data/spec/features/triggers/revert_spec.rb +13 -13
  43. data/spec/fx/adapters/postgres/functions_spec.rb +26 -30
  44. data/spec/fx/adapters/postgres/triggers_spec.rb +34 -38
  45. data/spec/fx/adapters/postgres_spec.rb +107 -109
  46. data/spec/fx/command_recorder_spec.rb +41 -39
  47. data/spec/fx/configuration_spec.rb +20 -9
  48. data/spec/fx/definition_spec.rb +30 -38
  49. data/spec/fx/function_spec.rb +45 -48
  50. data/spec/fx/schema_dumper_spec.rb +123 -0
  51. data/spec/fx/statements_spec.rb +217 -0
  52. data/spec/fx/trigger_spec.rb +37 -40
  53. data/spec/fx_spec.rb +28 -0
  54. data/spec/generators/fx/function/function_generator_spec.rb +6 -6
  55. data/spec/generators/fx/trigger/trigger_generator_spec.rb +10 -10
  56. data/spec/spec_helper.rb +5 -0
  57. data/spec/support/definition_helpers.rb +5 -9
  58. data/spec/support/generator_setup.rb +1 -1
  59. data/spec/support/migration_helpers.rb +1 -1
  60. data/spec/support/warning_helper.rb +5 -0
  61. metadata +24 -213
  62. data/.hound.yml +0 -2
  63. data/.rubocop.yml +0 -648
  64. data/.travis.yml +0 -60
  65. data/Appraisals +0 -45
  66. data/bin/appraisal +0 -17
  67. data/gemfiles/rails42.gemfile +0 -10
  68. data/gemfiles/rails50.gemfile +0 -8
  69. data/gemfiles/rails51.gemfile +0 -8
  70. data/gemfiles/rails52.gemfile +0 -8
  71. data/gemfiles/rails60.gemfile +0 -8
  72. data/gemfiles/rails61.gemfile +0 -8
  73. data/gemfiles/rails_edge.gemfile +0 -8
  74. data/lib/fx/command_recorder/arguments.rb +0 -43
  75. data/lib/fx/command_recorder/function.rb +0 -30
  76. data/lib/fx/command_recorder/trigger.rb +0 -30
  77. data/lib/fx/schema_dumper/function.rb +0 -38
  78. data/lib/fx/schema_dumper/trigger.rb +0 -29
  79. data/lib/fx/statements/function.rb +0 -115
  80. data/lib/fx/statements/trigger.rb +0 -146
  81. data/spec/fx/command_recorder/arguments_spec.rb +0 -41
  82. data/spec/fx/schema_dumper/function_spec.rb +0 -80
  83. data/spec/fx/schema_dumper/trigger_spec.rb +0 -40
  84. data/spec/fx/statements/function_spec.rb +0 -103
  85. data/spec/fx/statements/trigger_spec.rb +0 -132
data/Appraisals DELETED
@@ -1,45 +0,0 @@
1
- if RUBY_VERSION < "2.6.0"
2
- appraise "rails42" do
3
- gem "activerecord", "~> 4.2.0"
4
- gem "railties", "~> 4.2.0"
5
- gem "pg", "~> 0.15.0"
6
- gem "bigdecimal", "1.3.5"
7
- end
8
- end
9
-
10
- if RUBY_VERSION >= "2.2.0" and RUBY_VERSION < "3.0.0"
11
- appraise "rails50" do
12
- gem "activerecord", "~> 5.0.0"
13
- gem "railties", "~> 5.0.0"
14
- end
15
-
16
- appraise "rails51" do
17
- gem "activerecord", "~> 5.1.0"
18
- gem "railties", "~> 5.1.0"
19
- end
20
-
21
- appraise "rails52" do
22
- gem "activerecord", "~> 5.2.0"
23
- gem "railties", "~> 5.2.0"
24
- end
25
-
26
- end
27
-
28
- if RUBY_VERSION >= "2.5.0"
29
- appraise "rails60" do
30
- gem "activerecord", "~> 6.0.0"
31
- gem "railties", "~> 6.0.0"
32
- end
33
-
34
- appraise "rails61" do
35
- gem "activerecord", "~> 6.1.0"
36
- gem "railties", "~> 6.1.0"
37
- end
38
- end
39
-
40
- if RUBY_VERSION >= "2.7.0"
41
- appraise "rails-edge" do
42
- gem "rails", github: "rails/rails", branch: "main"
43
- gem "arel", :github => "rails/arel"
44
- end
45
- end
data/bin/appraisal DELETED
@@ -1,17 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # frozen_string_literal: true
3
- #
4
- # This file was generated by Bundler.
5
- #
6
- # The application 'appraisal' is installed as part of a gem, and
7
- # this file is here to facilitate running it.
8
- #
9
-
10
- require "pathname"
11
- ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
12
- Pathname.new(__FILE__).realpath)
13
-
14
- require "rubygems"
15
- require "bundler/setup"
16
-
17
- load Gem.bin_path("appraisal", "appraisal")
@@ -1,10 +0,0 @@
1
- # This file was generated by Appraisal
2
-
3
- source "https://rubygems.org"
4
-
5
- gem "activerecord", "~> 4.2.0"
6
- gem "railties", "~> 4.2.0"
7
- gem "pg", "~> 0.15.0"
8
- gem "bigdecimal", "1.3.5"
9
-
10
- gemspec path: "../"
@@ -1,8 +0,0 @@
1
- # This file was generated by Appraisal
2
-
3
- source "https://rubygems.org"
4
-
5
- gem "activerecord", "~> 5.0.0"
6
- gem "railties", "~> 5.0.0"
7
-
8
- gemspec path: "../"
@@ -1,8 +0,0 @@
1
- # This file was generated by Appraisal
2
-
3
- source "https://rubygems.org"
4
-
5
- gem "activerecord", "~> 5.1.0"
6
- gem "railties", "~> 5.1.0"
7
-
8
- gemspec path: "../"
@@ -1,8 +0,0 @@
1
- # This file was generated by Appraisal
2
-
3
- source "https://rubygems.org"
4
-
5
- gem "activerecord", "~> 5.2.0"
6
- gem "railties", "~> 5.2.0"
7
-
8
- gemspec path: "../"
@@ -1,8 +0,0 @@
1
- # This file was generated by Appraisal
2
-
3
- source "https://rubygems.org"
4
-
5
- gem "activerecord", "~> 6.0.0"
6
- gem "railties", "~> 6.0.0"
7
-
8
- gemspec path: "../"
@@ -1,8 +0,0 @@
1
- # This file was generated by Appraisal
2
-
3
- source "https://rubygems.org"
4
-
5
- gem "activerecord", "~> 6.1.0"
6
- gem "railties", "~> 6.1.0"
7
-
8
- gemspec path: "../"
@@ -1,8 +0,0 @@
1
- # This file was generated by Appraisal
2
-
3
- source "https://rubygems.org"
4
-
5
- gem "rails", github: "rails/rails", branch: "main"
6
- gem "arel", github: "rails/arel"
7
-
8
- gemspec path: "../"
@@ -1,43 +0,0 @@
1
- module Fx
2
- module CommandRecorder
3
- # @api private
4
- class Arguments
5
- def initialize(args)
6
- @args = args.freeze
7
- end
8
-
9
- def function
10
- @args[0]
11
- end
12
-
13
- def version
14
- options[:version]
15
- end
16
-
17
- def revert_to_version
18
- options[:revert_to_version]
19
- end
20
-
21
- def invert_version
22
- Arguments.new([function, options_for_revert])
23
- end
24
-
25
- def to_a
26
- @args.to_a
27
- end
28
-
29
- private
30
-
31
- def options
32
- @options ||= @args[1] || {}
33
- end
34
-
35
- def options_for_revert
36
- options.clone.tap do |revert_options|
37
- revert_options[:version] = revert_to_version
38
- revert_options.delete(:revert_to_version)
39
- end
40
- end
41
- end
42
- end
43
- end
@@ -1,30 +0,0 @@
1
- module Fx
2
- module CommandRecorder
3
- # @api private
4
- module Function
5
- def create_function(*args)
6
- record(:create_function, args)
7
- end
8
-
9
- def drop_function(*args)
10
- record(:drop_function, args)
11
- end
12
-
13
- def update_function(*args)
14
- record(:update_function, args)
15
- end
16
-
17
- def invert_create_function(args)
18
- [:drop_function, args]
19
- end
20
-
21
- def invert_drop_function(args)
22
- perform_inversion(:create_function, args)
23
- end
24
-
25
- def invert_update_function(args)
26
- perform_inversion(:update_function, args)
27
- end
28
- end
29
- end
30
- end
@@ -1,30 +0,0 @@
1
- module Fx
2
- module CommandRecorder
3
- # @api private
4
- module Trigger
5
- def create_trigger(*args)
6
- record(:create_trigger, args)
7
- end
8
-
9
- def drop_trigger(*args)
10
- record(:drop_trigger, args)
11
- end
12
-
13
- def update_trigger(*args)
14
- record(:update_trigger, args)
15
- end
16
-
17
- def invert_create_trigger(args)
18
- [:drop_trigger, args]
19
- end
20
-
21
- def invert_drop_trigger(args)
22
- perform_inversion(:create_trigger, args)
23
- end
24
-
25
- def invert_update_trigger(args)
26
- perform_inversion(:update_trigger, args)
27
- end
28
- end
29
- end
30
- end
@@ -1,38 +0,0 @@
1
- require "rails"
2
-
3
- module Fx
4
- module SchemaDumper
5
- # @api private
6
- module Function
7
- def tables(stream)
8
- if Fx.configuration.dump_functions_at_beginning_of_schema
9
- functions(stream)
10
- empty_line(stream)
11
- end
12
-
13
- super
14
-
15
- unless Fx.configuration.dump_functions_at_beginning_of_schema
16
- functions(stream)
17
- empty_line(stream)
18
- end
19
- end
20
-
21
- def empty_line(stream)
22
- stream.puts if dumpable_functions_in_database.any?
23
- end
24
-
25
- def functions(stream)
26
- dumpable_functions_in_database.each do |function|
27
- stream.puts(function.to_schema)
28
- end
29
- end
30
-
31
- private
32
-
33
- def dumpable_functions_in_database
34
- @_dumpable_functions_in_database ||= Fx.database.functions
35
- end
36
- end
37
- end
38
- end
@@ -1,29 +0,0 @@
1
- require "rails"
2
-
3
- module Fx
4
- module SchemaDumper
5
- # @api private
6
- module Trigger
7
- def tables(stream)
8
- super
9
- triggers(stream)
10
- end
11
-
12
- def triggers(stream)
13
- if dumpable_triggers_in_database.any?
14
- stream.puts
15
- end
16
-
17
- dumpable_triggers_in_database.each do |trigger|
18
- stream.puts(trigger.to_schema)
19
- end
20
- end
21
-
22
- private
23
-
24
- def dumpable_triggers_in_database
25
- @_dumpable_triggers_in_database ||= Fx.database.triggers
26
- end
27
- end
28
- end
29
- end
@@ -1,115 +0,0 @@
1
- require "rails"
2
-
3
- module Fx
4
- module Statements
5
- # Methods that are made available in migrations for managing Fx functions.
6
- module Function
7
- # Create a new database function.
8
- #
9
- # @param name [String, Symbol] The name of the database function.
10
- # @param version [Fixnum] The version number of the function, used to
11
- # find the definition file in `db/functions`. This defaults to `1` if
12
- # not provided.
13
- # @param sql_definition [String] The SQL query for the function schema.
14
- # If both `sql_definition` and `version` are provided,
15
- # `sql_definition` takes prescedence.
16
- # @return The database response from executing the create statement.
17
- #
18
- # @example Create from `db/functions/uppercase_users_name_v02.sql`
19
- # create_function(:uppercase_users_name, version: 2)
20
- #
21
- # @example Create from provided SQL string
22
- # create_function(:uppercase_users_name, sql_definition: <<-SQL)
23
- # CREATE OR REPLACE FUNCTION uppercase_users_name()
24
- # RETURNS trigger AS $$
25
- # BEGIN
26
- # NEW.upper_name = UPPER(NEW.name);
27
- # RETURN NEW;
28
- # END;
29
- # $$ LANGUAGE plpgsql;
30
- # SQL
31
- #
32
- def create_function(name, options = {})
33
- version = options.fetch(:version, 1)
34
- sql_definition = options[:sql_definition]
35
-
36
- if version.nil? && sql_definition.nil?
37
- raise(
38
- ArgumentError,
39
- "version or sql_definition must be specified",
40
- )
41
- end
42
- sql_definition = sql_definition.strip_heredoc if sql_definition
43
- sql_definition ||= Fx::Definition.new(name: name, version: version).to_sql
44
-
45
- Fx.database.create_function(sql_definition)
46
- end
47
-
48
- # Drop a database function by name.
49
- #
50
- # @param name [String, Symbol] The name of the database function.
51
- # @param revert_to_version [Fixnum] Used to reverse the `drop_function`
52
- # command on `rake db:rollback`. The provided version will be passed as
53
- # the `version` argument to {#create_function}.
54
- # @return The database response from executing the drop statement.
55
- #
56
- # @example Drop a function, rolling back to version 2 on rollback
57
- # drop_function(:uppercase_users_name, revert_to_version: 2)
58
- #
59
- def drop_function(name, options = {})
60
- revert_to_version = options[:revert_to_version]
61
- Fx.database.drop_function(name)
62
- end
63
-
64
- # Update a database function.
65
- #
66
- # @param name [String, Symbol] The name of the database function.
67
- # @param version [Fixnum] The version number of the function, used to
68
- # find the definition file in `db/functions`. This defaults to `1` if
69
- # not provided.
70
- # @param sql_definition [String] The SQL query for the function schema.
71
- # If both `sql_definition` and `version` are provided,
72
- # `sql_definition` takes prescedence.
73
- # @return The database response from executing the create statement.
74
- #
75
- # @example Update function to a given version
76
- # update_function(
77
- # :uppercase_users_name,
78
- # version: 3,
79
- # revert_to_version: 2,
80
- # )
81
- #
82
- # @example Update function from provided SQL string
83
- # update_function(:uppercase_users_name, sql_definition: <<-SQL)
84
- # CREATE OR REPLACE FUNCTION uppercase_users_name()
85
- # RETURNS trigger AS $$
86
- # BEGIN
87
- # NEW.upper_name = UPPER(NEW.name);
88
- # RETURN NEW;
89
- # END;
90
- # $$ LANGUAGE plpgsql;
91
- # SQL
92
- #
93
- def update_function(name, options = {})
94
- version = options[:version]
95
- sql_definition = options[:sql_definition]
96
- revert_to_version = options[:revert_to_version]
97
-
98
- if version.nil? && sql_definition.nil?
99
- raise(
100
- ArgumentError,
101
- "version or sql_definition must be specified",
102
- )
103
- end
104
-
105
- sql_definition = sql_definition.strip_heredoc if sql_definition
106
- sql_definition ||= Fx::Definition.new(
107
- name: name,
108
- version: version,
109
- ).to_sql
110
-
111
- Fx.database.update_function(name, sql_definition)
112
- end
113
- end
114
- end
115
- end
@@ -1,146 +0,0 @@
1
- module Fx
2
- module Statements
3
- # Methods that are made available in migrations for managing Fx triggers.
4
- module Trigger
5
- # @api private
6
- DEFINITION_TYPE = "trigger".freeze
7
-
8
- # Create a new database trigger.
9
- #
10
- # @param name [String, Symbol] The name of the database trigger.
11
- # @param version [Fixnum] The version number of the trigger, used to
12
- # find the definition file in `db/triggers`. This defaults to `1` if
13
- # not provided.
14
- # @param sql_definition [String] The SQL query for the function. An error
15
- # will be raised if `sql_definition` and `version` are both set,
16
- # as they are mutually exclusive.
17
- # @return The database response from executing the create statement.
18
- #
19
- # @example Create trigger from `db/triggers/uppercase_users_name_v01.sql`
20
- # create_trigger(:uppercase_users_name, version: 1)
21
- #
22
- # @example Create trigger from provided SQL string
23
- # create_trigger(:uppercase_users_name, sql_definition: <<-SQL)
24
- # CREATE TRIGGER uppercase_users_name
25
- # BEFORE INSERT ON users
26
- # FOR EACH ROW
27
- # EXECUTE PROCEDURE uppercase_users_name();
28
- # SQL
29
- #
30
- def create_trigger(name, options = {})
31
- version = options[:version]
32
- _on = options[:on]
33
- sql_definition = options[:sql_definition]
34
-
35
- if version.present? && sql_definition.present?
36
- raise(
37
- ArgumentError,
38
- "sql_definition and version cannot both be set",
39
- )
40
- end
41
-
42
- if version.nil?
43
- version = 1
44
- end
45
-
46
- sql_definition = sql_definition.strip_heredoc if sql_definition
47
- sql_definition ||= Fx::Definition.new(
48
- name: name,
49
- version: version,
50
- type: DEFINITION_TYPE,
51
- ).to_sql
52
-
53
- Fx.database.create_trigger(sql_definition)
54
- end
55
-
56
- # Drop a database trigger by name.
57
- #
58
- # @param name [String, Symbol] The name of the database trigger.
59
- # @param on [String, Symbol] The name of the table the database trigger
60
- # is associated with.
61
- # @param revert_to_version [Fixnum] Used to reverse the `drop_trigger`
62
- # command on `rake db:rollback`. The provided version will be passed as
63
- # the `version` argument to {#create_trigger}.
64
- # @return The database response from executing the drop statement.
65
- #
66
- # @example Drop a trigger, rolling back to version 3 on rollback
67
- # drop_trigger(:log_inserts, on: :users, revert_to_version: 3)
68
- #
69
- def drop_trigger(name, options = {})
70
- on = options.fetch(:on)
71
- revert_to_version = options[:revert_to_version]
72
- Fx.database.drop_trigger(name, on: on)
73
- end
74
-
75
- # Update a database trigger to a new version.
76
- #
77
- # The existing trigger is dropped and recreated using the supplied `on`
78
- # and `version` parameter.
79
- #
80
- # @param name [String, Symbol] The name of the database trigger.
81
- # @param version [Fixnum] The version number of the trigger.
82
- # @param on [String, Symbol] The name of the table the database trigger
83
- # is associated with.
84
- # @param sql_definition [String] The SQL query for the function. An error
85
- # will be raised if `sql_definition` and `version` are both set,
86
- # as they are mutually exclusive.
87
- # @param revert_to_version [Fixnum] The version number to rollback to on
88
- # `rake db rollback`
89
- # @return The database response from executing the create statement.
90
- #
91
- # @example Update trigger to a given version
92
- # update_trigger(
93
- # :log_inserts,
94
- # on: :users,
95
- # version: 3,
96
- # revert_to_version: 2,
97
- # )
98
- #
99
- # @example Update trigger from provided SQL string
100
- # update_trigger(:uppercase_users_name, sql_definition: <<-SQL)
101
- # CREATE TRIGGER uppercase_users_name
102
- # BEFORE INSERT ON users
103
- # FOR EACH ROW
104
- # EXECUTE PROCEDURE uppercase_users_name();
105
- # SQL
106
- #
107
- def update_trigger(name, options = {})
108
- version = options[:version]
109
- on = options[:on]
110
- sql_definition = options[:sql_definition]
111
- revert_to_version = options[:revert_to_version]
112
-
113
- if version.nil? && sql_definition.nil?
114
- raise(
115
- ArgumentError,
116
- "version or sql_definition must be specified",
117
- )
118
- end
119
-
120
- if version.present? && sql_definition.present?
121
- raise(
122
- ArgumentError,
123
- "sql_definition and version cannot both be set",
124
- )
125
- end
126
-
127
- if on.nil?
128
- raise ArgumentError, "on is required"
129
- end
130
-
131
- sql_definition = sql_definition.strip_heredoc if sql_definition
132
- sql_definition ||= Fx::Definition.new(
133
- name: name,
134
- version: version,
135
- type: DEFINITION_TYPE,
136
- ).to_sql
137
-
138
- Fx.database.update_trigger(
139
- name,
140
- on: on,
141
- sql_definition: sql_definition,
142
- )
143
- end
144
- end
145
- end
146
- end
@@ -1,41 +0,0 @@
1
- require "spec_helper"
2
-
3
- module Fx::CommandRecorder
4
- describe Arguments do
5
- describe "#function" do
6
- it "returns the function name" do
7
- raw_args = [:spaceships, { foo: :bar }]
8
- args = Arguments.new(raw_args)
9
-
10
- expect(args.function).to eq :spaceships
11
- end
12
- end
13
-
14
- describe "#revert_to_version" do
15
- it "is the revert_to_version from the keyword arguments" do
16
- raw_args = [:spaceships, { revert_to_version: 42 }]
17
- args = Arguments.new(raw_args)
18
-
19
- expect(args.revert_to_version).to eq 42
20
- end
21
-
22
- it "is nil if the revert_to_version was not supplied" do
23
- raw_args = [:spaceships, { foo: :bar }]
24
- args = Arguments.new(raw_args)
25
-
26
- expect(args.revert_to_version).to be nil
27
- end
28
- end
29
-
30
- describe "#invert_version" do
31
- it "returns object with version set to revert_to_version" do
32
- raw_args = [:meatballs, { version: 42, revert_to_version: 15 }]
33
-
34
- inverted_args = Arguments.new(raw_args).invert_version
35
-
36
- expect(inverted_args.version).to eq 15
37
- expect(inverted_args.revert_to_version).to be nil
38
- end
39
- end
40
- end
41
- end
@@ -1,80 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe Fx::SchemaDumper::Function, :db do
4
- it "dumps a create_function for a function in the database" do
5
- sql_definition = <<-EOS
6
- CREATE OR REPLACE FUNCTION my_function()
7
- RETURNS text AS $$
8
- BEGIN
9
- RETURN 'test';
10
- END;
11
- $$ LANGUAGE plpgsql;
12
- EOS
13
- connection.create_function :my_function, sql_definition: sql_definition
14
- connection.create_table :my_table
15
- stream = StringIO.new
16
- output = stream.string
17
-
18
- ActiveRecord::SchemaDumper.dump(connection, stream)
19
-
20
- expect(output).to(
21
- match(/table "my_table".*function :my_function.*RETURN 'test';/m),
22
- )
23
- end
24
-
25
- it "dumps a create_function for a function in the database" do
26
- begin
27
- Fx.configuration.dump_functions_at_beginning_of_schema = true
28
- sql_definition = <<-EOS
29
- CREATE OR REPLACE FUNCTION my_function()
30
- RETURNS text AS $$
31
- BEGIN
32
- RETURN 'test';
33
- END;
34
- $$ LANGUAGE plpgsql;
35
- EOS
36
- connection.create_function :my_function, sql_definition: sql_definition
37
- connection.create_table :my_table
38
- stream = StringIO.new
39
- output = stream.string
40
-
41
- ActiveRecord::SchemaDumper.dump(connection, stream)
42
-
43
- expect(output).to(
44
- match(/function :my_function.*RETURN 'test';.*table "my_table"/m),
45
- )
46
- ensure
47
- Fx.configuration.dump_functions_at_beginning_of_schema = false
48
- end
49
- end
50
-
51
- it "does not dump a create_function for aggregates in the database" do
52
- sql_definition = <<-EOS
53
- CREATE OR REPLACE FUNCTION test(text, text)
54
- RETURNS text AS $$
55
- BEGIN
56
- RETURN 'test';
57
- END;
58
- $$ LANGUAGE plpgsql;
59
- EOS
60
-
61
- aggregate_sql_definition = <<-EOS
62
- CREATE AGGREGATE aggregate_test(text)
63
- (
64
- sfunc = test,
65
- stype = text
66
- );
67
- EOS
68
-
69
- connection.create_function :test, sql_definition: sql_definition
70
- connection.execute aggregate_sql_definition
71
- stream = StringIO.new
72
-
73
- ActiveRecord::SchemaDumper.dump(connection, stream)
74
-
75
- output = stream.string
76
- expect(output).to include "create_function :test, sql_definition: <<-'SQL'"
77
- expect(output).to include "RETURN 'test';"
78
- expect(output).not_to include "aggregate_test"
79
- end
80
- end