From cd0fb08a955a0328ecec6e026c4e9b44cac640b8 Mon Sep 17 00:00:00 2001 From: Nicke van Oorschot Date: Wed, 13 Nov 2019 16:08:11 +0100 Subject: [PATCH 1/5] Order table CONSTRAINTs and place them behind the columns --- .../clean_dump.rb | 49 +++++-------------- 1 file changed, 11 insertions(+), 38 deletions(-) diff --git a/lib/activerecord-clean-db-structure/clean_dump.rb b/lib/activerecord-clean-db-structure/clean_dump.rb index 8abb6c5..fb0bad8 100644 --- a/lib/activerecord-clean-db-structure/clean_dump.rb +++ b/lib/activerecord-clean-db-structure/clean_dump.rb @@ -129,50 +129,23 @@ def run dump.gsub!(/\n{2,}/m, "\n\n") if options[:order_column_definitions] == true - dump.replace(order_column_definitions(dump)) + order_column_definitions end end - def order_column_definitions(source) - result = [] - - parse_column_name = ->(line) { line.match(/^ "?([^" ]+)/)[1] } - with_column_separator = ->(line) { line.sub(/,?\n$/, ",\n") } - without_column_separator = ->(line) { line.sub(/,\n$/, "\n") } - - inside_table = false - columns = [] - - source.each_line do |source_line| - if source_line.start_with?("CREATE TABLE") - inside_table = true - columns = [] - result << source_line - elsif source_line.start_with?(");") - if inside_table - inside_table = false - columns.sort_by!(&:first) - - columns[0..-2].each do |_, line| - result << with_column_separator[line] - end - - result << without_column_separator[columns.last[1]] - end - - result << source_line - elsif inside_table - columns << [parse_column_name[source_line], source_line] - else - result << source_line - end - end + private - result.join + # Orders the columns definitions alphabetically + # - keeps the columns at the top and places the constraints at the bottom. + def order_column_definitions + dump.gsub!(/^(?CREATE TABLE .+?\(\n)(?.+?)(?=\n\);$)/m) do + [ + $~[:table], + $~[:columns].split(",\n").sort.partition { |column| !column.match?(/\A *CONSTRAINT/) }.flatten.join(",\n") + ].join + end end - private - # Cleanup of schema_migrations values to prevent merge conflicts: # - sorts all values chronological # - places the comma's in front of each value (except for the first) From 3ec038c4f839eb399823d443933b4c8538864cd2 Mon Sep 17 00:00:00 2001 From: Nicke van Oorschot Date: Wed, 13 Nov 2019 16:08:31 +0100 Subject: [PATCH 2/5] End the file with a single end-of-line character --- lib/activerecord-clean-db-structure/clean_dump.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/activerecord-clean-db-structure/clean_dump.rb b/lib/activerecord-clean-db-structure/clean_dump.rb index fb0bad8..d8a0468 100644 --- a/lib/activerecord-clean-db-structure/clean_dump.rb +++ b/lib/activerecord-clean-db-structure/clean_dump.rb @@ -127,6 +127,8 @@ def run # Reduce 2+ lines of whitespace to one line of whitespace dump.gsub!(/\n{2,}/m, "\n\n") + # End the file with a single end-of-line character + dump.sub!(/\n*\z/m, "\n") if options[:order_column_definitions] == true order_column_definitions From 69512d6e630a2d973042f1440056171811990f76 Mon Sep 17 00:00:00 2001 From: Nicke van Oorschot Date: Thu, 9 Jan 2020 22:28:08 +0100 Subject: [PATCH 3/5] Ignore any double qoutes when sorting the columns --- lib/activerecord-clean-db-structure/clean_dump.rb | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/activerecord-clean-db-structure/clean_dump.rb b/lib/activerecord-clean-db-structure/clean_dump.rb index d8a0468..ec4539b 100644 --- a/lib/activerecord-clean-db-structure/clean_dump.rb +++ b/lib/activerecord-clean-db-structure/clean_dump.rb @@ -138,13 +138,19 @@ def run private # Orders the columns definitions alphabetically + # - ignores quotes which surround column names that are equal to reserved PostgreSQL names. # - keeps the columns at the top and places the constraints at the bottom. def order_column_definitions dump.gsub!(/^(?
CREATE TABLE .+?\(\n)(?.+?)(?=\n\);$)/m) do - [ - $~[:table], - $~[:columns].split(",\n").sort.partition { |column| !column.match?(/\A *CONSTRAINT/) }.flatten.join(",\n") - ].join + columns = + $~[:columns] + .split(",\n") + .sort_by { |column| column[/[^ "]+/] } + .partition { |column| !column.match?(/\A *CONSTRAINT/) } + .flatten + .join(",\n") + + [$~[:table], columns].join end end From 805e3391629907bbfeea8a1244a0f145680275c1 Mon Sep 17 00:00:00 2001 From: Nicke van Oorschot Date: Wed, 22 Jan 2020 12:05:01 +0100 Subject: [PATCH 4/5] Capture the table before using other regexps --- lib/activerecord-clean-db-structure/clean_dump.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/activerecord-clean-db-structure/clean_dump.rb b/lib/activerecord-clean-db-structure/clean_dump.rb index ec4539b..1970d9d 100644 --- a/lib/activerecord-clean-db-structure/clean_dump.rb +++ b/lib/activerecord-clean-db-structure/clean_dump.rb @@ -142,6 +142,7 @@ def run # - keeps the columns at the top and places the constraints at the bottom. def order_column_definitions dump.gsub!(/^(?
CREATE TABLE .+?\(\n)(?.+?)(?=\n\);$)/m) do + table = $~[:table] columns = $~[:columns] .split(",\n") @@ -150,7 +151,7 @@ def order_column_definitions .flatten .join(",\n") - [$~[:table], columns].join + [table, columns].join end end From b934f71aa5bff18a979655d972b33440d52b9394 Mon Sep 17 00:00:00 2001 From: Nicke van Oorschot Date: Tue, 12 May 2020 16:42:48 +0200 Subject: [PATCH 5/5] Fix sorting constraints --- lib/activerecord-clean-db-structure/clean_dump.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/activerecord-clean-db-structure/clean_dump.rb b/lib/activerecord-clean-db-structure/clean_dump.rb index 1970d9d..be05125 100644 --- a/lib/activerecord-clean-db-structure/clean_dump.rb +++ b/lib/activerecord-clean-db-structure/clean_dump.rb @@ -146,7 +146,7 @@ def order_column_definitions columns = $~[:columns] .split(",\n") - .sort_by { |column| column[/[^ "]+/] } + .sort_by { |column| column.delete('"') } .partition { |column| !column.match?(/\A *CONSTRAINT/) } .flatten .join(",\n")