@@ -18,40 +18,61 @@ def run
1818
1919 # Remove pg_stat_statements extension (its not relevant to the code)
2020 dump . gsub! ( /^CREATE EXTENSION IF NOT EXISTS pg_stat_statements.*/ , '' )
21- dump . gsub! ( /^COMMENT ON EXTENSION pg_stat_statements.*/ , '' )
2221 dump . gsub! ( /^-- Name: (EXTENSION )?pg_stat_statements;.*/ , '' )
2322
23+ # Remove pg_buffercache extension (its not relevant to the code)
24+ dump . gsub! ( /^CREATE EXTENSION IF NOT EXISTS pg_buffercache.*/ , '' )
25+ dump . gsub! ( /^-- Name: (EXTENSION )?pg_buffercache;.*/ , '' )
26+
27+ # Remove comments on extensions, they create problems if the extension is owned by another user
28+ dump . gsub! ( /^COMMENT ON EXTENSION .*/ , '' )
29+
2430 # Remove useless, version-specific parts of comments
2531 dump . gsub! ( /^-- (.*); Schema: (public|-); Owner: -.*/ , '-- \1' )
2632
2733 # Remove useless comment lines
2834 dump . gsub! ( /^--$/ , '' )
2935
30- # Mask user mapping
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' )
36+ # remove comments
37+ dump . gsub! ( /^-- Name.+ Type: (COMMENT|EXTENSION|INDEX|TABLE|TYPE)/ , '' )
38+
39+ ### NEED FOR TEST DATABASE `rails db:setup`
40+ # remove server and user for FDW
41+ dump . gsub! ( /^-- Name: [\w ]+; Type: SERVER.+?;/m , '' )
42+ dump . gsub! ( /^-- Name: [\w ]+; Type: USER MAPPING.+?;/m , '' )
43+
44+ # FDW table to native table
45+ dump . gsub! ( /^CREATE FOREIGN TABLE (.+?)\n SERVER.+?;/m , "CREATE TABLE \\ 1;" )
46+ ###
3347
3448 # Reduce noise for id fields by making them SERIAL instead of integer+sequence stuff
3549 #
3650 # This is a bit optimistic, but works as long as you don't have an id field thats not a sequence/uuid
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;" )
39-
40- dump . gsub! ( /^ id uuid DEFAULT uuid_generate_v4\( \) NOT NULL,$/ , ' id uuid DEFAULT uuid_generate_v4() PRIMARY KEY,' )
41- dump . gsub! ( /^CREATE SEQUENCE \w +_id_seq\s +START WITH 1\s +INCREMENT BY 1\s +NO MINVALUE\s +NO MAXVALUE\s +CACHE 1;$/ , '' )
42- dump . gsub! ( /^ALTER SEQUENCE \w +_id_seq OWNED BY .*;$/ , '' )
43- dump . gsub! ( /^ALTER TABLE ONLY \w + ALTER COLUMN id SET DEFAULT nextval\( '\w +_id_seq'::regclass\) ;$/ , '' )
44- dump . gsub! ( /^ALTER TABLE ONLY \w +\s +ADD CONSTRAINT \w +_pkey PRIMARY KEY \( id\) ;$/ , '' )
51+ dump . gsub! ( /^CREATE TABLE ([\w .]+) \( \n id integer NOT NULL(.*?);/m , "CREATE TABLE \\ 1 (\n id SERIAL PRIMARY KEY\\ 2;" )
52+ dump . gsub! ( /^CREATE TABLE ([\w .]+) \( \n id bigint NOT NULL(.*?);/m , "CREATE TABLE \\ 1 (\n id BIGSERIAL PRIMARY KEY\\ 2;" )
53+
54+ dump . gsub! ( /^ id uuid DEFAULT (public\. )?uuid_generate_v4\( \) NOT NULL(,)?$/ , ' id uuid DEFAULT \1uuid_generate_v4() PRIMARY KEY\2' )
55+ dump . gsub! ( /^ id uuid DEFAULT (public\. )?gen_random_uuid\( \) NOT NULL(,)?$/ , ' id uuid DEFAULT \1gen_random_uuid() PRIMARY KEY\2' )
56+ 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;$/ , '' )
57+ dump . gsub! ( /^ALTER SEQUENCE [\w \. ]+_id_seq OWNED BY .*;$/ , '' )
58+ dump . gsub! ( /^ALTER TABLE ONLY [\w \. ]+ ALTER COLUMN id SET DEFAULT nextval\( '[\w \. ]+_id_seq'::regclass\) ;$/ , '' )
59+ dump . gsub! ( /^ALTER TABLE ONLY [\w \. ]+\s +ADD CONSTRAINT [\w \. ]+_pkey PRIMARY KEY \( id\) ;$/ , '' )
4560 dump . gsub! ( /^-- Name: (\w +\s +)?id; Type: DEFAULT$/ , '' )
4661 dump . gsub! ( /^-- .*_id_seq; Type: SEQUENCE.*/ , '' )
4762 dump . gsub! ( /^-- Name: (\w +\s +)?\w +_pkey; Type: CONSTRAINT$/ , '' )
4863
4964 # Remove inherited tables
50- inherited_tables_regexp = /-- Name: ([\w _]+); Type: TABLE\n \n [^;]+?INHERITS \( [\w _]+\) ;/m
65+ inherited_tables_regexp = /-- Name: ([\w _\. ]+); Type: TABLE\n \n [^;]+?INHERITS \( [\w _\. ]+\) ;/m
5166 inherited_tables = dump . scan ( inherited_tables_regexp ) . map ( &:first )
5267 dump . gsub! ( inherited_tables_regexp , '' )
5368 inherited_tables . each do |inherited_table |
54- dump . gsub! ( /ALTER TABLE ONLY #{ inherited_table } [^;]+;/ , '' )
69+ dump . gsub! ( /ALTER TABLE ONLY (public\. )?#{ inherited_table } [^;]+;/ , '' )
70+
71+ index_regexp = /CREATE INDEX ([\w _]+) ON (public\. )?#{ inherited_table } [^;]+;/m
72+ dump . scan ( index_regexp ) . map ( &:first ) . each do |inherited_table_index |
73+ dump . gsub! ( "-- Name: #{ inherited_table_index } ; Type: INDEX" , '' )
74+ end
75+ dump . gsub! ( index_regexp , '' )
5576 end
5677
5778 # Remove whitespace between schema migration INSERTS to make editing easier
0 commit comments