@@ -128,17 +128,29 @@ def clean_options
128128 dump . gsub! ( /^(INSERT INTO schema_migrations .*)\n \n / , "\\ 1\n " )
129129 end
130130
131- if options [ :indexes_after_tables ] == true
132- # Extract indexes, remove comments and place them just after the respective tables
131+ if options [ :meta_tables_after_main ] == true
132+ # Extract indexes
133133 indexes =
134134 dump
135135 . scan ( /^CREATE.+INDEX.+ON.+\n / )
136136 . group_by { |line | line . scan ( /\b \w +\. \w +\b / ) . first }
137137 . transform_values ( &:join )
138138
139+ # Extract deferrable constraints
140+ deferrable_constraints =
141+ dump
142+ . scan ( /^ALTER.+TABLE.+ONLY.+\n .+DEFERRABLE.+\n / )
143+ . group_by { |line | line . scan ( /\b \w +\. \w +\b / ) . first }
144+ . transform_values ( &:join )
145+
146+ # Remove indexes and deferrable constraints from the dump
139147 dump . gsub! ( /^CREATE( UNIQUE)? INDEX \w + ON .+\n +/ , '' )
140148 dump . gsub! ( /^-- Name: \w +; Type: INDEX; Schema: \w +\n +/ , '' )
141- indexes . each do |table , indexes_for_table |
149+
150+ dump . gsub! ( /^-- Name.+\n \n ALTER.+TABLE.+ONLY.+\n .+DEFERRABLE.+\n +/ , '' )
151+
152+ indexes_and_constraints = indexes . merge ( deferrable_constraints ) { |_ , v1 , v2 | v1 + v2 }
153+ indexes_and_constraints . each do |table , indexes_for_table |
142154 dump . gsub! ( /^(CREATE TABLE #{ table } \b (:?[^;\n ]*\n )+\) ;*\n (?:.*);*)/ ) { $1 + "\n \n " + indexes_for_table }
143155 end
144156 end
0 commit comments