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

Feature: support trilogy adapter #607

Merged
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
c5edd75
add trilogy to Gemfile
zmariscal Sep 12, 2023
802587b
create trilogy rake task and add it to Rakefile
zmariscal Sep 12, 2023
8c55d81
bundle current github workflows without trilogy
zmariscal Sep 12, 2023
a331a2a
add trilogy to databases.ci.yml
zmariscal Sep 12, 2023
2109996
ensure trilogy AR connection files are added
zmariscal Sep 12, 2023
2b6df26
add private method to control #value returned
zmariscal Sep 12, 2023
bcf5e48
connect to the db if trilogy adapter used
zmariscal Sep 12, 2023
5d8bbf5
create github trilogy workflow
zmariscal Sep 12, 2023
dc079a2
use the trilogy adapter in the db.ci yml file
zmariscal Sep 21, 2023
f46d5ac
remove require statements as they aren't needed
zmariscal Sep 21, 2023
3fc6466
Remove constant and follow pattern of if block
zmariscal Sep 21, 2023
32ef97f
revert trilogy additions in test file
zmariscal Sep 21, 2023
69aced2
when adding trilogy to various places
zmariscal Sep 26, 2023
c95dc75
if value doesn't respond to #empty? we don't want to continue
zmariscal Sep 26, 2023
e5f311e
when running tests we need to ensure AR is aware of Trilogy
zmariscal Sep 26, 2023
3de57ac
add github action steps to resolve database.yml or DATABASE_URL
zmariscal Oct 10, 2023
334b5a8
update github envs appropriately
zmariscal Oct 10, 2023
1bc20ba
target host when executing github action
zmariscal Oct 14, 2023
9311e0d
update workflow and order the args for the mysql command differently
zmariscal Oct 21, 2023
663f12a
Merge branch 'master' into feature--support-trilogy-adapter
zmariscal Nov 3, 2023
f79e913
remove trilogy github action for now
zmariscal Nov 3, 2023
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
2 changes: 1 addition & 1 deletion .github/workflows/mariadb.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
--health-timeout 5s
--health-retries 5
env:
BUNDLE_WITHOUT: "db2 oracle sqlserver sqlite postgresql"
BUNDLE_WITHOUT: "db2 oracle sqlserver sqlite postgresql trilogy"
BUNDLE_JOBS: 4
BUNDLE_PATH: vendor/bundle
steps:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/mysql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
--health-timeout 5s
--health-retries 5
env:
BUNDLE_WITHOUT: "db2 oracle sqlserver sqlite postgresql"
BUNDLE_WITHOUT: "db2 oracle sqlserver sqlite postgresql trilogy"
BUNDLE_JOBS: 4
BUNDLE_PATH: vendor/bundle
steps:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/postgresql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
--health-timeout 5s
--health-retries 5
env:
BUNDLE_WITHOUT: "db2 oracle sqlserver mysql splite"
BUNDLE_WITHOUT: "db2 oracle sqlserver mysql sqlite trilogy"
BUNDLE_JOBS: 4
BUNDLE_PATH: vendor/bundle
steps:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/sqlite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
ruby: [2.7, "3.0", 3.1, 3.2]
runs-on: ${{matrix.os}}
env:
BUNDLE_WITHOUT: "db2 oracle sqlserver postgresql mysql"
BUNDLE_WITHOUT: "db2 oracle sqlserver postgresql mysql trilogy"
BUNDLE_JOBS: 4
BUNDLE_PATH: vendor/bundle
steps:
Expand Down
45 changes: 45 additions & 0 deletions .github/workflows/trilogy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: trilogy

on: [push, pull_request]

jobs:
trilogy_test:
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest]
ruby: [2.7, 3.0, 3.1, 3.2]
runs-on: ${{matrix.os}}
services:
mysql:
image: mysql
ports:
- 3306:3306
env:
MYSQL_ROOT_PASSWORD: root
MYSQL_USER: github
MYSQL_PASSWORD: github
MYSQL_DATABASE: composite_primary_keys_unittest
options: >-
--health-cmd "mysqladmin ping -h localhost"
--health-interval 10s
--health-timeout 5s
--health-retries 5
env:
BUNDLE_WITHOUT: "db2 oracle sqlserver sqlite postgresql trilogy"
BUNDLE_JOBS: 4
BUNDLE_PATH: vendor/bundle
steps:
- uses: actions/checkout@v3
- uses: ruby/setup-ruby@v1
with:
ruby-version: ${{matrix.ruby}}
bundler-cache: true
- name: Setup database config
run: cp test/connections/databases.ci.yml test/connections/databases.yml
- name: Prepare databases
run: bundle exec rake trilogy:rebuild_database
env:
MYSQL_ROOT_PASSWORD: root
- name: Trilogy test
run: bundle exec rake trilogy:test
5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ group :sqlserver do
gem 'activerecord-sqlserver-adapter'
end

group :trilogy do
gem 'trilogy'
gem 'activerecord-trilogy-adapter'
end

group :test do
gem 'minitest'
end
Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Dir.glob('tasks/**/*.rake').each do |rake_file|
end

# Set up test tasks for each supported connection adapter
%w(mysql sqlite oracle oracle_enhanced postgresql ibm_db sqlserver).each do |adapter|
%w(mysql sqlite oracle oracle_enhanced postgresql ibm_db sqlserver trilogy).each do |adapter|
namespace adapter do
desc "Run tests using the #{adapter} adapter"
task "test" do
Expand Down
4 changes: 3 additions & 1 deletion lib/composite_primary_keys.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@
require 'active_record/connection_adapters/abstract/database_statements'
require 'active_record/connection_adapters/abstract_adapter'
require 'active_record/connection_adapters/postgresql/database_statements'
require "active_record/connection_adapters/trilogy_adapter"
require "active_record/connection_adapters/trilogy/database_statements"
zmariscal marked this conversation as resolved.
Show resolved Hide resolved

require 'active_record/relation/where_clause'
require 'active_record/table_metadata'
Expand Down Expand Up @@ -114,4 +116,4 @@

require_relative 'composite_primary_keys/arel/to_sql'
require_relative 'composite_primary_keys/arel/sqlserver'
require_relative 'composite_primary_keys/table_metadata'
require_relative 'composite_primary_keys/table_metadata'
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ def insert(arel, name = nil, pk = nil, id_value = nil, sequence_name = nil, bind
value = exec_insert(sql, name, binds, pk, sequence_name)

return id_value if id_value

if pk.is_a?(Array) && !value.empty?
if pk.is_a?(Array) && !active_record_result(value).empty?
# This is a CPK model and the query result is not empty. Thus we can figure out the new ids for each
# auto incremented field
pk.map {|key| value.first[key]}
Expand All @@ -32,6 +31,16 @@ def insert(arel, name = nil, pk = nil, id_value = nil, sequence_name = nil, bind
last_inserted_id(value)
end
end

private

def active_record_result(value)
return value if @config[:adapter] != "trilogy"

# Create an AR Result so we can respond to #empty?
# since Trilogy:Result doesn't respond to it.
ActiveRecord::Result.new(value.fields, value.to_a)
zmariscal marked this conversation as resolved.
Show resolved Hide resolved
end
end
end
end
4 changes: 4 additions & 0 deletions lib/composite_primary_keys/relation.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module ActiveRecord
class Relation
AR_CONNECTION_TRILOGY_ADAPTER = ActiveRecord::ConnectionAdapters::TrilogyAdapter
zmariscal marked this conversation as resolved.
Show resolved Hide resolved

alias :initialize_without_cpk :initialize
def initialize(klass, table: klass.arel_table, predicate_builder: klass.predicate_builder, values: {})
initialize_without_cpk(klass, table: table, predicate_builder: predicate_builder, values: values)
Expand Down Expand Up @@ -102,6 +104,8 @@ def cpk_subquery(stmt)
# database adapter to decide how to proceed.
if defined?(ActiveRecord::ConnectionAdapters::Mysql2Adapter) && connection.is_a?(ActiveRecord::ConnectionAdapters::Mysql2Adapter)
cpk_mysql_subquery(stmt)
elsif defined?(AR_CONNECTION_TRILOGY_ADAPTER) && connection.is_a?(AR_CONNECTION_TRILOGY_ADAPTER)
cpk_mysql_subquery(stmt)
zmariscal marked this conversation as resolved.
Show resolved Hide resolved
elsif defined?(ActiveRecord::ConnectionAdapters::SQLServerAdapter) && connection.is_a?(ActiveRecord::ConnectionAdapters::SQLServerAdapter)
cpk_exists_subquery(stmt)
else
Expand Down
4 changes: 2 additions & 2 deletions scripts/console.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env ruby

#
# if run as script, load the file as library while starting irb
# if run as script, load the file as library while starting irb
#
if __FILE__ == $0
irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
Expand All @@ -12,7 +12,7 @@
#
# check if the given adapter is supported (default: mysql)
#
adapters = %w[mysql sqlite oracle oracle_enhanced postgresql ibm_db]
adapters = %w[mysql sqlite oracle oracle_enhanced postgresql ibm_db trilogy]
adapter = ENV['ADAPTER'] || 'mysql'
unless adapters.include? adapter
puts "Usage: #{__FILE__} <adapter>"
Expand Down
23 changes: 23 additions & 0 deletions tasks/databases/trilogy.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace :trilogy do
task :setup do
require 'bundler'
Bundler.require(:default, :trilogy)
end

task :create_database => :setup do
Rake::Task["mysql:create_database"].invoke
end

desc 'Build the MySQL test database'
task :build_database => [:create_database] do
Rake::Task["mysql:build_database"].invoke
end

desc 'Drop the MySQL test database'
task :drop_database => :setup do
Rake::Task["mysql:drop_database"].invoke
end

desc 'Rebuild the MySQL test database'
task :rebuild_database => [:drop_database, :build_database]
end
5 changes: 5 additions & 0 deletions test/abstract_unit.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
spec_name = ENV['ADAPTER'] || 'postgresql'
require 'bundler'
require 'minitest/autorun'
if spec_name == "trilogy"
require "activerecord-trilogy-adapter"
require "trilogy_adapter/connection"
ActiveRecord::Base.extend TrilogyAdapter::Connection
end
zmariscal marked this conversation as resolved.
Show resolved Hide resolved

Bundler.setup(:default, spec_name.to_sym)
Bundler.require(:default, spec_name.to_sym)
Expand Down
10 changes: 10 additions & 0 deletions test/connections/databases.ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,13 @@ sqlite:
adapter: sqlite3
database: <%= File.join(project_root, 'db', 'composite_primary_keys_unittest.sqlite') %>

trilogy:
adapter: mysql2
zmariscal marked this conversation as resolved.
Show resolved Hide resolved
username: github
password: github
host: 127.0.0.1
port: 3306
encoding: utf8mb4
charset: utf8mb4
collation: utf8mb4_bin
database: composite_primary_keys_unittest
Loading