From 50a53a05c4e47457046c5815b4329f9dd97df45b Mon Sep 17 00:00:00 2001 From: Michael Fairchild Date: Sun, 10 Nov 2019 12:47:07 -0800 Subject: [PATCH] Add config option to ignore_inherited_tables Inherited tables are necessary in some cases, such as when using chronomodel --- .../clean_dump.rb | 28 ++++++++++--------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/lib/activerecord-clean-db-structure/clean_dump.rb b/lib/activerecord-clean-db-structure/clean_dump.rb index 8abb6c5..d328be9 100644 --- a/lib/activerecord-clean-db-structure/clean_dump.rb +++ b/lib/activerecord-clean-db-structure/clean_dump.rb @@ -54,21 +54,23 @@ def run dump.gsub!(/^-- .*_id_seq; Type: SEQUENCE.*/, '') dump.gsub!(/^-- Name: (\w+\s+)?\w+_pkey; Type: CONSTRAINT$/, '') end - - # Remove inherited tables - inherited_tables_regexp = /-- Name: ([\w_\.]+); Type: TABLE\n\n[^;]+?INHERITS \([\w_\.]+\);/m - inherited_tables = dump.scan(inherited_tables_regexp).map(&:first) - dump.gsub!(inherited_tables_regexp, '') - inherited_tables.each do |inherited_table| - dump.gsub!(/ALTER TABLE ONLY ([\w_]+\.)?#{inherited_table}[^;]+;/, '') - - index_regexp = /CREATE INDEX ([\w_]+) ON ([\w_]+\.)?#{inherited_table}[^;]+;/m - dump.scan(index_regexp).map(&:first).each do |inherited_table_index| - dump.gsub!("-- Name: #{inherited_table_index}; Type: INDEX", '') + + unless options[:ignore_inherited_tables] == true + # Remove inherited tables + inherited_tables_regexp = /-- Name: ([\w_\.]+); Type: TABLE\n\n[^;]+?INHERITS \([\w_\.]+\);/m + inherited_tables = dump.scan(inherited_tables_regexp).map(&:first) + dump.gsub!(inherited_tables_regexp, '') + inherited_tables.each do |inherited_table| + dump.gsub!(/ALTER TABLE ONLY ([\w_]+\.)?#{inherited_table}[^;]+;/, '') + + index_regexp = /CREATE INDEX ([\w_]+) ON ([\w_]+\.)?#{inherited_table}[^;]+;/m + dump.scan(index_regexp).map(&:first).each do |inherited_table_index| + dump.gsub!("-- Name: #{inherited_table_index}; Type: INDEX", '') + end + dump.gsub!(index_regexp, '') end - dump.gsub!(index_regexp, '') end - + # Remove partitioned tables partitioned_tables = []