aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAri Pollak <ajp@aripollak.com>2017-02-24 12:44:11 -0500
committerAri Pollak <ajp@aripollak.com>2017-02-24 12:46:00 -0500
commitdf050484cd3b5e888b50d2e0554a4077407459a8 (patch)
tree7245a326ea2f49a05ec64d4758531f9884bd4990
parentaa56dc235a341876762f0219738cac46d181fcd3 (diff)
downloadrails-df050484cd3b5e888b50d2e0554a4077407459a8.tar.gz
rails-df050484cd3b5e888b50d2e0554a4077407459a8.tar.bz2
rails-df050484cd3b5e888b50d2e0554a4077407459a8.zip
Drop comments from structure.sql in postgresql
Fixes #28153.
-rw-r--r--activerecord/CHANGELOG.md7
-rw-r--r--activerecord/lib/active_record/tasks/postgresql_database_tasks.rb15
-rw-r--r--activerecord/test/cases/tasks/postgresql_rake_test.rb19
3 files changed, 38 insertions, 3 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index a2566ae5fb..9f42ebd175 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -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*
diff --git a/activerecord/lib/active_record/tasks/postgresql_database_tasks.rb b/activerecord/lib/active_record/tasks/postgresql_database_tasks.rb
index 5155ced0e2..83b82506d3 100644
--- a/activerecord/lib/active_record/tasks/postgresql_database_tasks.rb
+++ b/activerecord/lib/active_record/tasks/postgresql_database_tasks.rb
@@ -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
diff --git a/activerecord/test/cases/tasks/postgresql_rake_test.rb b/activerecord/test/cases/tasks/postgresql_rake_test.rb
index a23100c32a..a28217c2e6 100644
--- a/activerecord/test/cases/tasks/postgresql_rake_test.rb
+++ b/activerecord/test/cases/tasks/postgresql_rake_test.rb
@@ -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"]