From df050484cd3b5e888b50d2e0554a4077407459a8 Mon Sep 17 00:00:00 2001 From: Ari Pollak Date: Fri, 24 Feb 2017 12:44:11 -0500 Subject: Drop comments from structure.sql in postgresql Fixes #28153. --- activerecord/CHANGELOG.md | 7 +++++++ .../active_record/tasks/postgresql_database_tasks.rb | 15 +++++++++++++++ activerecord/test/cases/tasks/postgresql_rake_test.rb | 19 ++++++++++++++++--- 3 files changed, 38 insertions(+), 3 deletions(-) (limited to 'activerecord') 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"] -- cgit v1.2.3 From cfe67a63b6d1c48fb4ddcb9cf5a303bb47036b5f Mon Sep 17 00:00:00 2001 From: Ari Pollak Date: Fri, 24 Feb 2017 16:54:55 -0500 Subject: Only remove comments before the first statement --- .../lib/active_record/tasks/postgresql_database_tasks.rb | 13 +++++++++---- activerecord/test/cases/tasks/postgresql_rake_test.rb | 6 +++--- 2 files changed, 12 insertions(+), 7 deletions(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/tasks/postgresql_database_tasks.rb b/activerecord/lib/active_record/tasks/postgresql_database_tasks.rb index 83b82506d3..f1af90c1e8 100644 --- a/activerecord/lib/active_record/tasks/postgresql_database_tasks.rb +++ b/activerecord/lib/active_record/tasks/postgresql_database_tasks.rb @@ -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 diff --git a/activerecord/test/cases/tasks/postgresql_rake_test.rb b/activerecord/test/cases/tasks/postgresql_rake_test.rb index a28217c2e6..512388af6b 100644 --- a/activerecord/test/cases/tasks/postgresql_rake_test.rb +++ b/activerecord/test/cases/tasks/postgresql_rake_test.rb @@ -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 -- cgit v1.2.3