Class: ActiveRecordCleanDbStructure::CleanDump

Inherits:
Object
  • Object
show all
Defined in:
lib/activerecord-clean-db-structure/clean_dump.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dump) ⇒ CleanDump

Returns a new instance of CleanDump.



4
5
6
# File 'lib/activerecord-clean-db-structure/clean_dump.rb', line 4

def initialize(dump)
  @dump = dump
end

Instance Attribute Details

#dumpObject (readonly)

Returns the value of attribute dump.



3
4
5
# File 'lib/activerecord-clean-db-structure/clean_dump.rb', line 3

def dump
  @dump
end

Instance Method Details

#runObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/activerecord-clean-db-structure/clean_dump.rb', line 8

def run
  # Remove trailing whitespace
  dump.gsub!(/[ \t]+$/, '')
  dump.gsub!(/\A\n/, '')
  dump.gsub!(/\n\n\z/, "\n")

  # Remove version-specific output
  dump.gsub!(/^-- Dumped.*/, '')
  dump.gsub!(/^SET row_security = off;$/, '') # 9.5
  dump.gsub!(/^SET idle_in_transaction_session_timeout = 0;$/, '') # 9.6

  # Remove pg_stat_statements extension (its not relevant to the code)
  dump.gsub!(/^CREATE EXTENSION IF NOT EXISTS pg_stat_statements.*/, '')
  dump.gsub!(/^COMMENT ON EXTENSION pg_stat_statements.*/, '')
  dump.gsub!(/^-- Name: (EXTENSION )?pg_stat_statements;.*/, '')

  # Remove useless, version-specific parts of comments
  dump.gsub!(/^-- (.*); Schema: (public|-); Owner: -.*/, '-- \1')

  # Remove useless comment lines
  dump.gsub!(/^--$/, '')

  # Reduce noise for id fields by making them SERIAL instead of integer+sequence stuff
  #
  # This is a bit optimistic, but works as long as you don't have an id field thats not a sequence/uuid
  dump.gsub!(/^    id integer NOT NULL(,)?$/, '    id SERIAL PRIMARY KEY\1')
  dump.gsub!(/^    id bigint NOT NULL(,)?$/, '    id BIGSERIAL PRIMARY KEY\1')
  dump.gsub!(/^    id uuid DEFAULT uuid_generate_v4\(\) NOT NULL(,)?$/, '    id uuid DEFAULT uuid_generate_v4() PRIMARY KEY\1')
  dump.gsub!(/^    id uuid DEFAULT gen_random_uuid\(\) NOT NULL(,)?$/, '    id uuid DEFAULT gen_random_uuid() PRIMARY KEY\1')
  dump.gsub!(/^CREATE SEQUENCE \w+_id_seq\s+START WITH 1\s+INCREMENT BY 1\s+NO MINVALUE\s+NO MAXVALUE\s+CACHE 1;$/, '')
  dump.gsub!(/^ALTER SEQUENCE \w+_id_seq OWNED BY .*;$/, '')
  dump.gsub!(/^ALTER TABLE ONLY \w+ ALTER COLUMN id SET DEFAULT nextval\('\w+_id_seq'::regclass\);$/, '')
  dump.gsub!(/^ALTER TABLE ONLY \w+\s+ADD CONSTRAINT \w+_pkey PRIMARY KEY \(id\);$/, '')
  dump.gsub!(/^-- Name: (\w+\s+)?id; Type: DEFAULT$/, '')
  dump.gsub!(/^-- .*_id_seq; Type: SEQUENCE.*/, '')
  dump.gsub!(/^-- Name: (\w+\s+)?\w+_pkey; Type: CONSTRAINT$/, '')

  # 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 #{inherited_table}[^;]+;/, '')
  end

  # Remove whitespace between schema migration INSERTS to make editing easier
  dump.gsub!(/^(INSERT INTO schema_migrations .*)\n\n/, "\\1\n")

  # Reduce 2+ lines of whitespace to one line of whitespace
  dump.gsub!(/\n{2,}/m, "\n\n")
end