Skip to content

Commit c1c5f04

Browse files
committed
refactoring and optimization
1 parent eddd996 commit c1c5f04

File tree

1 file changed

+4
-27
lines changed

1 file changed

+4
-27
lines changed

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

+4-27
Original file line numberDiff line numberDiff line change
@@ -28,37 +28,14 @@ def run
2828
dump.gsub!(/^--$/, '')
2929

3030
# Mask user mapping
31-
dump.gsub!(
32-
/^CREATE USER MAPPING FOR \w+ SERVER (\w+) .*;/m,
33-
'CREATE USER MAPPING FOR some_user SERVER \1;'
34-
)
35-
dump.gsub!(
36-
/^-- Name: USER MAPPING \w+ SERVER (\w+); Type: USER MAPPING/,
37-
'-- Name: USER MAPPING some_user SERVER \1; Type: USER MAPPING'
38-
)
31+
dump.gsub!(/^CREATE USER MAPPING FOR \w+ SERVER (\w+) .*?;/m, 'CREATE USER MAPPING FOR some_user SERVER \1;')
32+
dump.gsub!(/^-- Name: USER MAPPING \w+ SERVER (\w+); Type: USER MAPPING/, '-- Name: USER MAPPING some_user SERVER \1; Type: USER MAPPING')
3933

4034
# Reduce noise for id fields by making them SERIAL instead of integer+sequence stuff
4135
#
4236
# This is a bit optimistic, but works as long as you don't have an id field thats not a sequence/uuid
43-
is_table = false, count_open_brackets = 0, count_close_brackets = 0
44-
@dump = dump.lines.map do |line|
45-
is_table = true if line =~ /CREATE TABLE (\w+) \(/
46-
47-
count_open_brackets += line.count('(')
48-
count_close_brackets += line.count(')')
49-
50-
is_table = false if is_table && count_open_brackets == count_close_brackets
51-
52-
if !is_table # optimization speed
53-
line
54-
elsif line =~ /^ id integer NOT NULL/
55-
line.sub('id integer NOT NULL', 'id SERIAL PRIMARY KEY')
56-
elsif line =~ /^ id bigint NOT NULL/
57-
line.sub('id bigint NOT NULL', 'id BIGSERIAL PRIMARY KEY')
58-
else
59-
line
60-
end
61-
end.join
37+
dump.gsub!(/^CREATE TABLE (\w+) \(\n id integer NOT NULL(.*?);/m, "CREATE TABLE \\1 (\n id SERIAL PRIMARY KEY\\2;")
38+
dump.gsub!(/^CREATE TABLE (\w+) \(\n id bigint NOT NULL(.*?);/m, "CREATE TABLE \\1 (\n id BIGSERIAL PRIMARY KEY\\2;")
6239

6340
dump.gsub!(/^ id uuid DEFAULT uuid_generate_v4\(\) NOT NULL,$/, ' id uuid DEFAULT uuid_generate_v4() PRIMARY KEY,')
6441
dump.gsub!(/^CREATE SEQUENCE \w+_id_seq\s+START WITH 1\s+INCREMENT BY 1\s+NO MINVALUE\s+NO MAXVALUE\s+CACHE 1;$/, '')

0 commit comments

Comments
 (0)