Issue Details (XML | Word | Printable)

Key: JRUBY-2715
Type: Bug Bug
Status: Resolved Resolved
Resolution: Fixed
Priority: Minor Minor
Assignee: Nick Sieger
Reporter: Peter Williams
Votes: 1
Watchers: 4
Operations

If you were logged in you would be able to see more operations.
JRuby

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

Created: 27/Jun/08 02:35 PM   Updated: 24/Nov/08 05:19 PM
Component/s: ActiveRecord-JDBC
Affects Version/s: JRuby 1.1.2
Fix Version/s: None

Time Tracking:
Not Specified

File Attachments: 1. Text File createdb.patch (0.6 kB)

Environment: Solaris 10u4 x86, PostgreSQL 8.2, JRuby 1.1.2, JDK 1.6.0_06, NetBeans 6.5 latest nightly builds, make sure the logged in user has PostgreSQL createdb rights and that there is a database matching that username. PSQL w/ no arguments should work properly in otherwords. It should not say "Database "username" does not exist."


 Description  « Hide
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.



 All   Comments   Work Log   Change History      Sort Order: Ascending order - Click to sort in descending order
Peter Williams added a comment - 27/Jun/08 02:38 PM
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.


Peter Williams added a comment - 27/Jun/08 03:15 PM
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


Charles Oliver Nutter added a comment - 03/Jul/08 10:01 AM
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.

Peter Williams added a comment - 03/Jul/08 06:28 PM
patch attached. Needs review.

Nick Sieger added a comment - 24/Nov/08 05:19 PM
Fixed in 61ef839 in ar-jdbc repository. Thanks! This will be in 0.9.