|
| 1 | +module ActiveRecordCleanDbStructure |
| 2 | + class CleanDump |
| 3 | + attr_reader :dump |
| 4 | + def initialize(dump) |
| 5 | + @dump = dump |
| 6 | + end |
| 7 | + |
| 8 | + def run |
| 9 | + # Remove trailing whitespace |
| 10 | + dump.gsub!(/[ \t]+$/, '') |
| 11 | + dump.gsub!(/\A\n/, '') |
| 12 | + dump.gsub!(/\n\n\z/, "\n") |
| 13 | + |
| 14 | + # Remove version-specific output |
| 15 | + dump.gsub!(/^-- Dumped.*/, '') |
| 16 | + dump.gsub!(/^SET row_security = off;$/, '') # 9.5 |
| 17 | + dump.gsub!(/^SET idle_in_transaction_session_timeout = 0;$/, '') # 9.6 |
| 18 | + |
| 19 | + # Remove pg_stat_statements extension (its not relevant to the code) |
| 20 | + dump.gsub!(/^CREATE EXTENSION IF NOT EXISTS pg_stat_statements.*/, '') |
| 21 | + dump.gsub!(/^COMMENT ON EXTENSION pg_stat_statements.*/, '') |
| 22 | + dump.gsub!(/^-- Name: (EXTENSION )?pg_stat_statements;.*/, '') |
| 23 | + |
| 24 | + # Remove useless, version-specific parts of comments |
| 25 | + dump.gsub!(/^-- (.*); Schema: (public|-); Owner: -.*/, '-- \1') |
| 26 | + |
| 27 | + # Remove useless comment lines |
| 28 | + dump.gsub!(/^--$/, '') |
| 29 | + |
| 30 | + # Reduce noise for id fields by making them SERIAL instead of integer+sequence stuff |
| 31 | + # |
| 32 | + # This is a bit optimistic, but works as long as you don't have an id field thats not a sequence/uuid |
| 33 | + dump.gsub!(/^ id integer NOT NULL,$/, ' id SERIAL PRIMARY KEY,') |
| 34 | + dump.gsub!(/^ id bigint NOT NULL,$/, ' id BIGSERIAL PRIMARY KEY,') |
| 35 | + dump.gsub!(/^ id uuid DEFAULT uuid_generate_v4\(\) NOT NULL,$/, ' id uuid DEFAULT uuid_generate_v4() PRIMARY KEY,') |
| 36 | + dump.gsub!(/^CREATE SEQUENCE \w+_id_seq\s+START WITH 1\s+INCREMENT BY 1\s+NO MINVALUE\s+NO MAXVALUE\s+CACHE 1;$/, '') |
| 37 | + dump.gsub!(/^ALTER SEQUENCE \w+_id_seq OWNED BY .*;$/, '') |
| 38 | + dump.gsub!(/^ALTER TABLE ONLY \w+ ALTER COLUMN id SET DEFAULT nextval\('\w+_id_seq'::regclass\);$/, '') |
| 39 | + dump.gsub!(/^ALTER TABLE ONLY \w+\s+ADD CONSTRAINT \w+_pkey PRIMARY KEY \(id\);$/, '') |
| 40 | + dump.gsub!(/^-- Name: [\w_]+ id; Type: DEFAULT$/, '') |
| 41 | + dump.gsub!(/^-- .*_id_seq; Type: SEQUENCE.*/, '') |
| 42 | + dump.gsub!(/^-- Name: [\w_]+ \w+_pkey; Type: CONSTRAINT$/, '') |
| 43 | + |
| 44 | + # Remove inherited tables |
| 45 | + inherited_tables_regexp = /-- Name: ([\w_]+); Type: TABLE\n\n[^;]+?INHERITS \([\w_]+\);/m |
| 46 | + inherited_tables = dump.scan(inherited_tables_regexp).map(&:first) |
| 47 | + dump.gsub!(inherited_tables_regexp, '') |
| 48 | + inherited_tables.each do |inherited_table| |
| 49 | + dump.gsub!(/ALTER TABLE ONLY #{inherited_table}[^;]+;/, '') |
| 50 | + end |
| 51 | + |
| 52 | + # Remove whitespace between schema migration INSERTS to make editing easier |
| 53 | + dump.gsub!(/^(INSERT INTO schema_migrations .*)\n\n/, "\\1\n") |
| 54 | + |
| 55 | + # Reduce 2+ lines of whitespace to one line of whitespace |
| 56 | + dump.gsub!(/\n{2,}/m, "\n\n") |
| 57 | + end |
| 58 | + end |
| 59 | +end |
0 commit comments