Skip to content

Commit

Permalink
Merge pull request #28154 from aripollak/remove-comments-from-structu…
Browse files Browse the repository at this point in the history
…re-sql

Drop comments from structure.sql in postgresql
  • Loading branch information
rafaelfranca committed Mar 17, 2017
2 parents 7b02f86 + cfe67a6 commit 6f7e7aa
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
7 changes: 7 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
* Remove comments from structure.sql when using postgresql adapter to avoid
version-specific parts of the file.

Fixes #28153.

*Ari Pollak*

* Add `:default` option to `belongs_to`.

Use it to specify that an association should be initialized with a particular
Expand Down
20 changes: 20 additions & 0 deletions activerecord/lib/active_record/tasks/postgresql_database_tasks.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
require "tempfile"

module ActiveRecord
module Tasks # :nodoc:
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
Expand Down Expand Up @@ -65,6 +68,7 @@ def structure_dump(filename, extra_flags)
end
args << configuration["database"]
run_cmd("pg_dump", args, "dumping")
remove_sql_header_comments(filename)
File.open(filename, "a") { |f| f << "SET search_path TO #{connection.schema_search_path};\n\n" }
end

Expand Down Expand Up @@ -110,6 +114,22 @@ def run_cmd_error(cmd, args, action)
msg << "Please check the output above for any errors and make sure that `#{cmd}` is installed in your PATH and has proper permissions.\n\n"
msg
end

def remove_sql_header_comments(filename)
removing_comments = true
tempfile = Tempfile.open("uncommented_structure.sql")
begin
File.foreach(filename) do |line|
unless removing_comments && (line.start_with?(SQL_COMMENT_BEGIN) || line.blank?)
tempfile << line
removing_comments = false
end
end
ensure
tempfile.close
end
FileUtils.mv(tempfile.path, filename)
end
end
end
end
19 changes: 16 additions & 3 deletions activerecord/test/cases/tasks/postgresql_rake_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -217,17 +217,21 @@ def test_db_retrieves_collation

class PostgreSQLStructureDumpTest < ActiveRecord::TestCase
def setup
@connection = stub(structure_dump: true)
@connection = stub(schema_search_path: nil, structure_dump: true)
@configuration = {
"adapter" => "postgresql",
"database" => "my-app-db"
}
@filename = "awesome-file.sql"
@filename = "/tmp/awesome-file.sql"
FileUtils.touch(@filename)

ActiveRecord::Base.stubs(:connection).returns(@connection)
ActiveRecord::Base.stubs(:establish_connection).returns(true)
Kernel.stubs(:system)
File.stubs(:open)
end

def teardown
FileUtils.rm_f(@filename)
end

def test_structure_dump
Expand All @@ -236,6 +240,15 @@ def test_structure_dump
ActiveRecord::Tasks::DatabaseTasks.structure_dump(@configuration, @filename)
end

def test_structure_dump_header_comments_removed
Kernel.stubs(:system).returns(true)
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 [" statement \n", "-- lower comment\n"], File.readlines(@filename).first(2)
end

def test_structure_dump_with_extra_flags
expected_command = ["pg_dump", "-s", "-x", "-O", "-f", @filename, "--noop", "my-app-db"]

Expand Down

0 comments on commit 6f7e7aa

Please sign in to comment.