JRuby

rake db:create doesn't work on JDBC/PostgreSQL

Details

  • Type: Bug Bug
  • Status: Closed Closed
  • Priority: Minor Minor
  • Resolution: Fixed
  • Affects Version/s: JRuby 1.1.2
  • Fix Version/s: None
  • Component/s: None
  • Labels:
    None
  • Environment:
  • Number of attachments :
    1

Description

Add activerecord-jdbcpostgresql-adapter gem (v 0.8.2) to gems used by NetBeans JRuby 1.1.2 install. This will also upgrade the activerecord-jdbc-adaptor gem to 0.8.2
Create new Rails project (Rails 2.1.0 IIRC), any server. Select JDBC, PostgreSQL, enter username, no password, on db page.
Modify database.yml to use jdbcpostgres adapter
Make sure the database required by the project does not yet exist.
Run rake db:create
It will run w/o visible errors
Check to see if database was created – it won't be.

I debugged this extensively and what it resolved to in my case was this:
In jdbc.rake, around line 33, the code tries to create the database with this line:

ActiveRecord::Base.connection.create_database(config['database'])

It was throwing a no such method exception because the JdbcAdapter instance referenced by ActiveRecord::Base.connection did not define a create_database method.
I also noted that jdbc_postgres.rb does not define this method. When I added a definition of create_database to this file, the rake task succeeded as expected.

rake db:drop has similar issues as I suspect do some of the other jdbc adapters.

Activity

Hide
Peter Williams added a comment -

If interested, here is are the methods I added to jdbc_postgres.rb:

def create_database(name, options = {}) #:nodoc:
execute "CREATE DATABASE #{name} ENCODING='#{options[:charset] || 'utf8'}'"
end

def drop_database(name) #:nodoc:
execute "DROP DATABASE #{name}"
end

The encoding is probably not correct (should use encoding field from database.yml, I'm not sure this does that correctly). There may also be other options required. PostgreSQL does not appear to support the "IF EXISTS / IF NOT EXISTS" capability that MySQL does, so these commands (create/drop) should only be executed if it is known that the action is required.

Show
Peter Williams added a comment - If interested, here is are the methods I added to jdbc_postgres.rb: def create_database(name, options = {}) #:nodoc: execute "CREATE DATABASE #{name} ENCODING='#{options[:charset] || 'utf8'}'" end def drop_database(name) #:nodoc: execute "DROP DATABASE #{name}" end The encoding is probably not correct (should use encoding field from database.yml, I'm not sure this does that correctly). There may also be other options required. PostgreSQL does not appear to support the "IF EXISTS / IF NOT EXISTS" capability that MySQL does, so these commands (create/drop) should only be executed if it is known that the action is required.
Hide
Peter Williams added a comment -

Added quotes to db name in above execute statements - this makes the code a little bit better (allows mixed and uppercase database names)

def create_database(name, options = {}) #:nodoc:
execute "CREATE DATABASE \"#{name}\" ENCODING='#{options[:charset] || 'utf8'}'"
end

def drop_database(name) #:nodoc:
execute "DROP DATABASE \"#{name}\""
end

Show
Peter Williams added a comment - Added quotes to db name in above execute statements - this makes the code a little bit better (allows mixed and uppercase database names) def create_database(name, options = {}) #:nodoc: execute "CREATE DATABASE \"#{name}\" ENCODING='#{options[:charset] || 'utf8'}'" end def drop_database(name) #:nodoc: execute "DROP DATABASE \"#{name}\"" end
Hide
Charles Oliver Nutter added a comment -

Can you structure that in the form of a patch and attach it? JIRA shows bugs which have patches, so it will probably be applied more quickly.

Show
Charles Oliver Nutter added a comment - Can you structure that in the form of a patch and attach it? JIRA shows bugs which have patches, so it will probably be applied more quickly.
Hide
Peter Williams added a comment -

patch attached. Needs review.

Show
Peter Williams added a comment - patch attached. Needs review.
Hide
Nick Sieger added a comment -

Fixed in 61ef839 in ar-jdbc repository. Thanks! This will be in 0.9.

Show
Nick Sieger added a comment - Fixed in 61ef839 in ar-jdbc repository. Thanks! This will be in 0.9.

People

Vote (1)
Watch (4)

Dates

  • Created:
    Updated:
    Resolved: