Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

detect Rails DB connection and use it #232

Merged
merged 9 commits into from
Oct 10, 2014
8 changes: 7 additions & 1 deletion lib/queue_classic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,13 @@ def self.has_connection?
end

def self.default_conn_adapter
@conn_adapter ||= ConnAdapter.new
return @conn_adapter if @conn_adapter
if Object.const_defined?("ActiveRecord") && ActiveRecord::Base.respond_to?("connection")
@conn_adapter = ConnAdapter.new(ActiveRecord::Base.connection.raw_connection)
else
@conn_adapter = ConnAdapter.new
end
@conn_adapter
end

def self.default_conn_adapter=(conn)
Expand Down
25 changes: 13 additions & 12 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,18 +148,6 @@ rails generate queue_classic:install
rake db:migrate
```

By default, queue_classic will use the QC_DATABASE_URL falling back on DATABASE_URL. The URL must be in the following format: `postgres://username:password@localhost/database_name`. If you use Heroku's PostgreSQL service, this will already be set. If you don't want to set this variable, you can set the connection in an initializer. **QueueClassic will maintain its own connection to the database.** This may double the number of connections to your database.

You can share your active record connection with queue_classic –**however this is not thread safe.**

```ruby
require 'queue_classic'
QC.default_conn_adapter = QC::ConnAdapter.new(
ActiveRecord::Base.connection.raw_connection)
```

**Note on using ActiveRecord migrations:** If you use the migration, and you wish to use commands that reset the database from the stored schema (e.g. `rake db:reset`), your application must be configured with `config.active_record.schema_format = :sql` in `config/application.rb`. If you don't do this, the PL/pgSQL function that queue_classic creates will be lost when you reset the database.

### Rake Task Setup

Alternatively, you can use the Rake task to prepare your database.
Expand All @@ -172,6 +160,19 @@ $ bundle exec rake qc:create
$ bundle exec rake qc:drop
```

### Database connection

#### Ruby on Rails

Starting with with queue_classic 3.1, Rails is automatically detected and its connection is used.

**Note on using ActiveRecord migrations:** If you use the migration, and you wish to use commands that reset the database from the stored schema (e.g. `rake db:reset`), your application must be configured with `config.active_record.schema_format = :sql` in `config/application.rb`. If you don't do this, the PL/pgSQL function that queue_classic creates will be lost when you reset the database.


#### Other Ruby apps

By default, queue_classic will use the QC_DATABASE_URL falling back on DATABASE_URL. The URL must be in the following format: `postgres://username:password@localhost/database_name`. If you use Heroku's PostgreSQL service, this will already be set. If you don't want to set this variable, you can set the connection in an initializer. **QueueClassic will maintain its own connection to the database.** This may double the number of connections to your database.

## Upgrade from V2 to V3
If you are upgrading from a previous version of queue_classic, you might need some new database columns and/or functions. Luckily enough for you, it is easy to do so.

Expand Down
28 changes: 28 additions & 0 deletions test/lib/queue_classic_rails_connection_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require File.expand_path("../../helper.rb", __FILE__)

class QueueClassicRailsConnectionTest < QCTest
def before_setup
Object.send :const_set, :ActiveRecord, Module.new
ActiveRecord.const_set :Base, Module.new

QC.default_conn_adapter = nil
end

def after_teardown
ensure
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need the unsure?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question! I don't remember...I'll test and if it's required, I'll document the reason.

ActiveRecord.send :remove_const, :Base
Object.send :remove_const, :ActiveRecord
end

def test_uses_active_record_connection_if_exists
connection = Minitest::Mock.new
connection.expect(:raw_connection, QC::ConnAdapter.new.connection)

ActiveRecord::Base.define_singleton_method(:connection) do
connection
end

QC.default_conn_adapter
assert connection.verify
end
end