Skip to content

Commit

Permalink
Remove Postgresql and use sqlite3 for testing ActiveRecord
Browse files Browse the repository at this point in the history
  • Loading branch information
seuros committed Apr 18, 2021
1 parent 1c7bdd2 commit f7beff6
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 52 deletions.
10 changes: 0 additions & 10 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,6 @@ jobs:
gemfile: gemfiles/rails-6-0.gemfile
experimental: false
runs-on: ubuntu-20.04
services:
postgres:
image: postgres:11
ports:
- 5432:5432
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: carrierwave_test
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
env:
BUNDLE_GEMFILE: ${{ matrix.gemfile }}
EXPERIMENTAL: ${{ matrix.experimental }}
Expand Down
5 changes: 0 additions & 5 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,3 @@ You should now be able to run the remote tests:
REMOTE=true bundle exec rake
Please test with the latest Ruby 2.2.x version using RVM if possible.
## Running active record tests
Make sure you have a local PostgreSQL database named `carrierwave_test` with the username
`postgres`
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
source "https://rubygems.org"

gem "activemodel-serializers-xml"
gem 'sqlite3', platforms: :ruby
gem "activerecord-jdbcsqlite3-adapter", platform: [:jruby, :truffleruby]

gemspec
5 changes: 0 additions & 5 deletions carrierwave.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@ Gem::Specification.new do |s|
s.add_dependency "marcel", "~> 1.0.0"
s.add_dependency "addressable", "~> 2.6"
s.add_dependency "ssrf_filter", "~> 1.0"
if RUBY_ENGINE == 'jruby'
s.add_development_dependency 'activerecord-jdbcpostgresql-adapter'
else
s.add_development_dependency "pg"
end
s.add_development_dependency "rails", ">= 5.0.0"
s.add_development_dependency "cucumber", "~> 2.3"
s.add_development_dependency "rspec", "~> 3.4"
Expand Down
4 changes: 2 additions & 2 deletions gemfiles/rails-5-0.gemfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
source "https://rubygems.org"

gem "rails", "~> 5.0.0"
gem 'pg', '~> 0.21.0', platforms: :ruby
gem "activemodel-serializers-xml"
gem "activerecord-jdbcpostgresql-adapter", "~> 50.0", platforms: :jruby
if RUBY_VERSION < '2.3'
gem "ruby-vips", "2.0.13"
end
gem 'sqlite3', '~> 1.3.6', platforms: :ruby
gem "activerecord-jdbcsqlite3-adapter", platform: [:jruby, :truffleruby]

gemspec :path => "../"
4 changes: 2 additions & 2 deletions gemfiles/rails-5-1.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ source "https://rubygems.org"

gem "rails", "~> 5.1.0"
gem "activemodel-serializers-xml"
gem 'pg', '~> 0.21.0', platforms: :ruby
gem "activerecord-jdbcpostgresql-adapter", '~> 51.0', platforms: :jruby
gem 'sqlite3', '~> 1.3.6', platforms: :ruby
gem "activerecord-jdbcsqlite3-adapter", platform: [:jruby, :truffleruby]

gemspec :path => "../"
3 changes: 2 additions & 1 deletion gemfiles/rails-5-2.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ source "https://rubygems.org"

gem "rails", "~> 5.2.0"
gem "activemodel-serializers-xml"
gem 'pg', '~> 0.21.0', platforms: :ruby
gem 'sqlite3', platforms: :ruby
gem "activerecord-jdbcsqlite3-adapter", platform: [:jruby, :truffleruby]

gemspec :path => "../"
3 changes: 2 additions & 1 deletion gemfiles/rails-6-0.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ source "https://rubygems.org"

gem "rails", "~> 6.0.0"
gem "activemodel-serializers-xml"
gem 'pg', '~> 0.21.0', platforms: :ruby
gem 'sqlite3', platforms: :ruby
gem "activerecord-jdbcsqlite3-adapter", platform: [:jruby, :truffleruby]

gemspec :path => "../"
3 changes: 3 additions & 0 deletions gemfiles/rails-6-1.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@ if RUBY_VERSION >= '3'
gem 'rmagick', github: 'rmagick/rmagick', platform: :ruby
gem 'fog-google', github: 'binti-family/fog-google', branch: 'ruby-3.0'
end
gem 'sqlite3', platforms: :ruby
gem "activerecord-jdbcsqlite3-adapter", platform: [:jruby, :truffleruby]


gemspec :path => "../"
2 changes: 2 additions & 0 deletions gemfiles/rails-master.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ gem "activemodel-serializers-xml"
gem "activerecord-jdbcpostgresql-adapter", github: "jruby/activerecord-jdbc-adapter", platforms: :jruby
gem 'rmagick', platform: :ruby
gem 'fog-google', github: 'binti-family/fog-google', branch: 'ruby-3.0'
gem 'sqlite3', platforms: :ruby
gem "activerecord-jdbcsqlite3-adapter", platform: [:jruby, :truffleruby]

gemspec :path => "../"
9 changes: 8 additions & 1 deletion spec/orm/activerecord_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'spec_helper'
require 'support/activerecord'


def create_table(name)
ActiveRecord::Base.connection.create_table(name, force: true) do |t|
t.column :image, :string
Expand All @@ -17,7 +18,13 @@ def drop_table(name)

def reset_class(class_name)
Object.send(:remove_const, class_name) rescue nil
Object.const_set(class_name, Class.new(ActiveRecord::Base))
klass = Object.const_set(class_name, Class.new(ActiveRecord::Base))
# TODO Remove when Rails 5.2 is dropped
klass.class_eval do
attribute :images, :json
attribute :textfiles, :json
end
klass
end

describe CarrierWave::ActiveRecord do
Expand Down
57 changes: 32 additions & 25 deletions spec/support/activerecord.rb
Original file line number Diff line number Diff line change
@@ -1,33 +1,40 @@
if RUBY_ENGINE == 'jruby'
require 'activerecord-jdbcpostgresql-adapter'
else
require 'pg'
end
require 'active_record'
require 'carrierwave/orm/activerecord'
Bundler.require

# Change this if PG is unavailable
dbconfig = {
:host => '127.0.0.1',
:adapter => 'postgresql',
:database => 'carrierwave_test',
:encoding => 'utf8',
:username => 'postgres',
:password => 'postgres'
}

database = dbconfig.delete(:database)

ActiveRecord::Base.establish_connection(dbconfig.merge(database: "template1"))
begin
ActiveRecord::Base.connection.create_database database
rescue ActiveRecord::StatementInvalid => e # database already exists
end
ActiveRecord::Base.establish_connection(dbconfig.merge(:database => database))
ActiveRecord::Base.establish_connection({ adapter: 'sqlite3', database: ':memory:' })

ActiveRecord::Migration.verbose = false

if ActiveRecord::VERSION::STRING >= '4.2' && ActiveRecord::VERSION::STRING < '5.0'
ActiveRecord::Base.raise_in_transactional_callbacks = true
if ActiveRecord.version < Gem::Version.new("5.2")
module ActiveRecord
module Type
class Json < ActiveModel::Type::Value
include ActiveModel::Type::Helpers::Mutable

def type
:json
end

def deserialize(value)
return value unless value.is_a?(::String)
ActiveSupport::JSON.decode(value) rescue nil
end

def serialize(value)
ActiveSupport::JSON.encode(value) unless value.nil?
end

def changed_in_place?(raw_old_value, new_value)
deserialize(raw_old_value) != new_value
end

def accessor
ActiveRecord::Store::StringKeyedHashAccessor
end
end
end
end

ActiveRecord::Type.register(:json, ActiveRecord::Type::Json, override: false)
end

0 comments on commit f7beff6

Please sign in to comment.