@@ -18,40 +18,61 @@ def run
18
18
19
19
# Remove pg_stat_statements extension (its not relevant to the code)
20
20
dump . gsub! ( /^CREATE EXTENSION IF NOT EXISTS pg_stat_statements.*/ , '' )
21
- dump . gsub! ( /^COMMENT ON EXTENSION pg_stat_statements.*/ , '' )
22
21
dump . gsub! ( /^-- Name: (EXTENSION )?pg_stat_statements;.*/ , '' )
23
22
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
+
24
30
# Remove useless, version-specific parts of comments
25
31
dump . gsub! ( /^-- (.*); Schema: (public|-); Owner: -.*/ , '-- \1' )
26
32
27
33
# Remove useless comment lines
28
34
dump . gsub! ( /^--$/ , '' )
29
35
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
+ ###
33
47
34
48
# Reduce noise for id fields by making them SERIAL instead of integer+sequence stuff
35
49
#
36
50
# 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\) ;$/ , '' )
45
60
dump . gsub! ( /^-- Name: (\w +\s +)?id; Type: DEFAULT$/ , '' )
46
61
dump . gsub! ( /^-- .*_id_seq; Type: SEQUENCE.*/ , '' )
47
62
dump . gsub! ( /^-- Name: (\w +\s +)?\w +_pkey; Type: CONSTRAINT$/ , '' )
48
63
49
64
# 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
51
66
inherited_tables = dump . scan ( inherited_tables_regexp ) . map ( &:first )
52
67
dump . gsub! ( inherited_tables_regexp , '' )
53
68
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 , '' )
55
76
end
56
77
57
78
# Remove whitespace between schema migration INSERTS to make editing easier
0 commit comments