diff --git a/spec/basic_history_spec.rb b/spec/basic_history_spec.rb index cb4217d..ee80871 100644 --- a/spec/basic_history_spec.rb +++ b/spec/basic_history_spec.rb @@ -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] } 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..a174597 --- /dev/null +++ b/spec/support/database_helper.rb @@ -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 diff --git a/spec/temporal_tables/temporal_adapter_spec.rb b/spec/temporal_tables/temporal_adapter_spec.rb index 33bd76b..c41d8db 100644 --- a/spec/temporal_tables/temporal_adapter_spec.rb +++ b/spec/temporal_tables/temporal_adapter_spec.rb @@ -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