Skip to content

Commit cac86ac

Browse files
committed
Rails migrations should specify the version
class CreateDelayedJobs < ActiveRecord::Migration[4.2] rather than class CreateDelayedJobs < ActiveRecord::Migration
1 parent 36f434c commit cac86ac

File tree

5 files changed

+33
-15
lines changed

5 files changed

+33
-15
lines changed

lib/generators/delayed_job/active_record_generator.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,19 @@ class ActiveRecordGenerator < ::DelayedJobGenerator
1212
source_paths << File.join(File.dirname(__FILE__), "templates")
1313

1414
def create_migration_file
15-
migration_template "migration.rb", "db/migrate/create_delayed_jobs.rb"
15+
migration_template "migration.rb", "db/migrate/create_delayed_jobs.rb", migration_version: migration_version
1616
end
1717

1818
def self.next_migration_number(dirname)
1919
ActiveRecord::Generators::Base.next_migration_number dirname
2020
end
21+
22+
private
23+
24+
def migration_version
25+
if ActiveRecord::VERSION::MAJOR >= 5
26+
"[#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}]"
27+
end
28+
end
2129
end
2230
end

lib/generators/delayed_job/templates/migration.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
class CreateDelayedJobs < ActiveRecord::Migration
1+
class CreateDelayedJobs < ActiveRecord::Migration<%= migration_version %>
22
def self.up
33
create_table :delayed_jobs, force: true do |table|
44
table.integer :priority, default: 0, null: false # Allows some jobs to jump to the front of the queue

lib/generators/delayed_job/templates/upgrade_migration.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
class AddQueueToDelayedJobs < ActiveRecord::Migration
1+
class AddQueueToDelayedJobs < ActiveRecord::Migration<%= migration_version %>
22
def self.up
33
add_column :delayed_jobs, :queue, :string
44
end

lib/generators/delayed_job/upgrade_generator.rb

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,9 @@
55

66
# Extend the DelayedJobGenerator so that it creates an AR migration
77
module DelayedJob
8-
class UpgradeGenerator < ::DelayedJobGenerator
9-
include Rails::Generators::Migration
10-
extend NextMigrationVersion
11-
12-
source_paths << File.join(File.dirname(__FILE__), "templates")
13-
8+
class UpgradeGenerator < ActiveRecordGenerator
149
def create_migration_file
15-
migration_template "upgrade_migration.rb", "db/migrate/add_queue_to_delayed_jobs.rb"
16-
end
17-
18-
def self.next_migration_number(dirname)
19-
ActiveRecord::Generators::Base.next_migration_number dirname
10+
migration_template "upgrade_migration.rb", "db/migrate/add_queue_to_delayed_jobs.rb", migration_version: migration_version
2011
end
2112
end
2213
end

spec/helper.rb

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,26 @@
3333
ActiveRecord::Base.logger = Delayed::Worker.logger
3434
ActiveRecord::Migration.verbose = false
3535

36-
require "generators/delayed_job/templates/migration"
36+
migration_template = File.open("lib/generators/delayed_job/templates/migration.rb")
37+
38+
# need to eval the template with the migration_version intact
39+
migration_context = Class.new do
40+
def get_binding
41+
binding
42+
end
43+
44+
private
45+
46+
def migration_version
47+
if ActiveRecord::VERSION::MAJOR >= 5
48+
"[#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}]"
49+
end
50+
end
51+
end
52+
53+
migration_ruby = ERB.new(migration_template.read).result(migration_context.new.get_binding)
54+
eval(migration_ruby)
55+
3756
ActiveRecord::Schema.define do
3857
CreateDelayedJobs.up
3958

0 commit comments

Comments
 (0)