Skip to content

Commit 5826e71

Browse files
committed
Fix regular expressions to support schema qualification changes in 10.3
1 parent a6e494a commit 5826e71

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

lib/activerecord-clean-db-structure/clean_dump.rb

+9-9
Original file line numberDiff line numberDiff line change
@@ -38,24 +38,24 @@ def run
3838
# This is a bit optimistic, but works as long as you don't have an id field thats not a sequence/uuid
3939
dump.gsub!(/^ id integer NOT NULL(,)?$/, ' id SERIAL PRIMARY KEY\1')
4040
dump.gsub!(/^ id bigint NOT NULL(,)?$/, ' id BIGSERIAL PRIMARY KEY\1')
41-
dump.gsub!(/^ id uuid DEFAULT uuid_generate_v4\(\) NOT NULL(,)?$/, ' id uuid DEFAULT uuid_generate_v4() PRIMARY KEY\1')
42-
dump.gsub!(/^ id uuid DEFAULT gen_random_uuid\(\) NOT NULL(,)?$/, ' id uuid DEFAULT gen_random_uuid() PRIMARY KEY\1')
43-
dump.gsub!(/^CREATE SEQUENCE \w+_id_seq\s+(AS integer\s+)?START WITH 1\s+INCREMENT BY 1\s+NO MINVALUE\s+NO MAXVALUE\s+CACHE 1;$/, '')
44-
dump.gsub!(/^ALTER SEQUENCE \w+_id_seq OWNED BY .*;$/, '')
45-
dump.gsub!(/^ALTER TABLE ONLY \w+ ALTER COLUMN id SET DEFAULT nextval\('\w+_id_seq'::regclass\);$/, '')
46-
dump.gsub!(/^ALTER TABLE ONLY \w+\s+ADD CONSTRAINT \w+_pkey PRIMARY KEY \(id\);$/, '')
41+
dump.gsub!(/^ id uuid DEFAULT (public\.)?uuid_generate_v4\(\) NOT NULL(,)?$/, ' id uuid DEFAULT \1uuid_generate_v4() PRIMARY KEY\2')
42+
dump.gsub!(/^ id uuid DEFAULT (public\.)?gen_random_uuid\(\) NOT NULL(,)?$/, ' id uuid DEFAULT \1gen_random_uuid() PRIMARY KEY\2')
43+
dump.gsub!(/^CREATE SEQUENCE [\w\.]+_id_seq\s+(AS integer\s+)?START WITH 1\s+INCREMENT BY 1\s+NO MINVALUE\s+NO MAXVALUE\s+CACHE 1;$/, '')
44+
dump.gsub!(/^ALTER SEQUENCE [\w\.]+_id_seq OWNED BY .*;$/, '')
45+
dump.gsub!(/^ALTER TABLE ONLY [\w\.]+ ALTER COLUMN id SET DEFAULT nextval\('[\w\.]+_id_seq'::regclass\);$/, '')
46+
dump.gsub!(/^ALTER TABLE ONLY [\w\.]+\s+ADD CONSTRAINT [\w\.]+_pkey PRIMARY KEY \(id\);$/, '')
4747
dump.gsub!(/^-- Name: (\w+\s+)?id; Type: DEFAULT$/, '')
4848
dump.gsub!(/^-- .*_id_seq; Type: SEQUENCE.*/, '')
4949
dump.gsub!(/^-- Name: (\w+\s+)?\w+_pkey; Type: CONSTRAINT$/, '')
5050

5151
# Remove inherited tables
52-
inherited_tables_regexp = /-- Name: ([\w_]+); Type: TABLE\n\n[^;]+?INHERITS \([\w_]+\);/m
52+
inherited_tables_regexp = /-- Name: ([\w_\.]+); Type: TABLE\n\n[^;]+?INHERITS \([\w_\.]+\);/m
5353
inherited_tables = dump.scan(inherited_tables_regexp).map(&:first)
5454
dump.gsub!(inherited_tables_regexp, '')
5555
inherited_tables.each do |inherited_table|
56-
dump.gsub!(/ALTER TABLE ONLY #{inherited_table}[^;]+;/, '')
56+
dump.gsub!(/ALTER TABLE ONLY (public\.)?#{inherited_table}[^;]+;/, '')
5757

58-
index_regexp = /CREATE INDEX ([\w_]+) ON #{inherited_table}[^;]+;/m
58+
index_regexp = /CREATE INDEX ([\w_]+) ON (public\.)?#{inherited_table}[^;]+;/m
5959
dump.scan(index_regexp).map(&:first).each do |inherited_table_index|
6060
dump.gsub!("-- Name: #{inherited_table_index}; Type: INDEX", '')
6161
end

0 commit comments

Comments
 (0)