-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- correctly remove temporal table - remove functions, too - add spec and refactor support
- Loading branch information
Showing
4 changed files
with
76 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,43 @@ | ||
# frozen_string_literal: true | ||
|
||
module TemporalTables | ||
module DatabaseAdapter | ||
def self.adapter_name | ||
if Gemika::Env.gem?('pg') | ||
'postgresql' | ||
elsif Gemika::Env.gem?('mysql2') | ||
'mysql' | ||
end | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
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' | ||
|
||
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 | ||
|
||
it 'correctly removes history table and triggers' do | ||
skip 'other adapters than mysql have functions, too' if 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 | ||
end | ||
end |