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

PostgreSQL: Fix db:structure:load silent failure on SQL error

The command line flag "-v ON_ERROR_STOP=1" should be used when invoking psql to make sure errors are not suppressed.

Example: psql -v ON_ERROR_STOP=1 -q -f awesome-file.sql my-app-db

Fixes #23818.
This commit is contained in:
Ralin Chimev 2016-05-10 20:44:33 +03:00
parent 932655a4ef
commit 09a90bb6a0
3 changed files with 18 additions and 3 deletions

View file

@ -1,2 +1,16 @@
* PostgreSQL: Fix db:structure:load silent failure on SQL error
The command line flag "-v ON_ERROR_STOP=1" should be used
when invoking psql to make sure errors are not suppressed.
Example:
psql -v ON_ERROR_STOP=1 -q -f awesome-file.sql my-app-db
Fixes #23818.
*Ralin Chimev*
Please check [5-0-stable](https://github.com/rails/rails/blob/5-0-stable/activerecord/CHANGELOG.md) for previous changes. Please check [5-0-stable](https://github.com/rails/rails/blob/5-0-stable/activerecord/CHANGELOG.md) for previous changes.

View file

@ -2,6 +2,7 @@ module ActiveRecord
module Tasks # :nodoc: module Tasks # :nodoc:
class PostgreSQLDatabaseTasks # :nodoc: class PostgreSQLDatabaseTasks # :nodoc:
DEFAULT_ENCODING = ENV['CHARSET'] || 'utf8' DEFAULT_ENCODING = ENV['CHARSET'] || 'utf8'
ON_ERROR_STOP_1 = 'ON_ERROR_STOP=1'.freeze
delegate :connection, :establish_connection, :clear_active_connections!, delegate :connection, :establish_connection, :clear_active_connections!,
to: ActiveRecord::Base to: ActiveRecord::Base
@ -67,7 +68,7 @@ module ActiveRecord
def structure_load(filename) def structure_load(filename)
set_psql_env set_psql_env
args = [ '-q', '-f', filename, configuration['database'] ] args = [ '-v', ON_ERROR_STOP_1, '-q', '-f', filename, configuration['database'] ]
run_cmd('psql', args, 'loading' ) run_cmd('psql', args, 'loading' )
end end

View file

@ -288,14 +288,14 @@ module ActiveRecord
def test_structure_load def test_structure_load
filename = "awesome-file.sql" filename = "awesome-file.sql"
Kernel.expects(:system).with('psql', '-q', '-f', filename, @configuration['database']).returns(true) Kernel.expects(:system).with('psql', '-v', 'ON_ERROR_STOP=1', '-q', '-f', filename, @configuration['database']).returns(true)
ActiveRecord::Tasks::DatabaseTasks.structure_load(@configuration, filename) ActiveRecord::Tasks::DatabaseTasks.structure_load(@configuration, filename)
end end
def test_structure_load_accepts_path_with_spaces def test_structure_load_accepts_path_with_spaces
filename = "awesome file.sql" filename = "awesome file.sql"
Kernel.expects(:system).with('psql', '-q', '-f', filename, @configuration['database']).returns(true) Kernel.expects(:system).with('psql', '-v', 'ON_ERROR_STOP=1', '-q', '-f', filename, @configuration['database']).returns(true)
ActiveRecord::Tasks::DatabaseTasks.structure_load(@configuration, filename) ActiveRecord::Tasks::DatabaseTasks.structure_load(@configuration, filename)
end end