Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #8659 (new defect)

Opened 1 year ago

Last modified 4 months ago

[PATCH] fix postgresql_adapter index dumping with multiple schemas

Reported by: jean.helou Assigned to: bitsweat
Priority: normal Milestone: 2.x
Component: ActiveRecord Version: edge
Severity: normal Keywords: postgresql schema index
Cc:

Description

I recently started using postgres more and using schemas in postgres to keep the number of databases under control.

When using schemas in a database and using rails, if two tables in two different schemas have the same name , the output of rake db:schema:dump is incorrect

To reproduce (the following steps assume you have a database railstest with public, dev and test schemas, you may want to edit the database.yml differently if its not the case):

$>rails test
$>cd test
$>vi database.yml
development:
 adapter: postgresql
 database: railstest
 schema_search_path: dev
 encoding: utf8
 username: railstest
 password: test
 host: localhost
test:
 adapter: postgresql
 database: railstest
 schema_search_path: test
 encoding: utf8
 username: railstest
 password: test
 host: localhost
production:
 adapter: postgresql
 database: railstest
 encoding: utf8
 username: railstest
 password: test
 host: localhost
$>rake db:sessions:create
$>rake db:migrate
$>rake db:schema:dump

With the help of someone on IRC we found that the schema generated a this point is incorrect when using schemas and not when using different database (look at the add_index lines for this schema based dump):

# This file is autogenerated. Instead of editing this file, please use the
# migrations feature of ActiveRecord to incrementally modify your database, and
# then regenerate this schema definition.

ActiveRecord::Schema.define(:version => 1) do

 create_table "sessions", :force => true do |t|
   t.column "session_id", :string
   t.column "data",       :text
   t.column "updated_at", :datetime
 end

 add_index "sessions", ["session_id", "session_id"], :name => "index_sessions_on_session_id"
 add_index "sessions", ["updated_at", "updated_at"], :name => "index_sessions_on_updated_at"

end

I tracked this down to postgresql_adapter.rb in the indexes method, which does not differenciate between schemas when it queries the postgres system tables to get the indexes names and columns for a given table.

This ticket contains a failing test and a small patch to the indexes method of the postgresql adapter wich makes the test pass.

Attachments

postgresql_adapter.patch (1.1 kB) - added by jean.helou on 06/15/07 09:57:48.
Patch for postresql_adapter.rb and schema_test_postgresql.rb
index_dump_in_postgresql_adapter_and_matching_test_case.patch (3.0 kB) - added by jean.helou on 06/15/07 09:58:44.
Patch for postresql_adapter.rb and schema_test_postgresql.rb
updated_index_dump_in_postgresql_adapter_and_tests_against_edge.diff (4.4 kB) - added by sveiss on 02/12/08 23:55:30.
updated_index_dump_in_postgresql_adapter_and_tests_against_2-0-stable.diff (4.3 kB) - added by sveiss on 02/12/08 23:55:55.

Change History

06/15/07 09:57:48 changed by jean.helou

  • attachment postgresql_adapter.patch added.

Patch for postresql_adapter.rb and schema_test_postgresql.rb

06/15/07 09:58:44 changed by jean.helou

  • attachment index_dump_in_postgresql_adapter_and_matching_test_case.patch added.

Patch for postresql_adapter.rb and schema_test_postgresql.rb

06/15/07 10:00:27 changed by jean.helou

Please ignore the file named postgresql_adapter.patch I was to hasty in posting that one, and I don't see anything obvious allowing me to remove it.

06/15/07 15:10:35 changed by bitsweat

  • owner changed from core to bitsweat.
  • milestone set to 1.x.

Thanks Jean. Have you tested this with 7.x?

06/16/07 16:42:57 changed by jean.helou

I don't have posrgresql 7.x installed, so I didn't test it. A quick check in the postgresql reference documentation for 7.4 and 7.3 shows that 7.3 doesn't support schemas at all. 7.4 supports schemas,and contains the pg_table table which is referenced in my query with the same structure as that in 8.2 as far as the query is concerned.

What would be the best way to fix this ? adding a test to the indexes method on whether the schema search path exists or not and if not keep the previous request ?

Also I intend to submit another patch on the rake tasks with a conditional to avoid dropping the whole database and dropping the schema instead.

01/21/08 08:37:37 changed by Piet.Hadermann

+1 works fine under 8.2

02/12/08 23:54:38 changed by sveiss

The above patch doesn't seem to work when two indexes in different schemas have the same name, but index different columns. I've updated the patch to work in that situation, included additional tests to cover that, and updated the patch for current edge and 2-0-stable.

I don't know if Rails is still targeting PG 7.4 (looking at postgresql_adapter.rb, it seems 7.3 compatibility is already gone), but I'm not able to run the current AR test suite under 7.4. The tests in schema_test_postgresql.rb (the only test file this patch touches) do pass under 7.4, though.

Otherwise, this has been tested under PG 8.3 on Mac OS X 10.5.2, and PG 8.1 on Debian Etch. I'm attaching patches against edge and 2-0-stable -- the only difference is the location of schema_test_postgresql.rb.

02/12/08 23:55:30 changed by sveiss

  • attachment updated_index_dump_in_postgresql_adapter_and_tests_against_edge.diff added.

02/12/08 23:55:55 changed by sveiss

  • attachment updated_index_dump_in_postgresql_adapter_and_tests_against_2-0-stable.diff added.

02/22/08 05:15:29 changed by sveiss

With [8922] in, all tests now pass with this patch under PG 7.4.

03/03/08 06:56:34 changed by nzkoz

(In [8979]) Make the schema dumper respect the schema settings in database.yml. References #8659 [sveiss]

03/03/08 06:57:38 changed by nzkoz

I've applied this to trunk. Assuming everything looks good we can look at merging back to 2.0.x