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

ActiveRecord::Job::reserve and MSSQL #57

Merged
merged 1 commit into from
May 12, 2013

Conversation

avinmathew
Copy link
Contributor

I have a Rails 3 application that runs on multiple databases including Oracle and MS SQL Server via JRuby (1.7.3). I've encountered an issue with SQL statement generation from delayed_job (3.0.5) when calling Delayed::Worker.new.work_off. Using Oracle (activerecord-oracle_enhanced-adapter), this statement works fine. Using SQL Server, I receive the following error:

ActiveRecord::JDBCError: The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified.

The generated SQL is:

UPDATE [delayed_jobs]
SET [locked_at] = '2013-04-29 03:19:39', [locked_by] = N'host:PC pid:3160'
WHERE [delayed_jobs].[id] IN
(
    SELECT [delayed_jobs].[id]
    FROM [delayed_jobs]
    WHERE [delayed_jobs].[id] = 1
    AND ((run_at <= '2013-04-29 03:19:39' AND (locked_at IS NULL OR locked_at < '2013-04-28 23:19:39') OR locked_by = N'host:PC pid:3160') AND failed_at IS NULL)
    ORDER BY priority ASC, run_at ASC
)

I've tracked this down to Delayed::Backend::ActiveRecord::Job::reserve. Line 51 adds a priority to the query but this doesn't make sense in the context of the above update statement with subquery. The Oracle adapter automatically splits the generated SQL into two queries so a subquery isn't used.

Like the PostgreSQL and MySQL customisations, I've written one for MSSQL using a combination of their techniques. As I'm using JRuby, I've only tested against activerecord-jdbcmssql-adapter.

@coveralls
Copy link

Coverage Status

Coverage decreased (-4.62%) when pulling 9191429 on avinmathew:master into ebd0244 on collectiveidea:master.

@sferik
Copy link
Collaborator

sferik commented May 12, 2013

This code is untested but I don't know how to test it. 😩

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants