Skip to content

Commit

Permalink
Fix some issues from a recent PR
Browse files Browse the repository at this point in the history
  • Loading branch information
bkroeker committed Oct 22, 2024
1 parent e25f184 commit 2949601
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 56 deletions.
2 changes: 1 addition & 1 deletion lib/temporal_tables/temporal_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def remove_temporal_table(table_name)
return unless table_exists?(temporal_name(table_name))

drop_temporal_triggers table_name
drop_table temporal_name(table_name)
drop_table_without_temporal temporal_name(table_name)
end

def drop_table(table_name, **options)
Expand Down
12 changes: 2 additions & 10 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,9 @@
READ_DATABASE_CONFIG_LOCATION = 'spec/internal/config/database.ci.yml'
WRITE_DATABASE_CONFIG_LOCATION = 'spec/internal/config/database.yml'

def adapter_name
if Gemika::Env.gem?('mysql2')
'mysql'
else
'postgresql'
end
end

def database_config_from_gems(file_location)
config = YAML.load_file(file_location)
data = config.slice(adapter_name)
data = config.slice(TemporalTables::DatabaseHelper.adapter_name)
{ Rails.env.to_s => data }
end

Expand All @@ -31,7 +23,7 @@ def database_config_from_gems(file_location)
database_config_from_gems(READ_DATABASE_CONFIG_LOCATION).to_yaml
)

Rails.env = adapter_name
Rails.env = TemporalTables::DatabaseHelper.adapter_name
database = Gemika::Database.new
database.connect

Expand Down
43 changes: 0 additions & 43 deletions spec/support/database.rb

This file was deleted.

43 changes: 43 additions & 0 deletions spec/support/database_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# frozen_string_literal: true

module TemporalTables
module DatabaseHelper
def self.adapter_name
if Gemika::Env.gem?('pg')
'postgresql'
elsif Gemika::Env.gem?('mysql2')
'mysql'
else
raise 'Cannot determine adapter'
end
end

def self.function_exists?(name)
case adapter_name
when 'postgresql'
begin
ActiveRecord::Base.connection.execute("select(pg_get_functiondef('#{name}'::regprocedure))").present?
rescue ActiveRecord::StatementInvalid
false
end
when 'mysql' then raise NotImplementedError
else raise "Unknown adapter #{adapter_name}"
end
end

def self.trigger_exists?(name) # rubocop:disable Metrics/MethodLength
case adapter_name
when 'postgresql'
ActiveRecord::Base.connection.execute(
"select (pg_get_triggerdef(oid)) FROM pg_trigger WHERE tgname = '#{name}'"
).first.present?
when 'mysql'
ActiveRecord::Base.connection.execute(
'SHOW TRIGGERS FROM temporal_tables_test'
).find { |row| row.first == name }.present?
else
raise "Unknown adapter #{adapter_name}"
end
end
end
end
4 changes: 2 additions & 2 deletions spec/temporal_tables/temporal_adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
describe TemporalTables::TemporalAdapter do
describe '#remove_temporal_table' do
it 'correctly removes history table, functions and triggers' do
skip 'mysql has no functions' if adapter_name == 'mysql'
skip 'mysql has no functions' if TemporalTables::DatabaseHelper.adapter_name == 'mysql'

expect do
ActiveRecord::Schema.define { remove_temporal_table :people }
Expand All @@ -19,7 +19,7 @@
end

it 'correctly removes history table and triggers' do
skip 'other adapters than mysql have functions, too' if adapter_name != 'mysql'
skip 'other adapters than mysql have functions, too' if TemporalTables::DatabaseHelper.adapter_name != 'mysql'

expect do
ActiveRecord::Schema.define { remove_temporal_table :people }
Expand Down

0 comments on commit 2949601

Please sign in to comment.