Skip to content

Commit 14c8092

Browse files
authored
Merge pull request #1 from gitclear/prepare-gem-for-gitclear
Prepare gem for gitclear.
2 parents 42b693d + 1389eae commit 14c8092

File tree

16 files changed

+437
-150
lines changed

16 files changed

+437
-150
lines changed

.github/workflows/rubocop.yml

Lines changed: 0 additions & 12 deletions
This file was deleted.

.github/workflows/test-with-mysql.yml

Lines changed: 0 additions & 34 deletions
This file was deleted.

.github/workflows/test-with-postgresql.yml

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,17 @@
11
name: PostgreSQL
2-
on: [pull_request]
2+
on: [push]
33
jobs:
44
Test-With-PostgreSQL:
55
runs-on: ubuntu-latest
6-
container: ruby:${{ matrix.ruby }}
6+
container: ruby:3.2
77
strategy:
88
fail-fast: false
9-
matrix:
10-
active_record: [6.1.7.2, 6.0.6, 5.2.8.1]
11-
ruby: ['3.0', 3.1, 3.2]
12-
exclude:
13-
- active_record: 5.2.8.1
14-
ruby: '3.0'
15-
- active_record: 5.2.8.1
16-
ruby: 3.1
17-
- active_record: 5.2.8.1
18-
ruby: 3.2
199
env:
20-
ACTIVE_RECORD_VERSION: ${{ matrix.active_record }}
2110
DATABASE_ADAPTER: postgresql
22-
INSTALL_PG_GEM: true
2311
RAILS_ENV: test
2412
services:
2513
postgres:
26-
image: postgres
14+
image: postgres:14
2715
env:
2816
POSTGRES_PASSWORD: postgres
2917
options: >-

.github/workflows/test-with-sqlite.yml

Lines changed: 0 additions & 33 deletions
This file was deleted.

Dockerfile

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
FROM ruby:3.1
1+
FROM ruby:3.2
22

33
ENV APP_HOME /activerecord_cte
44
RUN mkdir $APP_HOME
55
WORKDIR $APP_HOME
66

77
ENV RAILS_ENV test
8-
ENV INSTALL_MYSQL_GEM true
9-
ENV INSTALL_PG_GEM true
10-
ENV MYSQL_HOST mysql
118

129
# Cache the bundle install
1310
COPY Gemfile* $APP_HOME/

Gemfile

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,8 @@ source "https://rubygems.org"
55
# Specify your gem's dependencies in activerecord-cte.gemspec
66
gemspec
77

8-
ACTIVE_RECORD_VERSION = ENV.fetch("ACTIVE_RECORD_VERSION", "6.1.7.2")
8+
gem "pg"
99

10-
gem "activerecord", ACTIVE_RECORD_VERSION
11-
12-
gem "mysql2" if ENV["INSTALL_MYSQL_GEM"]
13-
gem "pg" if ENV["INSTALL_PG_GEM"]
14-
15-
gem "sqlite3", "1.7.3"
10+
group :development, :test do
11+
gem "activerecord", "6.1.7.9"
12+
end

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,32 @@ Post.with("posts_with_tags AS (SELECT * FROM posts WHERE tags_count > 0)")
9494
# SELECT * FROM posts
9595
```
9696

97+
#### Enhanced String CTE Parsing
98+
99+
This gem includes robust string CTE parsing that handles various table name formats and provides detailed error messages. It supports:
100+
101+
- **Quoted table names**: `` `table_name` ``, `"table_name"`
102+
- **Unquoted table names**: `table_name`, `user_posts`, `table_2023`
103+
- **Case-insensitive AS keyword**: `AS`, `as`, `As`
104+
- **Complex SQL expressions**: Nested parentheses, subqueries, etc.
105+
- **Comprehensive validation**: Balanced parentheses, empty components, malformed syntax
106+
107+
```ruby
108+
# All of these work:
109+
Post.with("`quoted_table` AS (SELECT * FROM posts)")
110+
Post.with('"double_quoted" AS (SELECT * FROM posts)')
111+
Post.with("users_with_posts AS (SELECT * FROM posts WHERE id IN (SELECT post_id FROM comments))")
112+
Post.with("popular_posts as (SELECT * FROM posts WHERE views > 1000)") # lowercase 'as'
113+
```
114+
115+
If there's a syntax error, you'll get helpful error messages:
116+
- `"CTE string cannot be empty"`
117+
- `"CTE string must contain 'AS' keyword. Expected 'table_name AS (SELECT ...)' but got: ..."`
118+
- `"CTE expression must be enclosed in parentheses. Expected 'table_name AS (SELECT ...)' but got: ..."`
119+
- `"Unbalanced parentheses in CTE expression: ..."`
120+
121+
This parsing capability provides a workaround for Rails 6.1+ where string CTE support was broken (see [Rails PR #42563](https://github.com/rails/rails/pull/42563) which was rejected). The implementation is fully documented in `lib/activerecord/cte/string_cte_parser.rb`.
122+
97123
### Arel Nodes
98124

99125
If you already have `Arel::Node::As` node you can just pass it as is
@@ -214,6 +240,9 @@ bundle exec rubocop
214240
To run the tests using SQLite adapter and latest version on Rails run
215241

216242
```
243+
POSTGRES_USER={your_pg_user} \
244+
POSTGRES_PASSWORD={your_pg_password} \
245+
POSTGRES_HOST=localhost \
217246
bundle exec rake test
218247
```
219248

activerecord-cte.gemspec

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ Gem::Specification.new do |spec|
3434

3535
spec.add_development_dependency "bundler", "~> 2.0"
3636
spec.add_development_dependency "minitest", "~> 5.0"
37+
spec.add_development_dependency "pg", "~> 1.5.6"
3738
spec.add_development_dependency "rake", "~> 13.0.1"
38-
spec.add_development_dependency "rubocop", "~> 1.17.0"
39-
spec.add_development_dependency "rubocop-minitest", "~> 0.13.0"
40-
spec.add_development_dependency "rubocop-performance", "~> 1.11.3"
41-
spec.add_development_dependency "rubocop-rake", "~> 0.5.1"
42-
spec.add_development_dependency "sqlite3"
39+
spec.add_development_dependency "rubocop", "~> 1.53.0"
40+
spec.add_development_dependency "rubocop-minitest", "~> 0.30.0"
41+
spec.add_development_dependency "rubocop-performance", "~> 1.19.0"
42+
spec.add_development_dependency "rubocop-rake", "~> 0.6.0"
4343
end

bin/test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
require "English"
55
require "yaml"
66

7-
active_record_versions = %w[6.1.7.2 6.0.6]
8-
database_adapters = %w[mysql postgresql sqlite3]
7+
active_record_versions = %w[6.1.7.9]
8+
database_adapters = %w[postgresql]
99

1010
class Matrix
1111
def initialize(active_record_versions, database_adapters)

docker-compose.yml

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,12 @@ services:
44
lib:
55
build: .
66
links:
7-
- mysql
87
- postgres
98
volumes:
109
- ".:/activerecord_cte"
1110

12-
mysql:
13-
image: mysql:8.0
14-
command: mysqld --default-authentication-plugin=mysql_native_password --skip-mysqlx
15-
restart: always
16-
environment:
17-
MYSQL_DATABASE: activerecord_cte_test
18-
MYSQL_USER: root
19-
MYSQL_PASSWORD: root
20-
MYSQL_ROOT_PASSWORD: root
21-
ports:
22-
- 3306:3306
23-
expose:
24-
- 3306
25-
2611
postgres:
27-
image: postgres:12
12+
image: postgres:14
2813
restart: always
2914
environment:
3015
POSTGRES_DB: activerecord_cte_test

0 commit comments

Comments
 (0)