@@ -30,8 +30,26 @@ def run
3030 # Reduce noise for id fields by making them SERIAL instead of integer+sequence stuff
3131 #
3232 # 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! ( /^CREATE TABLE (\w +) \( \n id integer NOT NULL,$/ , "CREATE TABLE \\ 1 (\n id SERIAL PRIMARY KEY," )
34- dump . gsub! ( /^CREATE TABLE (\w +) \( \n id bigint NOT NULL,$/ , "CREATE TABLE \\ 1 (\n id BIGSERIAL PRIMARY KEY," )
33+ is_table = false , count_open_brackets = 0 , count_close_brackets = 0
34+ @dump = dump . lines . map do |line |
35+ is_table = true if line =~ /CREATE TABLE (\w +) \( /
36+
37+ count_open_brackets += line . count ( '(' )
38+ count_close_brackets += line . count ( ')' )
39+
40+ is_table = false if is_table && count_open_brackets == count_close_brackets
41+
42+ if !is_table # optimization speed
43+ line
44+ elsif line =~ /^ id integer NOT NULL/
45+ line . sub ( 'id integer NOT NULL' , 'id SERIAL PRIMARY KEY' )
46+ elsif line =~ /^ id bigint NOT NULL/
47+ line . sub ( 'id bigint NOT NULL' , 'id BIGSERIAL PRIMARY KEY' )
48+ else
49+ line
50+ end
51+ end . join
52+
3553 dump . gsub! ( /^ id uuid DEFAULT uuid_generate_v4\( \) NOT NULL,$/ , ' id uuid DEFAULT uuid_generate_v4() PRIMARY KEY,' )
3654 dump . gsub! ( /^CREATE SEQUENCE \w +_id_seq\s +START WITH 1\s +INCREMENT BY 1\s +NO MINVALUE\s +NO MAXVALUE\s +CACHE 1;$/ , '' )
3755 dump . gsub! ( /^ALTER SEQUENCE \w +_id_seq OWNED BY .*;$/ , '' )
0 commit comments