rails_sql_prettifier 7.0.4 → 7.0.6
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 +4 -4
- data/.github/FUNDING.yml +1 -0
- data/.github/dependabot.yml +6 -0
- data/.github/workflows/ci.yml +48 -0
- data/.github/workflows/rubocop.yml +17 -0
- data/.rubocop.yml +97 -0
- data/CHANGELOG.md +10 -0
- data/Dockerfile +3 -3
- data/Dockerfile_30 +3 -3
- data/Dockerfile_3_1 +20 -0
- data/Gemfile +3 -1
- data/Rakefile +3 -1
- data/bin/console +1 -0
- data/docker-compose.yml +9 -0
- data/lib/generators/niceql/install_generator.rb +4 -2
- data/lib/generators/templates/niceql_initializer.rb +4 -2
- data/lib/rails_sql_prettifier/abstract_adapter_log_prettifier.rb +15 -0
- data/lib/rails_sql_prettifier/ar_extensions.rb +20 -0
- data/lib/rails_sql_prettifier/nice_ql_config_ext.rb +47 -0
- data/lib/rails_sql_prettifier/niceql_error.rb +11 -0
- data/lib/rails_sql_prettifier/postgres_adapter_nice_ql.rb +10 -0
- data/lib/rails_sql_prettifier/protected_env.rb +13 -0
- data/lib/rails_sql_prettifier/version.rb +3 -1
- data/lib/rails_sql_prettifier.rb +15 -93
- data/rails_sql_prettifier.gemspec +19 -17
- metadata +53 -39
- data/.travis.yml +0 -7
- data/lib/benchmark/cat.rb +0 -34
- data/lib/benchmark/gsub.rb +0 -34
- data/lib/benchmark/txt +0 -748
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d88331480d01f22711f05510cce90fd841478b96e4d6b87be2b668e9270eb4de
|
4
|
+
data.tar.gz: 95c8cf45f0aff20292ba9585125173f7631dfc6e4ac802f26912666637ea9c8f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4a11b93c213572042479c7fbd6df4d8569b99e1d35d45dd04918b2935b993c34fd1f8ecc7b63b1b1ae742814fa4960f7286c7a0e275753b85026d1126c7e16d2
|
7
|
+
data.tar.gz: a846f1d875df5629781fb952f41df40c3e8f98a1eb6c3a42cbb1757f9b3a4c77de738d13ec99049a887d789492db7437ea738de456785405ff493fe859653a97
|
data/.github/FUNDING.yml
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
buy_me_a_coffee: alekseyl
|
@@ -0,0 +1,48 @@
|
|
1
|
+
name: CI
|
2
|
+
|
3
|
+
on: [push, pull_request]
|
4
|
+
|
5
|
+
jobs:
|
6
|
+
test:
|
7
|
+
runs-on: ubuntu-latest
|
8
|
+
|
9
|
+
strategy:
|
10
|
+
fail-fast: false
|
11
|
+
matrix:
|
12
|
+
ruby: ["2.7", "3.0", "3.1", "3.2", "3.3"]
|
13
|
+
|
14
|
+
# Service containers to run with `container-job`
|
15
|
+
services:
|
16
|
+
# Label used to access the service container
|
17
|
+
# more about postgres image can be read here: https://hub.docker.com/_/postgres
|
18
|
+
# more about using pg image in CI: https://docs.github.com/en/actions/use-cases-and-examples/using-containerized-services/creating-postgresql-service-containers
|
19
|
+
postgres:
|
20
|
+
# Docker Hub image
|
21
|
+
image: postgres
|
22
|
+
# Provide the password for postgres
|
23
|
+
env:
|
24
|
+
POSTGRES_PASSWORD: postgres
|
25
|
+
POSTGRES_DB: niceql-test
|
26
|
+
POSTGRES_USER: postgres
|
27
|
+
# this options should be kept, otherwise pg container will not be waited
|
28
|
+
options: >-
|
29
|
+
--health-cmd pg_isready
|
30
|
+
--health-interval 10s
|
31
|
+
--health-timeout 5s
|
32
|
+
--health-retries 5
|
33
|
+
--network-alias postgres
|
34
|
+
ports:
|
35
|
+
# Maps tcp port 5432 on service container to the host
|
36
|
+
- 5432:5432
|
37
|
+
|
38
|
+
steps:
|
39
|
+
- uses: actions/checkout@v3
|
40
|
+
- name: Set up Ruby
|
41
|
+
uses: ruby/setup-ruby@v1
|
42
|
+
with:
|
43
|
+
bundler-cache: true # 'bundle install' and cache gems
|
44
|
+
ruby-version: ${{ matrix.ruby }}
|
45
|
+
- name: Run tests
|
46
|
+
run: bundle exec rake test
|
47
|
+
env:
|
48
|
+
POSTGRES_HOST: localhost
|
@@ -0,0 +1,17 @@
|
|
1
|
+
name: RuboCop
|
2
|
+
|
3
|
+
on: [push]
|
4
|
+
|
5
|
+
jobs:
|
6
|
+
build:
|
7
|
+
runs-on: ubuntu-latest
|
8
|
+
|
9
|
+
steps:
|
10
|
+
- uses: actions/checkout@v3
|
11
|
+
- name: Set up Ruby
|
12
|
+
uses: ruby/setup-ruby@v1
|
13
|
+
with:
|
14
|
+
ruby-version: 2.7
|
15
|
+
bundler-cache: true # 'bundle install' and cache
|
16
|
+
- name: Run RuboCop
|
17
|
+
run: bundle exec rubocop --parallel
|
data/.rubocop.yml
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
inherit_gem:
|
2
|
+
rubocop-shopify: rubocop.yml
|
3
|
+
|
4
|
+
AllCops:
|
5
|
+
Exclude:
|
6
|
+
- "./lib/templates/active_record/model/model.rb"
|
7
|
+
- "./lib/modules/flea.rb"
|
8
|
+
|
9
|
+
Style/SingleLineMethods:
|
10
|
+
Description: 'Avoid single-line methods.'
|
11
|
+
StyleGuide: '#no-single-line-methods'
|
12
|
+
Enabled: false
|
13
|
+
VersionAdded: '0.9'
|
14
|
+
VersionChanged: '1.8'
|
15
|
+
AllowIfMethodIsEmpty: true
|
16
|
+
|
17
|
+
Style/AsciiComments:
|
18
|
+
Description: 'Use only ascii symbols in comments.'
|
19
|
+
StyleGuide: '#english-comments'
|
20
|
+
Enabled: false
|
21
|
+
VersionAdded: '0.9'
|
22
|
+
VersionChanged: '1.21'
|
23
|
+
AllowedChars:
|
24
|
+
- ©
|
25
|
+
|
26
|
+
Layout/LineLength:
|
27
|
+
Description: 'Checks that line length does not exceed the configured limit.'
|
28
|
+
StyleGuide: '#max-line-length'
|
29
|
+
Enabled: true
|
30
|
+
VersionAdded: '0.25'
|
31
|
+
VersionChanged: '1.4'
|
32
|
+
Max: 120
|
33
|
+
# To make it possible to copy or click on URIs in the code, we allow lines
|
34
|
+
# containing a URI to be longer than Max.
|
35
|
+
AllowHeredoc: true
|
36
|
+
AllowURI: true
|
37
|
+
URISchemes:
|
38
|
+
- http
|
39
|
+
- https
|
40
|
+
# The IgnoreCopDirectives option causes the LineLength rule to ignore cop
|
41
|
+
# directives like '# rubocop: enable ...' when calculating a line's length.
|
42
|
+
IgnoreCopDirectives: true
|
43
|
+
# The AllowedPatterns option is a list of !ruby/regexp and/or string
|
44
|
+
# elements. Strings will be converted to Regexp objects. A line that matches
|
45
|
+
# any regular expression listed in this option will be ignored by LineLength.
|
46
|
+
AllowedPatterns: []
|
47
|
+
IgnoredPatterns: [] # deprecated
|
48
|
+
Exclude:
|
49
|
+
- "./test/**/**/*"
|
50
|
+
|
51
|
+
Metrics/ClassLength:
|
52
|
+
Description: 'Avoid classes longer than 100 lines of code.'
|
53
|
+
Enabled: false
|
54
|
+
VersionAdded: '0.25'
|
55
|
+
VersionChanged: '0.87'
|
56
|
+
CountComments: false # count full line comments?
|
57
|
+
Max: 100
|
58
|
+
CountAsOne: []
|
59
|
+
|
60
|
+
Lint/MissingCopEnableDirective:
|
61
|
+
Description: 'Checks for a `# rubocop:enable` after `# rubocop:disable`.'
|
62
|
+
Enabled: true
|
63
|
+
VersionAdded: '0.52'
|
64
|
+
# Maximum number of consecutive lines the cop can be disabled for.
|
65
|
+
# 0 allows only single-line disables
|
66
|
+
# 1 would mean the maximum allowed is the following:
|
67
|
+
# # rubocop:disable SomeCop
|
68
|
+
# a = 1
|
69
|
+
# # rubocop:enable SomeCop
|
70
|
+
# .inf for any size
|
71
|
+
MaximumRangeSize: .inf
|
72
|
+
|
73
|
+
Style/MethodCallWithArgsParentheses:
|
74
|
+
Enabled: true
|
75
|
+
IgnoredMethods:
|
76
|
+
- require
|
77
|
+
- require_relative
|
78
|
+
- require_dependency
|
79
|
+
- yield
|
80
|
+
- raise
|
81
|
+
- puts
|
82
|
+
Exclude:
|
83
|
+
- "/**/Gemfile"
|
84
|
+
- "./db/**/*"
|
85
|
+
|
86
|
+
Layout/EmptyLinesAroundBlockBody:
|
87
|
+
# its more documentation than code, so it should be readable and
|
88
|
+
# there are huge amount of multiline description, looks nasty without spaces
|
89
|
+
Exclude:
|
90
|
+
- "./app_doc/**/*"
|
91
|
+
|
92
|
+
Style/ClassAndModuleChildren:
|
93
|
+
Enabled: false
|
94
|
+
|
95
|
+
Lint/UnderscorePrefixedVariableName:
|
96
|
+
Exclude:
|
97
|
+
- "./test/**/**/*"
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,13 @@
|
|
1
|
+
# 7.0.6
|
2
|
+
* Fixed issue with protected_env for a 7.2 rails
|
3
|
+
* github CI is working
|
4
|
+
* rubocoped a little
|
5
|
+
|
6
|
+
# 7.0.5
|
7
|
+
* Fixed tests
|
8
|
+
* Restructured code properly
|
9
|
+
* Fixed issue with exec_niceql over relation with error, now error will be prettified properly
|
10
|
+
|
1
11
|
# 7.0.4
|
2
12
|
* pg_adapter_with_nicesql setting will not be set to action for protected_env
|
3
13
|
* niceql version set to ~> 0.6 (fixed https://github.com/alekseyl/niceql/issues/16 !)
|
data/Dockerfile
CHANGED
@@ -5,13 +5,13 @@ RUN apt-get update && apt-get -y install lsb-release
|
|
5
5
|
#
|
6
6
|
RUN wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - && \
|
7
7
|
sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list' && \
|
8
|
-
apt-get update && apt-get -y install postgresql postgresql-client-
|
8
|
+
apt-get update && apt-get -y install postgresql postgresql-client-14
|
9
9
|
|
10
|
-
RUN sh -c 'echo "local all all trust" > /etc/postgresql/
|
10
|
+
RUN sh -c 'echo "local all all trust" > /etc/postgresql/$(ls /etc/postgresql)/main/pg_hba.conf' && \
|
11
11
|
service postgresql start && \
|
12
12
|
psql -U postgres -c 'CREATE DATABASE "niceql-test"'
|
13
13
|
|
14
|
-
RUN gem install bundler
|
14
|
+
RUN gem install bundler -v 2.4.22
|
15
15
|
|
16
16
|
COPY lib/rails_sql_prettifier/version.rb /app/lib/rails_sql_prettifier/version.rb
|
17
17
|
COPY rails_sql_prettifier.gemspec /app/
|
data/Dockerfile_30
CHANGED
@@ -5,13 +5,13 @@ RUN apt-get update && apt-get -y install lsb-release
|
|
5
5
|
#
|
6
6
|
RUN wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - && \
|
7
7
|
sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list' && \
|
8
|
-
apt-get update && apt-get -y install postgresql postgresql-client-
|
8
|
+
apt-get update && apt-get -y install postgresql postgresql-client-14
|
9
9
|
|
10
|
-
RUN sh -c 'echo "local all all trust" > /etc/postgresql/
|
10
|
+
RUN sh -c 'echo "local all all trust" > /etc/postgresql/$(ls /etc/postgresql)/main/pg_hba.conf' && \
|
11
11
|
service postgresql start && \
|
12
12
|
psql -U postgres -c 'CREATE DATABASE "niceql-test"'
|
13
13
|
|
14
|
-
RUN gem install bundler
|
14
|
+
RUN gem install bundler -v 2.4.22
|
15
15
|
|
16
16
|
COPY lib/rails_sql_prettifier/version.rb /app/lib/rails_sql_prettifier/version.rb
|
17
17
|
COPY rails_sql_prettifier.gemspec /app/
|
data/Dockerfile_3_1
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
FROM ruby:3.1-bullseye
|
2
|
+
|
3
|
+
WORKDIR /app
|
4
|
+
RUN apt-get update && apt-get -y install lsb-release
|
5
|
+
#
|
6
|
+
RUN wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - && \
|
7
|
+
sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list' && \
|
8
|
+
apt-get update && apt-get -y install postgresql postgresql-client-14
|
9
|
+
|
10
|
+
RUN sh -c 'echo "local all all trust" > /etc/postgresql/$(ls /etc/postgresql)/main/pg_hba.conf' && \
|
11
|
+
service postgresql start && \
|
12
|
+
psql -U postgres -c 'CREATE DATABASE "niceql-test"'
|
13
|
+
|
14
|
+
RUN gem install bundler
|
15
|
+
|
16
|
+
COPY lib/rails_sql_prettifier/version.rb /app/lib/rails_sql_prettifier/version.rb
|
17
|
+
COPY rails_sql_prettifier.gemspec /app/
|
18
|
+
COPY Gemfil* /app/
|
19
|
+
#
|
20
|
+
RUN bundle install
|
data/Gemfile
CHANGED
data/Rakefile
CHANGED
data/bin/console
CHANGED
data/docker-compose.yml
CHANGED
@@ -16,3 +16,12 @@ services:
|
|
16
16
|
command: /bin/bash -c 'service postgresql start && rake test'
|
17
17
|
volumes:
|
18
18
|
- '.:/app'
|
19
|
+
|
20
|
+
test_3_1:
|
21
|
+
build:
|
22
|
+
context: .
|
23
|
+
dockerfile: Dockerfile_3_1
|
24
|
+
image: niceql_3_1
|
25
|
+
command: /bin/bash -c 'service postgresql start && rake test'
|
26
|
+
volumes:
|
27
|
+
- '.:/app'
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Niceql
|
2
4
|
module Generators
|
3
5
|
class InstallGenerator < Rails::Generators::Base
|
@@ -5,10 +7,10 @@ module Niceql
|
|
5
7
|
desc "Creates Niceql initializer for your application"
|
6
8
|
|
7
9
|
def copy_initializer
|
8
|
-
template
|
10
|
+
template("niceql_initializer.rb", "config/initializers/niceql.rb")
|
9
11
|
|
10
12
|
puts "Install complete!"
|
11
13
|
end
|
12
14
|
end
|
13
15
|
end
|
14
|
-
end
|
16
|
+
end
|
@@ -1,5 +1,7 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
Niceql.configure do |c|
|
2
|
-
# You can adjust pg_adapter in
|
4
|
+
# You can adjust pg_adapter in production at your own risk!
|
3
5
|
# If you need it in production use exec_niceql
|
4
6
|
# default: false
|
5
7
|
# c.pg_adapter_with_nicesql = Rails.env.development?
|
@@ -8,4 +10,4 @@ Niceql.configure do |c|
|
|
8
10
|
# c.indentation_base = 2
|
9
11
|
# c.open_bracket_is_newliner = false
|
10
12
|
# c.prettify_active_record_log_output = false
|
11
|
-
end
|
13
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RailsSQLPrettifier
|
4
|
+
module AbstractAdapterLogPrettifier
|
5
|
+
private
|
6
|
+
|
7
|
+
def log(sql, *args, **kwargs, &block)
|
8
|
+
# \n need to be placed because AR log will start with action description + time info.
|
9
|
+
# rescue sql - just to be sure Prettifier wouldn't break production
|
10
|
+
formatted_sql = "\n" + Niceql::Prettifier.prettify_sql(sql) rescue sql
|
11
|
+
|
12
|
+
super(formatted_sql, *args, **kwargs, &block)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RailsSQLPrettifier
|
4
|
+
module ArExtentions
|
5
|
+
def exec_niceql(reraise = false)
|
6
|
+
connection.execute(to_niceql)
|
7
|
+
rescue StandardError => e
|
8
|
+
puts Niceql::Prettifier.prettify_pg_err(e.message, to_niceql)
|
9
|
+
raise if reraise
|
10
|
+
end
|
11
|
+
|
12
|
+
def to_niceql
|
13
|
+
Niceql::Prettifier.prettify_sql(to_sql, false)
|
14
|
+
end
|
15
|
+
|
16
|
+
def niceql(colorize = true)
|
17
|
+
puts Niceql::Prettifier.prettify_sql(to_sql, colorize)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RailsSQLPrettifier
|
4
|
+
module NiceQLConfigExt
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
attr_accessor :pg_adapter_with_nicesql,
|
9
|
+
:prettify_active_record_log_output,
|
10
|
+
:prettify_pg_errors
|
11
|
+
|
12
|
+
# we need to use a prepend otherwise it's not preceding Niceql.configure in a lookup chain
|
13
|
+
Niceql.singleton_class.prepend(Configure)
|
14
|
+
end
|
15
|
+
|
16
|
+
def ar_using_pg_adapter?
|
17
|
+
ActiveRecord::Base.connection_db_config.adapter == "postgresql"
|
18
|
+
end
|
19
|
+
|
20
|
+
def initialize
|
21
|
+
super
|
22
|
+
self.pg_adapter_with_nicesql = false
|
23
|
+
self.prettify_active_record_log_output = false
|
24
|
+
self.prettify_pg_errors = ar_using_pg_adapter?
|
25
|
+
end
|
26
|
+
|
27
|
+
module Configure
|
28
|
+
def configure
|
29
|
+
super
|
30
|
+
|
31
|
+
if config.pg_adapter_with_nicesql &&
|
32
|
+
defined?(::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter) && !protected_env?
|
33
|
+
|
34
|
+
::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.include(PostgresAdapterNiceQL)
|
35
|
+
end
|
36
|
+
|
37
|
+
if config.prettify_active_record_log_output
|
38
|
+
::ActiveRecord::ConnectionAdapters::AbstractAdapter.prepend(AbstractAdapterLogPrettifier)
|
39
|
+
end
|
40
|
+
|
41
|
+
if config.prettify_pg_errors && config.ar_using_pg_adapter?
|
42
|
+
::ActiveRecord::StatementInvalid.include(NiceqlError)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RailsSQLPrettifier
|
4
|
+
module NiceqlError
|
5
|
+
def to_s
|
6
|
+
# older rails version do not provide sql as a standalone query, instead they
|
7
|
+
# deliver joined message, and try(:sql) will set prettify_err with nil in that case
|
8
|
+
Niceql.config.prettify_pg_errors ? Niceql::Prettifier.prettify_err(super, try(:sql)) : super
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RailsSQLPrettifier
|
4
|
+
module PostgresAdapterNiceQL
|
5
|
+
def exec_query(sql, *args, **kwargs, &block)
|
6
|
+
# replacing sql with prettified sql, that's all
|
7
|
+
super(Niceql::Prettifier.prettify_sql(sql, false), *args, **kwargs, &block)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RailsSQLPrettifier
|
4
|
+
module ProtectedEnv
|
5
|
+
def protected_env?
|
6
|
+
migration_context = ActiveRecord::Base.connection.try(:migration_context) ||
|
7
|
+
ActiveRecord::Base.connection.try(:pool)&.migration_context # rails 7.2+
|
8
|
+
|
9
|
+
migration_context&.protected_environment? ||
|
10
|
+
defined?(Rails) && !(Rails.env.test? || Rails.env.development?)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
data/lib/rails_sql_prettifier.rb
CHANGED
@@ -1,99 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "rails_sql_prettifier/version"
|
2
|
-
require
|
4
|
+
require "rails_sql_prettifier/abstract_adapter_log_prettifier"
|
5
|
+
require "rails_sql_prettifier/ar_extensions"
|
6
|
+
require "rails_sql_prettifier/niceql_error"
|
7
|
+
require "rails_sql_prettifier/nice_ql_config_ext"
|
8
|
+
require "rails_sql_prettifier/postgres_adapter_nice_ql"
|
9
|
+
require "rails_sql_prettifier/protected_env"
|
10
|
+
|
11
|
+
require "active_record"
|
3
12
|
require "niceql"
|
4
13
|
|
5
14
|
module RailsSQLPrettifier
|
15
|
+
::ActiveRecord::Relation.include(ArExtentions)
|
16
|
+
::Arel::TreeManager.include(ArExtentions)
|
17
|
+
::Arel::Nodes::Node.include(ArExtentions)
|
6
18
|
|
7
|
-
|
8
|
-
def exec_niceql
|
9
|
-
connection.execute( to_niceql )
|
10
|
-
end
|
11
|
-
|
12
|
-
def to_niceql
|
13
|
-
Niceql::Prettifier.prettify_sql(to_sql, false)
|
14
|
-
end
|
15
|
-
|
16
|
-
def niceql( colorize = true )
|
17
|
-
puts Niceql::Prettifier.prettify_sql( to_sql, colorize )
|
18
|
-
end
|
19
|
-
|
20
|
-
end
|
21
|
-
|
22
|
-
module PostgresAdapterNiceQL
|
23
|
-
def exec_query(sql, name = "SQL", binds = [], prepare: false)
|
24
|
-
# replacing sql with prettified sql, thats all
|
25
|
-
super( Niceql::Prettifier.prettify_sql(sql, false), name, binds, prepare: prepare )
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
module AbstractAdapterLogPrettifier
|
30
|
-
private
|
31
|
-
def log( sql, *args, **kwargs, &block )
|
32
|
-
# \n need to be placed because AR log will start with action description + time info.
|
33
|
-
# rescue sql - just to be sure Prettifier wouldn't break production
|
34
|
-
formatted_sql = "\n" + Niceql::Prettifier.prettify_sql(sql) rescue sql
|
35
|
-
|
36
|
-
super( formatted_sql, *args, **kwargs, &block )
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
module ErrorExt
|
41
|
-
def to_s
|
42
|
-
# older rails version do not provide sql as a standalone query, instead they
|
43
|
-
# deliver joined message
|
44
|
-
Niceql.config.prettify_pg_errors ? Niceql::Prettifier.prettify_err(super, try(:sql) ) : super
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
module NiceQLConfigExt
|
49
|
-
extend ActiveSupport::Concern
|
50
|
-
|
51
|
-
included do
|
52
|
-
attr_accessor :pg_adapter_with_nicesql,
|
53
|
-
:prettify_active_record_log_output,
|
54
|
-
:prettify_pg_errors
|
55
|
-
end
|
56
|
-
|
57
|
-
def ar_using_pg_adapter?
|
58
|
-
ActiveRecord::Base.connection_db_config.adapter == 'postgresql'
|
59
|
-
end
|
60
|
-
|
61
|
-
def initialize
|
62
|
-
super
|
63
|
-
self.pg_adapter_with_nicesql = false
|
64
|
-
self.prettify_active_record_log_output = false
|
65
|
-
self.prettify_pg_errors = ar_using_pg_adapter?
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
|
-
module NiceqlExt
|
70
|
-
def configure
|
71
|
-
super
|
72
|
-
|
73
|
-
if config.pg_adapter_with_nicesql && defined?(::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter) && !protected_env?
|
74
|
-
::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.include(PostgresAdapterNiceQL)
|
75
|
-
end
|
76
|
-
|
77
|
-
::ActiveRecord::ConnectionAdapters::AbstractAdapter.prepend( AbstractAdapterLogPrettifier ) if config.prettify_active_record_log_output
|
78
|
-
|
79
|
-
::ActiveRecord::StatementInvalid.include( RailsSQLPrettifier::ErrorExt ) if config.prettify_pg_errors && config.ar_using_pg_adapter?
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
|
-
module ProtectedEnv
|
84
|
-
def protected_env?
|
85
|
-
ActiveRecord::Base.connection.migration_context.protected_environment? ||
|
86
|
-
defined?(Rails) && !(Rails.env.test? || Rails.env.development?)
|
87
|
-
end
|
88
|
-
end
|
89
|
-
|
90
|
-
[::ActiveRecord::Relation,
|
91
|
-
::Arel::TreeManager,
|
92
|
-
::Arel::Nodes::Node].each { |klass| klass.send(:include, ArExtentions) }
|
93
|
-
|
94
|
-
Niceql::NiceQLConfig.include( NiceQLConfigExt )
|
95
|
-
|
96
|
-
# we need to use a prepend otherwise it's not preceding Niceql.configure in a lookup chain
|
97
|
-
Niceql.singleton_class.prepend( NiceqlExt )
|
19
|
+
Niceql::NiceQLConfig.include(NiceQLConfigExt)
|
98
20
|
Niceql.extend(ProtectedEnv)
|
99
|
-
end
|
21
|
+
end
|
@@ -1,5 +1,6 @@
|
|
1
|
-
#
|
2
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path("lib", __dir__)
|
3
4
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
5
|
require "rails_sql_prettifier/version"
|
5
6
|
|
@@ -9,8 +10,8 @@ Gem::Specification.new do |spec|
|
|
9
10
|
spec.authors = ["alekseyl"]
|
10
11
|
spec.email = ["leshchuk@gmail.com"]
|
11
12
|
|
12
|
-
spec.summary =
|
13
|
-
spec.description =
|
13
|
+
spec.summary = "This is an ActiveRecord integration for the SQL prettifier gem niceql. "
|
14
|
+
spec.description = "This is an ActiveRecord integration for the SQL prettifier gem niceql. "
|
14
15
|
spec.homepage = "https://github.com/alekseyl/rails_sql_prettifier"
|
15
16
|
spec.license = "MIT"
|
16
17
|
|
@@ -23,26 +24,27 @@ Gem::Specification.new do |spec|
|
|
23
24
|
"public gem pushes."
|
24
25
|
end
|
25
26
|
|
26
|
-
spec.files
|
27
|
+
spec.files = %x(git ls-files -z).split("\x0").reject do |f|
|
27
28
|
f.match(%r{^(test|spec|features)/})
|
28
29
|
end
|
29
30
|
spec.bindir = "exe"
|
30
31
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
31
32
|
spec.require_paths = ["lib"]
|
32
33
|
|
33
|
-
|
34
|
-
spec.
|
35
|
-
spec.add_dependency
|
34
|
+
# for rails 7 you cannot use ruby below 2.7
|
35
|
+
spec.required_ruby_version = ">= 2.7"
|
36
|
+
spec.add_dependency("activerecord", ">= 7", "< 8")
|
37
|
+
spec.add_dependency("niceql", "~> 0.6")
|
36
38
|
|
37
|
-
spec.add_development_dependency
|
38
|
-
spec.add_development_dependency
|
39
|
-
spec.add_development_dependency
|
39
|
+
spec.add_development_dependency("bundler", ">= 1")
|
40
|
+
spec.add_development_dependency("minitest", "~> 5.0")
|
41
|
+
spec.add_development_dependency("rake", ">= 12.3.3")
|
40
42
|
|
41
|
-
spec.add_development_dependency
|
42
|
-
spec.add_development_dependency
|
43
|
-
spec.add_development_dependency
|
44
|
-
spec.add_development_dependency
|
45
|
-
spec.add_development_dependency
|
43
|
+
spec.add_development_dependency("awesome_print")
|
44
|
+
spec.add_development_dependency("differ", "~> 0.1")
|
45
|
+
spec.add_development_dependency("pg", "~> 1")
|
46
|
+
spec.add_development_dependency("pry-byebug", "~> 3.9")
|
47
|
+
spec.add_development_dependency("rubocop-shopify")
|
46
48
|
|
47
|
-
spec.add_development_dependency
|
49
|
+
spec.add_development_dependency("stubberry", "~> 0.2")
|
48
50
|
end
|