arfi 0.3.1 → 0.4.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
  SHA256:
3
- metadata.gz: 072f0f4098aec38e2d25e6d3350502930f9be8b0c5b6396545239662c2651570
4
- data.tar.gz: 8be6c8f4e5f8a38b381289c8fe127d07c9a23313aa87b24022ddb5457eb9429f
3
+ metadata.gz: 0ed9672d7951cb9d1aeb28fe390ee92fff1664b4bdfa56a52196c0595df272d2
4
+ data.tar.gz: dae5536d6035f67546136f12ffe1fc52d602bdffc8dd5eeba120fec2313c4cd7
5
5
  SHA512:
6
- metadata.gz: ab81783efb65e023578c70e578248ea267ba3bac815726d9911ced210c139424da8e777160f75c903cdbc45e718d137772109581fe120ef886f73d31cd524f01
7
- data.tar.gz: ce0c50d27cce3dedd894106ef50111d13b0424e00e690c2acd731c213863fcbd988d8e2a3fa7265962c58b874a320b3d04c6ab36b815524550c39472eca4b26e
6
+ metadata.gz: d9f5254996cf000979cc431862f6aedccb6599c277dccc5fd03e1052163a1c0aa7ee42a12e89b1925c867ba458fb93087e972cfe8a330a9b3547bedd70227d6d
7
+ data.tar.gz: 8cf8d948c812075b068479e25737a0d43999831f2dea87a018d6d62fe8c1049d96bb27076f88173ed91d4bd4ae1939669d768402ee9e8ce8e5c215c14014d909
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: arfi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - unurgunite
@@ -107,46 +107,30 @@ dependencies:
107
107
  - - ">="
108
108
  - !ruby/object:Gem::Version
109
109
  version: '1.21'
110
+ - !ruby/object:Gem::Dependency
111
+ name: steep
112
+ requirement: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - "~>"
115
+ - !ruby/object:Gem::Version
116
+ version: '1.10'
117
+ type: :development
118
+ prerelease: false
119
+ version_requirements: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - "~>"
122
+ - !ruby/object:Gem::Version
123
+ version: '1.10'
110
124
  description: 'ARFI — ActiveRecord Functional Indexes. Bring the ability to create
111
125
  and maintain functional indexes without transition to structure.sql.
112
126
 
113
127
  '
114
128
  email:
115
129
  - senpaiguru1488@gmail.com
116
- executables:
117
- - arfi
130
+ executables: []
118
131
  extensions: []
119
132
  extra_rdoc_files: []
120
- files:
121
- - ".rspec"
122
- - ".rubocop.yml"
123
- - ".ruby-version"
124
- - CHANGELOG.md
125
- - CODE_OF_CONDUCT.md
126
- - LICENSE.txt
127
- - README.md
128
- - Rakefile
129
- - exe/arfi
130
- - lib/arfi.rb
131
- - lib/arfi/Rakefile
132
- - lib/arfi/cli.rb
133
- - lib/arfi/commands/f_idx.rb
134
- - lib/arfi/commands/project.rb
135
- - lib/arfi/errors.rb
136
- - lib/arfi/extensions/active_record/base.rb
137
- - lib/arfi/extensions/active_record/connection_adapters/postgresql/database_statements.rb
138
- - lib/arfi/extensions/extensions.rb
139
- - lib/arfi/railtie.rb
140
- - lib/arfi/sql_function_loader.rb
141
- - lib/arfi/tasks/db.rake
142
- - lib/arfi/version.rb
143
- - sig/arfi.rbs
144
- - sig/lib/arfi/commands/f_idx.rbs
145
- - sig/lib/arfi/commands/project.rbs
146
- - sig/lib/arfi/errors.rbs
147
- - sig/lib/arfi/extensions/active_record/base.rbs
148
- - sig/lib/arfi/sql_function_loader.rbs
149
- - sig/lib/arfi/version.rbs
133
+ files: []
150
134
  homepage: https://github.com/unurgunite/arfi
151
135
  licenses:
152
136
  - MIT
@@ -170,7 +154,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
170
154
  - !ruby/object:Gem::Version
171
155
  version: '0'
172
156
  requirements: []
173
- rubygems_version: 3.6.8
157
+ rubygems_version: 3.6.9
174
158
  specification_version: 4
175
159
  summary: ActiveRecord Functional Indexes.
176
160
  test_files: []
data/.rspec DELETED
@@ -1,3 +0,0 @@
1
- --format documentation
2
- --color
3
- --require spec_helper
data/.rubocop.yml DELETED
@@ -1,9 +0,0 @@
1
- AllCops:
2
- TargetRubyVersion: 3.1
3
- NewCops: enable
4
- Exclude:
5
- - lib/arfi/extensions/active_record/connection_adapters/postgresql/database_statements.rb
6
- - 'vendor/**/*'
7
-
8
- Gemspec/DevelopmentDependencies:
9
- EnforcedStyle: gemspec
data/.ruby-version DELETED
@@ -1 +0,0 @@
1
- 3.4.2
data/CHANGELOG.md DELETED
@@ -1,5 +0,0 @@
1
- ## [Unreleased]
2
-
3
- ## [0.1.0] - 2025-04-22
4
-
5
- - Initial release
data/CODE_OF_CONDUCT.md DELETED
@@ -1,132 +0,0 @@
1
- # Contributor Covenant Code of Conduct
2
-
3
- ## Our Pledge
4
-
5
- We as members, contributors, and leaders pledge to make participation in our
6
- community a harassment-free experience for everyone, regardless of age, body
7
- size, visible or invisible disability, ethnicity, sex characteristics, gender
8
- identity and expression, level of experience, education, socio-economic status,
9
- nationality, personal appearance, race, caste, color, religion, or sexual
10
- identity and orientation.
11
-
12
- We pledge to act and interact in ways that contribute to an open, welcoming,
13
- diverse, inclusive, and healthy community.
14
-
15
- ## Our Standards
16
-
17
- Examples of behavior that contributes to a positive environment for our
18
- community include:
19
-
20
- * Demonstrating empathy and kindness toward other people
21
- * Being respectful of differing opinions, viewpoints, and experiences
22
- * Giving and gracefully accepting constructive feedback
23
- * Accepting responsibility and apologizing to those affected by our mistakes,
24
- and learning from the experience
25
- * Focusing on what is best not just for us as individuals, but for the overall
26
- community
27
-
28
- Examples of unacceptable behavior include:
29
-
30
- * The use of sexualized language or imagery, and sexual attention or advances of
31
- any kind
32
- * Trolling, insulting or derogatory comments, and personal or political attacks
33
- * Public or private harassment
34
- * Publishing others' private information, such as a physical or email address,
35
- without their explicit permission
36
- * Other conduct which could reasonably be considered inappropriate in a
37
- professional setting
38
-
39
- ## Enforcement Responsibilities
40
-
41
- Community leaders are responsible for clarifying and enforcing our standards of
42
- acceptable behavior and will take appropriate and fair corrective action in
43
- response to any behavior that they deem inappropriate, threatening, offensive,
44
- or harmful.
45
-
46
- Community leaders have the right and responsibility to remove, edit, or reject
47
- comments, commits, code, wiki edits, issues, and other contributions that are
48
- not aligned to this Code of Conduct, and will communicate reasons for moderation
49
- decisions when appropriate.
50
-
51
- ## Scope
52
-
53
- This Code of Conduct applies within all community spaces, and also applies when
54
- an individual is officially representing the community in public spaces.
55
- Examples of representing our community include using an official email address,
56
- posting via an official social media account, or acting as an appointed
57
- representative at an online or offline event.
58
-
59
- ## Enforcement
60
-
61
- Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
- reported to the community leaders responsible for enforcement at
63
- [INSERT CONTACT METHOD].
64
- All complaints will be reviewed and investigated promptly and fairly.
65
-
66
- All community leaders are obligated to respect the privacy and security of the
67
- reporter of any incident.
68
-
69
- ## Enforcement Guidelines
70
-
71
- Community leaders will follow these Community Impact Guidelines in determining
72
- the consequences for any action they deem in violation of this Code of Conduct:
73
-
74
- ### 1. Correction
75
-
76
- **Community Impact**: Use of inappropriate language or other behavior deemed
77
- unprofessional or unwelcome in the community.
78
-
79
- **Consequence**: A private, written warning from community leaders, providing
80
- clarity around the nature of the violation and an explanation of why the
81
- behavior was inappropriate. A public apology may be requested.
82
-
83
- ### 2. Warning
84
-
85
- **Community Impact**: A violation through a single incident or series of
86
- actions.
87
-
88
- **Consequence**: A warning with consequences for continued behavior. No
89
- interaction with the people involved, including unsolicited interaction with
90
- those enforcing the Code of Conduct, for a specified period of time. This
91
- includes avoiding interactions in community spaces as well as external channels
92
- like social media. Violating these terms may lead to a temporary or permanent
93
- ban.
94
-
95
- ### 3. Temporary Ban
96
-
97
- **Community Impact**: A serious violation of community standards, including
98
- sustained inappropriate behavior.
99
-
100
- **Consequence**: A temporary ban from any sort of interaction or public
101
- communication with the community for a specified period of time. No public or
102
- private interaction with the people involved, including unsolicited interaction
103
- with those enforcing the Code of Conduct, is allowed during this period.
104
- Violating these terms may lead to a permanent ban.
105
-
106
- ### 4. Permanent Ban
107
-
108
- **Community Impact**: Demonstrating a pattern of violation of community
109
- standards, including sustained inappropriate behavior, harassment of an
110
- individual, or aggression toward or disparagement of classes of individuals.
111
-
112
- **Consequence**: A permanent ban from any sort of public interaction within the
113
- community.
114
-
115
- ## Attribution
116
-
117
- This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118
- version 2.1, available at
119
- [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
120
-
121
- Community Impact Guidelines were inspired by
122
- [Mozilla's code of conduct enforcement ladder][Mozilla CoC].
123
-
124
- For answers to common questions about this code of conduct, see the FAQ at
125
- [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
126
- [https://www.contributor-covenant.org/translations][translations].
127
-
128
- [homepage]: https://www.contributor-covenant.org
129
- [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
130
- [Mozilla CoC]: https://github.com/mozilla/diversity
131
- [FAQ]: https://www.contributor-covenant.org/faq
132
- [translations]: https://www.contributor-covenant.org/translations
data/LICENSE.txt DELETED
@@ -1,21 +0,0 @@
1
- The MIT License (MIT)
2
-
3
- Copyright (c) 2025 daniil.lyubimcev
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in
13
- all copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
- THE SOFTWARE.
data/README.md DELETED
@@ -1,163 +0,0 @@
1
- # ARFI
2
-
3
- ![Alt](https://repobeats.axiom.co/api/embed/324b4f481b219890ef5a26e3c6fb73fff8929c93.svg "Repobeats analytics image")
4
-
5
- ---
6
-
7
- > [!WARNING]
8
- > This project only supports PostgreSQL databases, however, MySQL usage will also be available in upcoming updates.
9
- > Since SQLite3 does not support functional indexes with custom functions, support for this database will not be
10
- > available for a while, however, you can help the project by contributing to the open source.
11
-
12
- > [!NOTE]
13
- > This project requires Ruby 3.1.0+, it has not yet been tested on other versions, however, at the time of writing,
14
- > backward compatibility was maintained wherever possible.
15
-
16
- ---
17
-
18
- ARFI – *ActiveRecord Functional Indexes*
19
-
20
- ARFI gem brings you the ability to create and maintain functional indexes for your ActiveRecord models without
21
- transition to `structure.sql` (SQL-based schema).
22
-
23
- * [ARFI](#arfi)
24
- * [Installation](#installation)
25
- * [Usage](#usage)
26
- * [CLI](#cli)
27
- * [Project creation](#project-creation)
28
- * [Index creation](#index-creation)
29
- * [Index destroy](#index-destroy)
30
- * [Additional help](#additional-help)
31
- * [Demo](#demo)
32
- * [Library features](#library-features)
33
- * [Roadmap](#roadmap)
34
- * [Development](#development)
35
- * [Build from source](#build-from-source)
36
- * [Requirements](#requirements)
37
- * [Contributing](#contributing)
38
- * [Miscellaneous](#miscellaneous)
39
- * [License](#license)
40
- * [Code of Conduct](#code-of-conduct)
41
-
42
- ## Installation
43
-
44
- Install the gem and add to the application's Gemfile by executing:
45
-
46
- ```bash
47
- bundle add arfi
48
- ```
49
-
50
- ## Usage
51
-
52
- ### CLI
53
-
54
- ARFI uses Thor as a command line interface (CLI) instead of Rake, so it has a specific DSL.
55
-
56
- ### Project creation
57
-
58
- Firstly, run `bundle exec arfi project create` to create a new project. This command will create `db/functions`
59
- directory. ARFI uses `db/functions` directory to store your functional indexes.
60
-
61
- ### Index creation
62
-
63
- Run `bundle exec arfi f_idx create function_name` to create a new functional index. New index will be created in
64
- `db/functions` directory under `function_name_v01.sql` name. Edit you index and run `bundle exec rails db:migrate`. You
65
- can also use custom template for index. Type `bundle exec arfi f_idx help create` for additional info.
66
-
67
- ### Index destroy
68
-
69
- If you want to destroy your index, run `bundle exec arfi f_idx destroy function_name [revision (1 by default)]`
70
-
71
- ### Additional help
72
-
73
- Run `bundle exec arfi` for additional help.
74
-
75
- ## Demo
76
-
77
- Demo available as separate project built with Rails 7.2 and PostgreSQL 14: https://github.com/unurgunite/poc_arfi_72.
78
- README is also available.
79
-
80
- ## Library features
81
-
82
- 1. ARFI supports all types of database initialization
83
-
84
- | Task | Completed |
85
- |----------------|--------------------|
86
- | db:migrate | :white_check_mark: |
87
- | db:setup | :white_check_mark: |
88
- | db:prepare | :white_check_mark: |
89
- | db:schema:load | :white_check_mark: |
90
-
91
- 2. Database support
92
-
93
- | DB adapter | Tested |
94
- |----------------|-------------------------------------------------------|
95
- | PostgreSQL | 9+ :white_check_mark: |
96
- | MySQL | In progress :arrows_counterclockwise: |
97
- | SQLite3 | In progress (not primarily) :arrows_counterclockwise: |
98
-
99
- 3. Rails support
100
-
101
- | Rails version | Tested |
102
- |---------------|---------------------------------------|
103
- | 8 | In progress :arrows_counterclockwise: |
104
- | 7 | :white_check_mark: |
105
- | 6 | In progress :arrows_counterclockwise: |
106
-
107
- ## Roadmap
108
-
109
- 1. ~~Custom template for SQL functions using `--template` flag;~~
110
- 2. Multidb support (Rails 6+ feature);
111
- 3. Add support for 4+ ActiveRecord;
112
- 4. Add RSpec tests;
113
- 5. Add separate YARD doc page;
114
- 6. Update CI/CD;
115
- 7. Add support for Ruby 2.6+.
116
-
117
- ## Development
118
-
119
- ### Build from source
120
-
121
- The manual installation includes installation via command line interface. it is practically no different from what
122
- happens during the automatic build of the project:
123
-
124
- ```shell
125
- git clone https://github.com/unurgunite/arfi.git
126
- cd arfi
127
- bundle install
128
- gem build arfi.gemspec
129
- gem install arfi-0.3.1.gem
130
- ```
131
-
132
- Also, you can run `bin/setup` to automatically install everything needed.
133
-
134
- ## Requirements
135
-
136
- ARFI is built on top of the following gems:
137
-
138
- | Dependencies | Description |
139
- |--------------|--------------------------------------------------------------------------------------------|
140
- | ActiveRecord | Used to patch `ActiveRecord::Base` module with new methods. |
141
- | Rails | Used for fetching project settings (database connection settings, Rails environment, etc.) |
142
- | Thor | For CLI development. |
143
- | Rubocop | For static code analysis. |
144
- | Rake | For patching built-in Rails Rake tasks. |
145
-
146
- ## Contributing
147
-
148
- Bug reports and pull requests are welcome on GitHub at https://github.com/unurgunite/arfi. This project is intended to
149
- be a safe, welcoming space for collaboration, and contributors are expected to adhere to
150
- the [code of conduct](https://github.com/[USERNAME]/Arfi/blob/master/CODE_OF_CONDUCT.md).
151
-
152
- ## Miscellaneous
153
-
154
- ARFI is highly inspired by https://github.com/teoljungberg/fx project.
155
-
156
- ## License
157
-
158
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
159
-
160
- ## Code of Conduct
161
-
162
- Everyone interacting in the ARFI project's codebases, issue trackers, chat rooms and mailing lists is expected to follow
163
- the [code of conduct](https://github.com/[USERNAME]/Arfi/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile DELETED
@@ -1,12 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'bundler/gem_tasks'
4
- require 'rspec/core/rake_task'
5
-
6
- RSpec::Core::RakeTask.new(:spec)
7
-
8
- require 'rubocop/rake_task'
9
-
10
- RuboCop::RakeTask.new
11
-
12
- task default: %i[spec rubocop]
data/exe/arfi DELETED
@@ -1,7 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # frozen_string_literal: true
3
-
4
- require 'thor'
5
- require_relative '../lib/arfi/cli'
6
-
7
- Arfi::CLI.start(ARGV)
data/lib/arfi/Rakefile DELETED
@@ -1,6 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'arfi'
4
-
5
- path = File.expand_path(__dir__)
6
- Dir.glob("#{path}/tasks/**/*.rake").each { |f| import f }
data/lib/arfi/cli.rb DELETED
@@ -1,16 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'thor'
4
- require_relative 'commands/project'
5
- require_relative 'commands/f_idx'
6
-
7
- module Arfi
8
- # Top level CLI class
9
- class CLI < Thor
10
- desc 'project [COMMAND]', 'Project specific commands.'
11
- subcommand 'project', Commands::Project
12
-
13
- desc 'f_idx [COMMAND]', 'Command to handle functional indexes.'
14
- subcommand 'f_idx', Commands::FIdx
15
- end
16
- end
@@ -1,184 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'thor'
4
- require 'rails'
5
- require File.expand_path('config/environment', Dir.pwd)
6
-
7
- module Arfi
8
- module Commands
9
- # +Arfi::Commands::FIdx+ module contains commands for manipulating functional index in Rails project.
10
- class FIdx < Thor
11
- desc 'create INDEX_NAME', 'Initialize the functional index'
12
- option :template, type: :string, banner: 'template_file'
13
- # +Arfi::Commands::FIdx#create+ -> void
14
- #
15
- # This command is used to create the functional index.
16
- #
17
- # @example
18
- # bundle exec arfi f_idx create some_function
19
- #
20
- # ARFI also supports the use of custom templates for SQL functions, but now there are some restrictions and rules
21
- # according to which it is necessary to describe the function. First, the function must be written in a
22
- # Ruby-compatible syntax: the file name is not so important, but the name for the function name must be
23
- # interpolated with the +index_name+ variable name, and the function itself must be placed in the HEREDOC
24
- # statement. Below is an example file.
25
- #
26
- # @example
27
- # # ./template/my_custom_template
28
- # <<~SQL
29
- # CREATE OR REPLACE FUNCTION #{index_name}() RETURNS TEXT[]
30
- # LANGUAGE SQL
31
- # IMMUTABLE AS
32
- # $$
33
- # -- Function body here
34
- # $$
35
- # SQL
36
- #
37
- # To use a custom template, add the --template flag.
38
- #
39
- # @example
40
- # bundle exec arfi f_idx create some_function --template ./template/my_custom_template
41
- #
42
- # @param index_name [String] Name of the index.
43
- # @return [void]
44
- # @raise [Arfi::Errors::InvalidSchemaFormat] if ActiveRecord.schema_format is not :ruby
45
- # @raise [Arfi::Errors::NoFunctionsDir] if there is no `db/functions` directory
46
- # @see Arfi::Commands::FIdx#validate_schema_format!
47
- def create(index_name)
48
- validate_schema_format!
49
- content = build_sql_function(index_name)
50
- create_index_file(index_name, content)
51
- end
52
-
53
- desc 'destroy INDEX_NAME [REVISION]', 'Delete the functional index'
54
- # +Arfi::Commands::FIdx#destroy+ -> void
55
- #
56
- # This command is used to delete the functional index.
57
- #
58
- # @example
59
- # bundle exec arfi f_idx destroy some_function [revision index (just an integer, 1 is by default)]
60
- # @param index_name [String] Name of the index.
61
- # @param revision [String] Revision of the index.
62
- # @return [void]
63
- # @raise [Arfi::Errors::InvalidSchemaFormat] if ActiveRecord.schema_format is not :ruby
64
- def destroy(index_name, revision = '01')
65
- validate_schema_format!
66
-
67
- revision = "0#{revision}" if revision.match?(/^\d$/)
68
- FileUtils.rm("#{functions_dir}/#{index_name}_v#{revision}.sql")
69
- puts "Deleted: #{functions_dir}/#{index_name}_v#{revision}.sql"
70
- end
71
-
72
- private
73
-
74
- # +Arfi::Commands::FIdx#functions_dir+ -> Pathname
75
- #
76
- # Helper method to get path to `db/functions` directory.
77
- #
78
- # @!visibility private
79
- # @private
80
- # @return [Pathname] Path to `db/functions` directory
81
- def functions_dir
82
- Rails.root.join('db/functions')
83
- end
84
-
85
- # +Arfi::Commands::FIdx#validate_schema_format!+ -> void
86
- #
87
- # Helper method to validate the schema format.
88
- #
89
- # @!visibility private
90
- # @private
91
- # @raise [Arfi::Errors::InvalidSchemaFormat] if ActiveRecord.schema_format is not :ruby.
92
- # @return [nil] if the schema format is valid.
93
- def validate_schema_format!
94
- raise Arfi::Errors::InvalidSchemaFormat unless ActiveRecord.schema_format == :ruby
95
- end
96
-
97
- # +Arfi::Commands::FIdx#build_sql_function+ -> String
98
- #
99
- # Helper method to build the SQL function.
100
- #
101
- # @!visibility private
102
- # @private
103
- # @param index_name [String] Name of the index.
104
- # @return [String] SQL function body.
105
- def build_sql_function(index_name)
106
- return build_from_file(index_name) if options[:template]
107
-
108
- <<~SQL
109
- CREATE OR REPLACE FUNCTION #{index_name}() RETURNS TEXT[]
110
- LANGUAGE SQL
111
- IMMUTABLE AS
112
- $$
113
- -- Function body here
114
- $$
115
- SQL
116
- end
117
-
118
- # +Arfi::Commands::FIdx#build_from_file+ -> String
119
- #
120
- # Helper method to build the SQL function. Used with flag `--template`.
121
- #
122
- # @!visibility private
123
- # @private
124
- # @param index_name [String] Name of the index.
125
- # @return [String] SQL function body.
126
- # @see Arfi::Commands::FIdx#create
127
- # @see Arfi::Commands::FIdx#build_sql_function
128
- def build_from_file(index_name)
129
- RubyVM::InstructionSequence.compile("index_name = '#{index_name}'; #{File.read(options[:template])}").eval
130
- end
131
-
132
- # +Arfi::Commands::FIdx#create_index_file+ -> void
133
- #
134
- # Helper method to create the index file.
135
- #
136
- # @!visibility private
137
- # @private
138
- # @param index_name [String] Name of the index.
139
- # @param content [String] SQL function body.
140
- # @return [void]
141
- def create_index_file(index_name, content)
142
- existing_files = Dir.glob("#{functions_dir}/#{index_name}*.sql")
143
-
144
- return write_file(index_name, content, 1) if existing_files.empty?
145
-
146
- latest_version = extract_latest_version(existing_files)
147
- write_file(index_name, content, latest_version.next)
148
- end
149
-
150
- # +Arfi::Commands::FIdx#extract_latest_version+ -> Integer
151
- #
152
- # Helper method to extract the latest version of the index.
153
- #
154
- # @!visibility private
155
- # @private
156
- # @param files [Array<String>] List of files.
157
- # @return [String] Latest version of the index.
158
- def extract_latest_version(files)
159
- version_numbers = files.map do |file|
160
- File.basename(file)[/\w+_v(\d+)\.sql/, 1]
161
- end.compact
162
-
163
- version_numbers.max
164
- end
165
-
166
- # +Arfi::Commands::FIdx#write_file+ -> void
167
- #
168
- # Helper method to write the index file.
169
- #
170
- # @!visibility private
171
- # @private
172
- # @param index_name [String] Name of the index.
173
- # @param content [String] SQL function body.
174
- # @param version [Integer] Version of the index.
175
- # @return [void]
176
- def write_file(index_name, content, version)
177
- version_str = format('%02d', version)
178
- path = "#{functions_dir}/#{index_name}_v#{version_str}.sql"
179
- File.write(path, content)
180
- puts "Created: #{path}"
181
- end
182
- end
183
- end
184
- end
@@ -1,42 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'thor'
4
- require 'fileutils'
5
- require 'rails'
6
-
7
- module Arfi
8
- module Commands
9
- # +Arfi::Commands::Project+ class is used to create `db/functions` directory.
10
- class Project < Thor
11
- desc 'create', 'Initialize project by creating db/functions directory'
12
- # +Arfi::Commands::Project#create+ -> void
13
- #
14
- # This command is used to create `db/functions` directory.
15
- #
16
- # @example
17
- # bundle exec arfi project create
18
- # @return [void]
19
- # @raise [Arfi::Errors::InvalidSchemaFormat] if ActiveRecord.schema_format is not :ruby.
20
- def create
21
- raise Arfi::Errors::InvalidSchemaFormat unless ActiveRecord.schema_format == :ruby
22
- return puts "Directory #{functions_dir} already exists" if Dir.exist?(functions_dir)
23
-
24
- FileUtils.mkdir_p(functions_dir)
25
- puts "Created: #{functions_dir}"
26
- end
27
-
28
- private
29
-
30
- # +Arfi::Commands::Project#functions_dir+ -> Pathname
31
- #
32
- # Helper method to get path to `db/functions` directory.
33
- #
34
- # @!visibility private
35
- # @private
36
- # @return [Pathname] Path to `db/functions` directory
37
- def functions_dir
38
- Rails.root.join('db/functions')
39
- end
40
- end
41
- end
42
- end
data/lib/arfi/errors.rb DELETED
@@ -1,30 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Arfi
4
- module Errors
5
- # This error is raised when there is no `db/functions` directory
6
- class NoFunctionsDir < StandardError
7
- def initialize(message =
8
- 'There is no such directory: db/functions. Did you run `bundle exec arfi project:create`?')
9
- @message = message
10
- super
11
- end
12
- end
13
-
14
- # This error is raised when Rails project schema format is not +schema.rb+
15
- class InvalidSchemaFormat < StandardError
16
- def initialize(message = 'Invalid schema format. ARFI supports only ruby format schemas.')
17
- @message = message
18
- super
19
- end
20
- end
21
-
22
- # This error is raised when database adapter is not supported (for e.g., SQLite3).
23
- class AdapterNotSupported < StandardError
24
- def initialize(message = 'Adapter not supported')
25
- @message = message
26
- super
27
- end
28
- end
29
- end
30
- end
@@ -1,20 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'active_record'
4
-
5
- module ActiveRecord
6
- class Base # :nodoc:
7
- # +ActiveRecord::Base.function_exists?+ -> bool
8
- #
9
- # This method checks if a custom SQL function exists in the database.
10
- #
11
- # @example
12
- # ActiveRecord::Base.function_exists?('my_function') #=> true
13
- # ActiveRecord::Base.function_exists?('my_function123') #=> false
14
- # @param [String] function_name The name of the function to check.
15
- # @return [Boolean] Returns true if the function exists, false otherwise.
16
- def self.function_exists?(function_name)
17
- connection.execute("SELECT * FROM pg_proc WHERE proname = '#{function_name}'").any?
18
- end
19
- end
20
- end
@@ -1,31 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'active_record/connection_adapters/postgresql/database_statements'
4
-
5
- module ActiveRecord
6
- module ConnectionAdapters
7
- module PostgreSQL
8
- module DatabaseStatements
9
- def raw_execute(sql, name, async: false, allow_retry: false, materialize_transactions: true)
10
- log(sql, name, async: async) do |notification_payload|
11
- with_raw_connection(allow_retry: allow_retry, materialize_transactions: materialize_transactions) do |conn|
12
- result = conn.async_exec(sql)
13
- verified!
14
- handle_warnings(result)
15
- notification_payload[:row_count] = result.count
16
- result
17
- rescue => e
18
- if e.message.match?(/ERROR:\s{2}function (\S+\(\w+\)) does not exist/)
19
- function_name = e.message.match(/ERROR:\s{2}function (\S+\(\w+\)) does not exist/)[1][/^[^(]*/]
20
- if (function_file = Dir.glob(Rails.root.join('db', 'functions', "#{function_name}_v*.sql").to_s).first)
21
- conn.async_exec(File.read(function_file).chop)
22
- retry
23
- end
24
- end
25
- end
26
- end
27
- end
28
- end
29
- end
30
- end
31
- end
@@ -1,7 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'active_record'
4
- require 'active_record/schema'
5
- require 'arfi/sql_function_loader'
6
- require_relative 'active_record/base'
7
- require_relative 'active_record/connection_adapters/postgresql/database_statements'
data/lib/arfi/railtie.rb DELETED
@@ -1,15 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'arfi'
4
- require 'rails'
5
-
6
- module Arfi
7
- class Railtie < ::Rails::Railtie # :nodoc:
8
- railtie_name :arfi
9
-
10
- rake_tasks do
11
- path = File.expand_path(__dir__)
12
- Dir.glob("#{path}/tasks/**/*.rake").each { |f| load f }
13
- end
14
- end
15
- end
@@ -1,62 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Arfi
4
- # +Arfi::SqlFunctionLoader+ is a class which loads user defined SQL functions into database.
5
- class SqlFunctionLoader
6
- class << self
7
- # +Arfi::SqlFunctionLoader.load!+ -> (nil | void)
8
- #
9
- # Loads user defined SQL functions into database.
10
- #
11
- # @return [nil] if there is no `db/functions` directory.
12
- # @return [void] if there is no errors.
13
- # @raise [Arfi::Errors::AdapterNotSupported] if database adapter is SQLite.
14
- def load!
15
- return unless sql_files.any?
16
- raise Arfi::Errors::AdapterNotSupported if conn.adapter_name == 'SQLite'
17
-
18
- populate_db
19
- conn.close
20
- end
21
-
22
- private
23
-
24
- # +Arfi::SqlFunctionLoader#populate_db+ -> void
25
- #
26
- # Loads user defined SQL functions into database.
27
- #
28
- # @!visibility private
29
- # @private
30
- # @return [void]
31
- def populate_db
32
- sql_files.each do |file|
33
- sql = File.read(file).strip
34
- conn.execute(sql)
35
- puts "[ARFI] Loaded: #{File.basename(file)}"
36
- end
37
- end
38
-
39
- # +Arfi::SqlFunctionLoader#sql_files+ -> Array<String>
40
- #
41
- # Helper method to get list of SQL files.
42
- #
43
- # @!visibility private
44
- # @private
45
- # @return [Array<String>] List of SQL files.
46
- def sql_files
47
- Dir.glob(Rails.root.join('db', 'functions').join('*.sql'))
48
- end
49
-
50
- # +Arfi::SqlFunctionLoader#conn+ -> ActiveRecord::ConnectionAdapters::AbstractAdapter
51
- #
52
- # Helper method to get database connection.
53
- #
54
- # @!visibility private
55
- # @private
56
- # @return [ActiveRecord::ConnectionAdapters::AbstractAdapter] Database connection.
57
- def conn
58
- ActiveRecord::Base.lease_connection
59
- end
60
- end
61
- end
62
- end
@@ -1,17 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'rake'
4
- require 'arfi/sql_function_loader'
5
-
6
- namespace :_db do
7
- task :arfi_enhance do
8
- Arfi::SqlFunctionLoader.load!
9
- end
10
- end
11
-
12
- Rake::Task.define_task(:environment) unless Rake::Task.task_defined?(:environment)
13
- raise UnrecognizedCommandError, 'Warning: db:migrate task not found' unless Rake::Task.task_defined?('db:migrate')
14
-
15
- %w[db:migrate db:schema:load db:setup db:prepare].each do |task|
16
- Rake::Task[task].enhance(['_db:arfi_enhance']) if Rake::Task.task_defined?(task)
17
- end
data/lib/arfi/version.rb DELETED
@@ -1,5 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Arfi
4
- VERSION = '0.3.1'
5
- end
data/lib/arfi.rb DELETED
@@ -1,11 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'arfi/version'
4
- require_relative 'arfi/errors'
5
- require 'arfi/extensions/extensions'
6
- require 'rails' if defined?(Rails)
7
-
8
- # Top level module
9
- module Arfi
10
- require_relative 'arfi/railtie' if defined?(Rails)
11
- end
data/sig/arfi.rbs DELETED
@@ -1,2 +0,0 @@
1
- module Arfi
2
- end
@@ -1,94 +0,0 @@
1
- module Arfi
2
- module Commands
3
- class FIdx < Thor
4
- # +Arfi::Commands::FIdx#create+ -> void
5
- #
6
- # This command is used to create the functional index.
7
- #
8
- # @example
9
- # bundle exec arfi f_idx create some_function
10
- # @param index_name [String] Name of the index.
11
- # @return [void]
12
- # @raise [Arfi::Errors::InvalidSchemaFormat] if ActiveRecord.schema_format is not :ruby
13
- # @raise [Arfi::Errors::NoFunctionsDir] if there is no `db/functions` directory
14
- # @see Arfi::Commands::FIdx#validate_schema_format!
15
- def create: (string index_name) -> untyped
16
-
17
- # +Arfi::Commands::FIdx#destroy+ -> void
18
- #
19
- # This command is used to delete the functional index.
20
- #
21
- # @example
22
- # bundle exec arfi f_idx destroy some_function [revision index (just an integer, 1 is by default)]
23
- # @param index_name [String] Name of the index.
24
- # @param revision [String] Revision of the index.
25
- # @return [void]
26
- # @raise [Arfi::Errors::InvalidSchemaFormat] if ActiveRecord.schema_format is not :ruby
27
- def destroy: (string index_name, ?::String revision) -> untyped
28
-
29
- private
30
-
31
- # +Arfi::Commands::FIdx#functions_dir+ -> Pathname
32
- #
33
- # Helper method to get path to `db/functions` directory.
34
- #
35
- # @!visibility private
36
- # @private
37
- # @return [Pathname] Path to `db/functions` directory
38
- def functions_dir: () -> Pathname
39
-
40
- # +Arfi::Commands::FIdx#validate_schema_format!+ -> void
41
- #
42
- # Helper method to validate the schema format.
43
- #
44
- # @!visibility private
45
- # @private
46
- # @raise [Arfi::Errors::InvalidSchemaFormat] if ActiveRecord.schema_format is not :ruby.
47
- # @return [nil] if the schema format is valid.
48
- def validate_schema_format!: () -> (untyped | nil)
49
-
50
- # +Arfi::Commands::FIdx#build_sql_function+ -> String
51
- #
52
- # Helper method to build the SQL function.
53
- #
54
- # @!visibility private
55
- # @private
56
- # @param index_name [String] Name of the index.
57
- # @return [String] SQL function body.
58
- def build_sql_function: (string index_name) -> ::String
59
-
60
- # +Arfi::Commands::FIdx#create_index_file+ -> void
61
- #
62
- # Helper method to create the index file.
63
- #
64
- # @!visibility private
65
- # @private
66
- # @param index_name [String] Name of the index.
67
- # @param content [String] SQL function body.
68
- # @return [void]
69
- def create_index_file: (string index_name, string content) -> untyped
70
-
71
- # +Arfi::Commands::FIdx#extract_latest_version+ -> String
72
- #
73
- # Helper method to extract the latest version of the index.
74
- #
75
- # @!visibility private
76
- # @private
77
- # @param files [Array<String>] List of files.
78
- # @return [String] Latest version of the index.
79
- def extract_latest_version: (Array[String] files) -> string
80
-
81
- # +Arfi::Commands::FIdx#write_file+ -> void
82
- #
83
- # Helper method to write the index file.
84
- #
85
- # @!visibility private
86
- # @private
87
- # @param index_name [String] Name of the index.
88
- # @param content [String] SQL function body.
89
- # @param version [Integer] Version of the index.
90
- # @return [void]
91
- def write_file: (string index_name, string content, ::Integer version) -> untyped
92
- end
93
- end
94
- end
@@ -1,14 +0,0 @@
1
- module Arfi
2
- module Commands
3
- class Project < Thor
4
- # @return [void]
5
- def create: () -> void
6
-
7
- private
8
-
9
- # @private
10
- # @return [Pathname] Path to `db/functions` directory
11
- def functions_dir: () -> Pathname
12
- end
13
- end
14
- end
@@ -1,21 +0,0 @@
1
- module Arfi
2
- module Errors
3
- class NoFunctionsDir < StandardError
4
- @message: string
5
-
6
- def initialize: (?::String message) -> void
7
- end
8
-
9
- class InvalidSchemaFormat < StandardError
10
- @message: string
11
-
12
- def initialize: (?::String message) -> void
13
- end
14
-
15
- class AdapterNotSupported < StandardError
16
- @message: string
17
-
18
- def initialize: (?::String message) -> void
19
- end
20
- end
21
- end
@@ -1,14 +0,0 @@
1
- module ActiveRecord
2
- class Base
3
- # +ActiveRecord::Base.function_exists?+ -> bool
4
- #
5
- # This method checks if a custom SQL function exists in the database.
6
- #
7
- # @example
8
- # ActiveRecord::Base.function_exists?('my_function') #=> true
9
- # ActiveRecord::Base.function_exists?('my_function123') #=> false
10
- # @param [String] function_name The name of the function to check.
11
- # @return [Boolean] Returns true if the function exists, false otherwise.
12
- def self.function_exists?: (string function_name) -> bool
13
- end
14
- end
@@ -1,42 +0,0 @@
1
- module Arfi
2
- # +Arfi::SqlFunctionLoader+ is a class which loads user defined SQL functions into database.
3
- class SqlFunctionLoader
4
- # +Arfi::SqlFunctionLoader.load!+ -> (nil | void)
5
- #
6
- # Loads user defined SQL functions into database.
7
- #
8
- # @return [nil] if there is no `db/functions` directory.
9
- # @return [void] if there is no errors.
10
- # @raise [Arfi::Errors::AdapterNotSupported] if database adapter is SQLite.
11
- def self.load!: () -> (nil | untyped)
12
-
13
- private
14
-
15
- # +Arfi::SqlFunctionLoader#populate_db+ -> void
16
- #
17
- # Loads user defined SQL functions into database.
18
- #
19
- # @!visibility private
20
- # @private
21
- # @return [void]
22
- def self.populate_db: () -> untyped
23
-
24
- # +Arfi::SqlFunctionLoader#sql_files+ -> Array<String>
25
- #
26
- # Helper method to get list of SQL files.
27
- #
28
- # @!visibility private
29
- # @private
30
- # @return [Array<String>] List of SQL files.
31
- def self.sql_files: () -> Array[String]
32
-
33
- # +Arfi::SqlFunctionLoader#conn+ -> ActiveRecord::ConnectionAdapters::AbstractAdapter
34
- #
35
- # Helper method to get database connection.
36
- #
37
- # @!visibility private
38
- # @private
39
- # @return [ActiveRecord::ConnectionAdapters::AbstractAdapter] Database connection.
40
- def self.conn: () -> ::ActiveRecord::ConnectionAdapters::AbstractAdapter
41
- end
42
- end
@@ -1,3 +0,0 @@
1
- module Arfi
2
- VERSION: "0.3.1"
3
- end