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

Drop comments from structure.sql in postgresql

Fixes #28153.
This commit is contained in:
Ari Pollak 2017-02-24 12:44:11 -05:00
parent aa56dc235a
commit df050484cd
3 changed files with 38 additions and 3 deletions

View file

@ -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*
* Deprecate using `#quoted_id` in quoting.
*Ryuta Kamizono*

View file

@ -1,3 +1,5 @@
require "tempfile"
module ActiveRecord
module Tasks # :nodoc:
class PostgreSQLDatabaseTasks # :nodoc:
@ -65,6 +67,7 @@ module ActiveRecord
end
args << configuration["database"]
run_cmd("pg_dump", args, "dumping")
remove_sql_comments(filename)
File.open(filename, "a") { |f| f << "SET search_path TO #{connection.schema_search_path};\n\n" }
end
@ -110,6 +113,18 @@ module ActiveRecord
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_comments(filename)
tempfile = Tempfile.open('uncommented_structure.sql')
begin
File.foreach(filename) do |line|
tempfile << line unless line.start_with?('--')
end
ensure
tempfile.close
end
FileUtils.mv(tempfile.path, filename)
end
end
end
end

View file

@ -217,17 +217,21 @@ if current_adapter?(:PostgreSQLAdapter)
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
@ -236,6 +240,15 @@ if current_adapter?(:PostgreSQLAdapter)
ActiveRecord::Tasks::DatabaseTasks.structure_dump(@configuration, @filename)
end
def test_structure_dump_comments_removed
Kernel.stubs(:system).returns(true)
File.write(@filename, "-- comment\n not comment\n")
ActiveRecord::Tasks::DatabaseTasks.structure_dump(@configuration, @filename)
assert_equal " not comment\n", File.readlines(@filename).first
end
def test_structure_dump_with_extra_flags
expected_command = ["pg_dump", "-s", "-x", "-O", "-f", @filename, "--noop", "my-app-db"]