Skip to content

Commit

Permalink
[SchemaDumper] Handle edge cases for metric tables
Browse files Browse the repository at this point in the history
(AKA:  The "Martin H. Factor" commit)

So, when doing something outside of Rails to, say, backup a metrics
table before testing a migration by doing something like:

> cop[y] metric_rollups_01 to xmetric_rollups_01 as a backup

Then this will get picked up by the `METRIC_ROLLUP_TABLE_REGEXP`, and
work though the other methods (`determine_table_parent`) and cause the
following error:

    ** Execute db:schema:dump
    rake aborted!
    NoMethodError: undefined method `[]' for nil:NilClass
    manageiq-schema/lib/manageiq/schema/schema_dumper.rb:100:in `determine_table_parent'
    manageiq-schema/lib/manageiq/schema/schema_dumper.rb:91:in `track_miq_metric_table_inheritance'
    manageiq-schema/lib/manageiq/schema/schema_dumper.rb:16:in `table'

This does another check to handle these cases.  This means said table
can't be used with the Rails models, but that should be fine since this
table is definitely outside of the Rails managed tables.
  • Loading branch information
NickLaMuro committed Sep 29, 2020
1 parent 6e7d2e7 commit 02007f7
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/manageiq/schema/schema_dumper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ def triggers(stream)

def track_miq_metric_table_inheritance(table)
return unless table.match?(METRIC_ROLLUP_TABLE_REGEXP)
return unless (inherit_from = determine_table_parent(table))

inherit_from = determine_table_parent(table)
inherited_metrics_tables << [table, inherit_from]
end

Expand All @@ -97,12 +97,16 @@ def inherited_metrics_tables
end

def determine_table_parent(table_name)
@connection.execute(<<-SQL).first["parent_table"]
table = @connection.execute(<<-SQL)
SELECT pg_class.relname AS parent_table
FROM pg_catalog.pg_inherits
JOIN pg_class ON pg_class.oid = pg_inherits.inhparent
WHERE inhrelid = '#{table_name}'::regclass
SQL

return if table.first.nil?

table.first["parent_table"]
end

def column_spec_for_primary_key(column)
Expand Down

0 comments on commit 02007f7

Please sign in to comment.