From 2949601189afd76a2f54ea7cd86e6f80c429ec67 Mon Sep 17 00:00:00 2001 From: Brent Kroeker Date: Tue, 22 Oct 2024 14:57:57 -0500 Subject: [PATCH] Fix some issues from a recent PR --- lib/temporal_tables/temporal_adapter.rb | 2 +- spec/spec_helper.rb | 12 +----- spec/support/database.rb | 43 ------------------- spec/support/database_helper.rb | 43 +++++++++++++++++++ spec/temporal_tables/temporal_adapter_spec.rb | 4 +- 5 files changed, 48 insertions(+), 56 deletions(-) delete mode 100644 spec/support/database.rb create mode 100644 spec/support/database_helper.rb diff --git a/lib/temporal_tables/temporal_adapter.rb b/lib/temporal_tables/temporal_adapter.rb index cb9f45b..cfbc16b 100644 --- a/lib/temporal_tables/temporal_adapter.rb +++ b/lib/temporal_tables/temporal_adapter.rb @@ -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) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index b13682b..c4f331c 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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 @@ -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 diff --git a/spec/support/database.rb b/spec/support/database.rb deleted file mode 100644 index e87786e..0000000 --- a/spec/support/database.rb +++ /dev/null @@ -1,43 +0,0 @@ -# frozen_string_literal: true - -def adapter_name - if Gemika::Env.gem?('pg') - 'postgresql' - elsif Gemika::Env.gem?('mysql2') - 'mysql' - else - raise 'Cannot determine adapter' - end -end - -def table_exists?(name) - ActiveRecord::Base.connection.table_exists?(name) -end - -def 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 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 diff --git a/spec/support/database_helper.rb b/spec/support/database_helper.rb new file mode 100644 index 0000000..9063282 --- /dev/null +++ b/spec/support/database_helper.rb @@ -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 diff --git a/spec/temporal_tables/temporal_adapter_spec.rb b/spec/temporal_tables/temporal_adapter_spec.rb index 33bd76b..57cabe6 100644 --- a/spec/temporal_tables/temporal_adapter_spec.rb +++ b/spec/temporal_tables/temporal_adapter_spec.rb @@ -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 } @@ -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 }