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 d16ceb3
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 68 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
2 changes: 1 addition & 1 deletion spec/basic_history_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@
end
end

if TemporalTables::DatabaseAdapter.adapter_name != 'mysql'
if TemporalTables::DatabaseHelper.adapter_name != 'mysql'
describe 'when changing a creature with an array column' do
let!(:cat) { Cat.create name: 'Mr. Mittens', nicknames: %w[Blacky Kitty] }

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.

47 changes: 47 additions & 0 deletions spec/support/database_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# 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.table_exists?(name)
ActiveRecord::Base.connection.table_exists?(name)
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
26 changes: 13 additions & 13 deletions spec/temporal_tables/temporal_adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,28 @@
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 }
end.to change { table_exists?('people_h') }.from(true).to(false)
.and change { function_exists?('people_ai()') }.from(true).to(false)
.and change { function_exists?('people_au()') }.from(true).to(false)
.and change { function_exists?('people_ad()') }.from(true).to(false)
.and change { trigger_exists?('people_ai') }.from(true).to(false)
.and change { trigger_exists?('people_au') }.from(true).to(false)
.and change { trigger_exists?('people_ad') }.from(true).to(false)
end.to change { TemporalTables::DatabaseHelper.table_exists?('people_h') }.from(true).to(false)
.and change { TemporalTables::DatabaseHelper.function_exists?('people_ai()') }.from(true).to(false)
.and change { TemporalTables::DatabaseHelper.function_exists?('people_au()') }.from(true).to(false)
.and change { TemporalTables::DatabaseHelper.function_exists?('people_ad()') }.from(true).to(false)
.and change { TemporalTables::DatabaseHelper.trigger_exists?('people_ai') }.from(true).to(false)
.and change { TemporalTables::DatabaseHelper.trigger_exists?('people_au') }.from(true).to(false)
.and change { TemporalTables::DatabaseHelper.trigger_exists?('people_ad') }.from(true).to(false)
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 }
end.to change { table_exists?('people_h') }.from(true).to(false)
.and change { trigger_exists?('people_ai') }.from(true).to(false)
.and change { trigger_exists?('people_au') }.from(true).to(false)
.and change { trigger_exists?('people_ad') }.from(true).to(false)
end.to change { TemporalTables::DatabaseHelper.table_exists?('people_h') }.from(true).to(false)
.and change { TemporalTables::DatabaseHelper.trigger_exists?('people_ai') }.from(true).to(false)
.and change { TemporalTables::DatabaseHelper.trigger_exists?('people_au') }.from(true).to(false)
.and change { TemporalTables::DatabaseHelper.trigger_exists?('people_ad') }.from(true).to(false)
end
end
end

0 comments on commit d16ceb3

Please sign in to comment.