1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Only remove comments before the first statement

This commit is contained in:
Ari Pollak 2017-02-24 16:54:55 -05:00
parent df050484cd
commit cfe67a63b6
2 changed files with 12 additions and 7 deletions

View file

@ -5,6 +5,7 @@ module ActiveRecord
class PostgreSQLDatabaseTasks # :nodoc:
DEFAULT_ENCODING = ENV["CHARSET"] || "utf8"
ON_ERROR_STOP_1 = "ON_ERROR_STOP=1".freeze
SQL_COMMENT_BEGIN = "--".freeze
delegate :connection, :establish_connection, :clear_active_connections!,
to: ActiveRecord::Base
@ -67,7 +68,7 @@ module ActiveRecord
end
args << configuration["database"]
run_cmd("pg_dump", args, "dumping")
remove_sql_comments(filename)
remove_sql_header_comments(filename)
File.open(filename, "a") { |f| f << "SET search_path TO #{connection.schema_search_path};\n\n" }
end
@ -114,11 +115,15 @@ module ActiveRecord
msg
end
def remove_sql_comments(filename)
tempfile = Tempfile.open('uncommented_structure.sql')
def remove_sql_header_comments(filename)
removing_comments = true
tempfile = Tempfile.open("uncommented_structure.sql")
begin
File.foreach(filename) do |line|
tempfile << line unless line.start_with?('--')
unless removing_comments && (line.start_with?(SQL_COMMENT_BEGIN) || line.blank?)
tempfile << line
removing_comments = false
end
end
ensure
tempfile.close

View file

@ -240,13 +240,13 @@ if current_adapter?(:PostgreSQLAdapter)
ActiveRecord::Tasks::DatabaseTasks.structure_dump(@configuration, @filename)
end
def test_structure_dump_comments_removed
def test_structure_dump_header_comments_removed
Kernel.stubs(:system).returns(true)
File.write(@filename, "-- comment\n not comment\n")
File.write(@filename, "-- header comment\n\n-- more header comment\n statement \n-- lower comment\n")
ActiveRecord::Tasks::DatabaseTasks.structure_dump(@configuration, @filename)
assert_equal " not comment\n", File.readlines(@filename).first
assert_equal [" statement \n", "-- lower comment\n"], File.readlines(@filename).first(2)
end
def test_structure_dump_with_extra_flags