Skip to content
This repository was archived by the owner on Sep 9, 2021. It is now read-only.

Commit fdd3035

Browse files
committed
Added drop_inheritance schema migration helper
1 parent b8905ca commit fdd3035

File tree

6 files changed

+93
-54
lines changed

6 files changed

+93
-54
lines changed

lib/mti.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
require 'mti/railtie' if defined?(Rails::Railtie)
33

44
module MTI
5-
class UnsupportedError < StandardError; end
5+
class TableNotInheritedError < StandardError; end
66
end
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
module MTI
2+
module ActiveRecord
3+
module Inheritable
4+
def drop_inheritance(table_name, parent_name)
5+
unless inherited?(table_name, parent_name)
6+
raise MTI::TableNotInheritedError, "#{table_name} not inherited from #{parent_name}"
7+
end
8+
9+
execute(drop_inheritance_query(table_name, parent_name))
10+
end
11+
12+
private
13+
14+
def inherited?(table_name, parent_name)
15+
table_parents(table_name).any? { |x| x['relname'] == parent_name.to_s }
16+
end
17+
18+
def table_parents(table_name)
19+
execute(select_parents_query(table_name))
20+
end
21+
22+
def drop_inheritance_query(table_name, parent_name)
23+
"ALTER TABLE #{table_name} NO INHERIT #{parent_name};"
24+
end
25+
26+
def select_parents_query(table_name)
27+
<<-SQL
28+
SELECT pg_namespace.nspname, pg_class.relname
29+
FROM pg_catalog.pg_inherits
30+
INNER JOIN pg_catalog.pg_class ON (pg_inherits.inhparent = pg_class.oid)
31+
INNER JOIN pg_catalog.pg_namespace ON (pg_class.relnamespace = pg_namespace.oid)
32+
WHERE inhrelid = '#{table_name}'::regclass
33+
SQL
34+
end
35+
end
36+
end
37+
end
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
require 'mti/active_record/inheritable'
2+
3+
module MTI
4+
module ActiveRecord
5+
module SchemaStatements
6+
include MTI::ActiveRecord::Inheritable
7+
8+
def create_table(table_name, options = {})
9+
inherited_table = options.delete(:inherited_from)
10+
11+
prepare_options(inherited_table, options)
12+
13+
table_name = prepare_table_name(table_name, options)
14+
15+
super(table_name, options)
16+
end
17+
18+
private
19+
20+
def prepare_options(inherited_table, options)
21+
remove_pk_option(options)
22+
add_inheritance_query(inherited_table, options)
23+
end
24+
25+
def remove_pk_option(options)
26+
return unless options[:inherited_from]
27+
28+
options[:id] = false
29+
options.delete(:primary_key)
30+
end
31+
32+
def add_inheritance_query(inherited_table, options)
33+
return unless inherited_table
34+
35+
options[:options] = build_inherits_option(inherited_table)
36+
end
37+
38+
def prepare_table_name(table_name, options)
39+
schema = options.delete(:schema)
40+
41+
return table_name unless schema
42+
43+
"#{schema}.#{table_name}"
44+
end
45+
46+
def build_inherits_option(inherited_table)
47+
"INHERITS (#{inherited_table})"
48+
end
49+
end
50+
end
51+
end

lib/mti/connection_adapter/postgresql/schema_statements.rb

Lines changed: 0 additions & 49 deletions
This file was deleted.

lib/mti/railtie.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
require 'mti/connection_adapter/postgresql/schema_statements'
1+
require 'mti/active_record/schema_statements'
22

33
module MTI
44
class Railtie < Rails::Railtie
5-
initializer 'mti' do |_app|
5+
initializer 'mti' do
66
ActiveSupport.on_load(:active_record) do
7-
adapter_klass = ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
7+
adapter_klass = ::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
88

9-
adapter_klass.prepend(ConnectionAdapter::PostgreSQL::SchemaStatements)
9+
adapter_klass.prepend(MTI::ActiveRecord::SchemaStatements)
1010
end
1111
end
1212
end

lib/mti/sequel/schema_statements.rb

Whitespace-only changes.

0 commit comments

Comments
 (0)