Skip to content
This repository
Browse code

Use 'public' schema path when connecting to 'postgres' database. [#170

…state:resolved]

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
  • Loading branch information...
commit 3fee2378edd45188e41a7d14d4ca0a88280b541e 1 parent f498f22
Tarmo Tänav authored May 13, 2008 lifo committed May 13, 2008

Showing 1 changed file with 2 additions and 2 deletions. Show diff stats Hide diff stats

  1. 4  railties/lib/tasks/databases.rake
4  railties/lib/tasks/databases.rake
@@ -45,7 +45,7 @@ namespace :db do
45 45
       when 'postgresql'
46 46
         @encoding = config[:encoding] || ENV['CHARSET'] || 'utf8'
47 47
         begin
48  
-          ActiveRecord::Base.establish_connection(config.merge('database' => 'postgres'))
  48
+          ActiveRecord::Base.establish_connection(config.merge('database' => 'postgres', 'schema_search_path' => 'public'))
49 49
           ActiveRecord::Base.connection.create_database(config['database'], config.merge('encoding' => @encoding))
50 50
           ActiveRecord::Base.establish_connection(config)
51 51
         rescue
@@ -368,7 +368,7 @@ def drop_database(config)
368 368
   when /^sqlite/
369 369
     FileUtils.rm(File.join(RAILS_ROOT, config['database']))
370 370
   when 'postgresql'
371  
-    ActiveRecord::Base.establish_connection(config.merge('database' => 'postgres'))
  371
+    ActiveRecord::Base.establish_connection(config.merge('database' => 'postgres', 'schema_search_path' => 'public'))
372 372
     ActiveRecord::Base.connection.drop_database config['database']
373 373
   end
374 374
 end

10 notes on commit 3fee237

Blake Benthall

I'm trying to connect to a postgres schema other than "public" within my test environment, but it seems ActiveRecord is overwriting the schema_search_path to "public" due to this commit - inconvenient. I have a very complex postgres database which requires schemas for adequate testing.

Link to original bug: https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/170-postgresql-update-rake-tasks-to-use-full-settings-from-database-yml-bugfix

Tarmo Tänav

These connections are only used for creating and dropping a database and the connections aren't even made to your configured database but to the 'postgres' database which is expected to have the 'public' schema. https://rails.lighthouseapp.com/projects/8994/tickets/170

Nicola Bernardini

What if someone has to deal with a legacy postgres db which actually has some schema other than public? I could submit a patch to the database task to figure the schemas needed to be re-created, but I am not sure whether there isn't a very good reason to do so that I am not aware of (since it should be a pretty easy and useful thing to do). If someone can clarify why this feature is not already available, I am willing to put some work into it.

Nicola Bernardini

Is there any chance that this issue gets looked up and re-opened? Otherwise rake test:units is simply not usable.

Aaron Patterson
Owner

@nicb would changing the search path based on environment variable be acceptable? I would accept a patch for that. :-)

Nicola Bernardini

I would definitely think so, since there is already a schema_search_path key in the config/database.yml, that key could indeed be re-used in the rake task instead of flattening it to public. I will give it a try as soon as I get a chance to do it and report back (and provide a patch if I get to somewhere functional).

Tarmo Tänav

@nicb Sorry I couldn't be of more help here. Wondering about your legacy database though, why does it not have a 'public' schema in the 'postgres' database, was the database created before postgresql implemented schemas, or was another schema name intentionally used? Any reason you can't just create an empty 'public' schema for the 'postgres' database? (in case it isn't clear, the schema is just needed to connect to postgresql, it has nothing to do with the specific database for rails that is being created or deleted using that connection).

Nicola Bernardini

The database I'm working with has already been in production for several years. They had an 'old implementation' of the database in the public schema, and they did a new implementation using a different schema name. That sounds silly perhaps, but 'they' is somebody I don't know so I can't even ask why they did it this way. Since I cannot change the structure of the production database, I have two options:
1. use a different schema (using only public) for the testing suite
2. try to fix this issue
I could easily do (1) but I don't think it is the correct way to go. If the ruby pg adapter supports schemas and the rails implementation supports schemas (which it does, since you can define them in configuration), then we should be able to run tests on the database configured for tests as it is for production. Also because otherwise these tests are missing one important point which could crop up in production.

I am starting to think how to fix it. The super-easiest fix of them all is to set an environment variable like:

   sspath = ENV['PG_SCHEMA_SEARCH_PATH'] || 'public'
   ....
   ActiveRecord::Base.establish_connection(config.merge('database' => 'postgres', 'schema_search_path' => sspath))

I don't know if that is enough to actually do the job properly. I am proposing it here to you before checking it to bar probable naiveties on my side.

Aaron Patterson
Owner

The other option is that we could have a schema section in the configuration file. Rails could use the regular settings (the current defaults), or override if a schema configuration is present. Having a schema configuration would probably be cleaner, but I'm fine with an environment variable too.

Tarmo Tänav

@nicb could you try if just removing the 'schema_search_path' from the establish_connection line make this work for you? In shouldn't really matter what the schema search path is while running "create database" and "drop database" queries.

Please sign in to comment.
Something went wrong with that request. Please try again.