This project is archived and is in readonly mode.

#6277 new
Drew Chandler

results of db:test:clone_structure do not get committed in postgres if the user has autocommit turned off

Reported by Drew Chandler | January 11th, 2011 @ 07:05 AM

On Postgres when ActiveRecord::Base.schema_format is set to :sql and the user has turned off autocommit(most likely because they have "\set AUTOCOMMIT off" in their .psqlrc) the results of the rake task db:test:clone_structure will not be committed. This leaves the user with an empty test db.

By adding --single-transaction to the psql command, the contents of the structure sql file gets wrapped in a BEGIN and COMMIT. This will ensure the rake task will commit even if autocommit is disabled. This flag was introduced in 8.2 and is in all the current stable versions. Here is a link to the docs on it:

I have a patch to add the flag but did not add any tests. As far as I can tell these rake tasks are untested. If this is not the case I will be more than happy to augment the patch with tests.

Comments and changes to this ticket

  • Juan Manuel Cuello

    Juan Manuel Cuello April 9th, 2011 @ 07:22 PM

    I tested the patch and it works OK with PostgreSQL 9.0.3.

    But if --single-transaction was introduced in 8.2, I think would be better to check the server version before using the flag, maybe defining a new method in postgresql_adapter.rb to see if --single-transaction flag is supported, similar to 'supports_insert_with_returning?' method.

    Something like:

    def supports_single_transaction?
      postgresql_version >= 80200

    And then you can do:

    single_transaction = '--single-transaction' if ActiveRecord::Base.connection.supports_single_transaction?
    `psql -U "#{abcs["test"]["username"]}" #{single_transaction} -f #{Rails.root}/db/#{Rails.env}_structure.sql {abcs["test"]["database"]} #{abcs["test"]["template"]}`
  • Juan Manuel Cuello

    Juan Manuel Cuello April 18th, 2011 @ 02:20 AM

    But according to [35b2715456999662cc34390e258962738aaa8dc7], only pg >= 8.2 is supported, so there is no need to check the version.

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href=""></a>

People watching this ticket