From 424fe95ca42d35d692458c54d3c9f7b674b7c95d Mon Sep 17 00:00:00 2001 From: Cezary Baginski Date: Mon, 30 Mar 2009 02:39:34 +0200 Subject: [PATCH] Added postgresql socket detection and help --- railties/configs/databases/postgresql.yml | 22 ++++++++++++++++ .../generators/applications/app/app_generator.rb | 26 +++++++++++++++++-- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/railties/configs/databases/postgresql.yml b/railties/configs/databases/postgresql.yml index f600e05..84171d3 100644 --- a/railties/configs/databases/postgresql.yml +++ b/railties/configs/databases/postgresql.yml @@ -15,6 +15,13 @@ development: pool: 5 username: <%= app_name %> password: +<% if socket_dir -%> + + # A postgresql domain socket was detected and to use it the host + # parameter should point to the directory with the socket. + host: <%= socket_dir %> + port: <%= port %> +<% else -%> # Connect on a TCP socket. Omitted by default since the client uses a # domain socket that doesn't need configuration. Windows does not have @@ -22,6 +29,13 @@ development: #host: localhost #port: 5432 + # NOTE: to use a domain socket path other than the default used by + # libpq, set use the host parameter for the directory with the socket, + # e.g: + # + # host: /tmp +<% end -%> + # Schema search path. The server defaults to $user,public #schema_search_path: myapp,sharedapp,public @@ -41,6 +55,10 @@ test: pool: 5 username: <%= app_name %> password: +<% if socket_dir -%> + host: <%= socket_dir %> + port: <%= port %> +<% end -%> production: adapter: postgresql @@ -49,3 +67,7 @@ production: pool: 5 username: <%= app_name %> password: +<% if socket_dir -%> + host: <%= socket_dir %> + port: <%= port %> +<% end -%> diff --git a/railties/lib/rails_generator/generators/applications/app/app_generator.rb b/railties/lib/rails_generator/generators/applications/app/app_generator.rb index 2c31d89..5766515 100644 --- a/railties/lib/rails_generator/generators/applications/app/app_generator.rb +++ b/railties/lib/rails_generator/generators/applications/app/app_generator.rb @@ -168,8 +168,7 @@ class AppGenerator < Rails::Generator::Base def create_database_configuration_file(m) m.template "configs/databases/#{options[:db]}.yml", "config/database.yml", :assigns => { - :app_name => @app_name, - :socket => options[:db] == "mysql" ? mysql_socket_location : nil } + :app_name => @app_name}.merge(detect_additonal_options(options)) end def create_routes_file(m) @@ -255,4 +254,25 @@ class AppGenerator < Rails::Generator::Base "/opt/lampp/var/mysql/mysql.sock" # xampp for linux ].find { |f| File.exist?(f) } unless RUBY_PLATFORM =~ /(:?mswin|mingw)/ end -end \ No newline at end of file + + DEFAULT_PGSQL_PORT = 5432 + + def detect_additonal_options(db) + case options[:db] + when "mysql" + { :socket => mysql_socket_location } + when "postgresql" + s = postgresql_socket_location + { :socket_dir => s, :port => s ? DEFAULT_PGSQL_PORT : nil } + else + {} + end + end + + def postgresql_socket_location + [ + "/tmp", # default/ubuntu + "/var/run/postgresql", # broken libpq-dev? + ].find { |d| File.exist?(File.join(d,".s.PGSQL.#{DEFAULT_PGSQL_PORT}")) } unless RUBY_PLATFORM =~ /(:?mswin|mingw)/ + end +end -- 1.5.6.3