Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Reuse ActiveRecord connection for migrations. #57

Closed
wants to merge 2 commits into from

2 participants

@glenngillen

Added a convenience method to set the QC::Conn.connection and then method to switch the connection out for a single block of code. It means QC.load_functions can re-use the already open transaction from the Rails migration.

Considered either forcing the exec("commit") prior to running load_functions or allowing load_functions to optionally run outside of a transaction. Both felt a bit ghetto and could leave the app in a state where an error only did have a rollback and then required manual intervention. Wanted to refrain from forcing those awesome MySQL-esque features on users ;)

Discuss.

@ryandotsmith
Owner

Going to pass on this for now. Perhaps we can come back to it for a 2.1 release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 25 additions and 2 deletions.
  1. +15 −0 lib/queue_classic.rb
  2. +4 −0 lib/queue_classic/conn.rb
  3. +6 −2 readme.md
View
15 lib/queue_classic.rb
@@ -88,4 +88,19 @@ def self.log(data)
Scrolls.log({:lib => :queue_classic}.merge(data))
end
+ def self.with_connection(connection, &blk)
+ original_connection = QC::Conn.connection
+ if !connection.respond_to?(:exec) && connection.respond_to?(:execute)
+ connection.instance_eval do
+ def exec(sql, params = nil)
+ @connection.exec(sql, params)
+ end
+ end
+ end
+ QC::Conn.connection = connection
+ yield
+ ensure
+ QC::Conn.connection = original_connection
+ end
+
end
View
4 lib/queue_classic/conn.rb
@@ -62,6 +62,10 @@ def connection
@connection ||= connect
end
+ def connection=(conn)
+ @connection = conn
+ end
+
def disconnect
connection.finish
@connection = nil
View
8 readme.md
@@ -101,13 +101,17 @@ class CreateJobsTable < ActiveRecord::Migration
end
add_index :queue_classic_jobs, :id
require "queue_classic"
- QC::Queries.load_functions
+ QC.with_connection self.connection do
+ QC::Queries.load_functions
+ end
end
def self.down
drop_table :queue_classic_jobs
require "queue_classic"
- QC::Queries.drop_functions
+ QC.with_connection self.connection do
+ QC::Queries.drop_functions
+ end
end
end
Something went wrong with that request. Please try again.