Skip to content

Commit

Permalink
Merge branch 'sinatra-activerecord:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexRiedler authored Dec 18, 2024
2 parents 58e52e4 + aec9de6 commit f5e6d0e
Show file tree
Hide file tree
Showing 20 changed files with 284 additions and 30 deletions.
56 changes: 48 additions & 8 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,56 @@ jobs:
strategy:
matrix:
ruby:
- '2.5.x'
- '2.6.x'
- '2.7.x'
- '2.6'
- '2.7'
- '3.0'
- '3.1'
- '3.2'
- '3.3'
activerecord:
- '4.1'
- '5.0'
- '6.0'
- '6.1'
- '7.0'
- '8.0'
exclude:
# active record 4.1 is not compatible with ruby 2.7 due to BigDecimal changes
- ruby: '2.7.x'
- ruby: '2.7'
activerecord: '4.1'
# ActiveRecord 5.x doesn't work on Ruby 3.0+
- ruby: '3.0'
activerecord: '4.1'
- ruby: '3.0'
activerecord: '5.0'
- ruby: '3.1'
activerecord: '4.1'
- ruby: '3.1'
activerecord: '5.0'
- ruby: '3.2'
activerecord: '4.1'
- ruby: '3.2'
activerecord: '5.0'
- ruby: '3.3'
activerecord: '4.1'
- ruby: '3.3'
activerecord: '5.0'
# ActiveRecord 7.x doesn't work on Ruby 2.6 and below
- ruby: '2.6'
activerecord: '7.0'
# Ruby 3.1 has problem with Rails 7 currently (https://gist.github.com/yahonda/2776d8d7b6ea7045359f38c10449937b#add-classdescendants)
- ruby: '3.1'
activerecord: '7.0'
# Activerecord 8.x doesn't work on Ruby 3.1 and below
- ruby: '2.6'
activerecord: '8.0'
- ruby: '2.7'
activerecord: '8.0'
- ruby: '3.0'
activerecord: '8.0'
- ruby: '3.1'
activerecord: '8.0'


steps:
- name: Checkout current branch
Expand All @@ -34,20 +73,21 @@ jobs:
sudo apt-get install libsqlite3-dev
- name: Setup Ruby
uses: actions/setup-ruby@v1
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
bundler: '2.4.22'

- name : Install dependency
run: |
bundle install
bundle _2.4.22_ install
- name: Run appraisal to install different active records version
run: |
bundle exec appraisal
bundle _2.4.22_ exec appraisal install
- name: Run rspec test across active record
run: |
bundle exec appraisal activerecord-${{ matrix.activerecord }} rspec
bundle _2.4.22_ exec appraisal activerecord-${{ matrix.activerecord }} rspec
19 changes: 19 additions & 0 deletions Appraisals
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
if RUBY_VERSION >= '3.2.0'
appraise 'activerecord-8.0' do
gem 'activerecord', '~> 8.0.0'
end
end

if RUBY_VERSION >= '2.7.0'
appraise 'activerecord-7.0' do
gem 'activerecord', '~> 7.0.0'
gem 'sqlite3', '~> 1.4'
end
end

appraise 'activerecord-6.1' do
gem 'activerecord', '~> 6.1.0'
gem 'sqlite3', '~> 1.4'
end

appraise 'activerecord-6.0' do
gem 'activerecord', '~> 6.0.0'
gem 'sqlite3', '~> 1.4'
end

appraise 'activerecord-5.0' do
Expand Down
29 changes: 29 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,34 @@
# Changelog

## Version 2.0.28
Added support for ActiveRecord 8.0

## Version 2.0.27
Updated to call `ActiveRecord::Base.connection_handler.clear_active_connections!` on Rails 7 and above, as calling `ActiveRecord::Base.connection_handler.clear_active_connections!` is deprecated. Thanks Mr Rogres (@bunnymatic) for contributing to this

## Version 2.0.26
Removed unused variables, and removed support for ruby 2.5

## Version 2.0.25
- Added support for ActiveRecord 7.0

## Version 2.0.24
- Fixed compatibility with Psych version 4.0.0+ , use unsafe_load if available.
- Added Ruby 3.0 and head (latest version of Ruby) to the CI matrix
Thanks Onaka (@onk) for contributing to this.

## Version 2.0.23
- For ActiveRecord 6.0 and above, if the the configuration (database.yml) has multiple databases defined, it will discard the DATABASE_URL spec (if it is supplied from environment variables), following [Active Record convention here](https://github.com/rails/rails/blob/main/activerecord/lib/active_record/database_configurations.rb#L169). Thanks JoakimKlaxit (@JoakimKlaxit) for contributing to this.


## Version 2.0.22

- Added compability fix for ActiveRecord 6.1 ([#107](https://github.com/janko-m/sinatra-activerecord/issue/107)), as they have moved ConnectionUrlResolver to another module. Thanks Richard Peck(@richpeck) for contributing to this.

## Version 2.0.21

- If both config/database.yml and $DATABASE_URL is present, the database configuration will be merged from this two, and $DATABASE_URL's variables will take precedence. (https://github.com/sinatra-activerecord/sinatra-activerecord/pull/103)

## Version 2.0.9

- Don't try to load config/database.yml if `$DATABASE_URL` is present ([#55](https://github.com/janko-m/sinatra-activerecord/pull/55), thanks to @exviva)
Expand Down
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

Ensure the test passes before submitting a Pull Request,

Currently this gem supports ActiveRecord 4.1, 5.0 and 6.0, ensure the test works across these ActiveRecord version.
Currently this gem supports ActiveRecord 5.0, 6.0, 7.0 and 8.0, ensure the test works across these ActiveRecord version.

1. Run `bundle exec appraisal` to generate gemfiles required to support different Active Record version
2. Run `bundle exec appraisal rspec` to run test across different Active Record version
3. Ensure all of them passes

Thanks!
Thanks!
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ If you have a `config/database.yml`, it will automatically be loaded, no need
to specify it. Also, in production, the `$DATABASE_URL` environment variable
will automatically be read as the database (if you haven't specified otherwise).

If both `config/database.yml` and `$DATABASE_URL` are present, the database configuration of this two will be merged, with $DATABASE_URL's variable taking precedence over database.yml (for the same variable / key).


Note: If you are using ActiveRecord 6.0 and above, and have [defined multiple databases](https://guides.rubyonrails.org/active_record_multiple_databases.html#setting-up-your-application) for the database.yml, the $DATABASE_URL configuration will be discarded, following [Active Record convention here](https://github.com/rails/rails/blob/main/activerecord/lib/active_record/database_configurations.rb#L169).


Note that in **modular** Sinatra applications you will need to first register
the extension:

Expand Down Expand Up @@ -147,8 +153,9 @@ This gem was made in 2009 by Blake Mizerany, creator of Sinatra.

## Social

You can follow me on Twitter, I'm [@jankomarohnic](http://twitter.com/jankomarohnic).
You can follow Janko on Twitter, [@jankomarohnic](http://twitter.com/jankomarohnic).
You can follow Axel on Twitter, [@soulchildpls](http://twitter.com/soulchildpls).

## License

[MIT](https://github.com/janko-m/sinatra-activerecord/blob/master/LICENSE)
[MIT](https://github.com/sinatra-activerecord/sinatra-activerecord/blob/master/LICENSE)
2 changes: 1 addition & 1 deletion example/postgres/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ gem 'pg'
gem 'rake'
gem 'sinatra'
gem 'sinatra-activerecord', path: ".."
gem 'activerecord', "3.2.17"
gem 'activerecord', '>= 4.1'
gem 'pry'
gem 'pry-byebug'
2 changes: 1 addition & 1 deletion example/sqlite/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ gem 'sqlite3'
gem 'rake'
gem 'sinatra'
gem 'sinatra-activerecord', path: ".."
gem 'activerecord', "3.2.17"
gem 'activerecord', '>= 4.1'
gem 'pry'
gem 'pry-byebug'
1 change: 1 addition & 0 deletions gemfiles/activerecord_6.0.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ source "https://rubygems.org"
gem "pry"
gem "pry-byebug"
gem "activerecord", "~> 6.0.0"
gem "sqlite3", "~> 1.4"

gemspec path: "../"
10 changes: 10 additions & 0 deletions gemfiles/activerecord_6.1.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# This file was generated by Appraisal

source "https://rubygems.org"

gem "pry"
gem "pry-byebug"
gem "activerecord", "~> 6.1.0"
gem "sqlite3", "~> 1.4"

gemspec path: "../"
50 changes: 46 additions & 4 deletions lib/sinatra/activerecord.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
require 'yaml'
require 'erb'

require 'active_record/database_configurations/connection_url_resolver' if Gem.loaded_specs["activerecord"].version >= Gem::Version.create('6.1')

module Sinatra
module ActiveRecordHelper
def database
Expand All @@ -16,7 +18,35 @@ def database

module ActiveRecordExtension
def self.registered(app)
if ENV['DATABASE_URL']
if ENV['DATABASE_URL'] && File.exist?("#{Dir.pwd}/config/database.yml")
path = "#{Dir.pwd}/config/database.yml"
url = ENV['DATABASE_URL']
source = ERB.new(File.read(path)).result
file_spec = YAML.respond_to?(:unsafe_load) ? YAML.unsafe_load(source) : YAML.load(source)
file_spec ||= {}

# ActiveRecord 6.1+ has moved the connection url resolver to another module
if Gem.loaded_specs["activerecord"].version >= Gem::Version.create('6.1')
url_spec = ActiveRecord::DatabaseConfigurations::ConnectionUrlResolver.new(url).to_hash
else
url_spec = ActiveRecord::ConnectionAdapters::ConnectionSpecification::ConnectionUrlResolver.new(url).to_hash
end

# if the configuration concerns only one database, and url_spec exist, url_spec will override the same key
# if the configuration has multiple databases (Rails 6.0+ feature), url_spec is discarded
# Following Active Record config convention
# https://github.com/rails/rails/blob/main/activerecord/lib/active_record/database_configurations.rb#L169
final_spec = file_spec.keys.map do |env|
config = file_spec[env]
if config.is_a?(Hash) && config.all? { |_k, v| v.is_a?(Hash) }
[env, config]
else
[env, config.merge(url_spec)]
end
end.to_h

app.set :database, final_spec
elsif ENV['DATABASE_URL']
app.set :database, ENV['DATABASE_URL']
elsif File.exist?("#{Dir.pwd}/config/database.yml")
app.set :database_file, "#{Dir.pwd}/config/database.yml"
Expand All @@ -29,13 +59,25 @@ def self.registered(app)
app.helpers ActiveRecordHelper

# re-connect if database connection dropped (Rails 3 only)
app.before { ActiveRecord::Base.verify_active_connections! if ActiveRecord::Base.respond_to?(:verify_active_connections!) }
app.after { ActiveRecord::Base.clear_active_connections! }
app.before do
if ActiveRecord::VERSION::MAJOR == 3
ActiveRecord::Base.verify_active_connections! if ActiveRecord::Base.respond_to?(:verify_active_connections!)
end
end
app.after do
if ActiveRecord::VERSION::MAJOR < 7
ActiveRecord::Base.clear_active_connections!
else
ActiveRecord::Base.connection_handler.clear_active_connections!
end
end
end

def database_file=(path)
path = File.join(root, path) if Pathname(path).relative? and root
spec = YAML.load(ERB.new(File.read(path)).result) || {}
source = ERB.new(File.read(path)).result
spec = YAML.respond_to?(:unsafe_load) ? YAML.unsafe_load(source) : YAML.load(source)
spec ||= {}
set :database, spec
end

Expand Down
2 changes: 1 addition & 1 deletion lib/sinatra/activerecord/rake/activerecord_3.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def root
end

def env
ActiveSupport::StringInquirer.new(ENV["RACK_ENV"] || "development")
ActiveSupport::StringInquirer.new(ENV["APP_ENV"] || ENV["RACK_ENV"] || "development")
end

def application
Expand Down
2 changes: 1 addition & 1 deletion lib/sinatra/activerecord/rake/activerecord_4.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def load_seed

ActiveRecord::Tasks::DatabaseTasks.tap do |config|
config.root = Rake.application.original_dir
config.env = ENV["RACK_ENV"] || "development"
config.env = ENV["APP_ENV"] || ENV["RACK_ENV"] || "development"
config.db_dir = "db"
config.migrations_paths = ["db/migrate"]
config.fixtures_path = "test/fixtures"
Expand Down
2 changes: 1 addition & 1 deletion lib/sinatra/activerecord/rake/activerecord_5.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def load_seed

ActiveRecord::Tasks::DatabaseTasks.tap do |config|
config.root = Rake.application.original_dir
config.env = ENV["RACK_ENV"] || "development"
config.env = ENV["APP_ENV"] || ENV["RACK_ENV"] || "development"
config.db_dir = "db"
config.migrations_paths = ["db/migrate"]
config.fixtures_path = "test/fixtures"
Expand Down
2 changes: 1 addition & 1 deletion lib/sinatra/activerecord/rake/activerecord_6.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def load_seed

ActiveRecord::Tasks::DatabaseTasks.tap do |config|
config.root = Rake.application.original_dir
config.env = ENV["RACK_ENV"] || "development"
config.env = ENV["APP_ENV"] || ENV["RACK_ENV"] || "development"
config.db_dir = "db"
config.migrations_paths = ["db/migrate"]
config.fixtures_path = "test/fixtures"
Expand Down
23 changes: 23 additions & 0 deletions lib/sinatra/activerecord/rake/activerecord_7.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
seed_loader = Class.new do
def load_seed
load "#{ActiveRecord::Tasks::DatabaseTasks.db_dir}/seeds.rb"
end
end

ActiveRecord::Tasks::DatabaseTasks.tap do |config|
config.root = Rake.application.original_dir
config.env = ENV["APP_ENV"] || ENV["RACK_ENV"] || "development"
config.db_dir = "db"
config.migrations_paths = ["db/migrate"]
config.fixtures_path = "test/fixtures"
config.seed_loader = seed_loader.new
config.database_configuration = ActiveRecord::Base.configurations
end

# db:load_config can be overriden manually
Rake::Task["db:seed"].enhance(["db:load_config"])
Rake::Task["db:load_config"].clear

# define Rails' tasks as no-op
Rake::Task.define_task("db:environment")
Rake::Task["db:test:deprecated"].clear if Rake::Task.task_defined?("db:test:deprecated")
23 changes: 23 additions & 0 deletions lib/sinatra/activerecord/rake/activerecord_8.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
seed_loader = Class.new do
def load_seed
load "#{ActiveRecord::Tasks::DatabaseTasks.db_dir}/seeds.rb"
end
end

ActiveRecord::Tasks::DatabaseTasks.tap do |config|
config.root = Rake.application.original_dir
config.env = ENV["APP_ENV"] || ENV["RACK_ENV"] || "development"
config.db_dir = "db"
config.migrations_paths = ["db/migrate"]
config.fixtures_path = "test/fixtures"
config.seed_loader = seed_loader.new
config.database_configuration = ActiveRecord::Base.configurations
end

# db:load_config can be overriden manually
Rake::Task["db:seed"].enhance(["db:load_config"])
Rake::Task["db:load_config"].clear

# define Rails' tasks as no-op
Rake::Task.define_task("db:environment")
Rake::Task["db:test:deprecated"].clear if Rake::Task.task_defined?("db:test:deprecated")
Loading

0 comments on commit f5e6d0e

Please sign in to comment.