Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modernize supported dependencies + Fix CI #61

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: CI

on: [push, pull_request]

jobs:
test:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
ruby-version: ['2.7', '3.0', '3.1', '3.2']
gemfile:
- "activerecord_6_0"
- "activerecord_6_1"
- "activerecord_7_0"
services:
postgres:
image: postgres
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 5432:5432
env:
BUNDLE_GEMFILE: ${{ github.workspace }}/gemfiles/${{ matrix.gemfile }}.gemfile
TEST_DATABASE_USER: postgres
TEST_DATABASE_PASSWORD: postgres
TEST_DATABASE_HOST: localhost
TEST_DATABASE: postgres
steps:
- uses: actions/checkout@v3
- name: Set up Ruby ${{ matrix.ruby-version }}
uses: ruby/setup-ruby@v1
with:
bundler-cache: true
ruby-version: ${{ matrix.ruby-version }}
- name: Install dependencies
run: bundle install
- name: Run tests
run: bundle exec rake
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ pkg

## PROJECT::SPECIFIC
Gemfile.lock
gemfiles/*.gemfile.lock
27 changes: 0 additions & 27 deletions .travis.yml

This file was deleted.

13 changes: 6 additions & 7 deletions Appraisals
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
appraise "activerecord-4" do
gem "activerecord", "4.2.11.1"
gem "pg", "~> 0.15"
appraise "activerecord-6-0" do
gem "activerecord", "6.0.6"
end

appraise "activerecord-5" do
gem "activerecord", "5.2.3"
appraise "activerecord-6-1" do
gem "activerecord", "6.1.7"
end

appraise "activerecord-6" do
gem "activerecord", "6.0.0"
appraise "activerecord-7-0" do
gem "activerecord", "7.0.4"
end
4 changes: 1 addition & 3 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ task :console do
#IRB.start
end

desc "Setup testing database and table"
desc "Setup testing database"
task :setup do
sh %q(createdb postgresql_cursor_test)
sh %Q<echo "create table products ( id serial primary key, data varchar);" | psql postgresql_cursor_test>
sh %Q<echo "create table prices ( id serial primary key, data varchar, product_id integer);" | psql postgresql_cursor_test>
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

source "https://rubygems.org"

gem "activerecord", "5.2.3"
gem "activerecord", "6.0.6"

gemspec path: "../"
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

source "https://rubygems.org"

gem "activerecord", "6.0.0"
gem "activerecord", "6.1.7"

gemspec path: "../"
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

source "https://rubygems.org"

gem "activerecord", "4.2.11.1"
gem "pg", "~> 0.15"
gem "activerecord", "7.0.4"

gemspec path: "../"
4 changes: 2 additions & 2 deletions lib/postgresql_cursor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
# ActiveRecord 4.x
require 'active_record/connection_adapters/postgresql_adapter'
ActiveRecord::Base.extend(PostgreSQLCursor::ActiveRecord::SqlCursor)
ActiveRecord::Relation.send(:include, PostgreSQLCursor::ActiveRecord::Relation::CursorIterators)
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.send(:include, PostgreSQLCursor::ActiveRecord::ConnectionAdapters::PostgreSQLTypeMap)
ActiveRecord::Relation.include(PostgreSQLCursor::ActiveRecord::Relation::CursorIterators)
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.include(PostgreSQLCursor::ActiveRecord::ConnectionAdapters::PostgreSQLTypeMap)
end
23 changes: 5 additions & 18 deletions lib/postgresql_cursor/cursor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,8 @@ def each_array(&block)
def each_instance(klass = nil, &block)
klass ||= @type
each_tuple do |row|
if ::ActiveRecord::VERSION::MAJOR < 4
model = klass.send(:instantiate, row)
else
@column_types ||= column_types
model = klass.send(:instantiate, row, @column_types)
end
@column_types ||= column_types
model = klass.send(:instantiate, row, @column_types)
block.call(model)
end
end
Expand Down Expand Up @@ -144,12 +140,8 @@ def each_instance_batch(klass = nil, &block)
klass ||= @type
each_batch do |batch|
models = batch.map do |row|
if ::ActiveRecord::VERSION::MAJOR < 4
klass.send(:instantiate, row)
else
@column_types ||= column_types
klass.send(:instantiate, row, @column_types)
end
@column_types ||= column_types
klass.send(:instantiate, row, @column_types)
end
block.call(models)
end
Expand Down Expand Up @@ -223,7 +215,6 @@ def cast_types(row)
end

def column_types
return nil if ::ActiveRecord::VERSION::MAJOR < 4
return @column_types if @column_types

types = {}
Expand All @@ -233,11 +224,7 @@ def column_types
fmod = @result.fmod i
types[fname] = @connection.get_type_map.fetch(ftype.to_s, fmod) do |oid, mod|
# warn "unknown OID: #{fname}(#{oid}, #{mod}) (#{sql})"
if ::ActiveRecord::VERSION::MAJOR <= 4
::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::OID::Identity.new
else
::ActiveRecord::Type::Value.new
end
::ActiveRecord::Type::Value.new
end
end

Expand Down
8 changes: 3 additions & 5 deletions postgresql_cursor.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,12 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]

spec.required_ruby_version = '>= 2.7'

# Remove this for jruby which should use 'activerecord-jdbcpostgresql-adapter'
# spec.add_dependency 'pg'

spec.add_dependency "activerecord", ">= 3.1.0"
# spec.add_dependency 'activerecord', '~> 3.1.0'
# spec.add_dependency 'activerecord', '~> 4.1.0'
# spec.add_dependency 'activerecord', '~> 5.0.0'
# spec.add_dependency 'activerecord', '~> 6.0.0'
spec.add_dependency "activerecord", ">= 6.0"

spec.add_development_dependency "irb"
spec.add_development_dependency "minitest"
Expand Down
19 changes: 17 additions & 2 deletions test-app/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,24 @@
require 'active_record'
require 'postgresql_cursor'

ActiveRecord::Base.establish_connection( adapter: 'postgresql',
ActiveRecord::Base.establish_connection(
adapter: 'postgresql',
database: ENV['TEST_DATABASE'] || 'postgresql_cursor_test',
username: ENV['TEST_USER'] || ENV['USER'] || 'postgresql_cursor')
host: ENV['TEST_DATABASE_HOST'],
username: ENV['TEST_DATABASE_USER'] || ENV['USER'] || 'postgresql_cursor',
password: ENV['TEST_DATABASE_PASSWORD']
)

ActiveRecord::Schema.define(version: 0) do
create_table(:products, force: true) do |t|
t.string :data
end

create_table(:prices, force: true) do |t|
t.string :data
t.integer :product_id
end
end

class Product < ActiveRecord::Base
def self.generate(max=1_000)
Expand Down
19 changes: 17 additions & 2 deletions test/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,24 @@
require 'active_record'
require 'postgresql_cursor'

ActiveRecord::Base.establish_connection(adapter: 'postgresql',
ActiveRecord::Base.establish_connection(
adapter: 'postgresql',
database: ENV['TEST_DATABASE'] || 'postgresql_cursor_test',
username: ENV['TEST_USER'] || ENV['USER'] || 'postgresql_cursor')
host: ENV['TEST_DATABASE_HOST'],
username: ENV['TEST_DATABASE_USER'] || ENV['USER'] || 'postgresql_cursor',
password: ENV['TEST_DATABASE_PASSWORD']
)

ActiveRecord::Schema.define(version: 0) do
create_table(:products, force: true) do |t|
t.string :data
end

create_table(:prices, force: true) do |t|
t.string :data
t.integer :product_id
end
end

class Product < ActiveRecord::Base
has_many :prices
Expand Down
14 changes: 12 additions & 2 deletions test/test_postgresql_cursor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ def test_batch_exception
Product.each_row_batch_by_sql("select * from products") do |r|
raise "Oops"
end
flunk("Should have thrown an exception")
rescue => e
assert_equal e.message, "Oops"
end
Expand All @@ -149,16 +150,18 @@ def test_exception_in_failed_transaction
Product.each_row_by_sql("select * from products") do |r|
Product.connection.execute("select kaboom")
end
flunk("Should have thrown an exception")
rescue => e
assert_match(/PG::InFailedSqlTransaction/, e.message)
assert_kind_of(PG::UndefinedColumn, root_cause(e))
end

def test_batch_exception_in_failed_transaction
Product.each_row_batch_by_sql("select * from products") do |r|
Product.connection.execute("select kaboom")
end
flunk("Should have thrown an exception")
rescue => e
assert_match(/PG::InFailedSqlTransaction/, e.message)
assert_kind_of(PG::UndefinedColumn, root_cause(e))
end

def test_cursor
Expand Down Expand Up @@ -225,4 +228,11 @@ def test_relation_association_is_not_loaded
cursor = Product.first.prices.each_instance
refute cursor.instance_variable_get(:@type).loaded?
end

def root_cause(e)
until e.cause.nil?
e = e.cause
end
e
end
end